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 #define LOG_TAG "DataQuery"
17
18 #include "data_query.h"
19 #include "log_print.h"
20
21 namespace OHOS {
22 namespace DistributedKv {
23 const std::string DataQuery::EQUAL_TO = "^EQUAL";
24 const std::string DataQuery::NOT_EQUAL_TO = "^NOT_EQUAL";
25 const std::string DataQuery::GREATER_THAN = "^GREATER";
26 const std::string DataQuery::LESS_THAN = "^LESS";
27 const std::string DataQuery::GREATER_THAN_OR_EQUAL_TO = "^GREATER_EQUAL";
28 const std::string DataQuery::LESS_THAN_OR_EQUAL_TO = "^LESS_EQUAL";
29 const std::string DataQuery::IS_NULL = "^IS_NULL";
30 const std::string DataQuery::IN = "^IN";
31 const std::string DataQuery::NOT_IN = "^NOT_IN";
32 const std::string DataQuery::LIKE = "^LIKE";
33 const std::string DataQuery::NOT_LIKE = "^NOT_LIKE";
34 const std::string DataQuery::AND = "^AND";
35 const std::string DataQuery::OR = "^OR";
36 const std::string DataQuery::ORDER_BY_ASC = "^ASC";
37 const std::string DataQuery::ORDER_BY_DESC = "^DESC";
38 const std::string DataQuery::LIMIT = "^LIMIT";
39 const std::string DataQuery::SPACE = " ";
40 const std::string DataQuery::SPECIAL = "^";
41 const std::string DataQuery::SPECIAL_ESCAPE = "(^)";
42 const std::string DataQuery::SPACE_ESCAPE = "^^";
43 const std::string DataQuery::EMPTY_STRING = "^EMPTY_STRING";
44 const std::string DataQuery::START_IN = "^START";
45 const std::string DataQuery::END_IN = "^END";
46 const std::string DataQuery::BEGIN_GROUP = "^BEGIN_GROUP";
47 const std::string DataQuery::END_GROUP = "^END_GROUP";
48 const std::string DataQuery::KEY_PREFIX = "^KEY_PREFIX";
49 const std::string DataQuery::DEVICE_ID = "^DEVICE_ID";
50 const std::string DataQuery::IS_NOT_NULL = "^IS_NOT_NULL";
51 const std::string DataQuery::TYPE_STRING = "STRING";
52 const std::string DataQuery::TYPE_INTEGER = "INTEGER";
53 const std::string DataQuery::TYPE_LONG = "LONG";
54 const std::string DataQuery::TYPE_DOUBLE = "DOUBLE";
55 const std::string DataQuery::TYPE_BOOLEAN = "BOOL";
56 const std::string DataQuery::VALUE_TRUE = "true";
57 const std::string DataQuery::VALUE_FALSE = "false";
58 const std::string DataQuery::SUGGEST_INDEX = "^SUGGEST_INDEX";
59 const std::string DataQuery::IN_KEYS = "^IN_KEYS";
60 constexpr int MAX_QUERY_LENGTH = 5 * 1024; // Max query string length 5k
61
DataQuery()62 DataQuery::DataQuery()
63 {}
64
Reset()65 DataQuery& DataQuery::Reset()
66 {
67 str_ = "";
68 inkeysFlag_ = false;
69 return *this;
70 }
71
EqualTo(const std::string & field,const int value)72 DataQuery& DataQuery::EqualTo(const std::string &field, const int value)
73 {
74 std::string myField = field;
75 if (ValidateField(myField)) {
76 AppendCommon(EQUAL_TO, TYPE_INTEGER, myField, value);
77 }
78 return *this;
79 }
80
EqualTo(const std::string & field,const int64_t value)81 DataQuery& DataQuery::EqualTo(const std::string &field, const int64_t value)
82 {
83 std::string myField = field;
84 if (ValidateField(myField)) {
85 AppendCommon(EQUAL_TO, TYPE_LONG, myField, value);
86 }
87 return *this;
88 }
89
EqualTo(const std::string & field,const double value)90 DataQuery& DataQuery::EqualTo(const std::string &field, const double value)
91 {
92 std::string myField = field;
93 if (ValidateField(myField)) {
94 AppendCommon(EQUAL_TO, TYPE_DOUBLE, myField, value);
95 }
96 return *this;
97 }
98
EqualTo(const std::string & field,const std::string & value)99 DataQuery& DataQuery::EqualTo(const std::string &field, const std::string &value)
100 {
101 std::string myField = field;
102 std::string myValue = value;
103 if (ValidateField(myField)) {
104 AppendCommonString(EQUAL_TO, TYPE_STRING, myField, myValue);
105 }
106 return *this;
107 }
108
EqualTo(const std::string & field,const bool value)109 DataQuery& DataQuery::EqualTo(const std::string &field, const bool value)
110 {
111 std::string myField = field;
112 if (ValidateField(myField)) {
113 AppendCommonBoolean(EQUAL_TO, TYPE_BOOLEAN, myField, value);
114 }
115 return *this;
116 }
117
NotEqualTo(const std::string & field,const int value)118 DataQuery& DataQuery::NotEqualTo(const std::string &field, const int value)
119 {
120 std::string myField = field;
121 if (ValidateField(myField)) {
122 AppendCommon(NOT_EQUAL_TO, TYPE_INTEGER, myField, value);
123 }
124 return *this;
125 }
126
NotEqualTo(const std::string & field,const int64_t value)127 DataQuery& DataQuery::NotEqualTo(const std::string &field, const int64_t value)
128 {
129 std::string myField = field;
130 if (ValidateField(myField)) {
131 AppendCommon(NOT_EQUAL_TO, TYPE_LONG, myField, value);
132 }
133 return *this;
134 }
135
NotEqualTo(const std::string & field,const double value)136 DataQuery& DataQuery::NotEqualTo(const std::string &field, const double value)
137 {
138 std::string myField = field;
139 if (ValidateField(myField)) {
140 AppendCommon(NOT_EQUAL_TO, TYPE_DOUBLE, myField, value);
141 }
142 return *this;
143 }
144
NotEqualTo(const std::string & field,const std::string & value)145 DataQuery& DataQuery::NotEqualTo(const std::string &field, const std::string &value)
146 {
147 std::string myField = field;
148 std::string myValue = value;
149 if (ValidateField(myField)) {
150 AppendCommonString(NOT_EQUAL_TO, TYPE_STRING, myField, myValue);
151 }
152 return *this;
153 }
154
NotEqualTo(const std::string & field,const bool value)155 DataQuery& DataQuery::NotEqualTo(const std::string &field, const bool value)
156 {
157 std::string myField = field;
158 if (ValidateField(myField)) {
159 AppendCommonBoolean(NOT_EQUAL_TO, TYPE_BOOLEAN, myField, value);
160 }
161 return *this;
162 }
163
GreaterThan(const std::string & field,const int value)164 DataQuery& DataQuery::GreaterThan(const std::string &field, const int value)
165 {
166 std::string myField = field;
167 if (ValidateField(myField)) {
168 AppendCommon(GREATER_THAN, TYPE_INTEGER, myField, value);
169 }
170 return *this;
171 }
172
GreaterThan(const std::string & field,const int64_t value)173 DataQuery& DataQuery::GreaterThan(const std::string &field, const int64_t value)
174 {
175 std::string myField = field;
176 if (ValidateField(myField)) {
177 AppendCommon(GREATER_THAN, TYPE_LONG, myField, value);
178 }
179 return *this;
180 }
181
GreaterThan(const std::string & field,const double value)182 DataQuery& DataQuery::GreaterThan(const std::string &field, const double value)
183 {
184 std::string myField = field;
185 if (ValidateField(myField)) {
186 AppendCommon(GREATER_THAN, TYPE_DOUBLE, myField, value);
187 }
188 return *this;
189 }
190
GreaterThan(const std::string & field,const std::string & value)191 DataQuery& DataQuery::GreaterThan(const std::string &field, const std::string &value)
192 {
193 std::string myField = field;
194 std::string myValue = value;
195 if (ValidateField(myField)) {
196 AppendCommonString(GREATER_THAN, TYPE_STRING, myField, myValue);
197 }
198 return *this;
199 }
200
LessThan(const std::string & field,const int value)201 DataQuery& DataQuery::LessThan(const std::string &field, const int value)
202 {
203 std::string myField = field;
204 if (ValidateField(myField)) {
205 AppendCommon(LESS_THAN, TYPE_INTEGER, myField, value);
206 }
207 return *this;
208 }
209
LessThan(const std::string & field,const int64_t value)210 DataQuery& DataQuery::LessThan(const std::string &field, const int64_t value)
211 {
212 std::string myField = field;
213 if (ValidateField(myField)) {
214 AppendCommon(LESS_THAN, TYPE_LONG, myField, value);
215 }
216 return *this;
217 }
218
LessThan(const std::string & field,const double value)219 DataQuery& DataQuery::LessThan(const std::string &field, const double value)
220 {
221 std::string myField = field;
222 if (ValidateField(myField)) {
223 AppendCommon(LESS_THAN, TYPE_DOUBLE, myField, value);
224 }
225 return *this;
226 }
227
LessThan(const std::string & field,const std::string & value)228 DataQuery& DataQuery::LessThan(const std::string &field, const std::string &value)
229 {
230 std::string myField = field;
231 std::string myValue = value;
232 if (ValidateField(myField)) {
233 AppendCommonString(LESS_THAN, TYPE_STRING, myField, myValue);
234 }
235 return *this;
236 }
237
GreaterThanOrEqualTo(const std::string & field,const int value)238 DataQuery& DataQuery::GreaterThanOrEqualTo(const std::string &field, const int value)
239 {
240 std::string myField = field;
241 if (ValidateField(myField)) {
242 AppendCommon(GREATER_THAN_OR_EQUAL_TO, TYPE_INTEGER, myField, value);
243 }
244 return *this;
245 }
246
GreaterThanOrEqualTo(const std::string & field,const int64_t value)247 DataQuery& DataQuery::GreaterThanOrEqualTo(const std::string &field, const int64_t value)
248 {
249 std::string myField = field;
250 if (ValidateField(myField)) {
251 AppendCommon(GREATER_THAN_OR_EQUAL_TO, TYPE_LONG, myField, value);
252 }
253 return *this;
254 }
255
GreaterThanOrEqualTo(const std::string & field,const double value)256 DataQuery& DataQuery::GreaterThanOrEqualTo(const std::string &field, const double value)
257 {
258 std::string myField = field;
259 if (ValidateField(myField)) {
260 AppendCommon(GREATER_THAN_OR_EQUAL_TO, TYPE_DOUBLE, myField, value);
261 }
262 return *this;
263 }
264
GreaterThanOrEqualTo(const std::string & field,const std::string & value)265 DataQuery& DataQuery::GreaterThanOrEqualTo(const std::string &field, const std::string &value)
266 {
267 std::string myField = field;
268 std::string myValue = value;
269 if (ValidateField(myField)) {
270 AppendCommonString(GREATER_THAN_OR_EQUAL_TO, TYPE_STRING, myField, myValue);
271 }
272 return *this;
273 }
274
LessThanOrEqualTo(const std::string & field,const int value)275 DataQuery& DataQuery::LessThanOrEqualTo(const std::string &field, const int value)
276 {
277 std::string myField = field;
278 if (ValidateField(myField)) {
279 AppendCommon(LESS_THAN_OR_EQUAL_TO, TYPE_INTEGER, myField, value);
280 }
281 return *this;
282 }
283
LessThanOrEqualTo(const std::string & field,const int64_t value)284 DataQuery& DataQuery::LessThanOrEqualTo(const std::string &field, const int64_t value)
285 {
286 std::string myField = field;
287 if (ValidateField(myField)) {
288 AppendCommon(LESS_THAN_OR_EQUAL_TO, TYPE_LONG, myField, value);
289 }
290 return *this;
291 }
292
LessThanOrEqualTo(const std::string & field,const double value)293 DataQuery& DataQuery::LessThanOrEqualTo(const std::string &field, const double value)
294 {
295 std::string myField = field;
296 if (ValidateField(myField)) {
297 AppendCommon(LESS_THAN_OR_EQUAL_TO, TYPE_DOUBLE, myField, value);
298 }
299 return *this;
300 }
301
LessThanOrEqualTo(const std::string & field,const std::string & value)302 DataQuery& DataQuery::LessThanOrEqualTo(const std::string &field, const std::string &value)
303 {
304 std::string myField = field;
305 std::string myValue = value;
306 if (ValidateField(myField)) {
307 AppendCommonString(LESS_THAN_OR_EQUAL_TO, TYPE_STRING, myField, myValue);
308 }
309 return *this;
310 }
311
IsNull(const std::string & field)312 DataQuery& DataQuery::IsNull(const std::string &field)
313 {
314 std::string myField = field;
315 if (ValidateField(myField)) {
316 str_.append(SPACE);
317 str_.append(IS_NULL);
318 str_.append(SPACE);
319 EscapeSpace(myField);
320 str_.append(myField);
321 }
322 return *this;
323 }
324
IsNotNull(const std::string & field)325 DataQuery& DataQuery::IsNotNull(const std::string &field)
326 {
327 std::string myField = field;
328 if (ValidateField(myField)) {
329 str_.append(SPACE);
330 str_.append(IS_NOT_NULL);
331 str_.append(SPACE);
332 EscapeSpace(myField);
333 str_.append(myField);
334 }
335 return *this;
336 }
337
InInt(const std::string & field,const std::vector<int> & valueList)338 DataQuery& DataQuery::InInt(const std::string &field, const std::vector<int> &valueList)
339 {
340 std::string myField = field;
341 if (ValidateField(myField)) {
342 AppendCommonList(IN, TYPE_INTEGER, myField, valueList);
343 }
344 return *this;
345 }
346
InLong(const std::string & field,const std::vector<int64_t> & valueList)347 DataQuery& DataQuery::InLong(const std::string &field, const std::vector<int64_t> &valueList)
348 {
349 std::string myField = field;
350 if (ValidateField(myField)) {
351 AppendCommonList(IN, TYPE_LONG, myField, valueList);
352 }
353 return *this;
354 }
355
InDouble(const std::string & field,const std::vector<double> & valueList)356 DataQuery& DataQuery::InDouble(const std::string &field, const std::vector<double> &valueList)
357 {
358 std::string myField = field;
359 if (ValidateField(myField)) {
360 AppendCommonList(IN, TYPE_DOUBLE, myField, valueList);
361 }
362 return *this;
363 }
364
InString(const std::string & field,const std::vector<std::string> & valueList)365 DataQuery& DataQuery::InString(const std::string &field, const std::vector<std::string> &valueList)
366 {
367 std::string myField = field;
368 std::vector<std::string> myValueList(valueList);
369 if (ValidateField(myField)) {
370 AppendCommonListString(IN, TYPE_STRING, myField, myValueList);
371 }
372 return *this;
373 }
374
NotInInt(const std::string & field,const std::vector<int> & valueList)375 DataQuery& DataQuery::NotInInt(const std::string &field, const std::vector<int> &valueList)
376 {
377 std::string myField = field;
378 if (ValidateField(myField)) {
379 AppendCommonList(NOT_IN, TYPE_INTEGER, myField, valueList);
380 }
381 return *this;
382 }
383
NotInLong(const std::string & field,const std::vector<int64_t> & valueList)384 DataQuery& DataQuery::NotInLong(const std::string &field, const std::vector<int64_t> &valueList)
385 {
386 std::string myField = field;
387 if (ValidateField(myField)) {
388 AppendCommonList(NOT_IN, TYPE_LONG, myField, valueList);
389 }
390 return *this;
391 }
392
NotInDouble(const std::string & field,const std::vector<double> & valueList)393 DataQuery& DataQuery::NotInDouble(const std::string &field, const std::vector<double> &valueList)
394 {
395 std::string myField = field;
396 if (ValidateField(myField)) {
397 AppendCommonList(NOT_IN, TYPE_DOUBLE, myField, valueList);
398 }
399 return *this;
400 }
401
NotInString(const std::string & field,const std::vector<std::string> & valueList)402 DataQuery& DataQuery::NotInString(const std::string &field, const std::vector<std::string> &valueList)
403 {
404 std::string myField = field;
405 std::vector<std::string> myValueList(valueList);
406 if (ValidateField(myField)) {
407 AppendCommonListString(NOT_IN, TYPE_STRING, myField, myValueList);
408 }
409 return *this;
410 }
411
Like(const std::string & field,const std::string & value)412 DataQuery& DataQuery::Like(const std::string &field, const std::string &value)
413 {
414 std::string myField = field;
415 std::string myValue = value;
416 if (ValidateField(myField)) {
417 AppendCommonString(LIKE, myField, myValue);
418 }
419 return *this;
420 }
421
Unlike(const std::string & field,const std::string & value)422 DataQuery& DataQuery::Unlike(const std::string &field, const std::string &value)
423 {
424 std::string myField = field;
425 std::string myValue = value;
426 if (ValidateField(myField)) {
427 AppendCommonString(NOT_LIKE, myField, myValue);
428 }
429 return *this;
430 }
431
And()432 DataQuery& DataQuery::And()
433 {
434 str_.append(SPACE);
435 str_.append(AND);
436 return *this;
437 }
438
Or()439 DataQuery& DataQuery::Or()
440 {
441 str_.append(SPACE);
442 str_.append(OR);
443 return *this;
444 }
445
OrderByAsc(const std::string & field)446 DataQuery& DataQuery::OrderByAsc(const std::string &field)
447 {
448 std::string myField = field;
449 if (ValidateField(myField)) {
450 str_.append(SPACE);
451 str_.append(ORDER_BY_ASC);
452 str_.append(SPACE);
453 EscapeSpace(myField);
454 str_.append(myField);
455 }
456 return *this;
457 }
458
OrderByDesc(const std::string & field)459 DataQuery& DataQuery::OrderByDesc(const std::string &field)
460 {
461 std::string myField = field;
462 if (ValidateField(myField)) {
463 str_.append(SPACE);
464 str_.append(ORDER_BY_DESC);
465 str_.append(SPACE);
466 EscapeSpace(myField);
467 str_.append(myField);
468 }
469 return *this;
470 }
471
Limit(const int number,const int offset)472 DataQuery& DataQuery::Limit(const int number, const int offset)
473 {
474 if (number < 0 || offset < 0) {
475 ZLOGE("Invalid number param");
476 return *this;
477 }
478 str_.append(SPACE);
479 str_.append(LIMIT);
480 str_.append(SPACE);
481 str_.append(BasicToString(number));
482 str_.append(SPACE);
483 str_.append(BasicToString(offset));
484 return *this;
485 }
486
BeginGroup()487 DataQuery& DataQuery::BeginGroup()
488 {
489 str_.append(SPACE);
490 str_.append(BEGIN_GROUP);
491 return *this;
492 }
493
EndGroup()494 DataQuery& DataQuery::EndGroup()
495 {
496 str_.append(SPACE);
497 str_.append(END_GROUP);
498 return *this;
499 }
500
KeyPrefix(const std::string & prefix)501 DataQuery& DataQuery::KeyPrefix(const std::string &prefix)
502 {
503 std::string myPrefix = prefix;
504 if (ValidateField(myPrefix)) {
505 str_.append(SPACE);
506 str_.append(KEY_PREFIX);
507 str_.append(SPACE);
508 EscapeSpace(myPrefix);
509 str_.append(myPrefix);
510 }
511 return *this;
512 }
513
DeviceId(const std::string & deviceId)514 DataQuery& DataQuery::DeviceId(const std::string &deviceId)
515 {
516 std::string device = deviceId;
517 if (ValidateField(device)) {
518 std::string start;
519 start.append(SPACE);
520 start.append(DEVICE_ID);
521 start.append(SPACE);
522 EscapeSpace(device);
523 start.append(device);
524 str_ = start + str_; // start with diveceId
525 }
526 return *this;
527 }
528
SetSuggestIndex(const std::string & index)529 DataQuery& DataQuery::SetSuggestIndex(const std::string &index)
530 {
531 std::string suggestIndex = index;
532 if (ValidateField(suggestIndex)) {
533 str_.append(SPACE);
534 str_.append(SUGGEST_INDEX);
535 str_.append(SPACE);
536 EscapeSpace(suggestIndex);
537 str_.append(suggestIndex);
538 }
539 return *this;
540 }
541
InKeys(const std::vector<std::string> & keys)542 DataQuery& DataQuery::InKeys(const std::vector<std::string> &keys)
543 {
544 if (keys.empty()) {
545 ZLOGE("Invalid number param");
546 return *this;
547 }
548 if (inkeysFlag_) {
549 ZLOGE("cannot set inkeys more than once");
550 return *this;
551 }
552 inkeysFlag_ = true;
553 str_.append(SPACE);
554 str_.append(IN_KEYS);
555 str_.append(SPACE);
556 str_.append(START_IN);
557 str_.append(SPACE);
558 for (std::string key : keys) {
559 if (ValidateField(key)) {
560 EscapeSpace(key);
561 str_.append(key);
562 str_.append(SPACE);
563 }
564 }
565 str_.append(END_IN);
566 return *this;
567 }
568
ToString() const569 std::string DataQuery::ToString() const
570 {
571 if (str_.length() > MAX_QUERY_LENGTH) {
572 ZLOGE("Query is too long");
573 return std::string();
574 }
575 std::string str(str_.begin(), str_.end());
576 return str;
577 }
578
579 template<typename T>
AppendCommon(const std::string & keyword,const std::string & fieldType,std::string & field,const T & value)580 void DataQuery::AppendCommon(const std::string &keyword, const std::string &fieldType,
581 std::string &field, const T &value)
582 {
583 str_.append(SPACE);
584 str_.append(keyword);
585 str_.append(SPACE);
586 str_.append(fieldType);
587 str_.append(SPACE);
588 EscapeSpace(field);
589 str_.append(field);
590 str_.append(SPACE);
591 str_.append(BasicToString(value));
592 }
593
AppendCommonString(const std::string & keyword,const std::string & fieldType,std::string & field,std::string & value)594 void DataQuery::AppendCommonString(const std::string &keyword, const std::string &fieldType,
595 std::string &field, std::string &value)
596 {
597 str_.append(SPACE);
598 str_.append(keyword);
599 str_.append(SPACE);
600 str_.append(fieldType);
601 str_.append(SPACE);
602 EscapeSpace(field);
603 str_.append(field);
604 str_.append(SPACE);
605 EscapeSpace(value);
606 str_.append(value);
607 }
608
AppendCommonBoolean(const std::string & keyword,const std::string & fieldType,std::string & field,const bool & value)609 void DataQuery::AppendCommonBoolean(const std::string &keyword, const std::string &fieldType,
610 std::string &field, const bool &value)
611 {
612 str_.append(SPACE);
613 str_.append(keyword);
614 str_.append(SPACE);
615 str_.append(fieldType);
616 str_.append(SPACE);
617 EscapeSpace(field);
618 str_.append(field);
619 str_.append(SPACE);
620 if (value) {
621 str_.append(VALUE_TRUE);
622 } else {
623 str_.append(VALUE_FALSE);
624 }
625 }
626
AppendCommonString(const std::string & keyword,std::string & field,std::string & value)627 void DataQuery::AppendCommonString(const std::string &keyword, std::string &field, std::string &value)
628 {
629 str_.append(SPACE);
630 str_.append(keyword);
631 str_.append(SPACE);
632 EscapeSpace(field);
633 str_.append(field);
634 str_.append(SPACE);
635 EscapeSpace(value);
636 str_.append(value);
637 }
638
639 template<typename T>
AppendCommonList(const std::string & keyword,const std::string & fieldType,std::string & field,const std::vector<T> & valueList)640 void DataQuery::AppendCommonList(const std::string &keyword, const std::string &fieldType,
641 std::string &field, const std::vector<T> &valueList)
642 {
643 str_.append(SPACE);
644 str_.append(keyword);
645 str_.append(SPACE);
646 str_.append(fieldType);
647 str_.append(SPACE);
648 EscapeSpace(field);
649 str_.append(field);
650 str_.append(SPACE);
651 str_.append(START_IN);
652 str_.append(SPACE);
653 for (T object : valueList) {
654 str_.append(BasicToString(object));
655 str_.append(SPACE);
656 }
657 str_.append(END_IN);
658 }
659
AppendCommonListString(const std::string & keyword,const std::string & fieldType,std::string & field,std::vector<std::string> & valueList)660 void DataQuery::AppendCommonListString(const std::string &keyword, const std::string &fieldType,
661 std::string &field, std::vector<std::string> &valueList)
662 {
663 str_.append(SPACE);
664 str_.append(keyword);
665 str_.append(SPACE);
666 str_.append(fieldType);
667 str_.append(SPACE);
668 EscapeSpace(field);
669 str_.append(field);
670 str_.append(SPACE);
671 str_.append(START_IN);
672 str_.append(SPACE);
673 for (std::string str : valueList) {
674 EscapeSpace(str);
675 str_.append(str);
676 str_.append(SPACE);
677 }
678 str_.append(END_IN);
679 }
680
EscapeSpace(std::string & input)681 void DataQuery::EscapeSpace(std::string &input)
682 {
683 if (input.length() == 0) {
684 input = EMPTY_STRING;
685 }
686 size_t index = 0; // search from the beginning of the string
687 while (true) {
688 index = input.find(DataQuery::SPECIAL, index);
689 if (index == std::string::npos) {
690 break;
691 }
692 input.replace(index, 1, DataQuery::SPECIAL_ESCAPE); // 1 char to be replaced
693 index += 3; // replaced with 3 chars, keep searching the remaining string
694 }
695 index = 0; // search from the beginning of the string
696 while (true) {
697 index = input.find(DataQuery::SPACE, index);
698 if (index == std::string::npos) {
699 break;
700 }
701 input.replace(index, 1, DataQuery::SPACE_ESCAPE); // 1 char to be replaced
702 index += 2; // replaced with 2 chars, keep searching the remaining string
703 }
704 }
705
ValidateField(const std::string & field)706 bool DataQuery::ValidateField(const std::string &field)
707 {
708 if (field.empty() || field.find(DataQuery::SPECIAL) != std::string::npos) {
709 ZLOGE("invalid string argument");
710 return false;
711 }
712 return true;
713 }
714
715 template<typename T>
BasicToString(const T & value)716 std::string DataQuery::BasicToString(const T &value)
717 {
718 std::ostringstream oss;
719 oss << value;
720 return oss.str();
721 }
722 } // namespace DistributedKv
723 } // namespace OHOS
724