summaryrefslogtreecommitdiffstats
path: root/server/src/sqlite3.cppm
blob: 4623cdc1280fbc6f6034078f085ea4b9079d0fca (about) (plain) (blame)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
module;

#include <sqlite3.h>

export module routemon:sqlite3;

import std;
import :util;

namespace routemon::sqlite3 {

class mutex_guard
{
  explicit mutex_guard(::sqlite3_mutex* mut) noexcept : mut_{mut}
  {
    ::sqlite3_mutex_enter(mut_);
  }

  friend auto
  do_guarded(::sqlite3_mutex* mut, std::invocable<mutex_guard const&> auto f)
      -> decltype(f(std::declval<mutex_guard const&>()));

public:
  mutex_guard(mutex_guard const&) = delete;
  ~mutex_guard() { ::sqlite3_mutex_leave(mut_); }

private:
  ::sqlite3_mutex* mut_;
};

auto do_guarded(::sqlite3_mutex* mut, std::invocable<mutex_guard const&> auto f)
    -> decltype(f(std::declval<mutex_guard const&>()))
{
  return f(mutex_guard{mut});
}

auto do_guarded(::sqlite3* dbc, std::invocable<mutex_guard const&> auto f)
    -> decltype(f(std::declval<mutex_guard const&>()))
{
  return do_guarded(::sqlite3_db_mutex(dbc), f);
}

class error : public std::exception
{
  int code_;
  std::string message_;

public:
  explicit error(mutex_guard const&, int code, ::sqlite3* dbc)
    : code_{code}, message_{::sqlite3_errmsg(dbc)}
  {
  }

  explicit error(int code) : code_{code}, message_{::sqlite3_errstr(code)} {}

  [[nodiscard]] auto what() const noexcept -> char const* override
  {
    return message_.c_str();
  }

  [[nodiscard]] auto code() const noexcept -> int { return code_; }
};

template <class T, template <class U> concept C>
concept optional_of = requires {
  typename T::value_type;
  requires std::same_as<T, std::optional<typename T::value_type>>;
  requires C<typename T::value_type>;
};

template <class T>
concept scannable_prim = std::same_as<T, std::string> || std::same_as<T, double>
                         || std::same_as<T, std::int64_t>;

template <class T>
concept scannable = scannable_prim<T> || optional_of<T, scannable_prim>;

class statement
{
  ::sqlite3_stmt* stmt_;

public:
  explicit statement(::sqlite3_stmt* stmt) : stmt_{stmt} {}
  statement(statement const&) = delete;
  statement(statement&& s) noexcept
  {
    stmt_ = s.stmt_;
    s.stmt_ = nullptr;
  }
  ~statement() { ::sqlite3_finalize(stmt_); }
  auto get() -> ::sqlite3_stmt* { return stmt_; }
};

class row_reader
{
  statement stmt_;

  explicit row_reader(statement stmt) : stmt_{std::move(stmt)} {}

  friend class connection;

  void scan(int col, std::string& s)
  {
    if (::sqlite3_column_type(stmt_.get(), col) != SQLITE_TEXT)
      throw std::invalid_argument{"invalid type for scan"};
    unsigned char const* chs = ::sqlite3_column_text(stmt_.get(), col);
    auto size = util::size_from_int(::sqlite3_column_bytes(stmt_.get(), col));
    if (!size.has_value())
      throw std::logic_error{"unexpected negative amount of bytes in column"};
    s = std::string{reinterpret_cast<char const*>(chs), *size};
  }

  void scan(int col, double& v)
  {
    if (::sqlite3_column_type(stmt_.get(), col) != SQLITE_FLOAT)
      throw std::invalid_argument{"invalid type for scan"};
    v = ::sqlite3_column_double(stmt_.get(), col);
  }

  void scan(int col, std::int64_t& v)
  {
    if (::sqlite3_column_type(stmt_.get(), col) != SQLITE_INTEGER)
      throw std::invalid_argument{"invalid type for scan"};
    v = ::sqlite3_column_int64(stmt_.get(), col);
  }

  void scan(int col, optional_of<scannable> auto& v)
  {
    if (::sqlite3_column_type(stmt_.get(), col) == SQLITE_NULL)
    {
      v.reset();
    }
    else
    {
      typename std::remove_cvref_t<decltype(v)>::value_type tmp;
      scan(col, tmp);
      v = std::move(tmp);
    }
  }

public:
  auto next() -> bool
  {
    ::sqlite3* dbc = ::sqlite3_db_handle(stmt_.get());
    return do_guarded(
        dbc,
        [&](auto const& guard) -> bool
        {
          auto const s = ::sqlite3_step(stmt_.get());
          if (s == SQLITE_ROW)
            return true;
          if (s == SQLITE_DONE)
            return false;
          throw error{guard, s, dbc};
        });
  }

