summaryrefslogtreecommitdiffstats
path: root/src/library/queryutil.h
blob: 337232adeb4822829a6e1cf4d24fb49b21eeaaec (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
107
108
#ifndef QUERYUTIL_H
#define QUERYUTIL_H

#include <QtDebug>
#include <QtSql>

#define LOG_FAILED_QUERY(query) qDebug() << __FILE__ << __LINE__ << "FAILED QUERY [" \
    << (query).executedQuery() << "]" << (query).lastError()

class ScopedTransaction {
  public:
    explicit ScopedTransaction(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();
        qDebug() << "Committing transaction on"
                 << m_database.connectionName()
                 << "result:" << result;
        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 {
  public:
    FieldEscaper(const QSqlDatabase& database)
            : m_database(database),
              m_stringField("string", QVariant::String) {
    }
    virtual ~FieldEscaper() {
    }

    // 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);
    }

    // Escapes a string for use in a LIKE operation by prefixing instances of
    // LIKE wildcard characters (% and _) with escapeCharacter. This allows the
    // caller to then attach wildcard characters to the string. This does NOT
    // escape the string in the same way that escapeString() does.
    QString escapeStringForLike(const QString& escapeString, const QChar escapeCharacter) const {
        QString escapeCharacterStr(escapeCharacter);
        QString result = escapeString;
        // Replace instances of escapeCharacter with two escapeCharacters.
        result = result.replace(
            escapeCharacter, escapeCharacterStr + escapeCharacterStr);
        // Replace instances of % or _ with $escapeCharacter%.
        if (escapeCharacter != '%') {
            result = result.replace("%", escapeCharacterStr + "%");
        }
        if (escapeCharacter != '_') {
            result = result.replace("_", escapeCharacterStr + "_");
        }
        return result;
    }

  private:
    const QSqlDatabase& m_database;
    mutable QSqlField m_stringField;
};
#endif /* QUERYUTIL_H */