5#ifndef QUERY_HPP_INCLUDED
6#define QUERY_HPP_INCLUDED
72 const std::string &value);
79 void queryEndsWith(
const std::string attributeId,
const std::string &value);
86 void queryContains(
const std::string attributeId,
const std::string &value);
98 void queryContains(
const std::string attributeId, std::list<T> &value) {
99 std::string query =
"{\"method\":\"contains\",\"attribute\":\"" +
100 attributeId +
"\",\"values\":[" +
101 listToString(value) +
"]}";
102 if (contains_iter == queries.end()) {
103 queries.push_back(query);
104 contains_iter = std::prev(queries.end());
107 *contains_iter = query;
117 template <
typename T>
120 std::ostringstream oss;
122 oss << append_encoded(oss, value1);
124 oss << append_encoded(oss, value2);
126 std::string query =
"{\"method\":\"between\",\"attribute\":\"" +
127 attributeId +
"\",\"values\":[" + oss.str() +
"]}";
128 if (between_iter == queries.end()) {
129 queries.push_back(query);
130 between_iter = std::prev(queries.end());
133 *between_iter = query;
143 template <
typename T>
145 std::ostringstream oss;
146 oss << append_encoded(oss, value);
148 "{\"method\":\"greaterThanEqual\",\"attribute\":\"" + attributeId +
149 "\",\"values\":[" + oss.str() +
"]}";
150 if (greater_than_equal_iter == queries.end()) {
151 queries.push_back(query);
152 greater_than_equal_iter = std::prev(queries.end());
155 *greater_than_equal_iter = query;
164 template <
typename T>
166 std::ostringstream oss;
167 append_encoded(oss, value);
168 std::string query =
"{\"method\":\"greaterThan\",\"attribute\":\"" +
169 attributeId +
"\",\"values\":[" + oss.str() +
"]}";
170 if (greater_than_iter == queries.end()) {
171 queries.push_back(query);
172 greater_than_iter = std::prev(queries.end());
175 *greater_than_iter = query;
184 template <
typename T>
186 std::ostringstream oss;
187 append_encoded(oss, value);
188 std::string query =
"{\"method\":\"lessThanEqual\",\"attribute\":\"" +
189 attributeId +
"\",\"values\":[" + oss.str() +
"]}";
190 if (less_than_equal_iter == queries.end()) {
191 queries.push_back(query);
192 less_than_equal_iter = std::prev(queries.end());
195 *less_than_equal_iter = query;
204 template <
typename T>
206 std::ostringstream oss;
207 append_encoded(oss, value);
208 std::string query =
"{\"method\":\"lessThan\",\"attribute\":\"" +
209 attributeId +
"\",\"values\":[" + oss.str() +
"]}";
210 if (less_than_iter == queries.end()) {
211 queries.push_back(query);
212 less_than_iter = std::prev(queries.end());
215 *less_than_iter = query;
224 template <
typename T>
225 void queryEqual(
const std::string attributeId, std::list<T> &values) {
226 std::string query =
"{\"method\":\"equal\",\"attribute\":\"" +
227 attributeId +
"\",\"values\":[" +
228 listToString(values) +
"]}";
229 if (equal_iter == queries.end()) {
230 queries.push_back(query);
231 equal_iter = std::prev(queries.end());
243 template <
typename T>
244 void notEqual(
const std::string attributeId, std::list<T> &values) {
245 std::string query =
"{\"method\":\"notEqual\",\"attribute\":\"" +
246 attributeId +
"\",\"values\":[" +
247 listToString(values) +
"]}";
248 if (not_equal_iter == queries.end()) {
249 queries.push_back(query);
250 not_equal_iter = std::prev(queries.end());
253 *not_equal_iter = query;
274 std::string url_encode(
const std::string &value);
277 template <
typename T>
278 typename std::enable_if<std::is_same<T, std::string>::value,
void>::type
279 append_encoded(std::ostringstream &oss,
const T &iter) {
280 oss <<
"\"" << url_encode(iter) <<
"\"";
284 template <
typename T>
285 typename std::enable_if<!std::is_same<T, std::string>::value,
void>::type
286 append_encoded(std::ostringstream &oss,
const T &iter) {
291 inline void append_encoded(std::ostringstream &oss,
const char *iter) {
292 oss <<
"\"" << url_encode(iter) <<
"\"";
296 template <
typename T> std::string listToStringNoEncode(std::list<T> &ls) {
297 int size = ls.size(), count = 0;
298 std::ostringstream oss;
299 for (
auto iter : ls) {
300 oss <<
"\"" << iter <<
"\"";
301 if (count < size - 1)
309 template <
typename T> std::string listToString(std::list<T> &ls) {
310 int size = ls.size(), count = 0;
311 std::ostringstream oss;
312 oss << std::boolalpha;
314 for (
auto iter = ls.begin(); iter != ls.end(); iter++) {
315 append_encoded(oss, *iter);
316 if (count < size - 1) {
324 std::list<std::string>::iterator cursor_iter, limit_iter, equal_iter,
325 not_equal_iter, sel_iter, less_than_iter, less_than_equal_iter,
326 greater_than_iter, greater_than_equal_iter, between_iter, is_null_iter,
327 is_not_null_iter, starts_iter, ends_iter, contains_iter;
330 std::list<std::string> queries;
Utility class to construct and manage Appwrite-style database query filters.
void queryBetween(const std::string attributeId, const T &value1, const T &value2)
Filter documents where attribute is between two values.
void queryGreaterThanEqual(const std::string attributeId, const T &value)
Filter documents where attribute is greater than or equal to value.
void queryContains(const std::string attributeId, std::list< T > &value)
Filter documents where attribute contains any value from list.
void addComplexQuery(const std::string jsonQuery)
Add a raw JSON complex query.
void reset()
Resets the internal query list.
void queryEndsWith(const std::string attributeId, const std::string &value)
Filter documents where attribute ends with given value.
std::string to_string()
Serialize all added queries into a JSON string.
void queryCursorAfter(const std::string documentId)
Add a cursor query to paginate after the given document ID.
void queryIsNotNull(const std::string attributeId)
Filter documents where an attribute is not null.
void querySelect(std::list< std::string > &values)
Select only certain fields from the result.
void notEqual(const std::string attributeId, std::list< T > &values)
Filter documents where attribute does not equal any value in list.
void queryLessThan(const std::string attributeId, const T &value)
Filter documents where attribute is less than value.
void queryLessThanEqual(const std::string attributeId, const T &value)
Filter documents where attribute is less than or equal to value.
void queryEqual(const std::string attributeId, std::list< T > &values)
Filter documents where attribute equals any value in list.
void queryContains(const std::string attributeId, const std::string &value)
Filter documents where attribute contains the value.
void queryStartsWith(const std::string attributeId, const std::string &value)
Filter documents where attribute starts with given value.
bool removeJsonQuery(int index)
Remove a JSON query by index.
void queryLimit(int limit)
Limit the number of documents returned.
void queryIsNull(const std::string attributeId)
Filter documents where an attribute is null.
void queryGreaterThan(const std::string attributeId, const T &value)
Filter documents where attribute is greater than value.