-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenericdao.cpp
131 lines (110 loc) · 3.47 KB
/
genericdao.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
#include "genericdao.h"
#include "debugutils.h"
#include <QMetaObject>
#include <QMetaProperty>
#include <QStringList>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>
namespace DBUtils {
GenericDao::GenericDao(QObject *parent) :
QObject(parent)
{
}
bool GenericDao::insertOrUpdate(QObject *dto, QString tableName, QString fkName, QVariant fkValue)
{
const QMetaObject *metaObject = dto->metaObject();
int count = metaObject->propertyCount();
QStringList names;
QList<QVariant> values;
for ( int i=0; i<count; i++ ) {
QMetaProperty property = metaObject->property(i);
names << property.name();
values << dto->property(property.name());
}
foreach (QByteArray dynamicProperty, dto->dynamicPropertyNames()) {
names << QString(dynamicProperty);
values << dto->property(dynamicProperty.data());
}
names.removeOne("objectName");
values.removeOne(dto->property("objectName"));
if (!fkName.isEmpty()) {
names << fkName;
values << fkValue;
}
int valuesCount = values.count();
QStringList placeHolders;
for ( int i=0; i<valuesCount; i++) placeHolders << "?";
QSqlQuery query;
query.prepare("REPLACE INTO " + tableName + " (" + names.join(",") + ") " +
"VALUES (" + placeHolders.join(",") + ") ");
foreach (QVariant value, values)
query.addBindValue(value);
bool ok = query.exec();
if (ok && !dto->property("id").isValid()) {
dto->setProperty("id", query.lastInsertId());
}
return ok;
}
bool GenericDao::remove(QObject *dto, QString tableName)
{
QSqlQuery query;
query.prepare("DELETE FROM " + tableName + " WHERE id = ?");
query.addBindValue(dto->property("id"));
bool ok = query.exec();
return ok;
}
QObject * GenericDao::findById(int id, const QMetaObject *metaObject, QString tableName)
{
QObject * obj = metaObject->newInstance();
if ( !obj )
return obj;
QSqlQuery query(QSqlDatabase::database());
query.prepare("SELECT * FROM " + tableName + " WHERE id = ? ");
query.addBindValue(QVariant(id));
bool ok = query.exec();
if ( ok ) {
while ( query.next() ) {
QSqlRecord rec = query.record();
for ( int i=0; i<rec.count(); i++ ) {
QString field = rec.fieldName(i);
obj->setProperty(qPrintable(field), rec.value(field));
}
}
return obj;
}
else {
return 0;
}
}
QVector<QObject*> GenericDao::select(const QMetaObject *metaObject,QString where, QString tableName)
{
QVector<QObject*> results;
QSqlQuery query(QSqlDatabase::database());
bool ok = query.exec("SELECT * FROM " + tableName + " WHERE " + (where.isEmpty()?"1=1":where));
if ( ok ) {
while ( query.next() ) {
QSqlRecord rec = query.record();
QObject *obj = metaObject->newInstance();
if ( !obj )
continue;
for ( int i=0; i<rec.count(); i++ ) {
QString field = rec.fieldName(i);
obj->setProperty(qPrintable(field), rec.value(field));
}
results << obj;
}
}
// qDebug(qPrintable(query.lastQuery()));
return results;
}
bool GenericDao::lastOperationSuccess()
{
return QSqlDatabase::database().lastError().type() == QSqlError::NoError;
}
QString GenericDao::lastError()
{
return QSqlDatabase::database().lastError().text();
}
}