summaryrefslogtreecommitdiff
path: root/platform/default/sqlite3.cpp
blob: 5122e010156c15ed22adcb85cc6ef194c755c2ec (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
228
229
230
231
232
233
234
235
236
#include "sqlite3.hpp"
#include <sqlite3.h>

#include <cassert>
#include <cstring>
#include <cstdio>
#include <chrono>
#include <experimental/optional>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
// Check sqlite3 library version.
const static bool sqliteVersionCheck = []() {
    if (sqlite3_libversion_number() / 1000000 != SQLITE_VERSION_NUMBER / 1000000) {
        char message[96];
        snprintf(message, 96,
                 "sqlite3 libversion mismatch: headers report %d, but library reports %d",
                 SQLITE_VERSION_NUMBER, sqlite3_libversion_number());
        throw std::runtime_error(message);
    }

    return true;
}();
#pragma GCC diagnostic pop

namespace mapbox {
namespace sqlite {

template <typename T>
using optional = std::experimental::optional<T>;

Database::Database(const std::string &filename, int flags) {
    const int err = sqlite3_open_v2(filename.c_str(), &db, flags, nullptr);
    if (err != SQLITE_OK) {
        const auto message = sqlite3_errmsg(db);
        db = nullptr;
        throw Exception { err, message };
    }
}

Database::Database(Database &&other)
    : db(std::move(other.db)) {}

Database &Database::operator=(Database &&other) {
    std::swap(db, other.db);
    return *this;
}

Database::~Database() {
    if (db) {
        const int err = sqlite3_close(db);
        if (err != SQLITE_OK) {
            throw Exception { err, sqlite3_errmsg(db) };
        }
    }
}

Database::operator bool() const {
    return db != nullptr;
}

void Database::exec(const std::string &sql) {
    assert(db);
    char *msg = nullptr;
    const int err = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &msg);
    if (msg) {
        const std::string message = msg;
        sqlite3_free(msg);
        throw Exception { err, message };
    } else if (err != SQLITE_OK) {
        throw Exception { err, sqlite3_errmsg(db) };
    }
}

Statement Database::prepare(const char *query) {
    assert(db);
    return Statement(db, query);
}

Statement::Statement(sqlite3 *db, const char *sql) {
    const int err = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
    if (err != SQLITE_OK) {
        stmt = nullptr;
        throw Exception { err, sqlite3_errmsg(db) };
    }
}

Statement::Statement(Statement &&other) {
    *this = std::move(other);
}

Statement &Statement::operator=(Statement &&other) {
    std::swap(stmt, other.stmt);
    return *this;
}

Statement::~Statement() {
    if (stmt) {
        sqlite3_finalize(stmt);
    }
}

Statement::operator bool() const {
    return stmt != nullptr;
}

void Statement::check(int err) {
    if (err != SQLITE_OK) {
        throw Exception { err, sqlite3_errmsg(sqlite3_db_handle(stmt)) };
    }
}

template <> void Statement::bind(int offset, std::nullptr_t) {
    assert(stmt);
    check(sqlite3_bind_null(stmt, offset));
}

template <> void Statement::bind(int offset, int value) {
    assert(stmt);
    check(sqlite3_bind_int(stmt, offset, value));
}

template <> void Statement::bind(int offset, int64_t value) {
    assert(stmt);
    check(sqlite3_bind_int64(stmt, offset, value));
}

template <> void Statement::bind(int offset, double value) {
    assert(stmt);
    check(sqlite3_bind_double(stmt, offset, value));
}

template <> void Statement::bind(int offset, bool value) {
    assert(stmt);
    check(sqlite3_bind_int(stmt, offset, value));
}

template <> void Statement::bind(int offset, const char *value) {
    assert(stmt);
    check(sqlite3_bind_text(stmt, offset, value, -1, SQLITE_STATIC));
}

void Statement::bind(int offset, const std::string& value, bool retain) {
    assert(stmt);
    check(sqlite3_bind_blob(stmt, offset, value.data(), int(value.size()),
                            retain ? SQLITE_TRANSIENT : SQLITE_STATIC));
}

template <> void Statement::bind(int offset, std::chrono::system_clock::time_point value) {
    assert(stmt);
    check(sqlite3_bind_int64(stmt, offset, std::chrono::system_clock::to_time_t(value)));
}

template <> void Statement::bind(int offset, optional<std::string> value) {
    if (!value) {
        bind(offset, nullptr);
    } else {
        bind(offset, *value);
    }
}

template <> void Statement::bind(int offset, optional<std::chrono::system_clock::time_point> value) {
    if (!value) {
        bind(offset, nullptr);
    } else {
        bind(offset, *value);
    }
}

bool Statement::run() {
    assert(stmt);
    const int err = sqlite3_step(stmt);
    if (err == SQLITE_DONE) {
        return false;
    } else if (err == SQLITE_ROW) {
        return true;
    } else if (err != SQLITE_OK) {
        throw Exception { err, sqlite3_errmsg(sqlite3_db_handle(stmt)) };
    } else {
        return false;
    }
}

template <> int Statement::get(int offset) {
    assert(stmt);
    return sqlite3_column_int(stmt, offset);
}

template <> int64_t Statement::get(int offset) {
    assert(stmt);
    return sqlite3_column_int64(stmt, offset);
}

template <> double Statement::get(int offset) {
    assert(stmt);
    return sqlite3_column_double(stmt, offset);
}

template <> std::string Statement::get(int offset) {
    assert(stmt);
    return {
        reinterpret_cast<const char *>(sqlite3_column_blob(stmt, offset)),
        size_t(sqlite3_column_bytes(stmt, offset))
    };
}

template <> std::chrono::system_clock::time_point Statement::get(int offset) {
    assert(stmt);
    return std::chrono::system_clock::from_time_t(sqlite3_column_int64(stmt, offset));
}

template <> optional<std::string> Statement::get(int offset) {
    assert(stmt);
    if (sqlite3_column_type(stmt, offset) == SQLITE_NULL) {
        return optional<std::string>();
    } else {
        return get<std::string>(offset);
    }
}

template <> optional<std::chrono::system_clock::time_point> Statement::get(int offset) {
    assert(stmt);
    if (sqlite3_column_type(stmt, offset) == SQLITE_NULL) {
        return optional<std::chrono::system_clock::time_point>();
    } else {
        return get<std::chrono::system_clock::time_point>(offset);
    }
}

void Statement::reset() {
    assert(stmt);
    sqlite3_reset(stmt);
}

} // namespace sqlite
} // namespace mapbox