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 BeginsWith condition to the remote AbsRdbPredicates. 163 * 164 * This method indicates that the expected field begin with value. 165 */ 166 API_EXPORT AbsRdbPredicates *BeginsWith(const std::string &field, const std::string &value) override; 167 168 /** 169 * @brief Adds an EndsWith condition to the remote AbsRdbPredicates. 170 * 171 * This method indicates that the expected field end with value. 172 */ 173 API_EXPORT AbsRdbPredicates *EndsWith(const std::string &field, const std::string &value) override; 174 175 /** 176 * @brief Adds an IsNull condition to the remote AbsRdbPredicates. 177 * 178 * This method indicates that the expected field is null. 179 */ 180 API_EXPORT AbsRdbPredicates *IsNull(const std::string &field) override; 181 182 /** 183 * @brief Adds an IsNotNull condition to the remote AbsRdbPredicates. 184 * 185 * This method indicates that the expected field is not null. 186 */ 187 API_EXPORT AbsRdbPredicates *IsNotNull(const std::string &field) override; 188 189 /** 190 * @brief Adds an Like condition to the remote AbsRdbPredicates. 191 * 192 * This method is similar to Like of the SQL statement. 193 */ 194 API_EXPORT AbsRdbPredicates *Like(const std::string &field, const std::string &value) override; 195 196 /** 197 * @brief Adds an Glob condition to the remote AbsRdbPredicates. 198 * 199 * This method is similar to glob of the SQL statement. 200 */ 201 API_EXPORT AbsRdbPredicates *Glob(const std::string &field, const std::string &value) override; 202 203 /** 204 * @brief Adds an Distinct condition to the remote AbsRdbPredicates. 205 * 206 * This method is similar to distinct of the SQL statement. 207 */ 208 API_EXPORT AbsRdbPredicates *Distinct() override; 209 210 /** 211 * @brief Adds an IndexedBy condition to the remote AbsRdbPredicates. 212 * 213 * This method is similar to indexed by of the SQL statement. 214 */ 215 API_EXPORT AbsRdbPredicates *IndexedBy(const std::string &indexName) override; 216 217 /** 218 * @brief Adds an NotIn condition to the remote AbsRdbPredicates. 219 * 220 * This method is similar to not in of the SQL statement. 221 */ 222 API_EXPORT AbsRdbPredicates *NotIn(const std::string &field, const std::vector<std::string> &values) override; 223 224 /** 225 * @brief Adds an NotIn condition to the remote AbsRdbPredicates. 226 * 227 * This method is similar to not in of the SQL statement. 228 */ 229 API_EXPORT AbsRdbPredicates *NotIn(const std::string &field, const std::vector<ValueObject> &values) override; 230 231 /** 232 * @brief Restricts the ascending order of the return list. When there are several orders, 233 * the one close to the head has the highest priority. 234 * 235 * @param field Indicates the column name for sorting the return list. 236 */ 237 API_EXPORT AbsRdbPredicates* OrderByAsc(const std::string &field) override; 238 239 /** 240 * @brief Restricts the descending order of the return list. When there are several orders, 241 * the one close to the head has the highest priority. 242 * 243 * @param field Indicates the column name for sorting the return list. 244 */ 245 API_EXPORT AbsRdbPredicates* OrderByDesc(const std::string &field) override; 246 247 /** 248 * @brief Get predicates of remote device. 249 */ 250 API_EXPORT const DistributedRdb::PredicatesMemo & GetDistributedPredicates() const; 251 252 /** 253 * @brief Initialize relevant parameters of the union table. 254 */ 255 API_EXPORT virtual void InitialParam(); 256 257 /** 258 * @brief Obtains the join types in the predicates. 259 */ 260 API_EXPORT virtual std::vector<std::string> GetJoinTypes(); 261 262 /** 263 * @brief Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN}, 264 * and {@code CROSS JOIN}. 265 */ 266 API_EXPORT virtual void SetJoinTypes(const std::vector<std::string> &joinTypes); 267 268 /** 269 * @brief Obtains the database table names of the joins in the predicates. 270 */ 271 API_EXPORT virtual std::vector<std::string> GetJoinTableNames(); 272 273 /** 274 * @brief Sets the database table names of the joins in the predicates. 275 */ 276 API_EXPORT virtual void SetJoinTableNames(const std::vector<std::string> &joinTableNames); 277 278 /** 279 * @brief Obtains the join conditions in the predicates. 280 */ 281 API_EXPORT virtual std::vector<std::string> GetJoinConditions(); 282 283 /** 284 * @brief Sets the join conditions required in the predicates. 285 */ 286 API_EXPORT virtual void SetJoinConditions(const std::vector<std::string> &joinConditions); 287 288 /** 289 * @brief Obtains the join clause in the predicates. 290 */ 291 API_EXPORT virtual std::string GetJoinClause() const; 292 293 /** 294 * @brief Obtains the number of joins in the predicates. 295 */ 296 API_EXPORT virtual int GetJoinCount() const; 297 298 /** 299 * @brief Sets the number of joins in the predicates. 300 */ 301 API_EXPORT virtual void SetJoinCount(int joinCount); 302 303 protected: 304 std::vector<std::string> joinTypes; 305 std::vector<std::string> joinTableNames; 306 std::vector<std::string> joinConditions; 307 int joinCount = 0; 308 309 private: 310 std::string tableName_; 311 mutable DistributedRdb::PredicatesMemo predicates_; 312 }; 313 } // namespace OHOS::NativeRdb 314 315 #endif