• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "data_query.h"
17 
18 #include <string>
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 int DataQuery::defLimit_ = 100;
DataQuery()23 DataQuery::DataQuery() : limit_(defLimit_), offset_(0) {}
24 
~DataQuery()25 DataQuery::~DataQuery() {}
26 
Reset()27 DataQuery& DataQuery::Reset()
28 {
29     sql_.str("");
30     orderFields_.clear();
31     limit_ = defLimit_;
32     offset_ = 0;
33     return *this;
34 }
35 
Select(const std::vector<std::string> & fields)36 DOCSTORE_API DataQuery& DataQuery::Select(const std::vector<std::string>& fields)
37 {
38     selects_.insert(selects_.end(), fields.begin(), fields.end());
39     return *this;
40 }
41 
StartWith(const std::string & field,const std::string & value)42 DataQuery& DataQuery::StartWith(const std::string &field, const std::string &value)
43 {
44     sql_ << "/[" << field << " re \"" << value << "\"]";
45     return *this;
46 }
47 
NotStartWith(const std::string & field,const std::string & value)48 DataQuery& DataQuery::NotStartWith(const std::string &field, const std::string &value)
49 {
50     sql_ << "/[" << field << " not re \"" << value << ".*\"]";
51     return *this;
52 }
53 
And()54 DataQuery& DataQuery::And()
55 {
56     sql_ << " and ";
57     return *this;
58 }
59 
Or()60 DataQuery& DataQuery::Or()
61 {
62     sql_ << " or ";
63     return *this;
64 }
65 
OrderByAsc(const std::string & field)66 DataQuery& DataQuery::OrderByAsc(const std::string &field)
67 {
68     orderFields_.emplace_back(std::pair<bool, std::string>(true, field));
69     return *this;
70 }
71 
OrderByDesc(const std::string & field)72 DataQuery& DataQuery::OrderByDesc(const std::string &field)
73 {
74     orderFields_.emplace_back(std::pair<bool, std::string>(false, field));
75     return *this;
76 }
77 
Limit(const int number,const int offset)78 DataQuery& DataQuery::Limit(const int number, const int offset)
79 {
80     limit_ = number;
81     offset_ = offset;
82     return *this;
83 }
84 
BeginGroup()85 DataQuery& DataQuery::BeginGroup()
86 {
87     sql_ << "(";
88     return *this;
89 }
90 
EndGroup()91 DataQuery& DataQuery::EndGroup()
92 {
93     sql_ << ")";
94     return *this;
95 }
96 
ToString() const97 std::string DataQuery::ToString() const
98 {
99     std::stringstream sql;
100     sql << sql_.str();
101     if (!selects_.empty()) {
102         sql << " | ";
103         sql << "/{";
104         for (std::size_t index = 0; index < selects_.size(); index++) {
105             if (index != (selects_.size() - 1)) {
106                 sql << selects_[index] << ", ";
107             } else {
108                 sql << selects_[index];
109             }
110         }
111         sql << "}";
112     }
113     if (sql.str().empty()) {
114         sql << "/*";
115     }
116     sql << " |";
117     if (!orderFields_.empty()) {
118         sql << " ";
119         for (std::size_t index = 0; index < orderFields_.size(); index++) {
120             std::pair<bool, std::string> pair = orderFields_[index];
121             std::string order = pair.first ? "asc" : "desc";
122             sql << order << " /" << pair.second << " ";
123         }
124     }
125     if (offset_ > 0) {
126         sql << " skip " << offset_;
127     }
128     sql << " limit " << limit_;
129     return sql.str();
130 }
131 
ToDelString(int limit) const132 std::string DataQuery::ToDelString(int limit) const
133 {
134     std::string sql = sql_.str().empty() ? "/*" : sql_.str();
135     if (limit <= 0) {
136         return sql + " | del | count";
137     } else {
138         return sql + " | del | limit " + std::to_string(limit) + " count";
139     }
140 }
141 } // HiviewDFX
142 } // OHOS