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 DATASHARE_PREDICATES_H 17 #define DATASHARE_PREDICATES_H 18 19 #include <string> 20 21 #include "datashare_abs_predicates.h" 22 #include "datashare_errno.h" 23 #include "datashare_predicates_object.h" 24 #include "datashare_predicates_objects.h" 25 26 namespace OHOS { 27 namespace DataShare { 28 class DataSharePredicates : public DataShareAbsPredicates { 29 public: 30 31 /** 32 * @brief Constructor. 33 */ DataSharePredicates()34 DataSharePredicates() 35 { 36 } 37 38 /** 39 * @brief Constructor. 40 * 41 * A parameterized constructor used to create an DataSharePredicates instance. 42 * 43 * @param OperationList Indicates the operation list of the database. 44 */ DataSharePredicates(std::vector<OperationItem> operList)45 explicit DataSharePredicates(std::vector<OperationItem> operList) : operations_(std::move(operList)) 46 { 47 } 48 49 /** 50 * @brief Destructor. 51 */ ~DataSharePredicates()52 ~DataSharePredicates() 53 { 54 } 55 56 /** 57 * @brief The EqualTo of the predicate. 58 * 59 * @param field Indicates the target field. 60 * @param value Indicates the queried value. 61 */ EqualTo(const std::string & field,const SingleValue & value)62 DataSharePredicates *EqualTo(const std::string &field, const SingleValue &value) 63 { 64 SetOperationList(EQUAL_TO, field, value); 65 return this; 66 } 67 68 /** 69 * @brief The NotEqualTo of the predicate. 70 * 71 * @param field Indicates the target field. 72 * @param value Indicates the queried value. 73 */ NotEqualTo(const std::string & field,const SingleValue & value)74 DataSharePredicates *NotEqualTo(const std::string &field, const SingleValue &value) 75 { 76 SetOperationList(NOT_EQUAL_TO, field, value); 77 return this; 78 } 79 80 /** 81 * @brief The GreaterThan of the predicate. 82 * 83 * @param field Indicates the target field. 84 * @param value Indicates the queried value. 85 */ GreaterThan(const std::string & field,const SingleValue & value)86 DataSharePredicates *GreaterThan(const std::string &field, const SingleValue &value) 87 { 88 SetOperationList(GREATER_THAN, field, value); 89 return this; 90 } 91 92 /** 93 * @brief The LessThan of the predicate. 94 * 95 * @param field Indicates the target field. 96 * @param value Indicates the queried value. 97 */ LessThan(const std::string & field,const SingleValue & value)98 DataSharePredicates *LessThan(const std::string &field, const SingleValue &value) 99 { 100 SetOperationList(LESS_THAN, field, value); 101 return this; 102 } 103 104 /** 105 * @brief The GreaterThanOrEqualTo of the predicate. 106 * 107 * @param field Indicates the target field. 108 * @param value Indicates the queried value. 109 */ GreaterThanOrEqualTo(const std::string & field,const SingleValue & value)110 DataSharePredicates *GreaterThanOrEqualTo(const std::string &field, const SingleValue &value) 111 { 112 SetOperationList(GREATER_THAN_OR_EQUAL_TO, field, value); 113 return this; 114 } 115 116 /** 117 * @brief The LessThanOrEqualTo of the predicate. 118 * 119 * @param field Indicates the target field. 120 * @param value Indicates the queried value. 121 */ LessThanOrEqualTo(const std::string & field,const SingleValue & value)122 DataSharePredicates *LessThanOrEqualTo(const std::string &field, const SingleValue &value) 123 { 124 SetOperationList(LESS_THAN_OR_EQUAL_TO, field, value); 125 return this; 126 } 127 128 /** 129 * @brief The In of the predicate. 130 * 131 * @param field Indicates the target field. 132 * @param value Indicates the queried value. 133 */ In(const std::string & field,const MutliValue & values)134 DataSharePredicates *In(const std::string &field, const MutliValue &values) 135 { 136 SetOperationList(SQL_IN, field, values); 137 return this; 138 } 139 140 /** 141 * @brief The NotIn of the predicate. 142 * 143 * @param field Indicates the target field. 144 * @param value Indicates the queried value. 145 */ NotIn(const std::string & field,const MutliValue & values)146 DataSharePredicates *NotIn(const std::string &field, const MutliValue &values) 147 { 148 SetOperationList(NOT_IN, field, values); 149 return this; 150 } 151 152 /** 153 * @brief BeginWrap. 154 */ BeginWrap()155 DataSharePredicates *BeginWrap() 156 { 157 SetOperationList(BEGIN_WARP); 158 return this; 159 } 160 161 /** 162 * @brief EndWrap. 163 */ EndWrap()164 DataSharePredicates *EndWrap() 165 { 166 SetOperationList(END_WARP); 167 return this; 168 } 169 170 /** 171 * @brief Or. 172 */ Or()173 DataSharePredicates *Or() 174 { 175 SetOperationList(OR); 176 return this; 177 } 178 179 /** 180 * @brief And. 181 */ And()182 DataSharePredicates *And() 183 { 184 SetOperationList(AND); 185 return this; 186 } 187 188 /** 189 * @brief The Contains of the predicate. 190 * 191 * @param field Indicates the target field. 192 * @param value Indicates the queried value. 193 */ Contains(const std::string & field,const std::string & value)194 DataSharePredicates *Contains(const std::string &field, const std::string &value) 195 { 196 SetOperationList(CONTAINS, field, value); 197 return this; 198 } 199 200 /** 201 * @brief The BeginsWith of the predicate. 202 * 203 * @param field Indicates the target field. 204 * @param value Indicates the queried value. 205 */ BeginsWith(const std::string & field,const std::string & value)206 DataSharePredicates *BeginsWith(const std::string &field, const std::string &value) 207 { 208 SetOperationList(BEGIN_WITH, field, value); 209 return this; 210 } 211 212 /** 213 * @brief The EndsWith of the predicate. 214 * 215 * @param field Indicates the target field. 216 * @param value Indicates the queried value. 217 */ EndsWith(const std::string & field,const std::string & value)218 DataSharePredicates *EndsWith(const std::string &field, const std::string &value) 219 { 220 SetOperationList(END_WITH, field, value); 221 return this; 222 } 223 224 /** 225 * @brief The IsNull of the predicate. 226 * 227 * @param field Indicates the target field. 228 * @param value Indicates the queried value. 229 */ IsNull(const std::string & field)230 DataSharePredicates *IsNull(const std::string &field) 231 { 232 SetOperationList(IS_NULL, field); 233 return this; 234 } 235 236 /** 237 * @brief The IsNotNull of the predicate. 238 * 239 * @param field Indicates the target field. 240 * @param value Indicates the queried value. 241 */ IsNotNull(const std::string & field)242 DataSharePredicates *IsNotNull(const std::string &field) 243 { 244 SetOperationList(IS_NOT_NULL, field); 245 return this; 246 } 247 248 /** 249 * @brief The Like of the predicate. 250 * 251 * @param field Indicates the target field. 252 * @param value Indicates the queried value. 253 */ Like(const std::string & field,const std::string & value)254 DataSharePredicates *Like(const std::string &field, const std::string &value) 255 { 256 SetOperationList(LIKE, field, value); 257 return this; 258 } 259 260 /** 261 * @brief The Unlike of the predicate. 262 * 263 * @param field Indicates the target field. 264 * @param value Indicates the queried value. 265 */ Unlike(const std::string & field,const std::string & value)266 DataSharePredicates *Unlike(const std::string &field, const std::string &value) 267 { 268 SetOperationList(UNLIKE, field, value); 269 return this; 270 } 271 272 /** 273 * @brief The Glob of the predicate. 274 * 275 * @param field Indicates the target field. 276 * @param value Indicates the queried value. 277 */ Glob(const std::string & field,const std::string & value)278 DataSharePredicates *Glob(const std::string &field, const std::string &value) 279 { 280 SetOperationList(GLOB, field, value); 281 return this; 282 } 283 284 /** 285 * @brief The Between of the predicate. 286 * 287 * @param field Indicates the target field. 288 * @param value Indicates the queried value. 289 */ Between(const std::string & field,const std::string & low,const std::string & high)290 DataSharePredicates *Between(const std::string &field, const std::string &low, const std::string &high) 291 { 292 SetOperationList(BETWEEN, field, low, high); 293 return this; 294 } 295 296 /** 297 * @brief The NotBetween of the predicate. 298 * 299 * @param field Indicates the target field. 300 * @param value Indicates the queried value. 301 */ NotBetween(const std::string & field,const std::string & low,const std::string & high)302 DataSharePredicates *NotBetween(const std::string &field, const std::string &low, const std::string &high) 303 { 304 SetOperationList(NOTBETWEEN, field, low, high); 305 return this; 306 } 307 308 /** 309 * @brief The OrderByAsc of the predicate. 310 * 311 * @param field Indicates the target field. 312 * @param value Indicates the queried value. 313 */ OrderByAsc(const std::string & field)314 DataSharePredicates *OrderByAsc(const std::string &field) 315 { 316 SetOperationList(ORDER_BY_ASC, field); 317 return this; 318 } 319 320 /** 321 * @brief The OrderByDesc of the predicate. 322 * 323 * @param field Indicates the target field. 324 * @param value Indicates the queried value. 325 */ OrderByDesc(const std::string & field)326 DataSharePredicates *OrderByDesc(const std::string &field) 327 { 328 SetOperationList(ORDER_BY_DESC, field); 329 return this; 330 } 331 332 /** 333 * @brief Distinct predicate condition. 334 */ Distinct()335 DataSharePredicates *Distinct() 336 { 337 SetOperationList(DISTINCT); 338 return this; 339 } 340 341 /** 342 * @brief The Limit of the predicate. 343 * 344 * @param number Indicates the target number. 345 * @param offset Indicates the queried value. 346 */ Limit(const int number,const int offset)347 DataSharePredicates *Limit(const int number, const int offset) 348 { 349 SetOperationList(LIMIT, number, offset); 350 return this; 351 } 352 353 /** 354 * @brief The GroupBy of the predicate. 355 * 356 * @param field Indicates the target field. 357 */ GroupBy(const std::vector<std::string> & fields)358 DataSharePredicates *GroupBy(const std::vector<std::string> &fields) 359 { 360 SetOperationList(GROUP_BY, fields); 361 return this; 362 } 363 364 /** 365 * @brief The IndexedBy of the predicate. 366 * 367 * @param indexName indicates the query condition. 368 */ IndexedBy(const std::string & indexName)369 DataSharePredicates *IndexedBy(const std::string &indexName) 370 { 371 SetOperationList(INDEXED_BY, indexName); 372 return this; 373 } 374 375 /** 376 * @brief The KeyPrefix of the predicate. 377 * 378 * @param Search by prefix conditions. 379 */ KeyPrefix(const std::string & prefix)380 DataSharePredicates *KeyPrefix(const std::string &prefix) 381 { 382 SetOperationList(KEY_PREFIX, prefix); 383 return this; 384 } 385 386 /** 387 * @brief The InKeys of the predicate. 388 * 389 * @param Query based on key conditions. 390 */ InKeys(const std::vector<std::string> & keys)391 DataSharePredicates *InKeys(const std::vector<std::string> &keys) 392 { 393 SetOperationList(IN_KEY, keys); 394 return this; 395 } 396 397 /** 398 * @brief The CrossJoin of the predicate. 399 * 400 * @param tableName indicates the query condition. 401 */ CrossJoin(const std::string & tableName)402 DataSharePredicates *CrossJoin(const std::string &tableName) 403 { 404 SetOperationList(CROSSJOIN, tableName); 405 return this; 406 } 407 408 /** 409 * @brief The InnerJoin of the predicate. 410 * 411 * @param tableName indicates the query condition. 412 */ InnerJoin(const std::string & tableName)413 DataSharePredicates *InnerJoin(const std::string &tableName) 414 { 415 SetOperationList(INNERJOIN, tableName); 416 return this; 417 } 418 419 /** 420 * @brief The LeftOuterJoin of the predicate. 421 * 422 * @param tableName indicates the query condition. 423 */ LeftOuterJoin(const std::string & tableName)424 DataSharePredicates *LeftOuterJoin(const std::string &tableName) 425 { 426 SetOperationList(LEFTOUTERJOIN, tableName); 427 return this; 428 } 429 430 /** 431 * @brief The Using of the predicate. 432 * 433 * @param field Indicates the target field. 434 */ Using(const std::vector<std::string> & fields)435 DataSharePredicates *Using(const std::vector<std::string> &fields) 436 { 437 SetOperationList(USING, fields); 438 return this; 439 } 440 441 /** 442 * @brief The On of the predicate. 443 * 444 * @param field Indicates the target field. 445 */ On(const std::vector<std::string> & fields)446 DataSharePredicates *On(const std::vector<std::string> &fields) 447 { 448 SetOperationList(ON, fields); 449 return this; 450 } 451 452 /** 453 * @brief The GetOperationList of the predicate. 454 */ GetOperationList()455 const std::vector<OperationItem> &GetOperationList() const 456 { 457 return operations_; 458 } 459 460 /** 461 * @brief Used in the deserialization function to assign values to operations, 462 * Directly assignsing values to operations with Different usage from private methods. 463 * reference to the SetWhereClause, SetWhereArgs, SetOrder. 464 */ SetOperationList(std::vector<OperationItem> operations)465 int SetOperationList(std::vector<OperationItem> operations) 466 { 467 if ((settingMode_ != PREDICATES_METHOD) && (!operations.empty())) { 468 this->operations_ = operations; 469 settingMode_ = QUERY_LANGUAGE; 470 return E_OK; 471 } 472 return E_ERROR; 473 } 474 475 /** 476 * @brief The GetWhereClause of the predicate. 477 */ GetWhereClause()478 std::string GetWhereClause() const 479 { 480 return whereClause_; 481 } 482 483 /** 484 * @brief The SetWhereClause of the predicate. 485 * 486 * @param Query based on the whereClause. 487 */ SetWhereClause(const std::string & whereClause)488 int SetWhereClause(const std::string &whereClause) 489 { 490 if ((settingMode_ != PREDICATES_METHOD) && (!whereClause.empty())) { 491 this->whereClause_ = whereClause; 492 settingMode_ = QUERY_LANGUAGE; 493 return E_OK; 494 } 495 return E_ERROR; 496 } 497 498 /** 499 * @brief The GetWhereArgs of the predicate. 500 */ GetWhereArgs()501 std::vector<std::string> GetWhereArgs() const 502 { 503 return whereArgs_; 504 } 505 506 /** 507 * @brief The SetWhereArgs of the predicate. 508 * 509 * @param Query based on whereArgs conditions. 510 */ SetWhereArgs(const std::vector<std::string> & whereArgs)511 int SetWhereArgs(const std::vector<std::string> &whereArgs) 512 { 513 if ((settingMode_ != PREDICATES_METHOD) && (!whereArgs.empty())) { 514 if (!whereArgs.empty()) { 515 this->whereArgs_ = whereArgs; 516 settingMode_ = QUERY_LANGUAGE; 517 return E_OK; 518 } 519 } 520 return E_ERROR; 521 } 522 523 /** 524 * @brief The GetOrder of the predicate. 525 */ GetOrder()526 std::string GetOrder() const 527 { 528 return order_; 529 } 530 531 /** 532 * @brief The SetOrder of the predicate. 533 * 534 * @param Query based on order conditions.. 535 */ SetOrder(const std::string & order)536 int SetOrder(const std::string &order) 537 { 538 if ((settingMode_ != PREDICATES_METHOD) && (!order.empty())) { 539 this->order_ = order; 540 settingMode_ = QUERY_LANGUAGE; 541 return E_OK; 542 } 543 return E_ERROR; 544 } 545 546 /** 547 * @brief The GetSettingMode of the predicate. 548 */ GetSettingMode()549 int16_t GetSettingMode() const 550 { 551 return settingMode_; 552 } 553 554 /** 555 * @brief The SetSettingMode of the predicate. 556 */ SetSettingMode(int16_t settingMode)557 void SetSettingMode(int16_t settingMode) 558 { 559 settingMode_ = settingMode; 560 } 561 562 private: SetOperationList(OperationType operationType,const MutliValue & param)563 void SetOperationList(OperationType operationType, const MutliValue ¶m) 564 { 565 OperationItem operationItem {}; 566 operationItem.operation = operationType; 567 operationItem.multiParams.push_back(param.value); 568 operations_.push_back(operationItem); 569 if (settingMode_ != PREDICATES_METHOD) { 570 ClearQueryLanguage(); 571 settingMode_ = PREDICATES_METHOD; 572 } 573 } SetOperationList(OperationType operationType,const SingleValue & param1,const MutliValue & param2)574 void SetOperationList( 575 OperationType operationType, const SingleValue ¶m1, const MutliValue ¶m2) 576 { 577 OperationItem operationItem {}; 578 operationItem.operation = operationType; 579 operationItem.singleParams.push_back(param1.value); 580 operationItem.multiParams.push_back(param2.value); 581 operations_.push_back(operationItem); 582 if (settingMode_ != PREDICATES_METHOD) { 583 ClearQueryLanguage(); 584 settingMode_ = PREDICATES_METHOD; 585 } 586 } 587 void SetOperationList(OperationType operationType, const SingleValue ¶1 = {}, 588 const SingleValue ¶2 = {}, const SingleValue ¶3 = {}) 589 { 590 OperationItem operationItem {}; 591 operationItem.operation = operationType; 592 operationItem.singleParams.push_back(para1.value); 593 operationItem.singleParams.push_back(para2.value); 594 operationItem.singleParams.push_back(para3.value); 595 operations_.push_back(operationItem); 596 if (settingMode_ != PREDICATES_METHOD) { 597 ClearQueryLanguage(); 598 settingMode_ = PREDICATES_METHOD; 599 } 600 } ClearQueryLanguage()601 void ClearQueryLanguage() 602 { 603 whereClause_ = ""; 604 whereArgs_ = {}; 605 order_ = ""; 606 } 607 std::vector<OperationItem> operations_; 608 std::string whereClause_; 609 std::vector<std::string> whereArgs_; 610 std::string order_; 611 int16_t settingMode_ = {}; 612 }; 613 } // namespace DataShare 614 } // namespace OHOS 615 #endif