  auto scan(scannable auto&... args) -> void
  {
    auto const ncols = util::size_from_int(::sqlite3_data_count(stmt_.get()));
    if (!ncols.has_value())
      throw std::logic_error{"got unexpected negative amount of columns"};
    if (sizeof...(args) > *ncols)
      throw std::invalid_argument{
        "more scanning arguments provided than columns in result set"
      };
    auto col = 0;
    (..., scan(col++, args));
  }

  auto scan_single(scannable auto&... args) -> void
  {
    if (!next())
      throw std::logic_error{"no row in result set"};
    scan(args...);
    if (next())
    {
      throw std::logic_error{"more than one row in result set"};
    }
  }
};

class binder
{
  statement& stmt_;

  explicit binder(statement& stmt) : stmt_{stmt} {}

  friend class connection;

public:
  auto text(std::string const& param_name, std::string_view str) -> void
  {
    int const i =
        ::sqlite3_bind_parameter_index(stmt_.get(), param_name.c_str());
    if (i == 0)
      throw std::invalid_argument{std::format(
          "bind: no parameter with name {} found", param_name)};
    auto str_size = util::int_from_size(str.size());
    if (!str_size.has_value())
      throw std::invalid_argument{"bind: provided text is too long"};
    if (auto s = ::sqlite3_bind_text(
            stmt_.get(), i, str.data(), *str_size, SQLITE_TRANSIENT);
        s != SQLITE_OK)
    {
      throw error{s};
    }
  }

  static auto noop(binder&) -> void {}
};

export class connection
{
  ::sqlite3* dbc_;
  ::sqlite3_mutex* mut_;

  explicit connection(::sqlite3* dbc) : dbc_{dbc}, mut_{::sqlite3_db_mutex(dbc)}
  {
  }

  friend auto open(std::string const& filename) -> connection;

public:
  connection(connection const&) = delete;
  connection(connection&& c) noexcept
  {
    dbc_ = c.dbc_;
    mut_ = c.mut_;
    c.dbc_ = nullptr;
    c.mut_ = nullptr;
  }

  [[nodiscard]] auto query(
      std::string const& sql,
      std::function<void(binder&)> const& bf = binder::noop) -> row_reader
  {
    ::sqlite3_stmt* pstmt = nullptr;
    char const* sql_tail = nullptr;
    auto sql_size = util::int_from_size(sql.size());
    if (!sql_size.has_value()
        || *sql_size >= std::numeric_limits<int>::max() - 1)
      throw std::invalid_argument{"provided input text too large"};
    do_guarded(
        mut_,
        [&](auto const& guard) -> void
        {
          if (auto s = ::sqlite3_prepare_v2(
                  dbc_, sql.data(), *sql_size + 1, &pstmt, &sql_tail);
              s != SQLITE_OK)
          {
            if (pstmt != nullptr)
            {
              // Use contract_assert when having a compiler with
              // contracts available
              ::sqlite3_finalize(pstmt);
              throw std::logic_error{
                "expected stmt to be null after failed preparation"
              };
            }
            throw error{guard, s, dbc_};
          }
        });
    if (!pstmt)
      throw std::invalid_argument{"provided input text contains no SQL"};
    auto stmt = statement{pstmt};
    if (sql_tail && std::strlen(sql_tail) > 0)
      throw std::invalid_argument{
        "provided input text contains more than one SQL statement"
      };
    auto b = binder{stmt};
    bf(b);
    return row_reader{std::move(stmt)};
  }

  auto exec(
      std::string const& sql,
      std::function<void(binder&)> const& bf = binder::noop) -> void
  {
    auto reader = query(sql, bf);
    while (reader.next())
      ;
  }

  ~connection() { std::ignore = ::sqlite3_close(std::exchange(dbc_, nullptr)); }
};

export auto open(std::string const& filename) -> connection
{
  ::sqlite3* dbc = nullptr;
  auto s = ::sqlite3_open_v2(
      filename.c_str(), &dbc,
      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX
          | SQLITE_OPEN_EXRESCODE,
      nullptr);
  if (s != SQLITE_OK)
  {
    if (dbc)
    {
      do_guarded(
          dbc, [&](auto const& guard) -> void { throw error{guard, s, dbc}; });
    }
    else
    {
      throw error{s};
    }
  }
  return connection{dbc};
}

} // namespace routemon::sqlite3