JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
BinaryStringProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 06/09/2018.
3 //
4 
5 #ifndef JODA_BINARYSTRINGPROVIDER_H
6 #define JODA_BINARYSTRINGPROVIDER_H
7 
8 #include <cmath>
9 
10 #include "IValueProvider.h"
11 
12 namespace joda::query {
26 template <class Calc>
28  public:
30  std::vector<std::unique_ptr<IValueProvider>> &&parameters)
31  : IValueProvider(std::move(parameters)) {
32  checkParamSize(2);
34  DCHECK(isAtom()) << "Only atom ReturnTypes allowed";
35  };
36 
38  return Calc::retType;
39  }
40 
41  std::string getName() const override { return Calc::name; };
42 
43  std::string toString() const override { return IValueProvider::toString(); }
44 
45  std::unique_ptr<IValueProvider> duplicate() const override {
46  return std::make_unique<BinaryStringProvider<Calc>>(duplicateParameters());
47  };
48 
49  bool isConst() const override {
50  bool c = true;
51  for (const auto &param : params) {
52  c &= param->isConst();
53  }
54  return c;
55  }
56 
58  RJMemoryPoolAlloc &alloc) const override {
59  std::string lhsStr;
60  if (!getParamString(lhsStr, params[0], json)) return RJValue();
61  std::string rhsStr;
62  if (!getParamString(rhsStr, params[1], json)) return RJValue();
63 
64  return Calc::calculate(std::move(lhsStr), std::move(rhsStr), alloc);
65  };
66 
67  const RJValue *getValue(const RapidJsonDocument &json,
68  RJMemoryPoolAlloc &alloc) const override {
69  DCHECK(!isAtom()) << "Did not check for atom first";
70  return nullptr;
71  };
72 
74 
75  protected:
77  for (unsigned int i = 0; i < params.size(); ++i) {
79  }
80  }
81 };
82 
83 /*
84  * SCONTAINS
85  */
87  static constexpr auto name = "SCONTAINS";
89 
90  inline static RJValue calculate(std::string &&lhs, std::string &&rhs,
91  RJMemoryPoolAlloc &alloc) {
92  return RJValue(lhs.find(rhs) != std::string::npos);
93  };
94 };
95 
96 typedef BinaryStringProvider<BinarySCONTAINSCalculationFunction>
98 
99 /*
100  * STARTSWITH
101  */
103  static constexpr auto name = "STARTSWITH";
105 
106  inline static RJValue calculate(std::string &&lhs, std::string &&rhs,
107  RJMemoryPoolAlloc &alloc) {
108  if (rhs.size() > lhs.size()) return RJValue(false);
109  return RJValue(lhs.substr(0, rhs.size()) == rhs);
110  };
111 };
112 
113 typedef BinaryStringProvider<BinarySTARTSWITHCalculationFunction>
115 
116 /*
117  * CONCAT
118  */
120  static constexpr auto name = "CONCAT";
122 
123  inline static RJValue calculate(std::string &&lhs, std::string &&rhs,
124  RJMemoryPoolAlloc &alloc) {
125  return RJValue((lhs + rhs).c_str(), alloc);
126  };
127 };
128 
130 
131 /*
132  * FINDSTR
133  */
135  static constexpr auto name = "FINDSTR";
137 
138  inline static RJValue calculate(std::string &&lhs, std::string &&rhs,
139  RJMemoryPoolAlloc &alloc) {
140  return RJValue((int64_t)lhs.find(rhs));
141  };
142 };
143 
145 
147 
149 
151 
153 } // namespace joda::query
154 #endif // JODA_BINARYSTRINGPROVIDER_H
#define CREATE_FACTORY(FCLASS)
Definition: IValueProvider.h:20
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: BinaryStringProvider.h:27
bool isConst() const override
Definition: BinaryStringProvider.h:49
const RJValue * getValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: BinaryStringProvider.h:67
joda::query::IValueType getReturnType() const override
Definition: BinaryStringProvider.h:37
std::unique_ptr< IValueProvider > duplicate() const override
Definition: BinaryStringProvider.h:45
void checkAllParamTypes()
Definition: BinaryStringProvider.h:76
RJValue getAtomValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: BinaryStringProvider.h:57
std::string getName() const override
Definition: BinaryStringProvider.h:41
BinaryStringProvider(std::vector< std::unique_ptr< IValueProvider >> &&parameters)
Definition: BinaryStringProvider.h:29
std::string toString() const override
Definition: BinaryStringProvider.h:43
Definition: IValueProvider.h:143
std::vector< std::unique_ptr< IValueProvider > > params
Definition: IValueProvider.h:373
void checkParamSize(unsigned int expected)
Definition: IValueProvider.cpp:112
virtual bool isAtom() const
Definition: IValueProvider.h:215
std::vector< std::unique_ptr< IValueProvider > > duplicateParameters() const
Definition: IValueProvider.cpp:104
bool getParamString(std::string &ret, const std::unique_ptr< IValueProvider > &val, const RapidJsonDocument &json) const
Definition: IValueProvider.h:319
void checkParamType(unsigned int i, IValueType expected)
Definition: IValueProvider.cpp:125
virtual std::string toString() const
Definition: IValueProvider.cpp:99
Definition: AttributeStatAggregator.h:12
BinaryStringProvider< BinarySCONTAINSCalculationFunction > SCONTAINSProvider
Definition: BinaryStringProvider.h:97
BinaryStringProvider< BinaryFINDSTRCalculationFunction > FINDSTRProvider
Definition: BinaryStringProvider.h:144
BinaryStringProvider< BinaryConcatCalculationFunction > ConcatProvider
Definition: BinaryStringProvider.h:129
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
@ IV_Bool
Definition: IValueProvider.h:36
@ IV_String
Definition: IValueProvider.h:34
BinaryStringProvider< BinarySTARTSWITHCalculationFunction > STARTSWITHProvider
Definition: BinaryStringProvider.h:114
Definition: BinaryStringProvider.h:119
static constexpr auto name
Definition: BinaryStringProvider.h:120
static RJValue calculate(std::string &&lhs, std::string &&rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryStringProvider.h:123
static constexpr joda::query::IValueType retType
Definition: BinaryStringProvider.h:121
Definition: BinaryStringProvider.h:134
static RJValue calculate(std::string &&lhs, std::string &&rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryStringProvider.h:138
static constexpr auto name
Definition: BinaryStringProvider.h:135
static constexpr joda::query::IValueType retType
Definition: BinaryStringProvider.h:136
Definition: BinaryStringProvider.h:86
static RJValue calculate(std::string &&lhs, std::string &&rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryStringProvider.h:90
static constexpr joda::query::IValueType retType
Definition: BinaryStringProvider.h:88
static constexpr auto name
Definition: BinaryStringProvider.h:87
Definition: BinaryStringProvider.h:102
static constexpr auto name
Definition: BinaryStringProvider.h:103
static RJValue calculate(std::string &&lhs, std::string &&rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryStringProvider.h:106
static constexpr joda::query::IValueType retType
Definition: BinaryStringProvider.h:104