File: request.cpp

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 (275 lines) | stat: -rw-r--r-- 9,609 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
#include <components/sqlite3/db.hpp>
#include <components/sqlite3/request.hpp>
#include <components/sqlite3/statement.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <limits>
#include <tuple>
#include <vector>

namespace
{
    using namespace testing;
    using namespace Sqlite3;

    template <class T>
    struct InsertInt
    {
        static std::string_view text() noexcept { return "INSERT INTO ints (value) VALUES (:value)"; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, T value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct InsertReal
    {
        static std::string_view text() noexcept { return "INSERT INTO reals (value) VALUES (:value)"; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, double value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct InsertText
    {
        static std::string_view text() noexcept { return "INSERT INTO texts (value) VALUES (:value)"; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, std::string_view value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct InsertBlob
    {
        static std::string_view text() noexcept { return "INSERT INTO blobs (value) VALUES (:value)"; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, const std::vector<std::byte>& value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct GetAll
    {
        std::string mQuery;

        explicit GetAll(const std::string& table)
            : mQuery("SELECT value FROM " + table + " ORDER BY value")
        {
        }

        std::string_view text() noexcept { return mQuery; }
        static void bind(sqlite3&, sqlite3_stmt&) {}
    };

    template <class T>
    struct GetExact
    {
        std::string mQuery;

        explicit GetExact(const std::string& table)
            : mQuery("SELECT value FROM " + table + " WHERE value = :value")
        {
        }

        std::string_view text() noexcept { return mQuery; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, const T& value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct GetInt64
    {
        static std::string_view text() noexcept { return "SELECT value FROM ints WHERE value = :value"; }

        static void bind(sqlite3& db, sqlite3_stmt& statement, std::int64_t value)
        {
            bindParameter(db, statement, ":value", value);
        }
    };

    struct GetNull
    {
        static std::string_view text() noexcept { return "SELECT NULL"; }
        static void bind(sqlite3&, sqlite3_stmt&) {}
    };

    struct Int
    {
        int mValue = 0;

        Int() = default;

        explicit Int(int value)
            : mValue(value)
        {
        }

        Int& operator=(int value)
        {
            mValue = value;
            return *this;
        }

        friend bool operator==(const Int& l, const Int& r) { return l.mValue == r.mValue; }
    };

    constexpr const char schema[] = R"(
        CREATE TABLE ints ( value INTEGER );
        CREATE TABLE reals ( value REAL );
        CREATE TABLE texts ( value TEXT );
        CREATE TABLE blobs ( value BLOB );
    )";

    struct Sqlite3RequestTest : Test
    {
        const Db mDb = makeDb(":memory:", schema);
    };

    TEST_F(Sqlite3RequestTest, executeShouldSupportInt)
    {
        Statement insert(*mDb, InsertInt<int>{});
        EXPECT_EQ(execute(*mDb, insert, 13), 1);
        EXPECT_EQ(execute(*mDb, insert, 42), 1);
        Statement select(*mDb, GetAll("ints"));
        std::vector<std::tuple<int>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(13), std::tuple(42)));
    }

    TEST_F(Sqlite3RequestTest, executeShouldSupportInt64)
    {
        Statement insert(*mDb, InsertInt<std::int64_t>{});
        const std::int64_t value = 1099511627776;
        EXPECT_EQ(execute(*mDb, insert, value), 1);
        Statement select(*mDb, GetAll("ints"));
        std::vector<std::tuple<std::int64_t>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(value)));
    }

    TEST_F(Sqlite3RequestTest, executeShouldSupportReal)
    {
        Statement insert(*mDb, InsertReal{});
        EXPECT_EQ(execute(*mDb, insert, 3.14), 1);
        Statement select(*mDb, GetAll("reals"));
        std::vector<std::tuple<double>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(3.14)));
    }

    TEST_F(Sqlite3RequestTest, executeShouldSupportText)
    {
        Statement insert(*mDb, InsertText{});
        const std::string text = "foo";
        EXPECT_EQ(execute(*mDb, insert, text), 1);
        Statement select(*mDb, GetAll("texts"));
        std::vector<std::tuple<std::string>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(text)));
    }

