1 /*
2 * Copyright (c) 2021-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 #include "sys_event_query.h"
16
17 #include <algorithm>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include "data_query.h"
24 #include "hiview_global.h"
25 #include "store_mgr_proxy.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace EventStore {
30 std::string EventCol::DOMAIN = "domain_";
31 std::string EventCol::NAME = "name_";
32 std::string EventCol::TYPE = "type_";
33 std::string EventCol::TS = "time_";
34 std::string EventCol::TZ = "tz_";
35 std::string EventCol::PID = "pid_";
36 std::string EventCol::TID = "tid_";
37 std::string EventCol::UID = "uid_";
38 std::string EventCol::TRACE_ID = "traceid_";
39 std::string EventCol::TRACE_FLAG = "trace_flag_";
40 std::string EventCol::SPAN_ID = "spanid_";
41 std::string EventCol::PARENT_SPAN_ID = "pspanid_";
42 std::string EventCol::INFO = "info_";
43
IsInteger()44 bool FieldValue::IsInteger()
45 {
46 return (valueType_ == INTEGER);
47 }
48
IsFloat()49 bool FieldValue::IsFloat()
50 {
51 return (valueType_ == FLOAT);
52 }
53
IsDouble()54 bool FieldValue::IsDouble()
55 {
56 return (valueType_ == DOUBLE);
57 }
58
IsString()59 bool FieldValue::IsString()
60 {
61 return (valueType_ == STRING);
62 }
63
GetInteger()64 int64_t FieldValue::GetInteger()
65 {
66 return iValue_;
67 }
68
GetFloat()69 float FieldValue::GetFloat()
70 {
71 return fValue_;
72 }
73
GetDouble()74 double FieldValue::GetDouble()
75 {
76 return dValue_;
77 }
78
GetString()79 const std::string FieldValue::GetString()
80 {
81 return sValue_;
82 }
83
Cond(const std::string & col,Op op,int8_t value)84 Cond::Cond(const std::string &col, Op op, int8_t value): col_(col), op_(op), fieldValue_(value)
85 {
86 }
87
Cond(const std::string & col,Op op,int16_t value)88 Cond::Cond(const std::string &col, Op op, int16_t value): col_(col), op_(op), fieldValue_(value)
89 {
90 }
91
Cond(const std::string & col,Op op,int32_t value)92 Cond::Cond(const std::string &col, Op op, int32_t value): col_(col), op_(op), fieldValue_(value)
93 {
94 }
95
Cond(const std::string & col,Op op,int64_t value)96 Cond::Cond(const std::string &col, Op op, int64_t value): col_(col), op_(op), fieldValue_(value)
97 {
98 }
99
Cond(const std::string & col,Op op,float value)100 Cond::Cond(const std::string &col, Op op, float value): col_(col), op_(op), fieldValue_(value)
101 {
102 }
103
Cond(const std::string & col,Op op,double value)104 Cond::Cond(const std::string &col, Op op, double value): col_(col), op_(op), fieldValue_(value)
105 {
106 }
107
Cond(const std::string & col,Op op,const std::string & value)108 Cond::Cond(const std::string &col, Op op, const std::string &value): col_(col), op_(op), fieldValue_(value)
109 {
110 }
111
Cond(const std::string & col,Op op,const std::vector<int8_t> & ints)112 Cond::Cond(const std::string &col, Op op, const std::vector<int8_t> &ints): op_(NONE), fieldValue_(0)
113 {
114 for (int8_t value : ints) {
115 orConds_.emplace_back(Cond(col, op, value));
116 }
117 }
118
Cond(const std::string & col,Op op,const std::vector<int16_t> & ints)119 Cond::Cond(const std::string &col, Op op, const std::vector<int16_t> &ints): op_(NONE), fieldValue_(0)
120 {
121 for (int16_t value : ints) {
122 orConds_.emplace_back(Cond(col, op, value));
123 }
124 }
125
Cond(const std::string & col,Op op,const std::vector<int32_t> & ints)126 Cond::Cond(const std::string &col, Op op, const std::vector<int32_t> &ints): op_(NONE), fieldValue_(0)
127 {
128 for (int32_t value : ints) {
129 orConds_.emplace_back(Cond(col, op, value));
130 }
131 }
Cond(const std::string & col,Op op,const std::vector<int64_t> & longs)132 Cond::Cond(const std::string &col, Op op, const std::vector<int64_t> &longs): op_(NONE), fieldValue_(0)
133 {
134 for (int64_t value : longs) {
135 orConds_.emplace_back(Cond(col, op, value));
136 }
137 }
Cond(const std::string & col,Op op,const std::vector<float> & floats)138 Cond::Cond(const std::string &col, Op op, const std::vector<float> &floats): op_(NONE), fieldValue_(0)
139 {
140 for (float value : floats) {
141 orConds_.emplace_back(Cond(col, op, value));
142 }
143 }
Cond(const std::string & col,Op op,const std::vector<double> & doubles)144 Cond::Cond(const std::string &col, Op op, const std::vector<double> &doubles): op_(NONE), fieldValue_(0)
145 {
146 for (double value : doubles) {
147 orConds_.emplace_back(Cond(col, op, value));
148 }
149 }
Cond(const std::string & col,Op op,const std::vector<std::string> & strings)150 Cond::Cond(const std::string &col, Op op, const std::vector<std::string> &strings): op_(NONE), fieldValue_(0)
151 {
152 for (std::string value : strings) {
153 orConds_.emplace_back(Cond(col, op, value));
154 }
155 }
156
~Cond()157 Cond::~Cond()
158 {
159 }
160
And(const std::string & col,Op op,const int8_t value)161 Cond &Cond::And(const std::string &col, Op op, const int8_t value)
162 {
163 andConds_.emplace_back(Cond(col, op, value));
164 return *this;
165 }
166
And(const std::string & col,Op op,const int16_t value)167 Cond &Cond::And(const std::string &col, Op op, const int16_t value)
168 {
169 andConds_.emplace_back(Cond(col, op, value));
170 return *this;
171 }
172
And(const std::string & col,Op op,const int32_t value)173 Cond &Cond::And(const std::string &col, Op op, const int32_t value)
174 {
175 andConds_.emplace_back(Cond(col, op, value));
176 return *this;
177 }
178
And(const std::string & col,Op op,const int64_t value)179 Cond &Cond::And(const std::string &col, Op op, const int64_t value)
180 {
181 andConds_.emplace_back(Cond(col, op, value));
182 return *this;
183 }
184
And(const std::string & col,Op op,const float value)185 Cond &Cond::And(const std::string &col, Op op, const float value)
186 {
187 andConds_.emplace_back(Cond(col, op, value));
188 return *this;
189 }
190
And(const std::string & col,Op op,const double value)191 Cond &Cond::And(const std::string &col, Op op, const double value)
192 {
193 andConds_.emplace_back(Cond(col, op, value));
194 return *this;
195 }
196
And(const std::string & col,Op op,const std::string & value)197 Cond &Cond::And(const std::string &col, Op op, const std::string &value)
198 {
199 andConds_.emplace_back(Cond(col, op, value));
200 return *this;
201 }
202
And(const Cond & cond)203 Cond &Cond::And(const Cond &cond)
204 {
205 andConds_.emplace_back(cond);
206 return *this;
207 }
208
Or(const std::string & col,Op op,const int8_t value)209 Cond &Cond::Or(const std::string &col, Op op, const int8_t value)
210 {
211 orConds_.emplace_back(Cond(col, op, value));
212 return *this;
213 }
214
Or(const std::string & col,Op op,const int16_t value)215 Cond &Cond::Or(const std::string &col, Op op, const int16_t value)
216 {
217 orConds_.emplace_back(Cond(col, op, value));
218 return *this;
219 }
220
Or(const std::string & col,Op op,const int32_t value)221 Cond &Cond::Or(const std::string &col, Op op, const int32_t value)
222 {
223 orConds_.emplace_back(Cond(col, op, value));
224 return *this;
225 }
226
Or(const std::string & col,Op op,const int64_t value)227 Cond &Cond::Or(const std::string &col, Op op, const int64_t value)
228 {
229 orConds_.emplace_back(Cond(col, op, value));
230 return *this;
231 }
232
Or(const std::string & col,Op op,const float value)233 Cond &Cond::Or(const std::string &col, Op op, const float value)
234 {
235 orConds_.emplace_back(Cond(col, op, value));
236 return *this;
237 }
238
Or(const std::string & col,Op op,const double value)239 Cond &Cond::Or(const std::string &col, Op op, const double value)
240 {
241 orConds_.emplace_back(Cond(col, op, value));
242 return *this;
243 }
244
Or(const std::string & col,Op op,const std::string & value)245 Cond &Cond::Or(const std::string &col, Op op, const std::string &value)
246 {
247 orConds_.emplace_back(Cond(col, op, value));
248 return *this;
249 }
250
Or(const Cond & cond)251 Cond &Cond::Or(const Cond &cond)
252 {
253 orConds_.emplace_back(cond);
254 return *this;
255 }
256
GetCondEqualValue(DataQuery & dataQuery,Cond & cond)257 void Cond::GetCondEqualValue(DataQuery &dataQuery, Cond &cond)
258 {
259 if (cond.fieldValue_.IsInteger()) {
260 dataQuery.EqualTo(cond.col_, cond.fieldValue_.GetInteger());
261 return;
262 }
263 if (cond.fieldValue_.IsFloat()) {
264 dataQuery.EqualTo(cond.col_, cond.fieldValue_.GetFloat());
265 return;
266 }
267 if (cond.fieldValue_.IsDouble()) {
268 dataQuery.EqualTo(cond.col_, cond.fieldValue_.GetDouble());
269 return;
270 }
271 if (cond.fieldValue_.IsString()) {
272 dataQuery.EqualTo(cond.col_, cond.fieldValue_.GetString());
273 return;
274 }
275 return;
276 }
277
GetCondNotEqualValue(DataQuery & dataQuery,Cond & cond)278 void Cond::GetCondNotEqualValue(DataQuery &dataQuery, Cond &cond)
279 {
280 if (cond.fieldValue_.IsInteger()) {
281 dataQuery.NotEqualTo(cond.col_, cond.fieldValue_.GetInteger());
282 return;
283 }
284 if (cond.fieldValue_.IsFloat()) {
285 dataQuery.NotEqualTo(cond.col_, cond.fieldValue_.GetFloat());
286 return;
287 }
288 if (cond.fieldValue_.IsDouble()) {
289 dataQuery.NotEqualTo(cond.col_, cond.fieldValue_.GetDouble());
290 return;
291 }
292 if (cond.fieldValue_.IsString()) {
293 dataQuery.NotEqualTo(cond.col_, cond.fieldValue_.GetString());
294 return;
295 }
296 return;
297 }
298
GetCondLessThanValue(DataQuery & dataQuery,Cond & cond)299 void Cond::GetCondLessThanValue(DataQuery &dataQuery, Cond &cond)
300 {
301 if (cond.fieldValue_.IsInteger()) {
302 dataQuery.LessThan(cond.col_, cond.fieldValue_.GetInteger());
303 return;
304 }
305 if (cond.fieldValue_.IsFloat()) {
306 dataQuery.LessThan(cond.col_, cond.fieldValue_.GetFloat());
307 return;
308 }
309 if (cond.fieldValue_.IsDouble()) {
310 dataQuery.LessThan(cond.col_, cond.fieldValue_.GetDouble());
311 return;
312 }
313 if (cond.fieldValue_.IsString()) {
314 dataQuery.LessThan(cond.col_, cond.fieldValue_.GetString());
315 return;
316 }
317 }
318
GetCondLessEqualValue(DataQuery & dataQuery,Cond & cond)319 void Cond::GetCondLessEqualValue(DataQuery &dataQuery, Cond &cond)
320 {
321 if (cond.fieldValue_.IsInteger()) {
322 dataQuery.LessThanOrEqualTo(cond.col_, cond.fieldValue_.GetInteger());
323 return;
324 }
325 if (cond.fieldValue_.IsFloat()) {
326 dataQuery.LessThanOrEqualTo(cond.col_, cond.fieldValue_.GetFloat());
327 return;
328 }
329 if (cond.fieldValue_.IsDouble()) {
330 dataQuery.LessThanOrEqualTo(cond.col_, cond.fieldValue_.GetDouble());
331 return;
332 }
333 if (cond.fieldValue_.IsString()) {
334 dataQuery.LessThanOrEqualTo(cond.col_, cond.fieldValue_.GetString());
335 return;
336 }
337 }
338
GetCondGreatThanValue(DataQuery & dataQuery,Cond & cond)339 void Cond::GetCondGreatThanValue(DataQuery &dataQuery, Cond &cond)
340 {
341 if (cond.fieldValue_.IsInteger()) {
342 dataQuery.GreaterThan(cond.col_, cond.fieldValue_.GetInteger());
343 return;
344 }
345 if (cond.fieldValue_.IsFloat()) {
346 dataQuery.GreaterThan(cond.col_, cond.fieldValue_.GetFloat());
347 return;
348 }
349 if (cond.fieldValue_.IsDouble()) {
350 dataQuery.GreaterThan(cond.col_, cond.fieldValue_.GetDouble());
351 return;
352 }
353 if (cond.fieldValue_.IsString()) {
354 dataQuery.GreaterThan(cond.col_, cond.fieldValue_.GetString());
355 return;
356 }
357 }
358
GetCondGreatEqualValue(DataQuery & dataQuery,Cond & cond)359 void Cond::GetCondGreatEqualValue(DataQuery &dataQuery, Cond &cond)
360 {
361 if (cond.fieldValue_.IsInteger()) {
362 dataQuery.GreaterThanOrEqualTo(cond.col_, cond.fieldValue_.GetInteger());
363 return;
364 }
365 if (cond.fieldValue_.IsFloat()) {
366 dataQuery.GreaterThanOrEqualTo(cond.col_, cond.fieldValue_.GetFloat());
367 return;
368 }
369 if (cond.fieldValue_.IsDouble()) {
370 dataQuery.GreaterThanOrEqualTo(cond.col_, cond.fieldValue_.GetDouble());
371 return;
372 }
373 if (cond.fieldValue_.IsString()) {
374 dataQuery.GreaterThanOrEqualTo(cond.col_, cond.fieldValue_.GetString());
375 return;
376 }
377 }
378
GetCondStartWithValue(DataQuery & dataQuery,Cond & cond)379 void Cond::GetCondStartWithValue(DataQuery &dataQuery, Cond &cond)
380 {
381 if (cond.fieldValue_.IsString()) {
382 dataQuery.StartWith(cond.col_, cond.fieldValue_.GetString());
383 return;
384 }
385 }
386
GetCondNoStartWithValue(DataQuery & dataQuery,Cond & cond)387 void Cond::GetCondNoStartWithValue(DataQuery &dataQuery, Cond &cond)
388 {
389 if (cond.fieldValue_.IsString()) {
390 dataQuery.NotStartWith(cond.col_, cond.fieldValue_.GetString());
391 return;
392 }
393 }
394
GetCond(DataQuery & dataQuery,Cond & cond)395 void Cond::GetCond(DataQuery &dataQuery, Cond &cond)
396 {
397 switch (cond.op_) {
398 case EQ:
399 GetCondEqualValue(dataQuery, cond);
400 break;
401 case NE:
402 GetCondNotEqualValue(dataQuery, cond);
403 break;
404 case GT:
405 GetCondGreatThanValue(dataQuery, cond);
406 break;
407 case GE:
408 GetCondGreatEqualValue(dataQuery, cond);
409 break;
410 case LT:
411 GetCondLessThanValue(dataQuery, cond);
412 break;
413 case LE:
414 GetCondLessEqualValue(dataQuery, cond);
415 break;
416 case SW:
417 GetCondStartWithValue(dataQuery, cond);
418 break;
419 case NSW:
420 GetCondNoStartWithValue(dataQuery, cond);
421 break;
422 default:
423 break;
424 }
425 }
426
IsSimpleCond(Cond & cond)427 bool Cond::IsSimpleCond(Cond &cond)
428 {
429 if (!cond.col_.empty() && (!cond.andConds_.empty() || !cond.orConds_.empty())) {
430 return false;
431 }
432 if (cond.andConds_.size() > 1) {
433 return false;
434 }
435 if (cond.orConds_.size() > 1) {
436 return false;
437 }
438 return true;
439 }
440
Traval(DataQuery & dataQuery,Cond & cond)441 void Cond::Traval(DataQuery &dataQuery, Cond &cond)
442 {
443 if (!cond.col_.empty()) {
444 GetCond(dataQuery, cond);
445 }
446 if (!cond.andConds_.empty()) {
447 if (!cond.col_.empty()) {
448 dataQuery.And();
449 }
450 bool isFirst = true;
451 for (auto it = cond.andConds_.begin(); it != cond.andConds_.end(); it++) {
452 if (!isFirst) {
453 dataQuery.And();
454 }
455 isFirst = false;
456 if (IsSimpleCond(*it)) {
457 Traval(dataQuery, *it);
458 } else {
459 dataQuery.BeginGroup();
460 Traval(dataQuery, *it);
461 dataQuery.EndGroup();
462 }
463 }
464 }
465 if (!cond.orConds_.empty()) {
466 if (!cond.col_.empty() || !cond.andConds_.empty()) {
467 dataQuery.Or();
468 }
469 bool isFirst = true;
470 for (auto it = cond.orConds_.begin(); it != cond.orConds_.end(); it++) {
471 if (!isFirst) {
472 dataQuery.Or();
473 }
474 isFirst = false;
475 if (IsSimpleCond(*it)) {
476 Traval(dataQuery, *it);
477 } else {
478 dataQuery.BeginGroup();
479 Traval(dataQuery, *it);
480 dataQuery.EndGroup();
481 }
482 }
483 }
484 }
485
ResultSet()486 ResultSet::ResultSet(): iter_(eventRecords_.begin()), code_(0), has_(false)
487 {
488 }
489
~ResultSet()490 ResultSet::~ResultSet()
491 {
492 }
493
ResultSet(ResultSet && result)494 ResultSet::ResultSet(ResultSet &&result)
495 {
496 eventRecords_ = move(result.eventRecords_);
497 code_ = result.code_;
498 has_ = result.has_;
499 iter_ = result.iter_;
500 }
501
operator =(ResultSet && result)502 ResultSet& ResultSet::operator = (ResultSet &&result)
503 {
504 eventRecords_ = move(result.eventRecords_);
505 code_ = result.code_;
506 has_ = result.has_;
507 iter_ = result.iter_;
508 return *this;
509 }
510
GetErrCode() const511 int ResultSet::GetErrCode() const
512 {
513 return code_;
514 }
515
HasNext() const516 bool ResultSet::HasNext() const
517 {
518 return has_;
519 }
520
Next()521 ResultSet::RecordIter ResultSet::Next()
522 {
523 if (!has_) {
524 return eventRecords_.end();
525 }
526
527 auto tempIter = iter_;
528 iter_++;
529 if (iter_ == eventRecords_.end()) {
530 has_ = false;
531 }
532
533 return tempIter;
534 }
535
Set(int code,bool has)536 void ResultSet::Set(int code, bool has)
537 {
538 code_ = code;
539 has_ = has;
540 if (eventRecords_.size() > 0) {
541 iter_ = eventRecords_.begin();
542 }
543 }
544
SysEventQuery(const std::string & dbFile)545 SysEventQuery::SysEventQuery(const std::string &dbFile): dbFile_(dbFile)
546 {
547 }
548
~SysEventQuery()549 SysEventQuery::~SysEventQuery()
550 {
551 }
552
Select()553 SysEventQuery &SysEventQuery::Select()
554 {
555 return *this;
556 }
557
Select(const std::vector<std::string> & eventCols)558 SysEventQuery &SysEventQuery::Select(const std::vector<std::string> &eventCols)
559 {
560 for (std::string col : eventCols) {
561 auto it = std::find(eventCols_.begin(), eventCols_.end(), col);
562 if (it != eventCols_.end()) {
563 continue;
564 }
565 eventCols_.emplace_back(col);
566 }
567 return *this;
568 }
569
Where(const std::string & col,Op op,const int8_t value)570 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const int8_t value)
571 {
572 cond_.And(col, op, value);
573 return *this;
574 }
575
Where(const std::string & col,Op op,const int16_t value)576 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const int16_t value)
577 {
578 cond_.And(col, op, value);
579 return *this;
580 }
581
Where(const std::string & col,Op op,const int32_t value)582 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const int32_t value)
583 {
584 cond_.And(col, op, value);
585 return *this;
586 }
587
Where(const std::string & col,Op op,const int64_t value)588 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const int64_t value)
589 {
590 cond_.And(col, op, value);
591 return *this;
592 }
593
Where(const std::string & col,Op op,const float value)594 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const float value)
595 {
596 cond_.And(col, op, value);
597 return *this;
598 }
599
Where(const std::string & col,Op op,const double value)600 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const double value)
601 {
602 cond_.And(col, op, value);
603 return *this;
604 }
605
Where(const std::string & col,Op op,const std::string & value)606 SysEventQuery &SysEventQuery::Where(const std::string &col, Op op, const std::string &value)
607 {
608 cond_.And(col, op, value);
609 return *this;
610 }
611
Where(const Cond & cond)612 SysEventQuery &SysEventQuery::Where(const Cond &cond)
613 {
614 cond_.And(cond);
615 return *this;
616 }
617
And(const std::string & col,Op op,const int8_t value)618 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const int8_t value)
619 {
620 cond_.And(col, op, value);
621 return *this;
622 }
623
And(const std::string & col,Op op,const int16_t value)624 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const int16_t value)
625 {
626 cond_.And(col, op, value);
627 return *this;
628 }
629
And(const std::string & col,Op op,const int32_t value)630 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const int32_t value)
631 {
632 cond_.And(col, op, value);
633 return *this;
634 }
635
And(const std::string & col,Op op,const int64_t value)636 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const int64_t value)
637 {
638 cond_.And(col, op, value);
639 return *this;
640 }
641
And(const std::string & col,Op op,const float value)642 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const float value)
643 {
644 cond_.And(col, op, value);
645 return *this;
646 }
647
And(const std::string & col,Op op,const double value)648 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const double value)
649 {
650 cond_.And(col, op, value);
651 return *this;
652 }
653
And(const std::string & col,Op op,const std::string & value)654 SysEventQuery &SysEventQuery::And(const std::string &col, Op op, const std::string &value)
655 {
656 cond_.And(col, op, value);
657 return *this;
658 }
659
And(const Cond & cond)660 SysEventQuery &SysEventQuery::And(const Cond &cond)
661 {
662 cond_.And(cond);
663 return *this;
664 }
665
And(const std::vector<Cond> & conds)666 SysEventQuery &SysEventQuery::And(const std::vector<Cond> &conds)
667 {
668 for (Cond cond : conds) {
669 cond_.And(cond);
670 }
671 return *this;
672 }
673
Or(const std::string & col,Op op,const int8_t value)674 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const int8_t value)
675 {
676 cond_.Or(col, op, value);
677 return *this;
678 }
679
Or(const std::string & col,Op op,const int16_t value)680 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const int16_t value)
681 {
682 cond_.Or(col, op, value);
683 return *this;
684 }
685
Or(const std::string & col,Op op,const int32_t value)686 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const int32_t value)
687 {
688 cond_.Or(col, op, value);
689 return *this;
690 }
691
Or(const std::string & col,Op op,const int64_t value)692 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const int64_t value)
693 {
694 cond_.Or(col, op, value);
695 return *this;
696 }
697
Or(const std::string & col,Op op,const float value)698 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const float value)
699 {
700 cond_.Or(col, op, value);
701 return *this;
702 }
703
Or(const std::string & col,Op op,const double value)704 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const double value)
705 {
706 cond_.Or(col, op, value);
707 return *this;
708 }
709
Or(const std::string & col,Op op,const std::string & value)710 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::string &value)
711 {
712 cond_.Or(col, op, value);
713 return *this;
714 }
715
Or(const Cond & cond)716 SysEventQuery &SysEventQuery::Or(const Cond &cond)
717 {
718 cond_.Or(cond);
719 return *this;
720 }
721
Or(const std::string & col,Op op,const std::vector<int8_t> & ints)722 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<int8_t> &ints)
723 {
724 for (auto value : ints) {
725 cond_.Or(col, op, value);
726 }
727 return *this;
728 }
729
Or(const std::string & col,Op op,const std::vector<int16_t> & ints)730 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<int16_t> &ints)
731 {
732 for (auto value : ints) {
733 cond_.Or(col, op, value);
734 }
735 return *this;
736 }
737
Or(const std::string & col,Op op,const std::vector<int32_t> & ints)738 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<int32_t> &ints)
739 {
740 for (auto value : ints) {
741 cond_.Or(col, op, value);
742 }
743 return *this;
744 }
745
Or(const std::string & col,Op op,const std::vector<int64_t> & longs)746 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<int64_t> &longs)
747 {
748 for (auto value : longs) {
749 cond_.Or(col, op, value);
750 }
751 return *this;
752 }
753
Or(const std::string & col,Op op,const std::vector<float> & floats)754 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<float> &floats)
755 {
756 for (auto value : floats) {
757 cond_.Or(col, op, value);
758 }
759 return *this;
760 }
761
Or(const std::string & col,Op op,const std::vector<double> & doubles)762 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<double> &doubles)
763 {
764 for (auto value : doubles) {
765 cond_.Or(col, op, value);
766 }
767 return *this;
768 }
769
Or(const std::string & col,Op op,const std::vector<std::string> & strings)770 SysEventQuery &SysEventQuery::Or(const std::string &col, Op op, const std::vector<std::string> &strings)
771 {
772 for (auto value : strings) {
773 cond_.Or(col, op, value);
774 }
775 return *this;
776 }
777
Or(const std::vector<Cond> & conds)778 SysEventQuery &SysEventQuery::Or(const std::vector<Cond> &conds)
779 {
780 for (auto cond : conds) {
781 cond_.Or(cond);
782 }
783 return *this;
784 }
785
Order(const std::string & col,bool isAsc)786 SysEventQuery &SysEventQuery::Order(const std::string &col, bool isAsc)
787 {
788 orderCols_.emplace_back(std::make_pair<>(col, isAsc));
789 return *this;
790 }
791
BuildDataQuery(DataQuery & dataQuery,int limit)792 void SysEventQuery::BuildDataQuery(DataQuery &dataQuery, int limit)
793 {
794 dataQuery.Select(eventCols_);
795 Cond::Traval(dataQuery, cond_);
796 for (auto it = orderCols_.begin(); it != orderCols_.end(); it++) {
797 if (it->second) {
798 dataQuery.OrderByAsc(it->first);
799 } else {
800 dataQuery.OrderByDesc(it->first);
801 }
802 }
803 dataQuery.Limit(limit);
804 }
805
GetDbFile()806 std::string SysEventQuery::GetDbFile()
807 {
808 return dbFile_;
809 }
810
GetDataQuery(DataQuery & dataQuery)811 void SysEventQuery::GetDataQuery(DataQuery &dataQuery)
812 {
813 Cond::Traval(dataQuery, cond_);
814 }
815
Execute(int limit,DbQueryTag tag,QueryProcessInfo callerInfo,DbQueryCallback queryCallback)816 ResultSet SysEventQuery::Execute(int limit, DbQueryTag tag, QueryProcessInfo callerInfo,
817 DbQueryCallback queryCallback)
818 {
819 return ExecuteSQL(limit);
820 }
821
ExecuteSQL(int limit)822 ResultSet SysEventQuery::ExecuteSQL(int limit)
823 {
824 DataQuery dataQuery;
825 BuildDataQuery(dataQuery, limit);
826 ResultSet resultSet;
827 std::vector<Entry> entries;
828 std::shared_ptr<DocStore> docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile_);
829 int retCode = docStore->GetEntriesWithQuery(dataQuery, entries);
830 if (retCode != 0) {
831 resultSet.Set(retCode, false);
832 return resultSet;
833 }
834 if (entries.empty()) {
835 resultSet.Set(0, false);
836 return resultSet;
837 }
838 for (auto it = entries.begin(); it != entries.end(); it++) {
839 SysEvent sysEvent("", nullptr, it->value);
840 sysEvent.ParseJson();
841 sysEvent.SetSeq(it->id);
842 resultSet.eventRecords_.emplace_back(sysEvent);
843 }
844 resultSet.Set(0, true);
845 return resultSet;
846 }
847
ExecuteWithCallback(SysEventCallBack callback,int limit)848 int SysEventQuery::ExecuteWithCallback(SysEventCallBack callback, int limit)
849 {
850 DataQuery dataQuery;
851 BuildDataQuery(dataQuery, limit);
852
853 std::vector<Entry> entries;
854 std::shared_ptr<DocStore> docStore = StoreMgrProxy::GetInstance().GetDocStore(dbFile_);
855 std::function<int (int, const Entry&)> c = [&](int cnt, const Entry &entry) -> int {
856 SysEvent sysEvent("", nullptr, entry.value);
857 sysEvent.ParseJson();
858 sysEvent.SetSeq(entry.id);
859 return callback(sysEvent);
860 };
861 docStore->GetEntryDuringQuery(dataQuery, c);
862 return 0;
863 }
864 } // EventStore
865 } // namespace HiviewDFX
866 } // namespace OHOS