summaryrefslogtreecommitdiffstats
path: root/server/src/database/sqlite3.cppm
blob: 221610906716565a1b535bc1a0c862f48b6a8940 (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
export module routemon:database.sqlite3;

import std;
import :util;

extern "C"
{
  using sqlite3 = struct sqlite3;
  using sqlite3_mutex = struct sqlite3_mutex;
  using sqlite3_stmt = struct sqlite3_stmt;
}

namespace routemon::database::sqlite3 {

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 mutex_guard
{
  ::sqlite3* dbc_;

public:
  explicit mutex_guard(::sqlite3* dbc) noexcept;
  mutex_guard(mutex_guard const&) = delete;
  mutex_guard(mutex_guard&& rhs) = delete;
  auto operator=(mutex_guard const&) -> mutex_guard& = delete;
  auto operator=(mutex_guard&& rhs) -> mutex_guard& = delete;
  ~mutex_guard();

  auto for_db(::sqlite3 const* dbc) const -> bool { return dbc_ == dbc; }
};

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

public:
  explicit error(int code, ::sqlite3* dbc);
  explicit error(int code);

  [[nodiscard]] auto what() const noexcept -> char const* override;
  [[nodiscard]] auto code() const noexcept -> int;
};

class statement;
class row_reader;

// TODO: make consumable and mark release_guard only callable when unconsumed?
// Same might have to for release_guard in row_reader.
class locked_statement
{
  ::sqlite3_stmt* stmt_;
  std::shared_ptr<mutex_guard> guard_;

  friend class statement;
  friend class row_reader;

public:
  explicit locked_statement(
      ::sqlite3_stmt* stmt, std::shared_ptr<mutex_guard> dbc_locked);
  locked_statement(locked_statement const&) = delete;
  locked_statement(locked_statement&& s) noexcept;
  ~locked_statement();
};

class statement
{
  locked_statement stmt_;

  auto param_idx(util::zstring_view name) -> int;
  auto bind_null(util::zstring_view param_name) -> statement&;

public:
  explicit statement(locked_statement&& stmt);

  auto bind(util::zstring_view param_name, std::string_view str) -> statement&;
  auto bind(util::zstring_view param_name, std::int64_t x) -> statement&;
  auto bind(util::zstring_view param_name, std::optional<std::string_view> str)
      -> statement&;
  auto bind(util::zstring_view param_name, std::optional<std::int64_t> x)
      -> statement&;
  auto into_reader() && -> row_reader;
};

class row_reader
{
  locked_statement stmt_;

  friend class statement;

  explicit row_reader(locked_statement&& stmt);

  auto is_null(int col) -> bool;
  auto ncols() -> std::size_t;

  auto scan_col(int col, std::string& s) -> void;
  auto scan_col(int col, double& v) -> void;
  auto scan_col(int col, std::int64_t& v) -> void;
  auto scan_col(int col, optional_of<scannable_prim> auto& v) -> void
  {
    if (is_null(col))
    {
      v.reset();
    }
    else
    {
      typename std::remove_cvref_t<decltype(v)>::value_type tmp;
      scan_col(col, tmp);
      v = std::move(tmp);
    }
  }

public:
  auto next() -> bool;

  auto scan(scannable auto&... args) -> void
  {
    if (sizeof...(args) > ncols())
      throw std::invalid_argument{
        "more scanning arguments provided than columns in result set"
      };
    auto col = 0;
    (..., scan_col(col++, args));
  }

  template <scannable... args>
  auto scan() -> std::tuple<args...>
  {
    std::tuple<args...> res;
    auto& [... elems] = res;
    scan(elems...);
    return res;
  }

  auto scan_row_max1(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"};
    }
  }

  template <scannable... args>
  auto scan_row_max1() -> std::tuple<args...>
  {
    std::tuple<args...> res;
    auto& [... elems] = res;
    scan_row_max1(elems...);
    return res;
  }
};

// TODO: consider making this a concept instead.
using bindfn = std::function<auto(statement&)->void>;

auto noop_binder(statement&) -> void;

export class connection
{
  ::sqlite3* dbc_;

  explicit connection(::sqlite3* dbc);

  friend auto open(std::filesystem::path const& filename) -> connection;
  friend class tx;

public:
  connection(connection const&) = delete;
  connection(connection&& c) noexcept;

  [[nodiscard]] auto
  query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader;

  auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void;

  ~connection();
};

// TODO: define behavior when user issues queries that affect the
// transaction itself.
export class tx
{
  bool commit_ = true;
  util::not_null<connection*> dbc_;
  std::shared_ptr<mutex_guard> guard_;

public:
  explicit tx(connection& dbc);

  ~tx()
  {
    if (commit_)
      exec("COMMIT");
    else
      exec("ROLLBACK");
  }

  [[nodiscard]] auto
  query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader;

  auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void;
  auto exec_first(util::zstring_view& sql) -> void;
};

export auto open(std::filesystem::path const& filename) -> connection;

} // namespace routemon::database::sqlite3