summaryrefslogtreecommitdiff
path: root/tagstats/sqlite.hpp
blob: 8860fbfba7e481cdabd4c4e6dc657f4b78dcf5b0 (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
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
#ifndef SQLITE_HPP
#define SQLITE_HPP

/*

  Author: Jochen Topf <jochen@topf.org>

  https://github.com/joto/sqlite-cpp-wrapper

  This code is released into the Public Domain.

*/

#include <stdexcept>
#include <stdint.h>
#include <string>

#include <sqlite3.h>

/**
 *  @brief The %Sqlite classes wrap the %Sqlite C library.
 */
namespace Sqlite {

    /**
     *  Exception returned by Sqlite wrapper classes when there are errors in the Sqlite3 lib
     */
    class Exception : public std::runtime_error {

    public:

        Exception(const std::string& msg, const std::string& error) :
            std::runtime_error(msg + ": " + error + '\n') {
        }

    };

    /**
     *  Wrapper class for Sqlite database
     */
    class Database {

    public:

        Database(const char* filename, const int flags) {
            if (SQLITE_OK != sqlite3_open_v2(filename, &m_db, flags, 0)) {
                std::string error = errmsg();
                sqlite3_close(m_db);
                throw Sqlite::Exception("Can't open database", error);
            }
        }

        ~Database() {
            sqlite3_close(m_db);
        }

        std::string errmsg() {
            if (m_db) {
                return std::string(sqlite3_errmsg(m_db));
            } else {
                return std::string("Database is not open");
            }
        }

        sqlite3* get_sqlite3() {
            return m_db;
        }

        void exec(const std::string& sql) {
            if (SQLITE_OK != sqlite3_exec(m_db, sql.c_str(), 0, 0, 0)) {
                std::string error = errmsg();
                sqlite3_close(m_db);
                throw Sqlite::Exception("Database error", error);
            }
        }

        void begin_transaction() {
            exec("BEGIN TRANSACTION;");
        }

        void commit() {
            exec("COMMIT;");
        }

        void rollback() {
            exec("ROLLBACK;");
        }

    private:

        sqlite3* m_db;

    }; // class Database

    /**
     * Wrapper class for Sqlite prepared statement.
     */
    class Statement {

    public:

        Statement(Database& db, const char* sql) :
            m_db(db),
            m_statement(0),
            m_bindnum(1) {
            sqlite3_prepare_v2(db.get_sqlite3(), sql, -1, &m_statement, 0);
            if (m_statement == 0) {
                throw Sqlite::Exception("Can't prepare statement", m_db.errmsg());
            }
        }

        ~Statement() {
            sqlite3_finalize(m_statement);
        }

        Statement& bind_null() {
            if (SQLITE_OK != sqlite3_bind_null(m_statement, m_bindnum++)) {
                throw Sqlite::Exception("Can't bind null value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_text(const char* value) {
            if (SQLITE_OK != sqlite3_bind_text(m_statement, m_bindnum++, value, -1, SQLITE_STATIC)) {
                throw Sqlite::Exception("Can't bind text value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_text(const std::string& value) {
            if (SQLITE_OK != sqlite3_bind_text(m_statement, m_bindnum++, value.c_str(), -1, SQLITE_STATIC)) {
                throw Sqlite::Exception("Can't bind text value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_int(const int value) {
            if (SQLITE_OK != sqlite3_bind_int(m_statement, m_bindnum++, value)) {
                throw Sqlite::Exception("Can't bind int value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_int64(const int64_t value) {
            if (SQLITE_OK != sqlite3_bind_int64(m_statement, m_bindnum++, value)) {
                throw Sqlite::Exception("Can't bind int64 value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_double(const double value) {
            if (SQLITE_OK != sqlite3_bind_double(m_statement, m_bindnum++, value)) {
                throw Sqlite::Exception("Can't bind double value", m_db.errmsg());
            }
            return *this;
        }

        Statement& bind_blob(const void* value, const int length) {
            if (SQLITE_OK != sqlite3_bind_blob(m_statement, m_bindnum++, value, length, 0)) {
                throw Sqlite::Exception("Can't bind blob value", m_db.errmsg());
            }
            return *this;
        }

        void execute() {
            sqlite3_step(m_statement);
            if (SQLITE_OK != sqlite3_reset(m_statement)) {
                throw Sqlite::Exception("Can't execute statement", m_db.errmsg());
            }
            m_bindnum = 1;
        }

        bool read() {
            switch (sqlite3_step(m_statement)) {
                case SQLITE_ROW:
                    return true;
                case SQLITE_DONE:
                    return false;
                default:
                    throw Sqlite::Exception("Sqlite error", m_db.errmsg());
            }
        }

        int column_count() {
            return sqlite3_column_count(m_statement);
        }

        std::string get_text(int column) {
            if (column >= column_count()) {
                throw Sqlite::Exception("Column larger than max columns", "");
            }
            const char* textptr = reinterpret_cast<const char*>(sqlite3_column_text(m_statement, column));
            if (!textptr) {
                throw Sqlite::Exception("Error reading text column", m_db.errmsg());
            }
            return std::string(textptr);
        }

        const char* get_string(int column) {
            if (column >= column_count()) {
                throw Sqlite::Exception("Column larger than max columns", "");
            }
            const char* textptr = reinterpret_cast<const char*>(sqlite3_column_text(m_statement, column));
            if (!textptr) {
                throw Sqlite::Exception("Error reading text column", m_db.errmsg());
            }
            return textptr;
        }

        int get_int(int column) {
            if (column >= column_count()) {
                throw Sqlite::Exception("Column larger than max columns", m_db.errmsg());
            }
            return sqlite3_column_int(m_statement, column);
        }

    private:

        Database& m_db;
        sqlite3_stmt* m_statement;
        int m_bindnum;

    }; // class Statement

} // namespace Sqlite

#endif // SQLITE_HPP