JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
UnaryStringProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 06/09/2018.
3 //
4 
5 #ifndef JODA_UNARYSTRINGPROVIDER_H
6 #define JODA_UNARYSTRINGPROVIDER_H
7 
8 #include <joda/misc/RJFwd.h>
9 #include "IValueProvider.h"
10 
11 namespace joda::query {
25 template <class Calc>
27  public:
29  std::vector<std::unique_ptr<IValueProvider>> &&parameters)
30  : IValueProvider(std::move(parameters)) {
31  checkParamSize(1);
33  DCHECK(isAtom()) << "Only atom ReturnTypes allowed";
34  };
35 
37  return Calc::retType;
38  }
39 
40  std::string getName() const override { return Calc::name; };
41 
42  std::string toString() const override { return IValueProvider::toString(); }
43 
44  std::unique_ptr<IValueProvider> duplicate() const override {
45  return std::make_unique<UnaryStringProvider<Calc>>(duplicateParameters());
46  };
47 
48  bool isConst() const override {
49  bool c = true;
50  for (const auto &param : params) {
51  c &= param->isConst();
52  }
53  return c;
54  }
55 
57  RJMemoryPoolAlloc &alloc) const override {
58  std::string str;
59  if (!getParamString(str, params[0], json)) return RJValue();
60  return Calc::calculate(std::move(str), alloc);
61  };
62 
63  const RJValue *getValue(const RapidJsonDocument &json,
64  RJMemoryPoolAlloc &alloc) const override {
65  DCHECK(!isAtom()) << "Did not check for atom first";
66  return nullptr;
67  };
68 
70 
71  protected:
73  for (unsigned int i = 0; i < params.size(); ++i) {
75  }
76  }
77 };
78 
79 /*
80  * LEN
81  */
83  static constexpr auto name = "LEN";
85 
86  inline static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc) {
87  return RJValue(val.size());
88  };
89 };
90 
92 
93 /*
94  * UPPER
95  */
97  static constexpr auto name = "UPPER";
99 
100  inline static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc) {
101  transform(val.begin(), val.end(), val.begin(), ::toupper);
102  return RJValue(val.c_str(), alloc);
103  };
104 };
105 
107 
108 /*
109  * LOWER
110  */
112  static constexpr auto name = "LOWER";
114 
115  inline static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc) {
116  transform(val.begin(), val.end(), val.begin(), ::tolower);
117  return RJValue(val.c_str(), alloc);
118  };
119 };
120 
122 
123 /*
124  * LTRIM
125  */
127  static constexpr auto name = "LTRIM";
129 
130  inline static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc) {
131  val.erase(val.begin(), std::find_if(val.begin(), val.end(), [](int ch) {
132  return !std::isspace(ch);
133  }));
134  return RJValue(val.c_str(), alloc);
135  };
136 };
137 
139 
140 /*
141  * RTRIM
142  */
144  static constexpr auto name = "RTRIM";
146 
147  inline static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc) {
148  val.erase(std::find_if(val.rbegin(), val.rend(),
149  [](int ch) { return !std::isspace(ch); })
150  .base(),
151  val.end());
152  return RJValue(val.c_str(), alloc);
153  };
154 };
155 
157 
159 
161 
163 
165 
167 } // namespace joda::query
168 #endif // JODA_UNARYSTRINGPROVIDER_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: 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: UnaryStringProvider.h:26
RJValue getAtomValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: UnaryStringProvider.h:56
const RJValue * getValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: UnaryStringProvider.h:63
void checkAllParamTypes()
Definition: UnaryStringProvider.h:72
std::string getName() const override
Definition: UnaryStringProvider.h:40
std::string toString() const override
Definition: UnaryStringProvider.h:42
UnaryStringProvider(std::vector< std::unique_ptr< IValueProvider >> &&parameters)
Definition: UnaryStringProvider.h:28
std::unique_ptr< IValueProvider > duplicate() const override
Definition: UnaryStringProvider.h:44
joda::query::IValueType getReturnType() const override
Definition: UnaryStringProvider.h:36
bool isConst() const override
Definition: UnaryStringProvider.h:48
Definition: AttributeStatAggregator.h:12
UnaryStringProvider< UnaryRtrimCalculationFunction > RtrimProvider
Definition: UnaryStringProvider.h:156
UnaryStringProvider< UnaryUpperCalculationFunction > UpperProvider
Definition: UnaryStringProvider.h:106
UnaryStringProvider< UnaryLenCalculationFunction > LenProvider
Definition: UnaryStringProvider.h:91
UnaryStringProvider< UnaryLowerCalculationFunction > LowerProvider
Definition: UnaryStringProvider.h:121
UnaryStringProvider< UnaryLtrimCalculationFunction > LtrimProvider
Definition: UnaryStringProvider.h:138
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
@ IV_String
Definition: IValueProvider.h:34
Definition: UnaryStringProvider.h:82
static constexpr auto name
Definition: UnaryStringProvider.h:83
static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc)
Definition: UnaryStringProvider.h:86
static constexpr joda::query::IValueType retType
Definition: UnaryStringProvider.h:84
Definition: UnaryStringProvider.h:111
static constexpr auto name
Definition: UnaryStringProvider.h:112
static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc)
Definition: UnaryStringProvider.h:115
static constexpr joda::query::IValueType retType
Definition: UnaryStringProvider.h:113
Definition: UnaryStringProvider.h:126
static constexpr auto name
Definition: UnaryStringProvider.h:127
static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc)
Definition: UnaryStringProvider.h:130
static constexpr joda::query::IValueType retType
Definition: UnaryStringProvider.h:128
Definition: UnaryStringProvider.h:143
static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc)
Definition: UnaryStringProvider.h:147
static constexpr auto name
Definition: UnaryStringProvider.h:144
static constexpr joda::query::IValueType retType
Definition: UnaryStringProvider.h:145
Definition: UnaryStringProvider.h:96
static RJValue calculate(std::string &&val, RJMemoryPoolAlloc &alloc)
Definition: UnaryStringProvider.h:100
static constexpr joda::query::IValueType retType
Definition: UnaryStringProvider.h:98
static constexpr auto name
Definition: UnaryStringProvider.h:97