fhq-server  v0.2.33
Documentation for fhq-server. FreeHackQuest is an open source platform for competitions of computer security.
wsjcpp_storages.h
Go to the documentation of this file.
1 #ifndef WSJCPP_STORAGES_H
2 #define WSJCPP_STORAGES_H
3 
4 #include <map>
5 #include <string>
6 #include <vector>
7 #include <wsjcpp_core.h>
8 
9 #include <string>
10 #include <vector>
11 #include <map>
12 
13 // ---------------------------------------------------------------------
14 
20 };
21 
22 // TODO cast to string
23 
24 // ---------------------------------------------------------------------
25 
27  public:
28  WsjcppStorageColumnDef(const std::string &sColumnName);
31  WsjcppStorageColumnDef &string(int nSize);
38  WsjcppStorageColumnDef &defaultValue(const std::string& sDefault);
40  WsjcppStorageColumnDef &enableUniqueIndex(const std::string& sIndexName);
41 
42  std::string columnName() const;
43  std::string columnType();
44  bool isDefaultValue();
45  std::string columnDefaultValue();
46 
47  int columnTypeSize();
48  bool isAutoIncrement();
49  bool isPrimaryKey();
50  bool isNotNull();
51  bool isEnableIndex();
52  bool isEnableUniqueIndex();
53  std::string nameOfUniqueIndex();
54 
55  private:
56  std::string TAG;
57  std::string m_sColumnName;
58  std::string m_sType;
59  std::string m_sDefaultValue;
61 
63  bool m_bNotNull;
68  std::string m_sNameOfUniqueIndex;
69 };
70 
71 // ---------------------------------------------------------------------
72 
73 // TODO redesign
74 
76 
77 };
78 
79 // ---------------------------------------------------------------------
80 
90 };
91 
92 // ---------------------------------------------------------------------
93 
95  public:
96  WsjcppStorageChanges(const std::string &sTableName);
97  std::string getTableName() const;
98  virtual WsjcppStorageChangesType getType() const;
99  virtual std::string getStartApply() const;
100  virtual std::string getAppliedSuccess() const;
101  virtual std::string getAppliedFailed() const;
102 
103  protected:
104  std::string TAG;
105  std::string m_sTableName;
106 };
107 
108 // ---------------------------------------------------------------------
109 
111  public:
112  WsjcppStorageCreateTable(const std::string &sTableName);
113  virtual WsjcppStorageChangesType getType() const override;
114  virtual std::string getStartApply() const override;
115  virtual std::string getAppliedSuccess() const override;
116  virtual std::string getAppliedFailed() const override;
117 
118  WsjcppStorageColumnDef &addColumn(const std::string &sColumnName);
119  const std::vector<WsjcppStorageColumnDef> &getColumns() const;
120 
121  private:
122  std::string TAG;
123  std::vector<WsjcppStorageColumnDef> m_vColumns;
124 };
125 
126 // ---------------------------------------------------------------------
127 
129  public:
130  WsjcppStorageModifyTable(const std::string &sTableName);
131  virtual WsjcppStorageChangesType getType() const override;
132  virtual std::string getStartApply() const override;
133  virtual std::string getAppliedSuccess() const override;
134  virtual std::string getAppliedFailed() const override;
135 
136  WsjcppStorageColumnDef &addColumn(const std::string &sColumnName);
137  WsjcppStorageColumnDef &alterColumn(const std::string &sColumnName);
138  std::string dropColumn(const std::string &sColumnName);
139  const std::vector<WsjcppStorageColumnDef> &getAddColumns() const;
140  const std::vector<WsjcppStorageColumnDef> &getAlterColumns() const;
141  const std::vector<std::string> &getDropColumns() const;
142  bool isColumnDefined(const std::string &sColumnName, std::string &sError) const;
143 
144  private:
145  std::string TAG;
146  std::vector<WsjcppStorageColumnDef> m_vAddColumns;
147  std::vector<WsjcppStorageColumnDef> m_vAlterColumns;
148  std::vector<std::string> m_vDropColumns;
149 };
150 
151 // ---------------------------------------------------------------------
152 
154  public:
155  WsjcppStorageDropTable(const std::string &sTableName);
156  virtual WsjcppStorageChangesType getType() const override;
157  virtual std::string getStartApply() const override;
158  virtual std::string getAppliedSuccess() const override;
159  virtual std::string getAppliedFailed() const override;
160 
161  private:
162  std::string TAG;
163 };
164 
165 // ---------------------------------------------------------------------
166 
168  public:
169  WsjcppStorageColumnValue(const std::string &sColumnName, WsjcppStorageColumnType nType);
170  void setValue(std::string sValue);
171  void setValue(int nValue);
172  void setValue(double nValue);
173  std::string getColumnName();
174  WsjcppStorageColumnType getColumnType();
175  std::string getString();
176  int getInt();
177  double getDouble();
178 
179  private:
180  std::string m_sColumnName;
182 
183  std::string m_sStringValue;
186 };
187 
188 // ---------------------------------------------------------------------
189 
191  public:
192  WsjcppStorageTable(const std::string &sTableName);
194  std::string getTableName() const;
195  const std::vector<WsjcppStorageColumnDef> &getColumns() const;
196  void mergeWith(WsjcppStorageModifyTable &modifyTable);
197 
198  private:
199  std::string TAG;
200  std::string m_sTableName;
201  std::vector<WsjcppStorageColumnDef> m_vColumns;
202 };
203 
204 // ---------------------------------------------------------------------
205 
207  public:
208  WsjcppStorageInsert(const std::string &sTableName);
209  virtual WsjcppStorageChangesType getType() const override;
210  virtual std::string getStartApply() const override;
211  virtual std::string getAppliedSuccess() const override;
212  virtual std::string getAppliedFailed() const override;
213 
214  void bindValue(const std::string &sColumnName, const std::string &sValue);
215  void bindValue(const std::string &sColumnName, int nValue);
216  void bindValue(const std::string &sColumnName, double nValue);
217  std::vector<WsjcppStorageColumnValue> values() const;
218  bool isValid(const WsjcppStorageTable &storageTable) const;
219 
220  private:
221  bool exists(const std::string &sColumnName);
222  std::string TAG;
223  std::string m_sTableName;
224  std::vector<WsjcppStorageColumnValue> m_vValues;
225 };
226 
227 // ---------------------------------------------------------------------
228 
229 /*
230 class StorageUpdate {
231  public:
232  StorageUpdate(const StorageStruct &storageStruct);
233  std::string tableName() const;
234  void bindValue(const std::string &sColumnName, const std::string &sValue);
235  void bindValue(const std::string &sColumnName, int nValue);
236  void bindValue(const std::string &sColumnName, double nValue);
237  std::vector<StorageColumnValue> values() const;
238  bool isValid() const;
239  private:
240  StorageStruct m_storageStruct;
241  bool exists(const std::string &sColumnName);
242  std::string TAG;
243  std::string m_sTableName;
244  std::vector<StorageColumnValue> m_vValues;
245 };
246 */
247 
248 // ---------------------------------------------------------------------
249 
251  public:
253  virtual ~WsjcppStorageConnection();
254  virtual bool executeQuery(const std::string &sQuery) = 0; // TODO redesign in future
255  virtual std::string lastDatabaseVersion() = 0;
256  virtual std::vector<std::string> getInstalledVersions() = 0;
257  virtual bool insertUpdateInfo(const std::string &sVersion, const std::string &sDescription) = 0;
258 
259  long created();
260  long getConnectionDurationInSeconds();
261 
262  protected:
263  std::string TAG;
264 
265  private:
267 };
268 
269 // ---------------------------------------------------------------------
270 
272  public:
273  WsjcppStorage();
274  // WsjcppStorage(const std::string &sType);
275  static std::string type() { return "unknown"; };
276  virtual bool applyConfigFromFile(const std::string &sFilePath) = 0;
277  virtual WsjcppStorageConnection *connect() = 0;
278  virtual void clean() = 0;
279  virtual std::string prepareStringValue(const std::string &sValue) = 0;
280 
281  // virtual std::vector<std::string> prepareSqlQueries(StorageStruct &storageStruct) = 0; // TODO deprecated
282 
283  bool executeStorageChanges(WsjcppStorageConnection *pConn, WsjcppStorageChanges &storageChanges);
284  bool addStorageChanges(WsjcppStorageChanges &storageChanges);
285 
286  virtual std::vector<std::string> prepareSqlQueries(const WsjcppStorageInsert &storageInsert) = 0;
287  virtual std::vector<std::string> prepareSqlQueries(const WsjcppStorageCreateTable &storageCreateTable) = 0;
288  virtual std::vector<std::string> prepareSqlQueries(const WsjcppStorageModifyTable &storageModifyTable) = 0;
289  virtual std::vector<std::string> prepareSqlQueries(const WsjcppStorageDropTable &storageDropTable) = 0;
290 
291  bool insertRow(WsjcppStorageConnection *pConn, const WsjcppStorageInsert &storageInsert);
292  const std::map<std::string, WsjcppStorageTable> &getTables();
293  bool existsTable(const std::string &sTableName);
294  const WsjcppStorageTable &getTableDef(const std::string &sTableName);
295 
296  protected:
297  std::string TAG;
298 
299  private:
300  std::map<std::string, WsjcppStorageTable> m_mapTables;
301 };
302 
303 // ---------------------------------------------------------------------
304 
306  public:
307  virtual WsjcppStorage *create() = 0;
308 };
309 
310 // ---------------------------------------------------------------------
311 
312 extern std::map<std::string, IFabricWsjcppStorage*> *g_pFabricWsjcppStorages;
313 
315  public:
316  static void initGlobalVariables();
317  static void add(const std::string &sType, IFabricWsjcppStorage* pStorage);
318  static std::vector<std::string> list();
319  static bool support(const std::string &sType);
320  static WsjcppStorage* create(const std::string &sType);
321 };
322 
323 // ---------------------------------------------------------------------
324 
325 template<class T>
327  public:
329  WsjcppStorages::add(T::type(), this);
330  };
331  virtual WsjcppStorage *create() {
332  return new T();
333  };
334 };
335 
336 // ---------------------------------------------------------------------
337 // RegistryStorage
338 #define REGISTRY_WSJCPP_STORAGE( classname ) \
339  static FabricWsjcppStorage<classname> * pRegistryFabric ## classname = new FabricWsjcppStorage<classname>(); \
340 
341 // ---------------------------------------------------------------------
342 
344 
345  public:
346  WsjcppStorageUpdateBase(const std::string &sFromVersion, const std::string &sVersion, const std::string &sDescription);
347  const std::string &from_version();
348  const std::string &version();
349  const std::string &description();
350  const std::vector<WsjcppStorageChanges *> &getChanges();
351  void setWeight(int nWeight);
352  int getWeight();
353  protected:
354  std::string TAG;
355  WsjcppStorageCreateTable *createTable(std::string sTableName);
356  WsjcppStorageModifyTable *modifyTable(std::string sTableName);
357  WsjcppStorageDropTable *dropTable(std::string sTableName);
358  WsjcppStorageInsert *insertIntoTable(std::string sTableName);
359 
360  private:
361  void checkTableName(std::string sTableName);
362  std::vector<WsjcppStorageChanges *> m_vStorageChanges;
363  std::string m_sFromVersion;
364  std::string m_sVersion;
365  std::string m_sDescription;
367 };
368 
369 // ---------------------------------------------------------------------
370 
371 extern std::vector<WsjcppStorageUpdateBase *> *g_pWsjcppStorageUpdates;
372 
374  private:
375  static int calculateWeight(int nWeight, const std::string &sVersion);
376  static void sortByWeight(std::vector<WsjcppStorageUpdateBase*> &vUpdates);
377  static std::vector<WsjcppStorageUpdateBase*> findUpdatesFromVersion(const std::string &sVersion);
378  static void pushUpdatesFromVersion(std::vector<WsjcppStorageUpdateBase*> &vUpdates, const std::string &sVersion);
379  static void applyAllStorageChanges(WsjcppStorage *pStorage, WsjcppStorageUpdateBase *pUpdate);
380  static void executeAllStorageChanges(WsjcppStorage *pStorage, WsjcppStorageConnection *pConn, WsjcppStorageUpdateBase *pUpdate);
381 
382  public:
383  static std::vector<WsjcppStorageUpdateBase*> getSortedStorageUpdates();
384  static void initGlobalVariables();
385  static WsjcppStorageUpdateBase* findUpdateFromVersion(const std::string &sFromVersion);
386  static WsjcppStorageUpdateBase* findUpdateVersion(const std::string &sVersion);
387  // static bool apply(Storage *pStorage);
388  static bool apply2(WsjcppStorage *pStorage);
389 };
390 
391 // ---------------------------------------------------------------------
392 
393 // RegistryCmdHandler
394 #define REGISTRY_WSJCPP_STORAGE_UPDATE( classname ) \
395  static classname * pRegistry ## classname = new classname();
396 
397 
398 #endif // WSJCPP_STORAGES_H
std::string TAG
Definition: wsjcpp_storages.h:162
Definition: wsjcpp_storages.h:343
bool isPrimaryKey()
Definition: wsjcpp_storages.cpp:176
Definition: wsjcpp_storages.h:110
Definition: wsjcpp_storages.h:271
std::string m_sFromVersion
Definition: wsjcpp_storages.h:363
std::string TAG
Definition: wsjcpp_storages.h:145
virtual WsjcppStorage * create()
Definition: wsjcpp_storages.h:331
int m_nTypeSize
Definition: wsjcpp_storages.h:60
int m_nIntValue
Definition: wsjcpp_storages.h:184
int columnTypeSize()
Definition: wsjcpp_storages.cpp:164
std::string columnName() const
Definition: wsjcpp_storages.cpp:140
bool isEnableIndex()
Definition: wsjcpp_storages.cpp:188
std::string m_sDescription
Definition: wsjcpp_storages.h:365
bool m_bAutoIncrement
Definition: wsjcpp_storages.h:62
WsjcppStorageColumnDef(const std::string &sColumnName)
Definition: wsjcpp_storages.cpp:10
std::string TAG
Definition: wsjcpp_storages.h:199
WsjcppStorageColumnDef & enableUniqueIndex(const std::string &sIndexName)
Definition: wsjcpp_storages.cpp:132
std::string m_sStringValue
Definition: wsjcpp_storages.h:183
WsjcppStorageColumnDef & number()
Definition: wsjcpp_storages.cpp:76
std::vector< WsjcppStorageColumnValue > m_vValues
Definition: wsjcpp_storages.h:224
std::vector< WsjcppStorageColumnDef > m_vColumns
Definition: wsjcpp_storages.h:123
std::vector< WsjcppStorageUpdateBase * > * g_pWsjcppStorageUpdates
Definition: wsjcpp_storages.cpp:1091
std::string columnDefaultValue()
Definition: wsjcpp_storages.cpp:152
std::string TAG
Definition: wsjcpp_storages.h:104
Definition: wsjcpp_storages.h:84
Definition: wsjcpp_storages.h:326
Definition: wsjcpp_storages.h:85
std::vector< WsjcppStorageColumnDef > m_vAlterColumns
Definition: wsjcpp_storages.h:147
Definition: wsjcpp_storages.h:94
long m_nCreated
Definition: wsjcpp_storages.h:266
bool isNotNull()
Definition: wsjcpp_storages.cpp:182
WsjcppStorageColumnDef & defaultValue(const std::string &sDefault)
Definition: wsjcpp_storages.cpp:117
std::string m_sTableName
Definition: wsjcpp_storages.h:223
Definition: wsjcpp_storages.h:89
bool isAutoIncrement()
Definition: wsjcpp_storages.cpp:170
bool m_bPrimaryKey
Definition: wsjcpp_storages.h:64
Definition: wsjcpp_storages.h:153
Definition: wsjcpp_storages.h:86
Definition: wsjcpp_storages.h:75
Definition: wsjcpp_storages.h:83
Definition: wsjcpp_storages.h:206
Definition: wsjcpp_storages.h:19
int m_nWeight
Definition: wsjcpp_storages.h:366
WsjcppStorageColumnDef & enableIndex()
Definition: wsjcpp_storages.cpp:125
Definition: wsjcpp_storages.h:190
std::string m_sVersion
Definition: wsjcpp_storages.h:364
std::map< std::string, IFabricWsjcppStorage * > * g_pFabricWsjcppStorages
Definition: wsjcpp_storages.cpp:929
std::vector< std::string > m_vDropColumns
Definition: wsjcpp_storages.h:148
bool isEnableUniqueIndex()
Definition: wsjcpp_storages.cpp:194
std::string TAG
Definition: wsjcpp_storages.h:297
std::string m_sType
Definition: wsjcpp_storages.h:58
std::vector< WsjcppStorageChanges * > m_vStorageChanges
Definition: wsjcpp_storages.h:362
WsjcppStorageChangesType
Definition: wsjcpp_storages.h:81
Definition: wsjcpp_storages.h:18
WsjcppStorageColumnDef & string(int nSize)
Definition: wsjcpp_storages.cpp:42
Definition: wsjcpp_storages.h:16
Definition: wsjcpp_storages.h:314
Definition: wsjcpp_storages.h:128
bool m_bDefaultValue
Definition: wsjcpp_storages.h:65
WsjcppStorageColumnDef & notNull()
Definition: wsjcpp_storages.cpp:35
std::string m_sColumnName
Definition: wsjcpp_storages.h:180
std::map< std::string, WsjcppStorageTable > m_mapTables
Definition: wsjcpp_storages.h:300
double m_nDoubleValue
Definition: wsjcpp_storages.h:185
std::string m_sTableName
Definition: wsjcpp_storages.h:200
std::string TAG
Definition: wsjcpp_storages.h:56
Definition: wsjcpp_storages.h:250
std::string columnType()
Definition: wsjcpp_storages.cpp:146
bool isDefaultValue()
Definition: wsjcpp_storages.cpp:158
std::vector< WsjcppStorageColumnDef > m_vColumns
Definition: wsjcpp_storages.h:201
WsjcppStorageColumnType
Definition: wsjcpp_storages.h:15
WsjcppStorageColumnDef & datetime()
Definition: wsjcpp_storages.cpp:65
WsjcppStorageColumnType m_nColumnType
Definition: wsjcpp_storages.h:181
Definition: wsjcpp_storages.h:88
Definition: wsjcpp_storages.h:82
std::string m_sNameOfUniqueIndex
Definition: wsjcpp_storages.h:68
Definition: wsjcpp_storages.h:17
std::string m_sTableName
Definition: wsjcpp_storages.h:105
std::string m_sColumnName
Definition: wsjcpp_storages.h:57
bool m_bEnableIndex
Definition: wsjcpp_storages.h:66
Definition: wsjcpp_storages.h:26
WsjcppStorageColumnDef & primaryKey()
Definition: wsjcpp_storages.cpp:110
Definition: wsjcpp_storages.h:305
std::string m_sDefaultValue
Definition: wsjcpp_storages.h:59
std::string TAG
Definition: wsjcpp_storages.h:222
WsjcppStorageColumnDef & bigNumber()
Definition: wsjcpp_storages.cpp:87
Definition: wsjcpp_storages.h:167
WsjcppStorageColumnDef & autoIncrement()
Definition: wsjcpp_storages.cpp:28
Definition: wsjcpp_storages.h:87
Definition: wsjcpp_storages.h:373
FabricWsjcppStorage()
Definition: wsjcpp_storages.h:328
std::vector< WsjcppStorageColumnDef > m_vAddColumns
Definition: wsjcpp_storages.h:146
WsjcppStorageColumnDef & doubleNumber()
Definition: wsjcpp_storages.cpp:98
std::string TAG
Definition: wsjcpp_storages.h:122
bool m_bEnableUniqueIndex
Definition: wsjcpp_storages.h:67
std::string TAG
Definition: wsjcpp_storages.h:263
WsjcppStorageColumnDef & text()
Definition: wsjcpp_storages.cpp:54
bool m_bNotNull
Definition: wsjcpp_storages.h:63
std::string nameOfUniqueIndex()
Definition: wsjcpp_storages.cpp:200
static void add(const std::string &sType, IFabricWsjcppStorage *pStorage)
Definition: wsjcpp_storages.cpp:942
static std::string type()
Definition: wsjcpp_storages.h:275
std::string TAG
Definition: wsjcpp_storages.h:354