blob: f3e7d8a544da767a4923d3d810730bda0d9796f9 (
plain)
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
|
#ifndef QUERYUTIL_H
#define QUERYUTIL_H
#include <QtDebug>
#include <QtSql>
#define LOG_FAILED_QUERY(query) qWarning() << __FILE__ << __LINE__ << "FAILED QUERY [" \
<< (query).executedQuery() << "]" << (query).lastError()
class ScopedTransaction {
public:
explicit ScopedTransaction(const QSqlDatabase& database) :
m_database(database),
m_active(false) {
if (!transaction()) {
qDebug() << "ERROR: Could not start transaction on"
<< m_database.connectionName();
}
}
virtual ~ScopedTransaction() {
if (m_active) {
rollback();
}
}
bool active() const {
return m_active;
}
bool transaction() {
if (m_active) {
qDebug() << "WARNING: Transaction already active and received transaction() request on"
<< m_database.connectionName();
return false;
}
m_active = m_database.transaction();
return m_active;
}
bool commit() {
if (!m_active) {
qDebug() << "WARNING: commit() called on inactive transaction for"
<< m_database.connectionName();
return false;
}
bool result = m_database.commit();
if (result) {
qDebug() << "Committing transaction successfully on"
<< m_database.connectionName();
} else {
qInfo() << "Committing transaction failed on"
<< m_database.connectionName()
<< ":" << m_database.lastError();
}
m_active = false;
return result;
}
bool rollback() {
if (!m_active) {
qDebug() << "WARNING: rollback() called on inactive transaction for"
<< m_database.connectionName();
return false;
}
bool result = m_database.rollback();
qDebug() << "Rolling back transaction on"
<< m_database.connectionName()
<< "result:" << result;
m_active = false;
return result;
}
private:
QSqlDatabase m_database;
bool m_active;
};
class FieldEscaper final {
public:
FieldEscaper(const QSqlDatabase& database)
: m_database(database),
m_stringField("string", QVariant::String) {
}
// Escapes a string for use in a SQL query by wrapping with quotes and
// escaping embedded quote characters.
QString escapeString(const QString& escapeString) const {
m_stringField.setValue(escapeString);
return m_database.driver()->formatValue(m_stringField);
}
QStringList escapeStrings(const QStringList& escapeStrings) const {
QStringList result = escapeStrings;
escapeStringsInPlace(&result);
return result;
}
private:
void escapeStringsInPlace(QStringList* pEscapeStrings) const {
QMutableStringListIterator it(*pEscapeStrings);
while (it.hasNext()) {
it.setValue(escapeString(it.next()));
}
}
QSqlDatabase m_database;
mutable QSqlField m_stringField;
};
#endif /* QUERYUTIL_H */
|