OpenM++ runtime library (libopenm)
modelExpressionSql.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2013-2015 OpenM++
6 // This code is licensed under the MIT license (see LICENSE.txt for details)
7 
8 #ifndef MODEL_EXPRESSION_SQL_H
9 #define MODEL_EXPRESSION_SQL_H
10 
11 #include "libopenm/db/dbMetaRow.h"
12 using namespace std;
13 
14 namespace openm
15 {
17  enum class FncCode
18  {
20  undefined = 0,
21 
23  if_case,
24 
26  div_by,
27 
29  avg,
30 
32  sum,
33 
35  count,
36 
38  min,
39 
41  max,
42 
44  var,
45 
47  sd,
48 
50  se,
51 
53  cv
54  };
55 
58  {
60  string colName;
61 
63  string srcExpr;
64 
66  string sqlExpr;
67 
69  static int nextExprNumber;
70 
71  AggregationColumnExpr(const string & i_colName, const string & i_srcExpr) :
72  colName(i_colName), srcExpr(i_srcExpr)
73  { }
74  AggregationColumnExpr(const string & i_srcExpr) :
75  colName("ex" + to_string(nextExprNumber++)), srcExpr(i_srcExpr)
76  { }
77  };
78 
81  {
82  public:
93  const string & i_accTableName, const vector<string> & i_dimCols, const vector<int> & i_accIds, const vector<string> & i_accNames, const vector<string> & i_accCols
94  ) :
95  accTableName(i_accTableName),
96  dimCols(i_dimCols),
97  accIds(i_accIds),
98  accNames(i_accNames),
99  accCols(i_accCols)
100  {
101  accCount = (int)accIds.size();
102  }
103 
105  ~ModelBaseExpressionSql() noexcept { }
106 
107  protected:
109  const string accTableName;
110 
112  const vector<string> dimCols;
113 
115  const vector<int> accIds;
116 
118  const vector<string> accNames;
119 
121  const vector<string> accCols;
122 
124  int accCount;
125 
132  const string translateAllSimpleFnc(const string & i_srcMsg, bool i_isSkipAggr, const string & i_expr);
133 
135  static const string translateSimpleFnc(const string & i_srcMsg, FncCode i_code, const string & i_arg);
136 
137  private:
138  ModelBaseExpressionSql(const ModelBaseExpressionSql & i_other) = delete;
139  ModelBaseExpressionSql & operator=(const ModelBaseExpressionSql & i_other) = delete;
140  };
141 
144  {
145  public:
156  const string & i_accTableName,
157  const vector<string> & i_dimCols,
158  const vector<int> & i_accIds,
159  const vector<string> & i_accNames,
160  const vector<string> & i_accCols,
161  const vector<TableAccRow> & i_tableAcc
162  ) :
163  ModelBaseExpressionSql(i_accTableName, i_dimCols, i_accIds, i_accNames, i_accCols),
164  tableAccVec(i_tableAcc)
165  { }
166 
168  ~ModelAccumulatorSql() noexcept { }
169 
179  const string translateNativeAccExpr(const string & i_outTableName, const string & i_accName, int i_accId);
180 
191  const string translateDerivedAccExpr(const string & i_outTableName, const string & i_accName, const string & i_expr, const map<string, size_t> & i_nativeMap);
192 
193  private:
195  string srcMsg;
196 
198  const vector<TableAccRow> & tableAccVec;
199 
201  vector<bool> isAccUsedArr;
202 
203  private:
204  ModelAccumulatorSql(const ModelAccumulatorSql & i_other) = delete;
205  ModelAccumulatorSql & operator=(const ModelAccumulatorSql & i_other) = delete;
206  };
207 
210  {
211  public:
222  const string & i_accTableName,
223  const vector<string> & i_dimCols,
224  const vector<int> & i_accIds,
225  const vector<string> & i_accNames,
226  const vector<string> & i_accCols
227  ) :
228  ModelBaseExpressionSql(i_accTableName, i_dimCols, i_accIds, i_accNames, i_accCols)
229  { }
230 
232  ~ModelAggregationSql() noexcept { }
233 
244  const string translateAggregationExpr(const string & i_outTableName, const string & i_name, const string & i_colName, const string & i_expr);
245 
246  private:
248  string srcMsg;
249 
251  vector<bool> isAccUsedArr;
252 
254  vector<AggregationColumnExpr> nextExprArr;
255 
257  const string translateAggregationFnc(FncCode i_code, const string & i_nextInnerAlias, const string & i_arg);
258 
260  const string translateArg(const string & i_nextInnerAlias, const string & i_arg);
261 
263  const string processAccumulators(const string & i_expr);
264 
266  const string processAccumulators(bool i_isTranslate, int i_level, const string & i_fromAlias, const string & i_expr);
267 
269  const string pushAvgToNextLevel(const string & i_arg) { return pushToNextLevel("OM_AVG(" + i_arg + ")"); }
270 
272  const string pushToNextLevel(const string & i_fncExpr);
273 
275  bool isFirstUsedAcc(int i_accPos, const vector<bool> & i_accUsage) const;
276 
278  const string makeAccTableAlias(int i_accPos, const vector<bool> & i_accUsage, int i_level, const string i_firstAlias) const;
279 
280  private:
281  ModelAggregationSql(const ModelAggregationSql & i_other) = delete;
282  ModelAggregationSql & operator=(const ModelAggregationSql & i_other) = delete;
283  };
284 }
285 
286 #endif // MODEL_EXPRESSION_SQL_H
class to produce accumulators sql subqueries for otput table
Definition: modelExpressionSql.h:144
ModelAccumulatorSql(const string &i_accTableName, const vector< string > &i_dimCols, const vector< int > &i_accIds, const vector< string > &i_accNames, const vector< string > &i_accCols, const vector< TableAccRow > &i_tableAcc)
initialization: store output table definition parts.
Definition: modelExpressionSql.h:155
~ModelAccumulatorSql() noexcept
release sql builder resources.
Definition: modelExpressionSql.h:168
class to produce aggregation sql for otput table
Definition: modelExpressionSql.h:210
~ModelAggregationSql() noexcept
release sql builder resources.
Definition: modelExpressionSql.h:232
ModelAggregationSql(const string &i_accTableName, const vector< string > &i_dimCols, const vector< int > &i_accIds, const vector< string > &i_accNames, const vector< string > &i_accCols)
create aggregation sql builder for output table.
Definition: modelExpressionSql.h:221
base class to produce sql expressions and subqueries for otput table
Definition: modelExpressionSql.h:81
const string accTableName
accumulator database table name
Definition: modelExpressionSql.h:109
const vector< int > accIds
ids of table accumulators
Definition: modelExpressionSql.h:115
~ModelBaseExpressionSql() noexcept
release sql builder resources.
Definition: modelExpressionSql.h:105
const vector< string > accNames
names of table accumulators: Acc0, Acc1
Definition: modelExpressionSql.h:118
const vector< string > dimCols
names of table dimensions
Definition: modelExpressionSql.h:112
int accCount
number of accumulators
Definition: modelExpressionSql.h:124
ModelBaseExpressionSql(const string &i_accTableName, const vector< string > &i_dimCols, const vector< int > &i_accIds, const vector< string > &i_accNames, const vector< string > &i_accCols)
initialization: store output table definition parts.
Definition: modelExpressionSql.h:92
const vector< string > accCols
column names of table accumulators: acc0, acc1
Definition: modelExpressionSql.h:121
OpenM++ data library: db rows of model metadata tables.
openM++ namespace
Definition: log.h:32
FncCode
aggregation and non-aggregation function code
Definition: modelExpressionSql.h:18
@ sum
sum of values
@ max
maximum value
@ undefined
undefined function
@ sd
standard deviation
@ var
variance
@ div_by
divide by: wrap value as divisor
@ if_case
if condition then value else other value
@ min
minimal value
@ cv
coefficient of variation
@ count
count of values
@ avg
average value
@ se
standard error
column aggregation expression
Definition: modelExpressionSql.h:58
string colName
column name, ie: ex2
Definition: modelExpressionSql.h:60
string sqlExpr
sql expresiion, ie: AVG(M2.acc0)
Definition: modelExpressionSql.h:66
string srcExpr
source expression, ie: OM_AVG(acc0)
Definition: modelExpressionSql.h:63
static int nextExprNumber
number of aggregation epxpressions
Definition: modelExpressionSql.h:69