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 
17 #ifndef NATIVE_RDB_ABSRDBPREDICATES_H
18 #define NATIVE_RDB_ABSRDBPREDICATES_H
19 
20 #include "abs_predicates.h"
21 #include "rdb_types.h"
22 
23 /**
24  * The AbsRdbPredicates class of RDB.
25  */
26 namespace OHOS::NativeRdb {
27 class API_EXPORT AbsRdbPredicates : public AbsPredicates {
28 public:
29     /**
30      * @brief Constructor.
31      *
32      * A parameterized constructor used to create an AbsRdbPredicates instance.
33      *
34      * @param tableName Indicates the table name of the database.
35      */
36     API_EXPORT explicit AbsRdbPredicates(const std::string &tableName);
37 
38     /**
39      * @brief Constructor.
40      *
41      * A parameterized constructor used to create an AbsRdbPredicates instance.
42      *
43      * @param tableName Indicates the table name of the database.
44      */
45     API_EXPORT explicit AbsRdbPredicates(const std::vector<std::string> &tables);
46 
47     /**
48      * @brief Destructor.
49      */
~AbsRdbPredicates()50     API_EXPORT ~AbsRdbPredicates() override {}
51 
52     /**
53      * @brief Initalzie AbsRdbPredicates object.
54      */
55     API_EXPORT void Clear() override;
56 
57     /**
58      * @brief Obtains the parameters of the current AbsRdbPredicates object.
59      */
60     [[deprecated("Use GetStatement() instead.")]]
61     API_EXPORT std::string ToString() const;
62 
63     /**
64      * @brief Obtains the table name.
65      */
66     API_EXPORT std::string GetTableName() const;
67 
68     /**
69      * @brief Sync data between devices.
70      *
71      * When query database, this function should not be called.
72      *
73      * @param devices Indicates specified remote devices.
74      *
75      * @return Returns the self.
76      */
77     API_EXPORT AbsRdbPredicates *InDevices(std::vector<std::string>& devices);
78 
79     /**
80      * @brief Specify all remote devices which connect to local device when syncing distributed database.
81      *
82      * When query database, this function should not be called.
83      *
84      * @return Returns the self.
85      */
86     API_EXPORT AbsRdbPredicates *InAllDevices();
87 
88     /**
89      * @brief Restricts the value of the field to be equal to the specified value to the remote AbsRdbPredicates.
90      *
91      * This method is similar to = of the SQL statement.
92      *
93      * @param field Indicates the column name in the database table.
94      * @param value Indicates the value to match with the {@link RdbPredicates}.
95      *
96      * @return Returns the self.
97      */
98     API_EXPORT AbsRdbPredicates* EqualTo(const std::string &field, const ValueObject &value) override;
99 
100     /**
101      * @brief Restricts the value of the field to be not equal to the specified value to the remote AbsRdbPredicates.
102      *
103      * This method is similar to != of the SQL statement.
104      *
105      * @param field Indicates the column name in the database table.
106      * @param value Indicates the value to match with the {@link RdbPredicates}.
107      *
108      * @return Returns the self.
109      */
110     API_EXPORT AbsRdbPredicates* NotEqualTo(const std::string &field, const ValueObject &value) override;
111 
112     /**
113      * @brief Adds an and condition to the remote AbsRdbPredicates.
114      *
115      * This method is similar to or of the SQL statement.
116      */
117     API_EXPORT AbsRdbPredicates* And() override;
118 
119     /**
120      * @brief Adds an or condition to the remote AbsRdbPredicates.
121      *
122      * This method is similar to or of the SQL statement.
123      */
124     API_EXPORT AbsRdbPredicates* Or() override;
125 
126     /**
127      * @brief Adds an left bracket condition to the remote AbsRdbPredicates.
128      *
129      * This method is similar to left bracket of the SQL statement.
130      */
131     API_EXPORT AbsPredicates *BeginWrap() override;
132 
133     /**
134      * @brief Adds an right bracket condition to the remote AbsRdbPredicates.
135      *
136      * This method is similar to right bracket of the SQL statement.
137      */
138     API_EXPORT virtual AbsPredicates *EndWrap() override;
139 
140     /**
141      * @brief Adds an In condition to the remote AbsRdbPredicates.
142      *
143      * This method is similar to In of the SQL statement.
144      */
145     API_EXPORT virtual AbsPredicates *In(const std::string &field, const std::vector<ValueObject> &values) override;
146 
147     /**
148      * @brief Adds an In condition to the remote AbsRdbPredicates.
149      *
150      * This method is similar to In of the SQL statement.
151      */
152     API_EXPORT virtual AbsPredicates *In(const std::string &field, const std::vector<std::string> &values) override;
153 
154     /**
155      * @brief Adds an Contains condition to the remote AbsRdbPredicates.
156      *
157      * This method indicates that the expected field contains value.
158      */
159     API_EXPORT AbsRdbPredicates *Contains(const std::string &field, const std::string &value) override;
160 
161     /**
162      * @brief Adds an Not Contains condition to the remote AbsRdbPredicates.
163      *
164      * This method indicates that the expected field not contains value.
165      */
166     API_EXPORT AbsRdbPredicates *NotContains(const std::string &field, const std::string &value) override;
167 
168     /**
169      * @brief Adds an BeginsWith condition to the remote AbsRdbPredicates.
170      *
171      * This method indicates that the expected field begin with value.
172      */
173     API_EXPORT AbsRdbPredicates *BeginsWith(const std::string &field, const std::string &value) override;
174 
175     /**
176      * @brief Adds an EndsWith condition to the remote AbsRdbPredicates.
177      *
178      * This method indicates that the expected field end with value.
179      */
180     API_EXPORT AbsRdbPredicates *EndsWith(const std::string &field, const std::string &value) override;
181 
182     /**
183      * @brief Adds an IsNull condition to the remote AbsRdbPredicates.
184      *
185      * This method indicates that the expected field is null.
186      */
187     API_EXPORT AbsRdbPredicates *IsNull(const std::string &field) override;
188 
189     /**
190      * @brief Adds an IsNotNull condition to the remote AbsRdbPredicates.
191      *
192      * This method indicates that the expected field is not null.
193      */
194     API_EXPORT AbsRdbPredicates *IsNotNull(const std::string &field) override;
195 
196     /**
197      * @brief Adds an Like condition to the remote AbsRdbPredicates.
198      *
199      * This method is similar to Like of the SQL statement.
200      */
201     API_EXPORT AbsRdbPredicates *Like(const std::string &field, const std::string &value) override;
202 
203     /**
204      * @brief Adds an Like condition to the remote AbsRdbPredicates.
205      *
206      * This method is similar to Like of the SQL statement.
207      */
208     API_EXPORT AbsRdbPredicates *NotLike(const std::string &field, const std::string &value) override;
209 
210     /**
211      * @brief Adds an Glob condition to the remote AbsRdbPredicates.
212      *
213      * This method is similar to glob of the SQL statement.
214      */
215     API_EXPORT AbsRdbPredicates *Glob(const std::string &field, const std::string &value) override;
216 
217     /**
218      * @brief Adds an Distinct condition to the remote AbsRdbPredicates.
219      *
220      * This method is similar to distinct of the SQL statement.
221      */
222     API_EXPORT AbsRdbPredicates *Distinct() override;
223 
224     /**
225      * @brief Adds an IndexedBy condition to the remote AbsRdbPredicates.
226      *
227      * This method is similar to indexed by of the SQL statement.
228      */
229     API_EXPORT AbsRdbPredicates *IndexedBy(const std::string &indexName) override;
230 
231     /**
232      * @brief Adds an NotIn condition to the remote AbsRdbPredicates.
233      *
234      * This method is similar to not in of the SQL statement.
235      */
236     API_EXPORT AbsRdbPredicates *NotIn(const std::string &field, const std::vector<std::string> &values) override;
237 
238     /**
239      * @brief Adds an NotIn condition to the remote AbsRdbPredicates.
240      *
241      * This method is similar to not in of the SQL statement.
242      */
243     API_EXPORT AbsRdbPredicates *NotIn(const std::string &field, const std::vector<ValueObject> &values) override;
244 
245     /**
246      * @brief Restricts the ascending order of the return list. When there are several orders,
247      * the one close to the head has the highest priority.
248      *
249      * @param field Indicates the column name for sorting the return list.
250      */
251     API_EXPORT AbsRdbPredicates* OrderByAsc(const std::string &field) override;
252 
253     /**
254      * @brief Restricts the descending order of the return list. When there are several orders,
255      * the one close to the head has the highest priority.
256      *
257      * @param field Indicates the column name for sorting the return list.
258      */
259     API_EXPORT AbsRdbPredicates* OrderByDesc(const std::string &field) override;
260 
261     /**
262      * @brief Get predicates of remote device.
263      */
264     API_EXPORT const DistributedRdb::PredicatesMemo & GetDistributedPredicates() const;
265 
266     /**
267      * @brief Initialize relevant parameters of the union table.
268      */
269     API_EXPORT virtual void InitialParam();
270 
271     /**
272      * @brief Obtains the join types in the predicates.
273      */
274     API_EXPORT virtual std::vector<std::string> GetJoinTypes();
275 
276     /**
277      * @brief Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN},
278      * and {@code CROSS JOIN}.
279      */
280     API_EXPORT virtual void SetJoinTypes(const std::vector<std::string> &joinTypes);
281 
282     /**
283      * @brief Obtains the database table names of the joins in the predicates.
284      */
285     API_EXPORT virtual std::vector<std::string> GetJoinTableNames();
286 
287     /**
288      * @brief Sets the database table names of the joins in the predicates.
289      */
290     API_EXPORT virtual void SetJoinTableNames(const std::vector<std::string> &joinTableNames);
291 
292     /**
293      * @brief Obtains the join conditions in the predicates.
294      */
295     API_EXPORT virtual std::vector<std::string> GetJoinConditions();
296 
297     /**
298      * @brief Sets the join conditions required in the predicates.
299      */
300     API_EXPORT virtual void SetJoinConditions(const std::vector<std::string> &joinConditions);
301 
302     /**
303      * @brief Obtains the join clause in the predicates.
304      */
305     API_EXPORT virtual std::string GetJoinClause() const;
306 
307     /**
308      * @brief Obtains the number of joins in the predicates.
309      */
310     API_EXPORT virtual int GetJoinCount() const;
311 
312     /**
313      * @brief Sets the number of joins in the predicates.
314      */
315     API_EXPORT virtual void SetJoinCount(int joinCount);
316 
317     static constexpr const char *LOCK_STATUS = "#_status";
318     static constexpr int LOCKED = 2;
319     static constexpr int LOCK_CHANGED = 3;
320 
321 protected:
322     std::vector<std::string> joinTypes;
323     std::vector<std::string> joinTableNames;
324     std::vector<std::string> joinConditions;
325     int joinCount = 0;
326 
327 private:
328     std::string tableName_;
329     mutable DistributedRdb::PredicatesMemo predicates_;
330 };
331 } // namespace OHOS::NativeRdb
332 
333 #endif