ledger-core
ConditionQueryFilter.h
1 /*
2  *
3  * ConditionQueryFilter
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 30/06/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_CONDITIONQUERYFILTER_H
32 #define LEDGER_CORE_CONDITIONQUERYFILTER_H
33 
34 #include "QueryFilter.h"
35 #include <fmt/format.h>
36 #include <api/Amount.hpp>
37 
38 namespace ledger {
39  namespace core {
40  template <typename T>
42  public:
43  ConditionQueryFilter(const std::string& fieldName, const std::string& symbol, const T& value,
44  const std::string& prefix) {
45  _fieldName = std::move(fieldName);
46  _symbol = std::move(symbol);
47  _value = value;
48  if (!prefix.empty()) {
49  _prefixedName = fmt::format("{}.{}", prefix, _fieldName);
50  } else {
51  _prefixedName = _fieldName;
52  }
53  };
54 
55  void toString(std::stringstream &ss) const override {
56  if (_symbol.find("NULL") != std::string::npos) {
57  ss << _prefixedName << " " << _symbol;
58  } else {
59  ss << _prefixedName << " " << _symbol << " :" << _fieldName;
60  }
61  if (!isTail()) {
62  switch (getOperatorForNextFilter()) {
63  case QueryFilterOperator::OP_AND :
64  ss << " AND ";
65  break;
66  case QueryFilterOperator::OP_AND_NOT :
67  ss << " AND NOT ";
68  break;
69  case QueryFilterOperator::OP_OR :
70  ss << " OR ";
71  break;
72  case QueryFilterOperator::OP_OR_NOT :
73  ss << " OR NOT ";
74  break;
75  }
76  getNext()->toString(ss);
77  }
78  }
79 
80  void bindValue(soci::details::prepare_temp_type &statement) const override {
81  if (_symbol.find("NULL") == std::string::npos) {
82  statement, soci::use(_value);
83  }
84  if (!isTail()) {
85  getNext()->bindValue(statement);
86  }
87  }
88 
89  private:
90  std::string _fieldName;
91  std::string _symbol;
92  std::string _prefixedName;
93  T _value;
94  };
95 
97  public:
98  PlainTextConditionQueryFilter(const std::string& condition) : _condition(std::move(condition)) {};
99 
100  void toString(std::stringstream &ss) const override;
101 
102  void bindValue(soci::details::prepare_temp_type &statement) const override;
103 
104  private:
105  std::string _condition;
106  };
107  }
108 }
109 
110 
111 #endif //LEDGER_CORE_CONDITIONQUERYFILTER_H
Definition: ConditionQueryFilter.h:96
Definition: QueryFilter.h:51
Definition: Account.cpp:8
Definition: ConditionQueryFilter.h:41