-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuorm.cpp
298 lines (278 loc) · 6.6 KB
/
uorm.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
#include <QDebug>
#include <QSqlField>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QStringList>
#include <QSqlDatabase>
#include "uorm.h"
#include "uconnectionpool.h"
uORM::uORM(const QHash<QString, QVariant> fields, const QString &table) :
m_table(table),
m_primary("id"),
m_fields(fields),
m_error(),
m_loaded(false)
{
m_pool = &uConnectionPool::getInstance();
}
bool uORM::load(const QVariant &id)
{
QSqlDatabase con;
if (! m_pool->connection(con))
{
m_error = con.lastError().text();
qDebug() << m_error << Q_FUNC_INFO;
return false;
}
QString primary = primaryField(id);
QString q("SELECT * FROM %1 WHERE %2=? LIMIT 1");
q = q.arg(m_table).arg(primary);
QSqlQuery query(con);
query.prepare(q);
QVariant primaryKey = id.isValid() ? id : m_fields.value(primary);
query.bindValue(0, primaryKey);
if (! query.exec())
{
m_error = query.lastError().text();
qDebug() << m_error << Q_FUNC_INFO;
qDebug() << query.lastQuery();
return false;
}
if (! query.next())
{
m_loaded = false;
return true;
}
m_loaded = true;
QSqlRecord record = query.record();
QStringList fields = m_fields.keys();
for (int i = 0; i < fields.size(); i++)
{
QSqlField field = record.field(fields.at(i));
m_fields.insert(fields.at(i), field.value());
}
return true;
}
bool uORM::findWhere(const QString &field, const QVariant &value)
{
QSqlDatabase con;
if (! m_pool->connection(con))
{
m_error = con.lastError().text();
return false;
}
QString q("SELECT * FROM %1 WHERE %2=? LIMIT 1");
q = q.arg(m_table).arg(field);
QSqlQuery query(con);
query.prepare(q);
query.bindValue(0, value);
if (! query.exec())
{
m_error = query.lastError().text();
return false;
}
if (! query.next())
{
m_loaded = false;
return true;
}
m_loaded = true;
QSqlRecord record = query.record();
QStringList fields = m_fields.keys();
for (int i = 0; i < fields.size(); i++)
{
QSqlField field = record.field(fields.at(i));
m_fields.insert(fields.at(i), field.value());
}
return true;
}
bool uORM::isLoaded() const
{
return m_loaded;
}
bool uORM::setField(const QString &name, const QVariant &value)
{
if (m_fields.contains(name))
{
m_fields.insert(name, value);
return true;
}
qDebug() << QObject::trUtf8("Field %1 does not exists in table: %2").arg(name).arg(m_table);
return false;
}
QVariant uORM::field(const QString &name, const QVariant &value)
{
return m_fields.value(name, value);
}
QHash<QString, QVariant> uORM::fields() const
{
return m_fields;
}
bool uORM::save()
{
m_error.clear();
QVariant pk = m_fields.value(primaryField(), QVariant(QVariant::Invalid));
if (pk.isValid())
{
if (pk.canConvert(QVariant::Int))
{
bool ok = false;
long id = pk.toInt(&ok);
if (ok)
{
if (id == 0)
{
return create();
}
else {
return update();
}
}
}
if (pk.canConvert(QVariant::String))
{
QString id = pk.toString();
if (id.isEmpty())
{
return create();
}
else {
return update();
}
}
}
return create();
}
bool uORM::create()
{
QSqlDatabase con;
if (! m_pool->connection(con))
{
m_error = con.lastError().text();
return false;
}
QStringList fields = m_fields.keys();
// remove the primary key
for (int i = 0; i < fields.size(); i++)
{
if (fields.at(i) == m_primary)
{
fields.removeAt(i);
break;
}
}
// construct query string
QString q("INSERT INTO %1 (");
q = q.arg(m_table);
foreach (QString field, fields)
{
q.append(field);
q.append(",");
}
q = q.mid(0, q.length() -1);
q.append(") VALUES (");
for (int i = 0; i < fields.size(); i++)
{
q.append("?,");
}
q = q.mid(0, q.length() -1);
q.append(")");
QSqlQuery query(con);
query.prepare(q);
for (int i = 0; i < fields.size(); i++)
{
query.bindValue(i, m_fields.value(fields.at(i)));
}
if (! query.exec())
{
m_error = query.lastError().text();
return false;
}
m_fields.insert(primaryField(), query.lastInsertId());
return true;
}
bool uORM::update()
{
QSqlDatabase con;
if (! m_pool->connection(con))
{
m_error = con.lastError().text();
return false;
}
QStringList fields = m_fields.keys();
// remove the primary key
for (int i = 0; i < fields.size(); i++)
{
if (fields.at(i) == m_primary)
{
fields.removeAt(i);
break;
}
}
// construct query string
QString q("UPDATE %1 SET ");
foreach (QString field, fields)
{
q.append(field);
q.append("=?,");
}
q = q.mid(0, q.length() -1);
q.append(" WHERE %2=?");
q = q.arg(m_table).arg(primaryField());
QSqlQuery query(con);
query.prepare(q);
int i;
for (i = 0; i < fields.size(); i++)
{
query.bindValue(i, m_fields.value(fields.at(i)));
}
query.bindValue(i, m_fields.value(primaryField()));
if (! query.exec())
{
m_error = query.lastError().text();
return false;
}
return true;
}
bool uORM::remove()
{
// the primary key value
QVariant id = m_fields.value(primaryField(), QVariant(QVariant::Invalid));
if (! id.isValid())
{
qDebug() << QObject::tr("Cannot delete row in %1 without knowing the primary key.").arg(m_table);
return false;
}
QSqlDatabase con;
if (! m_pool->connection(con))
{
m_error = con.lastError().text();
return false;
}
QString q("DELETE FROM %1 WHERE %2=?");
q = q.arg(m_table).arg(primaryField());
QSqlQuery query(con);
query.prepare(q);
query.bindValue(0, id);
if (! query.exec())
{
m_error = query.lastError().text();
return false;
}
foreach (QString key, m_fields.keys())
{
m_fields.insert(key, QVariant(QVariant::Invalid));
}
m_loaded = false;
return true;
}
QString uORM::primaryField(const QVariant &id)
{
Q_UNUSED(id);
return m_primary;
}
QString uORM::lastError() const
{
return m_error;
}