• 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 #ifndef HIVIEW_ADAPTER_DBSTORE_INCLUDE_DATA_QUERY_H
17 #define HIVIEW_ADAPTER_DBSTORE_INCLUDE_DATA_QUERY_H
18 
19 #include <string>
20 #include <vector>
21 #include <sstream>
22 
23 #include "visibility.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class DataQuery {
28 public:
29     DOCSTORE_API DataQuery();
30 
31     DOCSTORE_API ~DataQuery();
32 
33     DOCSTORE_API DataQuery& Reset();
34 
35     DOCSTORE_API DataQuery& Select(const std::vector<std::string>& fields);
36 
37     template<typename T>
EqualTo(const std::string & field,const T & value)38     DOCSTORE_API DataQuery& EqualTo(const std::string &field, const T &value)
39     {
40         sql_ << "(/[" << field << "=";
41         AppendValue(sql_, value) << "])";
42         return *this;
43     }
44 
45     template<typename T>
NotEqualTo(const std::string & field,const T & value)46     DOCSTORE_API DataQuery& NotEqualTo(const std::string &field, const T &value)
47     {
48         sql_ << "(/[" << field << "!=";
49         AppendValue(sql_, value) << "])";
50         return *this;
51     }
52 
53     template<typename T>
GreaterThan(const std::string & field,const T & value)54     DOCSTORE_API DataQuery& GreaterThan(const std::string &field, const T &value)
55     {
56         sql_ << "(/[" << field << ">";
57         AppendValue(sql_, value) << "])";
58         return *this;
59     }
60 
61     template<typename T>
LessThan(const std::string & field,const T & value)62     DOCSTORE_API  DataQuery& LessThan(const std::string &field, const T &value)
63     {
64         sql_ << "(/[" << field << "<";
65         AppendValue(sql_, value) << "])";
66         return *this;
67     }
68 
69     template<typename T>
GreaterThanOrEqualTo(const std::string & field,const T & value)70     DOCSTORE_API DataQuery& GreaterThanOrEqualTo(const std::string &field, const T &value)
71     {
72         sql_ << "(/[" << field << ">=";
73         AppendValue(sql_, value) << "])";
74         return *this;
75     }
76 
77     template<typename T>
LessThanOrEqualTo(const std::string & field,const T & value)78     DOCSTORE_API  DataQuery& LessThanOrEqualTo(const std::string &field, const T &value)
79     {
80         sql_ << "(/[" << field << "<=";
81         AppendValue(sql_, value) << "])";
82         return *this;
83     }
84 
85     template<typename T>
In(const std::string & field,const std::vector<T> & valueList)86     DOCSTORE_API  DataQuery& In(const std::string &field, const std::vector<T> &valueList)
87     {
88         sql_ << "/[" << field << " in [";
89         for (int index = 0; index < valueList.size(); index++) {
90             if (index != (valueList.size() - 1)) {
91                 AppendValue(sql_, valueList[index]) << ", ";
92             }
93             else {
94                 AppendValue(sql_, valueList[index]);
95             }
96         }
97         sql_ << "]]";
98         return *this;
99     }
100 
101     template<typename T>
NotIn(const std::string & field,const std::vector<T> & valueList)102     DOCSTORE_API  DataQuery& NotIn(const std::string &field, const std::vector<T> &valueList)
103     {
104         sql_ << "/[" << field << " not in [";
105         for (int index = 0; index < valueList.size(); index++) {
106             if (index != (valueList.size() - 1)) {
107                 AppendValue(sql_, valueList[index]) << ", ";
108             }
109             else {
110                 AppendValue(sql_, valueList[index]);
111             }
112         }
113         sql_ << "]]";
114         return *this;
115     }
116 
117     DOCSTORE_API DataQuery& StartWith(const std::string &field, const std::string &value);
118 
119     DOCSTORE_API DataQuery& NotStartWith(const std::string &field, const std::string &value);
120 
121     DOCSTORE_API DataQuery& And();
122 
123     DOCSTORE_API DataQuery& Or();
124 
125     DOCSTORE_API DataQuery& OrderByAsc(const std::string &field);
126 
127     DOCSTORE_API DataQuery& OrderByDesc(const std::string &field);
128 
129     DOCSTORE_API DataQuery& Limit(const int number, const int offset = 0);
130 
131     DOCSTORE_API DataQuery& BeginGroup();
132 
133     DOCSTORE_API DataQuery& EndGroup();
134 
135     DOCSTORE_API std::string ToString() const;
136 
137 private:
AppendValue(std::stringstream & sql,const T & value)138     template<typename T> static std::stringstream& AppendValue(std::stringstream &sql, const T &value)
139     {
140         sql << value;
141         return sql;
142     }
143 
AppendValue(std::stringstream & sql,const std::string & value)144     static std::stringstream& AppendValue(std::stringstream &sql, const std::string &value)
145     {
146         sql << "\"" << value << "\"";
147         return sql;
148     }
149 
AppendValue(std::stringstream & sql,const char * value)150     static std::stringstream& AppendValue(std::stringstream &sql, const char* value)
151     {
152         sql << "\"" << value << "\"";
153         return sql;
154     }
155 
AppendValue(std::stringstream & sql,char * value)156     static std::stringstream& AppendValue(std::stringstream &sql, char* value)
157     {
158         sql << "\"" << value << "\"";
159         return sql;
160     }
161 
162     std::string ToDelString(int limit = 0) const;
163 private:
164     static int defLimit_;
165     friend class DocStore;
166     std::stringstream sql_;
167     std::vector<std::string> selects_;
168     std::vector<std::pair<bool, std::string>> orderFields_;
169     int limit_;
170     int offset_;
171 }; // DataQuery
172 } // HiviewDFX
173 } // OHOS
174 #endif // HIVIEW_ADAPTER_DBSTORE_INCLUDE_DATA_QUERY_H