1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rdb_predicates.h"
17 #include "string_utils.h"
18 #include "logger.h"
19
20 namespace OHOS {
21 namespace NativeRdb {
RdbPredicates(std::string tableName)22 RdbPredicates::RdbPredicates(std::string tableName) : AbsRdbPredicates(tableName)
23 {
24 InitialParam();
25 }
26
GetJoinClause() const27 std::string RdbPredicates::GetJoinClause() const
28 {
29 if (!joinTableNames.empty()) {
30 return ProcessJoins();
31 }
32 return GetTableName();
33 }
34
35 /**
36 * Adds a {@code cross join} condition to a SQL statement.
37 */
CrossJoin(std::string tableName)38 RdbPredicates *RdbPredicates::CrossJoin(std::string tableName)
39 {
40 return Join(JoinType::CROSS, tableName);
41 }
42
43 /**
44 * Adds an {@code inner join} condition to a SQL statement.
45 */
InnerJoin(std::string tableName)46 RdbPredicates *RdbPredicates::InnerJoin(std::string tableName)
47 {
48 return Join(JoinType::INNER, tableName);
49 }
50
51 /**
52 * Adds a {@code left outer join} condition to a SQL statement.
53 */
LeftOuterJoin(std::string tableName)54 RdbPredicates *RdbPredicates::LeftOuterJoin(std::string tableName)
55 {
56 return Join(JoinType::LEFT, tableName);
57 }
58 /**
59 * Adds a condition to a SQL statement.
60 */
Join(int join,std::string tableName)61 RdbPredicates *RdbPredicates::Join(int join, std::string tableName)
62 {
63 if (tableName.empty()) {
64 LOG_WARN("RdbPredicates join failed: table name is null or empty.");
65 return this;
66 }
67
68 joinTypes.push_back(GetGrammar(join));
69 joinTableNames.push_back(tableName);
70 joinCount++;
71 return this;
72 }
73
74 /**
75 * Adds a {@code using} condition to the predicate. This method is similar to {@code using} of the SQL statement.
76 */
Using(std::vector<std::string> fields)77 RdbPredicates *RdbPredicates::Using(std::vector<std::string> fields)
78 {
79 if (fields.size() == 0) {
80 LOG_WARN("RdbPredicates Using failed : clauses is null.");
81 return this;
82 }
83 if (joinCount <= 0) {
84 LOG_WARN("No active join operation before using.");
85 return this;
86 }
87 while (joinCount > 1) {
88 joinConditions.push_back("");
89 joinCount--;
90 }
91 joinCount--;
92 joinConditions.push_back(StringUtils::SurroundWithFunction("USING", ",", fields));
93 return this;
94 }
95
96 /**
97 * Adds an {@code on} condition to the predicate.
98 */
On(std::vector<std::string> clauses)99 RdbPredicates *RdbPredicates::On(std::vector<std::string> clauses)
100 {
101 if (clauses.size() == 0) {
102 LOG_WARN("RdbPredicates on failed : clauses is null.");
103 return this;
104 }
105 if (joinCount <= 0) {
106 LOG_WARN("No active join operation before on.");
107 return this;
108 }
109 while (joinCount > 1) {
110 joinConditions.push_back("");
111 joinCount--;
112 }
113 joinCount--;
114 joinConditions.push_back(StringUtils::SurroundWithFunction("ON", "AND", clauses));
115 return this;
116 }
117
ProcessJoins() const118 std::string RdbPredicates::ProcessJoins() const
119 {
120 std::string builder = GetTableName();
121 size_t size = joinTableNames.size();
122 for (size_t i = 0; i < size; i++) {
123 builder = builder + " " + joinTypes[i] + " " + joinTableNames[i];
124 if (joinConditions[i] != "") {
125 builder = builder + " " + joinConditions[i];
126 }
127 }
128 return builder;
129 }
130
GetGrammar(int type) const131 std::string RdbPredicates::GetGrammar(int type) const
132 {
133 if (type == INNER) {
134 return "INNER JOIN";
135 }
136 if (type == LEFT) {
137 return "LEFT OUTER JOIN";
138 }
139 return "CROSS JOIN";
140 }
141 } // namespace NativeRdb
142 } // namespace OHOS