-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenericObjectManager.h
376 lines (314 loc) · 14.6 KB
/
GenericObjectManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* File: GenericObjectManager.h
* Author: volker
*
* Created on March 2, 2014, 8:27 PM
*/
#pragma once
#include <optional> // for optional
#include <string> // for operator+, string, char_traits
#include <vector> // for allocator, vector
#include "DbTab.h" // for DbTab
#include "SqliteDatabase.h" // for SqliteDatabase
#include "TabRow.h" // for TabRow
#include "SqlStatement.h"
namespace SqliteOverlay
{
class WhereClause;
template<class T, typename DbClass>
std::vector<T> rowVector2Objects(const DbClass& db, const std::vector<TabRow>& rows)
{
std::vector<T> result;
for_each(rows.begin(), rows.end(), [&](const TabRow& r)
{
result.push_back(T{db, r});
});
return result;
}
//----------------------------------------------------------------------------
template<class T, typename DbClass, typename ValType>
std::vector<T> getObjectsByColumnValue(const DbClass& db, const DbTab& objectTab, const std::string& colName, const ValType& val)
{
const auto resultVector = objectTab.getRowsByColumnValue(colName, val);
return rowVector2Objects<T>(db, resultVector);
}
//----------------------------------------------------------------------------
template<class T, typename DbClass>
std::vector<T> getObjectsByWhereClause(const DbClass& db, const DbTab& objectTab, const WhereClause& w)
{
const auto resultVector = objectTab.getRowsByWhereClause(w);
return rowVector2Objects<T>(db, resultVector);
}
//----------------------------------------------------------------------------
template<class T, typename DbClass>
std::vector<T> getObjectsByWhereClause(const DbClass& db, const DbTab& objectTab, const std::string& w)
{
const auto resultVector = objectTab.getRowsByWhereClause(w);
return rowVector2Objects<T>(db, resultVector);
}
//----------------------------------------------------------------------------
template<class T, typename DbClass>
std::vector<T> getAllObjects(const DbClass& db, const DbTab& objectTab)
{
const auto resultVector = objectTab.getAllRows();
return rowVector2Objects<T>(db, resultVector);
}
//----------------------------------------------------------------------------
template<class T, typename DbClass, typename ValType>
std::optional<T> getSingleObjectByColumnValue(const DbClass& db, const DbTab& objectTab, const std::string& colName, const ValType& val)
{
auto r = objectTab.getSingleRowByColumnValue2(colName, val);
if (r) return T{db, *r};
return std::optional<T>{};
// throw all other execeptions, e.g. BUSY
}
//----------------------------------------------------------------------------
template<class T, typename DbClass>
std::optional<T> getSingleObjectByWhereClause(const DbClass& db, const DbTab& objectTab, const WhereClause& w)
{
auto r = objectTab.getSingleRowByWhereClause2(w);
if (r) return T{db, *r};
return std::optional<T>{};
// throw all other execeptions, e.g. BUSY
}
//----------------------------------------------------------------------------
template<class T, typename DbClass>
std::optional<T> getSingleObjectByWhereClause(const DbClass& db, const DbTab& objectTab, const std::string& w)
{
auto r = objectTab.getSingleRowByWhereClause2(w);
if (r) return T{db, *r};
return std::optional<T>{};
// throw all other execeptions, e.g. BUSY
}
//----------------------------------------------------------------------------
/** \brief Filters all rows that contain a given value in a given column; in
* all rows that match the given value in the given column, look up an integer
* in another given column; use that integer value as an ID for instanciating
* a database object of type T, most likely on a second table.
*
* Example: a table has the column "ObjType" and "RefId". With this function
* you can easily search for all rows in which "ObjType" equals "42" and then
* use the value in "RefId" to instantiate a new object of type T.
*
* \returns A list of database objects of type T whose IDs were retrieved from
* rows in a custom table that matched a specific column-value-pair.
*/
template<class T, typename DbClass, typename ValType>
std::vector<T> filterAndDereference(
const DbClass& db, ///< the specific, derived database we're operating on
const DbTab& srcTab, ///< table that contains the to-be-filtered column and the column with the reference IDs
const std::string& filterColName, ///< name of the column in `srcTab` that we should search in
const ValType& filterValue, ///< the value to look for in column 'filterColName'
const std::string& referingCol ///< the name of the column in `srcTab` that contains the reference IDs
)
{
return filterAndDereference(db, srcTab.name(), filterColName, filterValue, referingCol);
}
//----------------------------------------------------------------------------
/** \brief Filters all rows that contain a given value in a given column; in
* all rows that match the given value in the given column, look up an integer
* in another given column; use that integer value as an ID for instanciating
* a database object of type T, most likely on a second table.
*
* Example: a table has the column "ObjType" and "RefId". With this function
* you can easily search for all rows in which "ObjType" equals "42" and then
* use the value in "RefId" to instantiate a new object of type T.
*
* \returns A list of database objects of type T whose IDs were retrieved from
* rows in a custom table that matched a specific column-value-pair.
*/
template<class T, typename DbClass, typename ValType>
std::vector<T> filterAndDereference(
const DbClass& db, ///< the specific, derived database we're operating on
const std::string srcTabName, ///< table that contains the to-be-filtered column and the column with the reference IDs
const std::string& filterColName, ///< name of the column in `srcTab` that we should search in
const ValType& filterValue, ///< the value to look for in column 'filterColName'
const std::string& referingCol ///< the name of the column in `srcTab` that contains the reference IDs
)
{
std::string sql{
"SELECT " + referingCol + " FROM " + srcTabName +
" WHERE " + filterColName + " = ?"
};
auto stmt = db.prepStatement(sql);
stmt.bind(1, filterValue);
std::vector<T> result;
for (stmt.step(); stmt.hasData(); ++stmt)
{
const auto id = stmt.get<int>(0);
result.push_back(T{db, id});
}
return result;
}
//----------------------------------------------------------------------------
/** \brief Takes the value of a given column in a given row and uses
* it to instantiate a new database object of type T
*/
template<class T, typename DbClass>
std::optional<T> getSingleReferencedObject(
const DbClass& db, ///< the specific, derived database we're operating on
const TabRow& r, ///< the row containing the reference ID
const std::string& refColumnName ///< the name of the column containing the reference ID
)
{
auto objId = r.get2<int>(refColumnName);
return (objId.has_value()) ? T(db, *objId) : std::optional<T>{};
}
//----------------------------------------------------------------------------
/** \brief Looks up a single row using a WHERE clause and uses value from
* a given column in that row for instantiating a new database object of type T
*/
template<class T, typename DbClass>
std::optional<T> getSingleReferencedObject(
const DbClass& db, ///< the specific, derived database we're operating on
const DbTab& srcTab, ///< the table on which we should run the WHERE clause
const WhereClause& w, ///< the WHERE clause itself
const std::string& refColumnName ///< the name of the column in `srcTab` that contains the reference IDs
)
{
std::optional<TabRow> row = srcTab.getSingleRowByWhereClause2(w);
if (row.has_value())
{
return getSingleReferencedObject(db, row.value(), refColumnName);
}
return std::optional<TabRow>{};
}
//----------------------------------------------------------------------------
/** \brief Looks up a single row using a column/value-pair and uses value from
* a given column in that row for instantiating a new database object of type T
*/
template<class T, typename DbClass, typename ValType>
std::optional<T> getSingleReferencedObject(
const DbClass& db, ///< the specific, derived database we're operating on
const DbTab& srcTab, ///< the table on which we should run the WHERE clause
const std::string& filterCol, ///< the column to be used for filtering
const ValType& filterVal, ///< the value to search for in `filterCol`
const std::string& refColumnName ///< the name of the column in `srcTab` that contains the reference IDs
)
{
std::optional<TabRow> row = srcTab.getSingleRowByColumnValue2(filterCol, filterVal);
if (row.has_value())
{
return getSingleReferencedObject(db, row.value(), refColumnName);
}
return std::optional<TabRow>{};
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
template<class DB_CLASS = SqliteDatabase>
class GenericObjectManager
{
public:
using DatabaseClass = DB_CLASS;
GenericObjectManager (const DatabaseClass& _db, const DbTab& _tab)
:db(_db), tab(_tab)
{
static_assert (std::is_base_of_v<SqliteDatabase, DB_CLASS>, "DB classes must be derived from SqliteDatabase");
}
//----------------------------------------------------------------------------
GenericObjectManager (const DatabaseClass& _db, const std::string& tabName, bool forceNameCheck)
:db(_db), tab{db, tabName, forceNameCheck}
{
static_assert (std::is_base_of_v<SqliteDatabase, DB_CLASS>, "DB classes must be derived from SqliteDatabase");
}
//----------------------------------------------------------------------------
const DatabaseClass& getDatabaseHandle()
{
return db.get();
}
//----------------------------------------------------------------------------
int getObjCount() const
{
return tab.length();
}
//----------------------------------------------------------------------------
protected:
const DB_CLASS& db;
DbTab tab;
template<class T, typename ValType>
std::vector<T> getObjectsByColumnValue(const std::string& colName, const ValType& val) const
{
return SqliteOverlay::getObjectsByColumnValue<T, DB_CLASS, ValType>(db, tab, colName, val);
}
template<class T>
std::vector<T> getObjectsByWhereClause(const WhereClause& w) const
{
return SqliteOverlay::getObjectsByWhereClause<T, DB_CLASS>(db, tab, w);
}
template<class T>
std::vector<T> getObjectsByWhereClause(const std::string& w) const
{
return SqliteOverlay::getObjectsByWhereClause<T, DB_CLASS>(db, tab, w);
}
template<class T>
std::vector<T> getAllObjects() const
{
return SqliteOverlay::getAllObjects<T, DB_CLASS>(db, tab);
}
template<class T, typename ValType>
std::optional<T> getSingleObjectByColumnValue(const std::string& colName, const ValType& val) const
{
return SqliteOverlay::getSingleObjectByColumnValue<T, DB_CLASS>(db, tab, colName, val);
}
template<class T>
std::optional<T> getSingleObjectByWhereClause(const WhereClause& w) const
{
return SqliteOverlay::getSingleObjectByWhereClause<T, DB_CLASS>(db, tab, w);
}
template<class T>
std::optional<T> getSingleObjectByWhereClause(const std::string& w) const
{
return SqliteOverlay::getSingleObjectByWhereClause<T, DB_CLASS>(db, tab, w);
}
/** \brief Filters all rows that contain a given value in a given column; in
* all rows that match the given value in the given column, look up an integer
* in another given column; use that integer value as an ID for instanciating
* a database object of type T, most likely on a second table.
*
* Example: a table has the column "ObjType" and "RefId". With this function
* you can easily search for all rows in which "ObjType" equals "42" and then
* use the value in "RefId" to instantiate a new object of type T.
*
* \returns A list of database objects of type T whose IDs were retrieved from
* rows in a custom table that matched a specific column-value-pair.
*/
template<class T, typename ValType>
std::vector<T> filterAndDereference(
const std::string& filterColName, ///< name of the column in "our" tab that we should search in
const ValType& filterValue, ///< the value to look for in column 'filterColName'
const std::string& referingCol ///< the name of the column in "our" tab that contains the reference IDs
) const
{
return SqliteOverlay::filterAndDereference<T, DB_CLASS, ValType>(db, tab, filterColName, filterValue, referingCol);
}
/** \brief Takes the value of a given column in a given row and uses
* it to instantiate a new database object of type T
*/
template<class T>
std::optional<T> getSingleReferencedObject(const TabRow& r, const std::string& refColumnName) const
{
return SqliteOverlay::getSingleReferencedObject<T, DB_CLASS>(db, r, refColumnName);
}
/** \brief Takes the value of a given column in a row that is the first match in a WHERE clause
* and uses that column to instantiate a new database object of type T
*/
template<class T>
std::optional<T> getSingleReferencedObject(const WhereClause& w, const std::string& refColumnName) const
{
return SqliteOverlay::getSingleReferencedObject<T, DB_CLASS>(db, tab, w, refColumnName);
}
/** \brief Looks up a single row using a column/value-pair and uses value from
* a given column in that row for instantiating a new database object of type T
*/
template<class T, typename ValType>
std::optional<T> getSingleReferencedObject(
const std::string& filterCol, ///< the column to be used for filtering
const ValType& filterVal, ///< the value to search for in `filterCol`
const std::string& refColumnName ///< the name of the column in `srcTab` that contains the reference IDs
) const
{
return SqliteOverlay::getSingleReferencedObject<T, DB_CLASS, ValType>(db, tab, filterCol, filterVal, refColumnName);
}
};
}