File: request.hpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (291 lines) | stat: -rw-r--r-- 11,392 bytes parent folder | download
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef OPENMW_COMPONENTS_SQLITE3_REQUEST_H
#define OPENMW_COMPONENTS_SQLITE3_REQUEST_H

#include "statement.hpp"
#include "types.hpp"

#include <sqlite3.h>

#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

namespace Sqlite3
{
    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, int value)
    {
        if (const int ec = sqlite3_bind_int(&stmt, index, value); ec != SQLITE_OK)
            throw std::runtime_error(
                "Failed to bind int to parameter " + std::to_string(index) + ": " + std::string(sqlite3_errmsg(&db)));
    }

    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, std::int64_t value)
    {
        if (const int ec = sqlite3_bind_int64(&stmt, index, value); ec != SQLITE_OK)
            throw std::runtime_error(
                "Failed to bind int64 to parameter " + std::to_string(index) + ": " + std::string(sqlite3_errmsg(&db)));
    }

    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, double value)
    {
        if (const int ec = sqlite3_bind_double(&stmt, index, value); ec != SQLITE_OK)
            throw std::runtime_error("Failed to bind double to parameter " + std::to_string(index) + ": "
                + std::string(sqlite3_errmsg(&db)));
    }

    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, std::string_view value)
    {
        if (sqlite3_bind_text(&stmt, index, value.data(), static_cast<int>(value.size()), SQLITE_STATIC) != SQLITE_OK)
            throw std::runtime_error(
                "Failed to bind text to parameter " + std::to_string(index) + ": " + std::string(sqlite3_errmsg(&db)));
    }

    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, const std::vector<std::byte>& value)
    {
        if (sqlite3_bind_blob(&stmt, index, value.data(), static_cast<int>(value.size()), SQLITE_STATIC) != SQLITE_OK)
            throw std::runtime_error(
                "Failed to bind blob to parameter " + std::to_string(index) + ": " + std::string(sqlite3_errmsg(&db)));
    }

    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, int index, const ConstBlob& value)
    {
        if (sqlite3_bind_blob(&stmt, index, value.mData, value.mSize, SQLITE_STATIC) != SQLITE_OK)
            throw std::runtime_error(
                "Failed to bind blob to parameter " + std::to_string(index) + ": " + std::string(sqlite3_errmsg(&db)));
    }

    template <typename T>
    inline void bindParameter(sqlite3& db, sqlite3_stmt& stmt, const char* name, const T& value)
    {
        const int index = sqlite3_bind_parameter_index(&stmt, name);
        if (index == 0)
            throw std::logic_error("Parameter \"" + std::string(name) + "\" is not found");
        bindParameter(db, stmt, index, value);
    }

    inline std::string sqliteTypeToString(int value)
    {
        switch (value)
        {
            case SQLITE_INTEGER:
                return "SQLITE_INTEGER";
            case SQLITE_FLOAT:
                return "SQLITE_FLOAT";
            case SQLITE_TEXT:
                return "SQLITE_TEXT";
            case SQLITE_BLOB:
                return "SQLITE_BLOB";
            case SQLITE_NULL:
                return "SQLITE_NULL";
        }
        return "unsupported(" + std::to_string(value) + ")";
    }

    template <class T>
    inline auto copyColumn(sqlite3& /*db*/, sqlite3_stmt& /*statement*/, int index, int type, T*& value)
    {
        if (type != SQLITE_NULL)
            throw std::logic_error("Type of column " + std::to_string(index) + " is " + sqliteTypeToString(type)
                + " that does not match expected output type: SQLITE_INTEGER or SQLITE_FLOAT");
        value = nullptr;
    }

    template <class T>
    inline auto copyColumn(sqlite3& /*db*/, sqlite3_stmt& statement, int index, int type, T& value)
    {
        switch (type)
        {
            case SQLITE_INTEGER:
                value = static_cast<T>(sqlite3_column_int64(&statement, index));
                return;
            case SQLITE_FLOAT:
                value = static_cast<T>(sqlite3_column_double(&statement, index));
                return;
            case SQLITE_NULL:
                value = std::decay_t<T>{};
                return;
        }
        throw std::logic_error("Type of column " + std::to_string(index) + " is " + sqliteTypeToString(type)
            + " that does not match expected output type: SQLITE_INTEGER or SQLITE_FLOAT or SQLITE_NULL");
    }

    inline void copyColumn(sqlite3& db, sqlite3_stmt& statement, int index, int type, std::string& value)
    {
        if (type != SQLITE_TEXT)
            throw std::logic_error("Type of column " + std::to_string(index) + " is " + sqliteTypeToString(type)
                + " that does not match expected output type: SQLITE_TEXT");
        const unsigned char* const text = sqlite3_column_text(&statement, index);
        if (text == nullptr)
        {
            if (const int ec = sqlite3_errcode(&db); ec != SQLITE_OK)
                throw std::runtime_error(
                    "Failed to read text from column " + std::to_string(index) + ": " + sqlite3_errmsg(&db));
            value.clear();
            return;
        }
        const int size = sqlite3_column_bytes(&statement, index);
        if (size <= 0)
        {
            if (const int ec = sqlite3_errcode(&db); ec != SQLITE_OK)
                throw std::runtime_error(
                    "Failed to get column bytes " + std::to_string(index) + ": " + sqlite3_errmsg(&db));
            value.clear();
            return;
        }
        value.reserve(static_cast<std::size_t>(size));
        value.assign(reinterpret_cast<const char*>(text), reinterpret_cast<const char*>(text) + size);
    }

    inline void copyColumn(sqlite3& db, sqlite3_stmt& statement, int index, int type, std::vector<std::byte>& value)
    {
        if (type != SQLITE_BLOB)
            throw std::logic_error("Type of column " + std::to_string(index) + " is " + sqliteTypeToString(type)
                + " that does not match expected output type: SQLITE_BLOB");
        const void* const blob = sqlite3_column_blob(&statement, index);
        if (blob == nullptr)
        {
            if (const int ec = sqlite3_errcode(&db); ec != SQLITE_OK)
                throw std::runtime_error(
                    "Failed to read blob from column " + std::to_string(index) + ": " + sqlite3_errmsg(&db));
            value.clear();
            return;
        }
        const int size = sqlite3_column_bytes(&statement, index);
        if (size <= 0)
        {
            if (const int ec = sqlite3_errcode(&db); ec != SQLITE_OK)
                throw std::runtime_error(
                    "Failed to get column bytes " + std::to_string(index) + ": " + sqlite3_errmsg(&db));
            value.clear();
            return;
        }
        value.reserve(static_cast<std::size_t>(size));
        value.assign(static_cast<const std::byte*>(blob), static_cast<const std::byte*>(blob) + size);
    }

    template <int index, class T>
    inline void getColumnsImpl(sqlite3& db, sqlite3_stmt& statement, T& row)
    {
        if constexpr (0 < index && index <= std::tuple_size_v<T>)
        {
            const int column = index - 1;
            if (const int number = sqlite3_column_count(&statement); column >= number)
                throw std::out_of_range(
                    "Column number is out of range: " + std::to_string(column) + " >= " + std::to_string(number));
            const int type = sqlite3_column_type(&statement, column);
            switch (type)
            {
                case SQLITE_INTEGER:
                case SQLITE_FLOAT:
                case SQLITE_TEXT:
                case SQLITE_BLOB:
                case SQLITE_NULL:
                    copyColumn(db, statement, column, type, std::get<index - 1>(row));
                    break;
                default:
                    throw std::runtime_error("Column " + std::to_string(column)
                        + " has unnsupported column type: " + sqliteTypeToString(type));
            }
            getColumnsImpl<index - 1>(db, statement, row);
        }
    }

    template <class T>
    inline void getColumns(sqlite3& db, sqlite3_stmt& statement, T& row)
    {
        getColumnsImpl<std::tuple_size_v<T>>(db, statement, row);
    }

    template <class T>
    inline void getRow(sqlite3& db, sqlite3_stmt& statement, T& row)
    {
        auto tuple = std::tie(row);
        getColumns(db, statement, tuple);
    }

    template <class... Args>
    inline void getRow(sqlite3& db, sqlite3_stmt& statement, std::tuple<Args...>& row)
    {
        getColumns(db, statement, row);
    }

    template <class T>
    inline void getRow(sqlite3& db, sqlite3_stmt& statement, std::back_insert_iterator<T>& it)
    {
        typename T::value_type row;
        getRow(db, statement, row);
        it = std::move(row);
    }

    template <class T, class... Args>
    inline void prepare(sqlite3& db, Statement<T>& statement, Args&&... args)
    {
        if (statement.mNeedReset)
        {
            if (sqlite3_reset(statement.mHandle.get()) == SQLITE_OK
                && sqlite3_clear_bindings(statement.mHandle.get()) == SQLITE_OK)
                statement.mNeedReset = false;
            else
                statement.mHandle = makeStatementHandle(db, statement.mQuery.text());
        }
        statement.mQuery.bind(db, *statement.mHandle, std::forward<Args>(args)...);
    }

    template <class T>
    inline bool executeStep(sqlite3& db, const Statement<T>& statement)
    {
        switch (sqlite3_step(statement.mHandle.get()))
        {
            case SQLITE_ROW:
                return true;
            case SQLITE_DONE:
                return false;
        }
        throw std::runtime_error("Failed to execute statement step: " + std::string(sqlite3_errmsg(&db)));
    }

    template <class T, class I, class... Args>
    inline I request(sqlite3& db, Statement<T>& statement, I out, std::size_t max, Args&&... args)
    {
        try
        {
            statement.mNeedReset = true;
            prepare(db, statement, std::forward<Args>(args)...);
            for (std::size_t i = 0; executeStep(db, statement) && i < max; ++i)
                getRow(db, *statement.mHandle, *out++);
            return out;
        }
        catch (const std::exception& e)
        {
            throw std::runtime_error(
                "Failed perform request \"" + std::string(statement.mQuery.text()) + "\": " + std::string(e.what()));
        }
    }

    template <class T, class... Args>
    inline int execute(sqlite3& db, Statement<T>& statement, Args&&... args)
    {
        try
        {
            statement.mNeedReset = true;
            prepare(db, statement, std::forward<Args>(args)...);
            if (executeStep(db, statement))
                throw std::logic_error("Execute cannot return rows");
            return sqlite3_changes(&db);
        }
        catch (const std::exception& e)
        {
            throw std::runtime_error("Failed to execute statement \"" + std::string(statement.mQuery.text())
                + "\": " + std::string(e.what()));
        }
    }
}

#endif