    TEST_F(Sqlite3RequestTest, executeShouldSupportBlob)
    {
        Statement insert(*mDb, InsertBlob{});
        const std::vector<std::byte> blob({ std::byte(42), std::byte(13) });
        EXPECT_EQ(execute(*mDb, insert, blob), 1);
        Statement select(*mDb, GetAll("blobs"));
        std::vector<std::tuple<std::vector<std::byte>>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(blob)));
    }

    TEST_F(Sqlite3RequestTest, requestShouldSupportInt)
    {
        Statement insert(*mDb, InsertInt<int>{});
        const int value = 42;
        EXPECT_EQ(execute(*mDb, insert, value), 1);
        Statement select(*mDb, GetExact<int>("ints"));
        std::vector<std::tuple<int>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), value);
        EXPECT_THAT(result, ElementsAre(std::tuple(value)));
    }

    TEST_F(Sqlite3RequestTest, requestShouldSupportInt64)
    {
        Statement insert(*mDb, InsertInt<std::int64_t>{});
        const std::int64_t value = 1099511627776;
        EXPECT_EQ(execute(*mDb, insert, value), 1);
        Statement select(*mDb, GetExact<std::int64_t>("ints"));
        std::vector<std::tuple<std::int64_t>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), value);
        EXPECT_THAT(result, ElementsAre(std::tuple(value)));
    }

    TEST_F(Sqlite3RequestTest, requestShouldSupportReal)
    {
        Statement insert(*mDb, InsertReal{});
        const double value = 3.14;
        EXPECT_EQ(execute(*mDb, insert, value), 1);
        Statement select(*mDb, GetExact<double>("reals"));
        std::vector<std::tuple<double>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), value);
        EXPECT_THAT(result, ElementsAre(std::tuple(value)));
    }

    TEST_F(Sqlite3RequestTest, requestShouldSupportText)
    {
        Statement insert(*mDb, InsertText{});
        const std::string text = "foo";
        EXPECT_EQ(execute(*mDb, insert, text), 1);
        Statement select(*mDb, GetExact<std::string>("texts"));
        std::vector<std::tuple<std::string>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), text);
        EXPECT_THAT(result, ElementsAre(std::tuple(text)));
    }

    TEST_F(Sqlite3RequestTest, requestShouldSupportBlob)
    {
        Statement insert(*mDb, InsertBlob{});
        const std::vector<std::byte> blob({ std::byte(42), std::byte(13) });
        EXPECT_EQ(execute(*mDb, insert, blob), 1);
        Statement select(*mDb, GetExact<std::vector<std::byte>>("blobs"));
        std::vector<std::tuple<std::vector<std::byte>>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), blob);
        EXPECT_THAT(result, ElementsAre(std::tuple(blob)));
    }

    TEST_F(Sqlite3RequestTest, requestResultShouldSupportNull)
    {
        Statement select(*mDb, GetNull{});
        std::vector<std::tuple<void*>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
        EXPECT_THAT(result, ElementsAre(std::tuple(nullptr)));
    }

    TEST_F(Sqlite3RequestTest, requestResultShouldSupportConstructibleFromInt)
    {
        Statement insert(*mDb, InsertInt<int>{});
        const int value = 42;
        EXPECT_EQ(execute(*mDb, insert, value), 1);
        Statement select(*mDb, GetExact<int>("ints"));
        std::vector<std::tuple<Int>> result;
        request(*mDb, select, std::back_inserter(result), std::numeric_limits<std::size_t>::max(), value);
        EXPECT_THAT(result, ElementsAre(std::tuple(Int(value))));
    }

    TEST_F(Sqlite3RequestTest, requestShouldLimitOutput)
    {
        Statement insert(*mDb, InsertInt<int>{});
        EXPECT_EQ(execute(*mDb, insert, 13), 1);
        EXPECT_EQ(execute(*mDb, insert, 42), 1);
        Statement select(*mDb, GetAll("ints"));
        std::vector<std::tuple<int>> result;
        request(*mDb, select, std::back_inserter(result), 1);
        EXPECT_THAT(result, ElementsAre(std::tuple(13)));
    }
}