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