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 "visibility.h" 23 24 namespace OHOS { 25 namespace DistributedKv { 26 class DataQuery { 27 public: 28 KVSTORE_API DataQuery(); 29 30 KVSTORE_API ~DataQuery() = default; 31 32 // Reset the query. 33 // Return: 34 // This Query. 35 KVSTORE_API DataQuery& Reset(); 36 37 // Equal to int value. 38 // Parameters: 39 // field: the field name. 40 // value: the field value. 41 // Return: 42 // This Query. 43 KVSTORE_API DataQuery& EqualTo(const std::string &field, const int value); 44 45 // Equal to long value. 46 // Parameters: 47 // field: the field name. 48 // value: the field value. 49 // Return: 50 // This Query. 51 KVSTORE_API DataQuery& EqualTo(const std::string &field, const int64_t value); 52 53 // Equal to double value. 54 // Parameters: 55 // field: the field name. 56 // value: the field value. 57 // Return: 58 // This Query. 59 KVSTORE_API DataQuery& EqualTo(const std::string &field, const double value); 60 61 // Equal to String value. 62 // Parameters: 63 // field: the field name. 64 // value: the field value. 65 // Return: 66 // This Query. 67 KVSTORE_API DataQuery& EqualTo(const std::string &field, const std::string &value); 68 69 // Equal to boolean value. 70 // Parameters: 71 // field: the field name. 72 // value: the field value. 73 // Return: 74 // This Query. 75 KVSTORE_API DataQuery& EqualTo(const std::string &field, const bool value); 76 77 // Not equal to int value. 78 // Parameters: 79 // field: the field name. 80 // value: the field value. 81 // Return: 82 // This Query. 83 KVSTORE_API DataQuery& NotEqualTo(const std::string &field, const int value); 84 85 // Not equal to long value. 86 // Parameters: 87 // field: the field name. 88 // value: the field value. 89 // Return: 90 // This Query. 91 KVSTORE_API DataQuery& NotEqualTo(const std::string &field, const int64_t value); 92 93 // Not equal to double value. 94 // Parameters: 95 // field: the field name. 96 // value: the field value. 97 // Return: 98 // This Query. 99 KVSTORE_API DataQuery& NotEqualTo(const std::string &field, const double value); 100 101 // Not equal to String value. 102 // Parameters: 103 // field: the field name. 104 // value: the field value. 105 // Return: 106 // This Query. 107 KVSTORE_API DataQuery& NotEqualTo(const std::string &field, const std::string &value); 108 109 // Not equal to boolean value. 110 // Parameters: 111 // field: the field name. 112 // value: the field value. 113 // Return: 114 // This Query. 115 KVSTORE_API DataQuery& NotEqualTo(const std::string &field, const bool value); 116 117 // Greater than int value. 118 // Parameters: 119 // field: the field name. 120 // value: the field value. 121 // Return: 122 // This Query. 123 KVSTORE_API DataQuery& GreaterThan(const std::string &field, const int value); 124 125 // Greater than long value. 126 // Parameters: 127 // field: the field name. 128 // value: the field value. 129 // Return: 130 // This Query. 131 KVSTORE_API DataQuery& GreaterThan(const std::string &field, const int64_t value); 132 133 // Greater than double value. 134 // Parameters: 135 // field: the field name. 136 // value: the field value. 137 // Return: 138 // This Query. 139 KVSTORE_API DataQuery& GreaterThan(const std::string &field, const double value); 140 141 // Greater than String value. 142 // Parameters: 143 // field: the field name. 144 // value: the field value. 145 // Return: 146 // This Query. 147 KVSTORE_API DataQuery& GreaterThan(const std::string &field, const std::string &value); 148 149 // Less than int value. 150 // Parameters: 151 // field: the field name. 152 // value: the field value. 153 // Return: 154 // This Query. 155 KVSTORE_API DataQuery& LessThan(const std::string &field, const int value); 156 157 // Less than long value. 158 // Parameters: 159 // field: the field name. 160 // value: the field value. 161 // Return: 162 // This Query. 163 KVSTORE_API DataQuery& LessThan(const std::string &field, const int64_t value); 164 165 // Less than double value. 166 // Parameters: 167 // field: the field name. 168 // value: the field value. 169 // Return: 170 // This Query. 171 KVSTORE_API DataQuery& LessThan(const std::string &field, const double value); 172 173 // Less than String value. 174 // Parameters: 175 // field: the field name. 176 // value: the field value. 177 // Return: 178 // This Query. 179 KVSTORE_API DataQuery& LessThan(const std::string &field, const std::string &value); 180 181 // Greater than or equal to int value. 182 // Parameters: 183 // field: the field name. 184 // value: the field value. 185 // Return: 186 // This Query. 187 KVSTORE_API DataQuery& GreaterThanOrEqualTo(const std::string &field, const int value); 188 189 // Greater than or equal to long value. 190 // Parameters: 191 // field: the field name. 192 // value: the field value. 193 // Return: 194 // This Query. 195 KVSTORE_API DataQuery& GreaterThanOrEqualTo(const std::string &field, const int64_t value); 196 197 // Greater than or equal to double value. 198 // Parameters: 199 // field: the field name. 200 // value: the field value. 201 // Return: 202 // This Query. 203 KVSTORE_API DataQuery& GreaterThanOrEqualTo(const std::string &field, const double value); 204 205 // Greater than or equal to String value. 206 // Parameters: 207 // field: the field name. 208 // value: the field value. 209 // Return: 210 // This Query. 211 KVSTORE_API DataQuery& GreaterThanOrEqualTo(const std::string &field, const std::string &value); 212 213 // Less than or equal to int value. 214 // Parameters: 215 // field: the field name. 216 // value: the field value. 217 // Return: 218 // This Query. 219 KVSTORE_API DataQuery& LessThanOrEqualTo(const std::string &field, const int value); 220 221 // Less than or equal to long value. 222 // Parameters: 223 // field: the field name. 224 // value: the field value. 225 // Return: 226 // This Query. 227 KVSTORE_API DataQuery& LessThanOrEqualTo(const std::string &field, const int64_t value); 228 229 // Less than or equal to double value. 230 // Parameters: 231 // field: the field name. 232 // value: the field value. 233 // Return: 234 // This Query. 235 KVSTORE_API DataQuery& LessThanOrEqualTo(const std::string &field, const double value); 236 237 // Less than or equal to String value. 238 // Parameters: 239 // field: the field name. 240 // value: the field value. 241 // Return: 242 // This Query. 243 KVSTORE_API DataQuery& LessThanOrEqualTo(const std::string &field, const std::string &value); 244 245 // Is null field value. 246 // Parameters: 247 // field: the field name. 248 // Return: 249 // This Query. 250 KVSTORE_API DataQuery& IsNull(const std::string &field); 251 252 // Is not null field value. 253 // Parameters: 254 // field: the field name. 255 // Return: 256 // This Query. 257 KVSTORE_API DataQuery& IsNotNull(const std::string &field); 258 259 // In int value list. 260 // Parameters: 261 // field: the field name. 262 // value: the field value list. 263 // Return: 264 // This Query. 265 KVSTORE_API DataQuery& InInt(const std::string &field, const std::vector<int> &valueList); 266 267 // In long value list. 268 // Parameters: 269 // field: the field name. 270 // value: the field value list. 271 // Return: 272 // This Query. 273 KVSTORE_API DataQuery& InLong(const std::string &field, const std::vector<int64_t> &valueList); 274 275 // In Double value list. 276 // Parameters: 277 // field: the field name. 278 // value: the field value list. 279 // Return: 280 // This Query. 281 KVSTORE_API DataQuery& InDouble(const std::string &field, const std::vector<double> &valueList); 282 283 // In String value list. 284 // Parameters: 285 // field: the field name. 286 // value: the field value list. 287 // Return: 288 // This Query. 289 KVSTORE_API DataQuery& InString(const std::string &field, const std::vector<std::string> &valueList); 290 291 // Not in int value list. 292 // Parameters: 293 // field: the field name. 294 // value: the field value list. 295 // Return: 296 // This Query. 297 KVSTORE_API DataQuery& NotInInt(const std::string &field, const std::vector<int> &valueList); 298 299 // Not in long value list. 300 // Parameters: 301 // field: the field name. 302 // value: the field value list. 303 // Return: 304 // This Query. 305 KVSTORE_API DataQuery& NotInLong(const std::string &field, const std::vector<int64_t> &valueList); 306 307 // Not in Double value list. 308 // Parameters: 309 // field: the field name. 310 // value: the field value list. 311 // Return: 312 // This Query. 313 KVSTORE_API DataQuery& NotInDouble(const std::string &field, const std::vector<double> &valueList); 314 315 // Not in String value list. 316 // Parameters: 317 // field: the field name. 318 // value: the field value list. 319 // Return: 320 // This Query. 321 KVSTORE_API DataQuery& NotInString(const std::string &field, const std::vector<std::string> &valueList); 322 323 // Like String value. 324 // Parameters: 325 // field: the field name. 326 // value: the field value list. 327 // Return: 328 // This Query. 329 KVSTORE_API DataQuery& Like(const std::string &field, const std::string &value); 330 331 // Unlike String value. 332 // Parameters: 333 // field: the field name. 334 // value: the field value list. 335 // Return: 336 // This Query. 337 KVSTORE_API DataQuery& Unlike(const std::string &field, const std::string &value); 338 339 // And operator. 340 // Return: 341 // This Query. 342 KVSTORE_API DataQuery& And(); 343 344 // Or operator. 345 // Return: 346 // This Query. 347 KVSTORE_API DataQuery& Or(); 348 349 // Order by ascent. 350 // Parameters: 351 // field: the field name. 352 // Return: 353 // This Query. 354 KVSTORE_API DataQuery& OrderByAsc(const std::string &field); 355 356 // Order by descent. 357 // Parameters: 358 // field: the field name. 359 // Return: 360 // This Query. 361 KVSTORE_API DataQuery& OrderByDesc(const std::string &field); 362 363 // Limit result size. 364 // Parameters: 365 // number: the number of results. 366 // offset: the start position. 367 // Return: 368 // This Query. 369 KVSTORE_API DataQuery& Limit(const int number, const int offset); 370 371 // Begin group. 372 // Return: 373 // This Query. 374 KVSTORE_API DataQuery& BeginGroup(); 375 376 // End group. 377 // Return: 378 // This Query. 379 KVSTORE_API DataQuery& EndGroup(); 380 381 // Select results with specified key prefix. 382 // Parameters: 383 // prefix: key prefix. 384 // Return: 385 // This Query. 386 KVSTORE_API DataQuery& KeyPrefix(const std::string &prefix); 387 388 // Select results with specified device Identifier. 389 // Parameters: 390 // deviceId: device Identifier. 391 // Return: 392 // This Query. 393 KVSTORE_API DataQuery& DeviceId(const std::string &deviceId); 394 395 // Select results with suggested index. 396 // Parameters: 397 // index: suggested index. 398 // Return: 399 // This Query. 400 KVSTORE_API DataQuery& SetSuggestIndex(const std::string &index); 401 402 // Select results with many keys. 403 // Parameters: 404 // keys: the vector of keys for query 405 // Return: 406 // This Query. 407 KVSTORE_API DataQuery& InKeys(const std::vector<std::string> &keys); 408 409 // Get string representation 410 // Return: 411 // String representation of this query. 412 KVSTORE_API std::string ToString() const; 413 414 // equal to 415 static const std::string EQUAL_TO; 416 417 // not equal to 418 static const std::string NOT_EQUAL_TO; 419 420 // greater than 421 static const std::string GREATER_THAN; 422 423 // less than 424 static const std::string LESS_THAN; 425 426 // greater than or equal to 427 static const std::string GREATER_THAN_OR_EQUAL_TO; 428 429 // less than or equal to 430 static const std::string LESS_THAN_OR_EQUAL_TO; 431 432 // is null 433 static const std::string IS_NULL; 434 435 // in 436 static const std::string IN; 437 438 // not in 439 static const std::string NOT_IN; 440 441 // like 442 static const std::string LIKE; 443 444 // not like 445 static const std::string NOT_LIKE; 446 447 // and 448 static const std::string AND; 449 450 // or 451 static const std::string OR; 452 453 // order by asc 454 static const std::string ORDER_BY_ASC; 455 456 // order by desc 457 static const std::string ORDER_BY_DESC; 458 459 // limit 460 static const std::string LIMIT; 461 462 // space 463 static const std::string SPACE; 464 465 // '^' 466 static const std::string SPECIAL; 467 468 // '^' escape 469 static const std::string SPECIAL_ESCAPE; 470 471 // space escape 472 static const std::string SPACE_ESCAPE; 473 474 // empty string 475 static const std::string EMPTY_STRING; 476 477 // start in 478 static const std::string START_IN; 479 480 // end in 481 static const std::string END_IN; 482 483 // begin group 484 static const std::string BEGIN_GROUP; 485 486 // end group 487 static const std::string END_GROUP; 488 489 // key prefix 490 static const std::string KEY_PREFIX; 491 492 // device id 493 static const std::string DEVICE_ID; 494 495 // is not null 496 static const std::string IS_NOT_NULL; 497 498 // type string 499 static const std::string TYPE_STRING; 500 501 // type integer 502 static const std::string TYPE_INTEGER; 503 504 // type long 505 static const std::string TYPE_LONG; 506 507 // type double 508 static const std::string TYPE_DOUBLE; 509 510 // type boolean 511 static const std::string TYPE_BOOLEAN; 512 513 // value true 514 static const std::string VALUE_TRUE; 515 516 // value false 517 static const std::string VALUE_FALSE; 518 519 // suggested index 520 static const std::string SUGGEST_INDEX; 521 522 // in keys 523 static const std::string IN_KEYS; 524 private: 525 std::string str_; 526 527 bool inkeysFlag_ = false; 528 529 template<typename T> 530 void AppendCommon(const std::string &keyword, const std::string &fieldType, std::string &field, const T &value); 531 532 void AppendCommonString(const std::string &keyword, const std::string &fieldType, 533 std::string &field, std::string &value); 534 535 void AppendCommonBoolean(const std::string &keyword, const std::string &fieldType, 536 std::string &field, const bool &value); 537 538 void AppendCommonString(const std::string &keyword, std::string &field, std::string &value); 539 540 template<typename T> 541 void AppendCommonList(const std::string &keyword, const std::string &fieldType, 542 std::string &field, const std::vector<T> &valueList); 543 544 void AppendCommonListString(const std::string &keyword, const std::string &fieldType, 545 std::string &field, std::vector<std::string> &valueList); 546 547 void EscapeSpace(std::string &input); 548 549 bool ValidateField(const std::string &field); 550 551 bool ValidateValue(const std::string &value); 552 553 bool ValidateStringValueList(const std::vector<std::string> &valueList); 554 555 template<typename T> 556 std::string BasicToString(const T &value); 557 }; 558 } // namespace DistributedKv 559 } // namespace OHOS 560 561 #endif // DISTRIBUTED_DATA_QUERY_H 562