OpenM++ runtime library (libopenm)
modelSqlBuilder.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_SQL_BUILDER_H
9#define MODEL_SQL_BUILDER_H
10
11#include <fstream>
12#include <streambuf>
13#include "helper.h"
14#include "crc32.h"
15#include "md5.h"
16#include "dbExec.h"
20#include "dbMetaTable.h"
21#include "modelSqlWriter.h"
22#include "modelInsertSql.h"
23#include "modelExpressionSql.h"
24
25using namespace std;
26
27namespace openm
28{
31 {
32 public:
34 ModelSqlBuilder(const string & i_providerNames, const string & i_sqlDir, const string & i_outputDir, const string & i_sqliteDir);
35
37 ~ModelSqlBuilder() noexcept { }
38
40 bool isSqliteDb(void) const override { return isSqlite; }
41
43 void setModelMetaRows(MetaModelHolder & io_metaRows) const override;
44
46 void build(MetaModelHolder & io_metaRows) override;
47
49 void beginWorkset(const MetaModelHolder & i_metaRows, MetaSetLangHolder & io_metaSet) override;
50
53 const MetaModelHolder & i_metaRows, const MetaSetLangHolder & i_metaSet, const string & i_name, int i_subId, const string & i_value
54 ) override;
55
58 const MetaModelHolder & i_metaRows, const MetaSetLangHolder & i_metaSet, const string & i_name, int i_subId, const list<string> & i_valueLst
59 ) override;
60
62 void endWorkset(const MetaModelHolder & i_metaRows, const MetaSetLangHolder & i_metaSet) override;
63
65 void buildCompatibilityViews(const MetaModelHolder & i_metaRows) const override;
66
67 private:
68 string sqlDir; // sql scripts directory to create database
69 string sourceDir; // source code directory
70 string outputDir; // output directory to write sql script files
71 string sqliteDir; // output directory to create model.sqlite database
72 bool isSqlite; // if true then create SQLite database
73 list<string> dbProviderLst; // list of db provider names, ie: sqlite,postgresql,mysql
74 unique_ptr<IDbExec> dbExec; // database connection
75
77 struct DbTblInfo
78 {
80 int modelId;
81
83 int id;
84
86 string name;
87
89 vector<string> dimVec;
90
92 vector<string> colVec;
93
95 vector<int> typeIdVec;
96
97 DbTblInfo(void) : id(-1) { }
98 };
99
101 struct ParamTblInfo : DbTblInfo
102 {
104 vector<TypeDicRow>::const_iterator valueTypeIt;
105
107 bool isAdded;
108
110 bool isNullable;
111 };
112
114 struct OutTblInfo : DbTblInfo
115 {
117 vector<int> accIdVec;
118
120 vector<string> accNameVec;
121
123 vector<string> accColVec;
124 };
125
127 vector<ParamTblInfo> paramInfoVec;
128
130 vector<OutTblInfo> outInfoVec;
131
133 void prepare(MetaModelHolder & io_metaRows) const;
134
136 void buildCreateModel(const string & i_sqlProvider, const MetaModelHolder & i_metaRows, const string & i_filePath) const;
137
139 static void createModel(IDbExec * i_dbExec, MetaModelHolder & io_metaRows);
140
142 void buildCreateModelTables(const string & i_sqlProvider, const MetaModelHolder & i_metaRows, const string & i_filePath) const;
143
145 void buildDropModelTables(const MetaModelHolder & i_metaRows, const string & i_filePath) const;
146
148 void prepareWorkset(const MetaModelHolder & i_metaRows, MetaSetLangHolder & io_metaSet) const;
149
151 void doAddScalarWorksetParameter(
152 int i_setId,
153 const string & i_name,
154 const string & i_dbTableName,
155 const vector<ParamTblInfo>::const_iterator & i_paramInfo,
156 int i_subId,
157 const string & i_value
158 );
159
161 const void paramCreateTable(
162 const string & i_sqlProvider,
163 const string & i_dbTableName,
164 const string & i_runSetId,
165 const ParamTblInfo & i_tblInfo,
166 ModelSqlWriter & io_wr
167 ) const;
168
170 const void accCreateTable(
171 const string & i_sqlProvider,
172 const string & i_dbTableName,
173 const OutTblInfo & i_tblInfo,
174 ModelSqlWriter & io_wr
175 ) const;
176
178 const void valueCreateTable(
179 const string & i_sqlProvider,
180 const string & i_dbTableName,
181 const OutTblInfo & i_tblInfo,
182 ModelSqlWriter & io_wr
183 ) const;
184
186 const void accAllCreateView(
187 const string & i_sqlProvider,
188 const string & i_viewName,
189 const OutTblInfo & i_tblInfo,
190 const string & i_accTablName,
191 const vector<TableAccRow> & i_accVec,
192 ModelSqlWriter & io_wr
193 ) const;
194
196 const void paramCompatibilityView(
197 const string & i_sqlProvider,
198 const ModelDicRow & i_modelRow,
199 const string & i_viewName,
200 const string & i_srcTableName,
201 const vector<string> & i_colNames,
202 const vector<int> & i_dimsLowerBound,
203 int i_valueLowerBound,
204 ModelSqlWriter & io_wr
205 ) const;
206
208 const void outputCompatibilityView(
209 const string & i_sqlProvider,
210 const ModelDicRow & i_modelRow,
211 const string & i_viewName,
212 const string & i_srcTableName,
213 const vector<string> & i_colNames,
214 const vector<int> & i_dimsLowerBound,
215 ModelSqlWriter & io_wr
216 ) const;
217
219 void setWorksetRow(WorksetLstRow & io_wsRow) const;
220
222 void setTypeDicRows(MetaModelHolder & io_metaRows) const;
223
225 void setParamTableInfo(MetaModelHolder & io_metaRows);
226
228 void setOutTableInfo(MetaModelHolder & io_metaRows);
229
231 void setParamDicRows(MetaModelHolder & io_metaRows) const;
232
234 void setTableDicRows(MetaModelHolder & io_metaRows) const;
235
237 void setEntityDicRows(MetaModelHolder& io_metaRows) const;
238
240 void setModelProfileRows(MetaModelHolder & io_metaRows) const;
241
243 static void trimModelRows(MetaModelHolder & io_metaRows);
244
246 static void sortModelRows(MetaModelHolder & io_metaRows);
247
249 static void sortWorksetRows(MetaSetLangHolder & io_metaSet);
250
252 static void setColumnNames(MetaModelHolder & io_metaRows);
253
255 void setModelDicRow(ModelDicRow & io_mdRow) const;
256
258 static const string makeTypeDigest(const TypeDicRow & i_typeRow, const MetaModelHolder & i_metaRows);
259
261 static const tuple<string, string> makeParamDigest(const ParamDicRow & i_paramRow, const MetaModelHolder & i_metaRows);
262
264 static const tuple<string, string> makeOutTableDigest(const TableDicRow i_tableRow, const MetaModelHolder & i_metaRows);
265
267 static const string makeEntityDigest(const EntityDicRow i_entityRow, const MetaModelHolder& i_metaRows);
268
270 static const string makeModelDigest(const MetaModelHolder & i_metaRows);
271 };
272}
273
274#endif // MODEL_SQL_BUILDER_H
class to build model creation sql from metadata rows.
Definition: modelSqlBuilder.h:31
void addWorksetParameter(const MetaModelHolder &i_metaRows, const MetaSetLangHolder &i_metaSet, const string &i_name, int i_subId, const string &i_value) override
append scalar parameter value to sql script for new working set creation
Definition: modelSqlBuilder.cpp:517
void build(MetaModelHolder &io_metaRows) override
update metadata and write sql script to create new model from supplied metadata rows
Definition: modelSqlBuilder.cpp:34
~ModelSqlBuilder() noexcept
release builder resources.
Definition: modelSqlBuilder.h:37
ModelSqlBuilder(const string &i_providerNames, const string &i_sqlDir, const string &i_outputDir, const string &i_sqliteDir)
create new model builder.
Definition: modelSqlBuilder.cpp:19
void buildCompatibilityViews(const MetaModelHolder &i_metaRows) const override
write sql script to create backward compatibility views
Definition: modelSqlBuilder.cpp:771
void setModelMetaRows(MetaModelHolder &io_metaRows) const override
set meta rows values and calculate digests for types, parameters, tables and model
Definition: modelSqlBuilderPrepare.cpp:10
bool isSqliteDb(void) const override
if true then create SQLite database
Definition: modelSqlBuilder.h:40
void beginWorkset(const MetaModelHolder &i_metaRows, MetaSetLangHolder &io_metaSet) override
start sql script to create new working set
Definition: modelSqlBuilder.cpp:450
void endWorkset(const MetaModelHolder &i_metaRows, const MetaSetLangHolder &i_metaSet) override
finish sql script to create new working set
Definition: modelSqlBuilder.cpp:485
OpenM++ CRC-32 impelementation, it is same CRC-32 as V.42, gzip, PNG.
OpenM++ data library: public interface.
OpenM++ data library: db tables for model metadata.
OpenM++ common helper utilities.
OpenM++ common file utilities.
OpenM++ data library: public interface to produce model creation script.
OpenM++ data library: classes to translate output table accumulators and expressions into sql.
OpenM++ data library: class to produce insert sql statements for new model.
OpenM++ data library: wrapper around file stream to write into sql script file.
openM++ namespace
Definition: log.h:32
OpenM++ common file utilities.
public interface to build model creation script from metadata rows.
Definition: modelBuilder.h:22
Holder for model metadata db rows.
Definition: metaModelHolder.h:19
Holder for working sets metadata.
Definition: metaSetHolder.h:30