-
Notifications
You must be signed in to change notification settings - Fork 3
/
Event.cpp
400 lines (359 loc) · 8.34 KB
/
Event.cpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <QDateTime>
#include "qtimemage.h"
#include "DbRec.h"
#include "Event.h"
const DbField Event::S_DbField_arr[] = {
{
.colName = "id",
.type = DbField::PKEY_TYPE,
.colEnum = Event::ID_FIELD,
.offset = class_offsetof(Event, _id)
}, {
.colName = "project_id",
.type = DbField::INT64_TYPE,
.colEnum = Event::PROJECT_ID_FIELD,
.offset = class_offsetof(Event, _project_id)
}, {
.colName = "when_ts",
.type = DbField::INT64_TYPE,
.colEnum = Event::WHEN_TS_FIELD,
.offset = class_offsetof(Event, _when_ts)
}, {
.colName = "type_enum",
.type = DbField::INT32_TYPE,
.colEnum = Event::TYPE_ENUM_FIELD,
.offset = class_offsetof(Event, _type_enum)
}, {
.colName = NULL,
.type = DbField::UNKOWN_TYPE,
.colEnum = Event::UNKNOWN_FIELD,
.offset = 0
}
};
/****************************************************************************/
/*************************** Event_Table ***********************************/
/****************************************************************************/
Event_Table::
Event_Table(QSqlDatabase &db)
/*****************************************************************
* Initialize our table
*/
: DbTable(db, "event_tbl", Event::S_DbField_arr)
{
}
Event_Table::
~Event_Table()
/*****************************************************************
* Free resources
*/
{
}
Event*
Event_Table::
operator[](int64_t id)
/*****************************************************************
* Access to a common collection of objects.
*/
{
if(_hash.contains(id)) return _hash[id];
Event *ev= new Event;
ev->set_id(id);
if(ev->fetch(*this)) { /* Problem fetching ? */
delete ev;
return NULL;
}
_hash[id]= ev;
return ev;
}
DbRec*
Event_Table::
factory() const
/*****************************************************************
* Create an instance of Event, return result cast as DbRec*
*/
{
DbRec *rtn = new Event;
return rtn;
}
int
Event_Table::
fetchAll(QVector<int64_t> &idVec, const QString & sql_tail)
/*****************************************************************
* Fetch all Event rows matching sql_tail.
*/
{
int rtn = EOF - 1;
unsigned vecSz= 50000;
DbRec* DbRec_vec[vecSz];
Event *ev;
int64_t id;
if ((rtn = DbTable::fetchAll(DbRec_vec, vecSz, sql_tail)) < 0) goto abort;
/* Cast pointers back to Event */
for (int i = 0; i < rtn; ++i) {
ev= static_cast <Event*>(DbRec_vec[i]);
id= ev->id();
idVec.append(id);
if(_hash.contains(id))
delete ev;
else
_hash[id]= ev;
}
abort:
return rtn;
}
void
Event_Table::
emit_cast_sig_insert(DbRec *dbr)
/*****************************************************************
* emit sig_insert with the primary key.
*/
{
/* Get the id from dbr, and get our own copy of the record */
Event *ev= static_cast<Event*>(dbr);
int64_t id= ev->id();
/* Note: the provenance of dbr is unkown, so we have to get our
* own object.
*/
ev= new Event;
ev->set_id(id);
if(ev->fetch(*this)) Q_ASSERT(0);
_hash[id]= ev;
emit sig_insert(id);
}
void
Event_Table::
emit_cast_sig_update(DbRec *dbr)
/*****************************************************************
* emit sig_update with the primary key.
*/
{
Event *ev= static_cast<Event*>(dbr);
int64_t id;
if(ev->id(&id)) Q_ASSERT(0);
emit sig_update(id);
}
void
Event_Table::
emit_cast_sig_remove(DbRec *dbr)
/*****************************************************************
* emit sig_remove with the primary key.
*/
{
Event *ev= static_cast<Event*>(dbr);
int64_t id;
if(ev->id(&id)) Q_ASSERT(0);
Q_ASSERT(_hash.contains(id));
emit sig_remove(id);
_hash.remove(id);
delete ev;
}
/****************************************************************************/
/************************** Event *****************************************/
/****************************************************************************/
Event::
Event()
/*****************************************************************
* Default constructor
*/
: DbRec(this, S_DbField_arr)
{
}
Event::
~Event()
/*****************************************************************
* Destructor
*/
{
}
void
Event::
set_id(int64_t id)
/*****************************************************************
* Set the primary key for this record.
*/
{
/* Clear the null bit for this field */
set_field_null(false, ID_FIELD);
_id = id;
}
void
Event::
set_project_id(int64_t id)
/*****************************************************************
* Set the project_id for this record.
*/
{
/* Clear the null bit for this field */
set_field_null(false, PROJECT_ID_FIELD);
_project_id = id;
}
void
Event::
set_when_ts(int64_t secs)
/*****************************************************************
* Set the number of seconds since the Unix epoch for this record.
*/
{
/* Clear the null bit for this field */
set_field_null(false, WHEN_TS_FIELD);
_when_ts = secs;
}
void
Event::
set_when_ts(const QDateTime &when)
/*****************************************************************
* Set the number of seconds since the Unix epoch for this record.
*/
{
/* Clear the null bit for this field */
set_field_null(false, WHEN_TS_FIELD);
_when_ts= when.toSecsSinceEpoch();
}
void
Event::
set_type_enum(int32_t type)
/*****************************************************************
* Set the type_enum for this record.
*/
{
/* Clear the null bit for this field */
set_field_null(false, TYPE_ENUM_FIELD);
_type_enum = type;
}
bool
Event::
id(int64_t *rtn) const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(ID_FIELD)) return true;
*rtn= _id;
return false;
}
int64_t
Event::
id() const
/*****************************************************************
* Asserted access to field.
*/
{
if(is_field_null(ID_FIELD)) qFatal("Field is null");
return _id;
}
bool
Event::
project_id(int64_t *rtn) const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(PROJECT_ID_FIELD)) return true;
*rtn= _project_id;
return false;
}
int64_t
Event::
project_id() const
/*****************************************************************
* Asserted access to field.
*/
{
if(is_field_null(PROJECT_ID_FIELD)) qFatal("Field is null");
return _project_id;
}
bool
Event::
when_ts(int64_t *rtn) const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(WHEN_TS_FIELD)) return true;
*rtn= _when_ts;
return false;
}
bool
Event::
when_ts(QDateTime *rtn) const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(WHEN_TS_FIELD)) return true;
rtn->setSecsSinceEpoch(_when_ts);
return false;
}
int64_t
Event::
when_ts() const
/*****************************************************************
* Asserted access to field.
*/
{
if(is_field_null(WHEN_TS_FIELD)) qFatal("Field is null");
return _when_ts;
}
bool
Event::
type_enum(int *rtn) const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(TYPE_ENUM_FIELD)) return true;
*rtn= _type_enum;
return false;
}
int
Event::
type_enum() const
/*****************************************************************
* Asserted access to field.
*/
{
if(is_field_null(TYPE_ENUM_FIELD)) qFatal("Field is null");
return _type_enum;
}
const char*
Event::
type_str() const
/*****************************************************************
* Access to field.
*/
{
if(is_field_null(TYPE_ENUM_FIELD)) qFatal("Field is null");
switch(_type_enum) {
case START: return "START";
case STOP: return "STOP";
case PAUSE: return "PAUSE";
}
Q_ASSERT(0);
}
int
Event::
insert()
/*****************************************************************
* Convenience function for this app
*/
{
return DbRec::insert(G.eventTable);
}
int
Event::
update()
/*****************************************************************
* Convenience function for this app
*/
{
return DbRec::update(G.eventTable);
}
int
Event::
remove()
/*****************************************************************
* Convenience function for this app
*/
{
return DbRec::remove(G.eventTable);
}