summaryrefslogtreecommitdiff
path: root/src/libs/sqlite/sqlitestatement.h
blob: 4cdf5ac262d335ad359fbf97d04243575dbd9de2 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "sqliteglobal.h"

#include "sqliteexception.h"
#include "utf8stringvector.h"

#include <QString>
#include <QVariant>
#include <QVector>

#include <type_traits>
#include <memory>

struct sqlite3_stmt;
struct sqlite3;

class SQLITE_EXPORT SqliteStatement
{
protected:
    explicit SqliteStatement(const Utf8String &sqlStatementUtf8);

    static void deleteCompiledStatement(sqlite3_stmt *compiledStatement);

    bool next() const;
    void step() const;
    void reset() const;

    template<typename Type>
    Type value(int column) const;
    int columnCount() const;
    Utf8StringVector columnNames() const;

    void bind(int index, int value);
    void bind(int index, qint64 value);
    void bind(int index, double value);
    void bind(int index, const QString &text);
    void bind(int index, const QByteArray &blob);
    void bind(int index, const QVariant &value);

    template <typename Type>
    void bind(const Utf8String &name, const Type &value);

    int bindingIndexForName(const Utf8String &name);

    void bind(const RowDictionary &rowDictionary);
    void bindUnchecked(const RowDictionary &rowDictionary);

    void setBindingColumnNames(const Utf8StringVector &bindingColumnNames);
    const Utf8StringVector &bindingColumnNames() const;

    template <typename ContainerType>
    ContainerType values(const QVector<int> &columns, int size = 0) const;

    template <typename ContainerType>
    ContainerType values(int column = 0) const;

    QMap<QString, QVariant> rowColumnValueMap() const;
    QMap<QString, QVariant> twoColumnValueMap() const;

    static void execute(const Utf8String &sqlStatementUtf8);

    template <typename Type>
    static Type toValue(const Utf8String &sqlStatementUtf8);

    void prepare(const Utf8String &sqlStatementUtf8);
    void waitForUnlockNotify() const;

    void write(const RowDictionary &rowDictionary);
    void writeUnchecked(const RowDictionary &rowDictionary);

    static sqlite3 *sqliteDatabaseHandle();
    static TextEncoding databaseTextEncoding();


    bool checkForStepError(int resultCode) const;
    void checkForPrepareError(int resultCode) const;
    void setIfIsReadyToFetchValues(int resultCode) const;
    void checkIfIsReadyToFetchValues() const;
    void checkColumnsAreValid(const QVector<int> &columns) const;
    void checkColumnIsValid(int column) const;
    void checkBindingIndex(int index) const;
    void checkBindingName(int index) const;
    void checkParameterCanBeBound(const RowDictionary &rowDictionary, const Utf8String &columnName);
    void setBindingParameterCount();
    void setBindingColumnNamesFromStatement();
    void setColumnCount();
    void checkBindingValueMapIsEmpty(const RowDictionary &rowDictionary) const;
    bool isReadOnlyStatement() const;
    Q_NORETURN static void throwException(const char *whatHasHappened);

    template <typename ContainerType>
    ContainerType columnValues(const QVector<int> &columnIndices) const;

    QString columnName(int column) const;

private:
    std::unique_ptr<sqlite3_stmt, void (*)(sqlite3_stmt*)> compiledStatement;
    Utf8StringVector  bindingColumnNames_;
    int bindingParameterCount;
    int columnCount_;
    mutable bool isReadyToFetchValues;
};