forked from 7h0ma5/QLog
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathUpdatableSQLRecord.cpp
More file actions
137 lines (102 loc) · 3.13 KB
/
Copy pathUpdatableSQLRecord.cpp
File metadata and controls
137 lines (102 loc) · 3.13 KB
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
#include <QSqlField>
#include "UpdatableSQLRecord.h"
#include "core/debug.h"
MODULE_IDENTIFICATION("qlog.core.updatableqslrecord");
UpdatableSQLRecord::UpdatableSQLRecord(int interval, int maxUpdates, QObject *parent)
: QObject{parent},
interval(qMax(1, interval)),
maxUpdates(qMax(1, maxUpdates)),
currUpdateCnt(0)
{
FCT_IDENTIFICATION;
internalRecord.clear();
timer.setSingleShot(true);
timer.setInterval(interval);
connect(&timer, &QTimer::timeout, this, &UpdatableSQLRecord::emitStoreRecord);
}
UpdatableSQLRecord::~UpdatableSQLRecord()
{
FCT_IDENTIFICATION;
timer.stop();
}
void UpdatableSQLRecord::updateRecord(const QSqlRecord &record)
{
FCT_IDENTIFICATION;
if ( internalRecord.isEmpty() )
{
qCDebug(runtime) << "Internal record is empty, storing new record";
internalRecord = record;
startNextUpdateCycle();
return;
}
if ( matchQSO(QSOMatchingType, record) )
{
qCDebug(runtime) << "Records match, merging values";
timer.stop();
// merge
for ( int i = 0; i < record.count(); ++i )
{
const QString &fieldName = record.fieldName(i);
if ( !internalRecord.contains(fieldName) )
internalRecord.append(record.field(i));
else if ( !record.value(i).toString().isEmpty()
&& internalRecord.value(fieldName).toString().isEmpty() )
internalRecord.setValue(fieldName, record.value(i));
}
startNextUpdateCycle();
return;
}
qCDebug(runtime) << "Records do not match, emitting current record";
emitStoreRecord();
internalRecord = record;
startNextUpdateCycle();
}
void UpdatableSQLRecord::emitStoreRecord()
{
FCT_IDENTIFICATION;
timer.stop();
resetCnt();
if ( internalRecord.isEmpty() )
return;
qCDebug(runtime) << "emitting record";
emit recordReady(internalRecord);
internalRecord.clear();
}
bool UpdatableSQLRecord::matchQSO(const MatchingType matchingType,
const QSqlRecord &record)
{
FCT_IDENTIFICATION;
const QStringList &fields = matchingFields.value(matchingType);
for ( const QString &fieldName : fields )
{
qCDebug(runtime) << "compare field name " << fieldName
<< "In value" << internalRecord.value(fieldName)
<< "New value" << record.value(fieldName);
if ( internalRecord.value(fieldName) != record.value(fieldName))
return false;
}
return true;
}
void UpdatableSQLRecord::resetCnt()
{
FCT_IDENTIFICATION;
timer.stop();
currUpdateCnt = 0;
}
bool UpdatableSQLRecord::emitIfMaxUpdatesReached()
{
FCT_IDENTIFICATION;
if (currUpdateCnt < maxUpdates)
return false;
qCDebug(runtime) << "Maximum number of updates reached - emitting";
emitStoreRecord();
return true;
}
void UpdatableSQLRecord::startNextUpdateCycle()
{
FCT_IDENTIFICATION;
incrementCnt();
if ( emitIfMaxUpdatesReached() ) return;
qCDebug(runtime) << "Starting timer" << interval;
timer.start();
}