1 /* 2 * Copyright (c) 2021 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 DISTRIBUTED_DATA_QUERY_H 17 #define DISTRIBUTED_DATA_QUERY_H 18 19 #include <string> 20 #include <vector> 21 #include <sstream> 22 #include <memory> 23 #include "visibility.h" 24 namespace DistributedDB { 25 class Query; 26 } 27 namespace OHOS { 28 namespace DistributedKv { 29 class API_EXPORT DataQuery { 30 public: 31 /** 32 * @brief Constructor. 33 */ 34 DataQuery(); 35 36 /** 37 * @brief Destructor. 38 */ 39 ~DataQuery() = default; 40 41 /** 42 * @brief Reset the query. 43 * @return This query. 44 */ 45 DataQuery &Reset(); 46 47 /** 48 * @brief Equal to int value. 49 * @param field The field name. 50 * @param value The field value. 51 * @return This query 52 */ 53 DataQuery &EqualTo(const std::string &field, const int value); 54 55 /** 56 * @brief Equal to long value. 57 * @param field The field name. 58 * @param value The field value. 59 * @return This query 60 */ 61 DataQuery &EqualTo(const std::string &field, const int64_t value); 62 63 /** 64 * @brief Equal to double value. 65 * @param field The field name. 66 * @param value The field value. 67 * @return This query 68 */ 69 DataQuery &EqualTo(const std::string &field, const double value); 70 71 /** 72 * @brief Equal to string value. 73 * @param field The field name. 74 * @param value The field value. 75 * @return This query 76 */ 77 DataQuery &EqualTo(const std::string &field, const std::string &value); 78 79 /** 80 * @brief Equal to boolean value. 81 * @param field The field name. 82 * @param value The field value. 83 * @return This query 84 */ 85 DataQuery &EqualTo(const std::string &field, const bool value); 86 87 /** 88 * @brief Not equal to int value. 89 * @param field The field name. 90 * @param value The field value. 91 * @return This query 92 */ 93 DataQuery &NotEqualTo(const std::string &field, const int value); 94 95 /** 96 * @brief Not equal to long value. 97 * @param field The field name. 98 * @param value The field value. 99 * @return This query 100 */ 101 DataQuery &NotEqualTo(const std::string &field, const int64_t value); 102 103 /** 104 * @brief Not equal to double value. 105 * @param field The field name. 106 * @param value The field value. 107 * @return This query 108 */ 109 DataQuery &NotEqualTo(const std::string &field, const double value); 110 111 /** 112 * @brief Not equal to string value. 113 * @param field The field name. 114 * @param value The field value. 115 * @return This query 116 */ 117 DataQuery &NotEqualTo(const std::string &field, const std::string &value); 118 119 /** 120 * @brief Not equal to boolean value. 121 * @param field The field name. 122 * @param value The field value. 123 * @return This query 124 */ 125 DataQuery &NotEqualTo(const std::string &field, const bool value); 126 127 /** 128 * @brief Greater than int value. 129 * @param field The field name. 130 * @param value The field value. 131 * @return This query 132 */ 133 DataQuery &GreaterThan(const std::string &field, const int value); 134 135 /** 136 * @brief Greater than long value. 137 * @param field The field name. 138 * @param value The field value. 139 * @return This query 140 */ 141 DataQuery &GreaterThan(const std::string &field, const int64_t value); 142 143 /** 144 * @brief Greater than double value. 145 * @param field The field name. 146 * @param value The field value. 147 * @return This query 148 */ 149 DataQuery &GreaterThan(const std::string &field, const double value); 150 151 /** 152 * @brief Greater than string value. 153 * @param field The field name. 154 * @param value The field value. 155 * @return This query 156 */ 157 DataQuery &GreaterThan(const std::string &field, const std::string &value); 158 159 /** 160 * @brief Less than int value. 161 * @param field The field name. 162 * @param value The field value. 163 * @return This query 164 */ 165 DataQuery &LessThan(const std::string &field, const int value); 166 167 /** 168 * @brief Less than long value. 169 * @param field The field name. 170 * @param value The field value. 171 * @return This query 172 */ 173 DataQuery &LessThan(const std::string &field, const int64_t value); 174 175 /** 176 * @brief Less than double value. 177 * @param field The field name. 178 * @param value The field value. 179 * @return This query 180 */ 181 DataQuery &LessThan(const std::string &field, const double value); 182 183 /** 184 * @brief Less than string value. 185 * @param field The field name. 186 * @param value The field value. 187 * @return This query 188 */ 189 DataQuery &LessThan(const std::string &field, const std::string &value); 190 191 /** 192 * @brief Greater than or equal to int value. 193 * @param field The field name. 194 * @param value The field value. 195 * @return This query 196 */ 197 DataQuery &GreaterThanOrEqualTo(const std::string &field, const int value); 198 199 /** 200 * @brief Greater than or equal to long value. 201 * @param field The field name. 202 * @param value The field value. 203 * @return This query 204 */ 205 DataQuery &GreaterThanOrEqualTo(const std::string &field, const int64_t value); 206 207 /** 208 * @brief Greater than or equal to double value. 209 * @param field The field name. 210 * @param value The field value. 211 * @return This query 212 */ 213 DataQuery &GreaterThanOrEqualTo(const std::string &field, const double value); 214 215 /** 216 * @brief Greater than or equal to string value. 217 * @param field The field name. 218 * @param value The field value. 219 * @return This query 220 */ 221 DataQuery &GreaterThanOrEqualTo(const std::string &field, const std::string &value); 222 223 /** 224 * @brief Less than or equal to int value. 225 * @param field The field name. 226 * @param value The field value. 227 * @return This query 228 */ 229 DataQuery &LessThanOrEqualTo(const std::string &field, const int value); 230 231 /** 232 * @brief Less than or equal to long value. 233 * @param field The field name. 234 * @param value The field value. 235 * @return This query 236 */ 237 DataQuery &LessThanOrEqualTo(const std::string &field, const int64_t value); 238 239 /** 240 * @brief Less than or equal to double value. 241 * @param field The field name. 242 * @param value The field value. 243 * @return This query 244 */ 245 DataQuery &LessThanOrEqualTo(const std::string &field, const double value); 246 247 /** 248 * @brief Less than or equal to string value. 249 * @param field The field name. 250 * @param value The field value. 251 * @return This query 252 */ 253 DataQuery &LessThanOrEqualTo(const std::string &field, const std::string &value); 254 255 /** 256 * @brief Is null field value. 257 * @param field The field name. 258 * @return This query 259 */ 260 DataQuery &IsNull(const std::string &field); 261 262 /** 263 * @brief Greater than or equal to small value and less than or equal to large value. 264 * @param field The small value. 265 * @param value The large value. 266 * @return This query 267 */ 268 DataQuery& Between(const std::string& valueLow, const std::string& valueHigh); 269 270 /** 271 * @brief Is not null field value. 272 * @param field The field name. 273 * @return This query 274 */ 275 DataQuery &IsNotNull(const std::string &field); 276 277 /** 278 * @brief In int value list. 279 * @param field The field name. 280 * @param valueList The field value list. 281 * @return This query 282 */ 283 DataQuery &In(const std::string &field, const std::vector<int> &valueList); 284 285 /** 286 * @brief In long value list. 287 * @param field The field name. 288 * @param valueList The field value list. 289 * @return This query 290 */ 291 DataQuery &In(const std::string &field, const std::vector<int64_t> &valueList); 292 293 /** 294 * @brief In double value list. 295 * @param field The field name. 296 * @param valueList The field value list. 297 * @return This query 298 */ 299 DataQuery &In(const std::string &field, const std::vector<double> &valueList); 300 301 /** 302 * @brief In string value list. 303 * @param field The field name. 304 * @param valueList The field value list. 305 * @return This query 306 */ 307 DataQuery &In(const std::string &field, const std::vector<std::string> &valueList); 308 309 /** 310 * @brief Not in int value list. 311 * @param field The field name. 312 * @param valueList The field value list. 313 * @return This query 314 */ 315 DataQuery &NotIn(const std::string &field, const std::vector<int> &valueList); 316 317 /** 318 * @brief Not in long value list. 319 * @param field The field name. 320 * @param valueList The field value list. 321 * @return This query 322 */ 323 DataQuery &NotIn(const std::string &field, const std::vector<int64_t> &valueList); 324 325 /** 326 * @brief Not in double value list. 327 * @param field The field name. 328 * @param valueList The field value list. 329 * @return This query 330 */ 331 DataQuery &NotIn(const std::string &field, const std::vector<double> &valueList); 332 333 /** 334 * @brief Not in string value list. 335 * @param field The field name. 336 * @param valueList The field value list. 337 * @return This query 338 */ 339 DataQuery &NotIn(const std::string &field, const std::vector<std::string> &valueList); 340 341 /** 342 * @brief Like string value. 343 * @param field The field name. 344 * @param value The field value. 345 * @return This query 346 */ 347 DataQuery &Like(const std::string &field, const std::string &value); 348 349 /** 350 * @brief Unlike string value. 351 * @param field The field name. 352 * @param value The field value. 353 * @return This query 354 */ 355 DataQuery &Unlike(const std::string &field, const std::string &value); 356 357 /** 358 * @brief And operator. 359 * @return This query 360 */ 361 DataQuery &And(); 362 363 /** 364 * @brief Or operator. 365 * @return This query 366 */ 367 DataQuery &Or(); 368 369 /** 370 * @brief Order by ascent. 371 * @param field The field name. 372 * @return This query 373 */ 374 DataQuery &OrderByAsc(const std::string &field); 375 376 /** 377 * @brief Order by descent. 378 * @param field The field name. 379 * @return This query 380 */ 381 DataQuery &OrderByDesc(const std::string &field); 382 383 /** 384 * @brief Order by write time. 385 * @param isAsc Is ascent. 386 * @return This query 387 */ 388 DataQuery &OrderByWriteTime(bool isAsc); 389 390 /** 391 * @brief Limit result size. 392 * @param number The number of results. 393 * @param offset Start position. 394 * @return This query 395 */ 396 DataQuery &Limit(const int number, const int offset); 397 398 /** 399 * @brief Begin group. 400 * @return This query 401 */ 402 DataQuery &BeginGroup(); 403 404 /** 405 * @brief End group. 406 * @return This query 407 */ 408 DataQuery &EndGroup(); 409 410 /** 411 * @brief Select results with specified key prefix. 412 * @param prefix key prefix. 413 * @return This query 414 */ 415 DataQuery &KeyPrefix(const std::string &prefix); 416 417 /** 418 * @brief Select results with specified device Identifier. 419 * @param deviceId Device Identifier. 420 * @return This query 421 */ 422 DataQuery &DeviceId(const std::string &deviceId); 423 424 /** 425 * @brief Select results with suggested index. 426 * @param index Suggested index. 427 * @return This query 428 */ 429 DataQuery &SetSuggestIndex(const std::string &index); 430 431 /** 432 * @brief Select results with many keys. 433 * @param keys The vector of keys for query. 434 * @return This query 435 */ 436 DataQuery &InKeys(const std::vector<std::string> &keys); 437 438 /** 439 * @brief Get string representation 440 * @return String representation of this query. 441 */ 442 std::string ToString() const; 443 444 private: 445 446 friend class QueryHelper; 447 friend class DeviceConvertor; 448 friend class Convertor; 449 450 /** 451 * @brief equal to 452 */ 453 static const char * const EQUAL_TO; 454 455 /** 456 * @brief not equal to 457 */ 458 static const char * const NOT_EQUAL_TO; 459 460 /** 461 * @brief greater than 462 */ 463 static const char * const GREATER_THAN; 464 465 /** 466 * @brief less than 467 */ 468 static const char * const LESS_THAN; 469 470 /** 471 * @brief greater than or equal to 472 */ 473 static const char * const GREATER_THAN_OR_EQUAL_TO; 474 475 /** 476 * @brief less than or equal to 477 */ 478 static const char * const LESS_THAN_OR_EQUAL_TO; 479 480 /** 481 * @brief is null 482 */ 483 static const char * const IS_NULL; 484 485 /** 486 * @brief in 487 */ 488 static const char * const IN; 489 490 /** 491 * @brief not in 492 */ 493 static const char * const NOT_IN; 494 495 /** 496 * @brief like 497 */ 498 static const char * const LIKE; 499 500 /** 501 * @brief not like 502 */ 503 static const char * const NOT_LIKE; 504 505 /** 506 * @brief and 507 */ 508 static const char * const AND; 509 510 /** 511 * @brief or 512 */ 513 static const char * const OR; 514 515 /** 516 * @brief order by asc 517 */ 518 static const char * const ORDER_BY_ASC; 519 520 /** 521 * @brief order by desc 522 */ 523 static const char * const ORDER_BY_DESC; 524 525 /** 526 * @brief order by write time 527 */ 528 static const char * const ORDER_BY_WRITE_TIME; 529 530 /** 531 * @brief order by write time asc 532 */ 533 static const char * const IS_ASC; 534 535 /** 536 * @brief order by write time desc 537 */ 538 static const char * const IS_DESC; 539 540 /** 541 * @brief limit 542 */ 543 static const char * const LIMIT; 544 545 /** 546 * @brief space 547 */ 548 static const char * const SPACE; 549 550 /** 551 * @brief '^' 552 */ 553 static const char * const SPECIAL; 554 555 /** 556 * @brief '^' escape 557 */ 558 static const char * const SPECIAL_ESCAPE; 559 560 /** 561 * @brief space escape 562 */ 563 static const char * const SPACE_ESCAPE; 564 565 /** 566 * @brief empty string 567 */ 568 static const char * const EMPTY_STRING; 569 570 /** 571 * @brief start in 572 */ 573 static const char * const START_IN; 574 575 /** 576 * @brief end in 577 */ 578 static const char * const END_IN; 579 580 /** 581 * @brief begin group 582 */ 583 static const char * const BEGIN_GROUP; 584 585 /** 586 * @brief end group 587 */ 588 static const char * const END_GROUP; 589 590 /** 591 * @brief key prefix 592 */ 593 static const char * const KEY_PREFIX; 594 595 /** 596 * @brief device id 597 */ 598 static const char * const DEVICE_ID; 599 600 /** 601 * @brief is not null 602 */ 603 static const char * const IS_NOT_NULL; 604 605 /** 606 * @brief type string 607 */ 608 static const char * const TYPE_STRING; 609 610 /** 611 * @brief type integer 612 */ 613 static const char * const TYPE_INTEGER; 614 615 /** 616 * @brief type long 617 */ 618 static const char * const TYPE_LONG; 619 620 /** 621 * @brief type double 622 */ 623 static const char * const TYPE_DOUBLE; 624 625 /** 626 * @brief type boolean 627 */ 628 static const char * const TYPE_BOOLEAN; 629 630 /** 631 * @brief value true 632 */ 633 static const char * const VALUE_TRUE; 634 635 /** 636 * @brief value false 637 */ 638 static const char * const VALUE_FALSE; 639 640 /** 641 * @brief suggested index 642 */ 643 static const char * const SUGGEST_INDEX; 644 645 /** 646 * @brief in keys 647 */ 648 static const char * const IN_KEYS; 649 650 std::string str_; 651 652 bool hasKeys_ = false; 653 bool hasPrefix_ = false; 654 std::shared_ptr<DistributedDB::Query> query_; 655 std::string deviceId_; 656 std::string prefix_; 657 std::vector<std::string> keys_; 658 659 template<typename T> 660 void AppendCommon(const std::string &keyword, const std::string &fieldType, std::string &field, const T &value); 661 662 void AppendCommonString(const std::string &keyword, const std::string &fieldType, std::string &field, 663 std::string &value); 664 665 void AppendCommonBoolean(const std::string &keyword, const std::string &fieldType, std::string &field, 666 const bool &value); 667 668 void AppendCommonString(const std::string &keyword, std::string &field, std::string &value); 669 670 template<typename T> 671 void AppendCommonList(const std::string &keyword, const std::string &fieldType, std::string &field, 672 const std::vector<T> &valueList); 673 674 void AppendCommonListString(const std::string &keyword, const std::string &fieldType, std::string &field, 675 std::vector<std::string> &valueList); 676 677 void EscapeSpace(std::string &input); 678 679 bool ValidateField(const std::string &field); 680 681 template<typename T> 682 std::string BasicToString(const T &value); 683 }; 684 } // namespace DistributedKv 685 } // namespace OHOS 686 687 #endif // DISTRIBUTED_DATA_QUERY_H 688