• 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 
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 Restricts the ascending order of the return list. When there are several orders,
128      * the one close to the head has the highest priority.
129      *
130      * @param field Indicates the column name for sorting the return list.
131      */
132     API_EXPORT AbsRdbPredicates* OrderByAsc(const std::string &field) override;
133 
134     /**
135      * @brief Restricts the descending order of the return list. When there are several orders,
136      * the one close to the head has the highest priority.
137      *
138      * @param field Indicates the column name for sorting the return list.
139      */
140     API_EXPORT AbsRdbPredicates* OrderByDesc(const std::string &field) override;
141 
142     /**
143      * @brief Get predicates of remote device.
144      */
145     API_EXPORT const DistributedRdb::PredicatesMemo & GetDistributedPredicates() const;
146 
147     /**
148      * @brief Initialize relevant parameters of the union table.
149      */
150     API_EXPORT virtual void InitialParam();
151 
152     /**
153      * @brief Obtains the join types in the predicates.
154      */
155     API_EXPORT virtual std::vector<std::string> GetJoinTypes();
156 
157     /**
158      * @brief Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN},
159      * and {@code CROSS JOIN}.
160      */
161     API_EXPORT virtual void SetJoinTypes(const std::vector<std::string> &joinTypes);
162 
163     /**
164      * @brief Obtains the database table names of the joins in the predicates.
165      */
166     API_EXPORT virtual std::vector<std::string> GetJoinTableNames();
167 
168     /**
169      * @brief Sets the database table names of the joins in the predicates.
170      */
171     API_EXPORT virtual void SetJoinTableNames(const std::vector<std::string> &joinTableNames);
172 
173     /**
174      * @brief Obtains the join conditions in the predicates.
175      */
176     API_EXPORT virtual std::vector<std::string> GetJoinConditions();
177 
178     /**
179      * @brief Sets the join conditions required in the predicates.
180      */
181     API_EXPORT virtual void SetJoinConditions(const std::vector<std::string> &joinConditions);
182 
183     /**
184      * @brief Obtains the join clause in the predicates.
185      */
186     API_EXPORT virtual std::string GetJoinClause() const;
187 
188     /**
189      * @brief Obtains the number of joins in the predicates.
190      */
191     API_EXPORT virtual int GetJoinCount() const;
192 
193     /**
194      * @brief Sets the number of joins in the predicates.
195      */
196     API_EXPORT virtual void SetJoinCount(int joinCount);
197 
198 protected:
199     std::vector<std::string> joinTypes;
200     std::vector<std::string> joinTableNames;
201     std::vector<std::string> joinConditions;
202     int joinCount = 0;
203 
204 private:
205     std::string tableName_;
206     mutable DistributedRdb::PredicatesMemo predicates_;
207 };
208 } // namespace OHOS::NativeRdb
209 
210 #endif