NGen
Loading...
Searching...
No Matches
ngen_sqlite.hpp
1#ifndef NGEN_GEOPACKAGE_SQLITE_H
2#define NGEN_GEOPACKAGE_SQLITE_H
3
4#include <memory>
5#include <set>
6#include <vector>
7
8#include <sqlite3.h>
9
10#include <span>
11#include "traits.hpp"
12
13namespace ngen {
14namespace sqlite {
15
16struct sqlite_error : public std::runtime_error
17{
18 using std::runtime_error::runtime_error;
19
20 sqlite_error(const std::string& origin_func, int code, const std::string& extra = "");
21};
22
24{
26 struct deleter
27 {
28 void operator()(sqlite3* db)
29 {
30 sqlite3_close_v2(db);
31 }
32
33 void operator()(sqlite3_stmt* stmt)
34 {
35 sqlite3_finalize(stmt);
36 }
37 };
38
39 using sqlite_t = std::unique_ptr<sqlite3, deleter>;
40 using stmt_t = std::unique_ptr<sqlite3_stmt, deleter>;
41
42 public:
43 struct iterator
44 {
45 iterator() = delete;
46
47 explicit iterator(stmt_t&& stmt);
48
52 bool done() const noexcept;
53
59 iterator& next();
60
66
70 int current_row() const noexcept;
71
74 int num_columns() const noexcept;
75
80 int find(const std::string& name) const noexcept;
81
84 const std::span<const std::string> columns() const noexcept;
85
91 const std::span<const int> types() const noexcept;
92
93
99 template<typename Tp>
100 Tp get(int col) const;
101
107 template<typename Tp>
108 Tp get(const std::string& col) const
109 { return get<Tp>(find(col)); }
110
111 private:
112
113 sqlite3_stmt* ptr_() const noexcept;
114 void handle_get_index_(int col) const;
115
117
118 int step_ = -1;
119 int done_ = false;
120 int ncol_ = 0;
121
122 std::vector<std::string> names_;
123 std::vector<int> types_;
124 }; // struct iterator
125
126 // Prevent default construction and copy
127 // construction/assignment.
128 database() = delete;
129 database(const database&) = delete;
130 database& operator=(const database&) = delete;
131
132 // Allow move operations
133 database(database&&) = default;
134 database& operator=(database&&) = default;
135 ~database() = default;
136
139 explicit database(const std::string& path);
140
141
144 sqlite3* connection() const noexcept;
145
150 bool contains(const std::string& table) const;
151
155 std::set<std::string> columns(const std::string& table) const;
156
161 iterator query(const std::string& statement, const std::span<const std::string> binds = {}) const;
162
167 template<
168 typename... Ts,
169 std::enable_if_t<
170 ngen::traits::all_is_convertible<std::string, Ts...>::value,
171 bool
172 > = true
173 >
174 iterator query(const std::string& statement, const Ts&... params) const
175 {
176 std::array<std::string, sizeof...(params)> binds = { params... };
177 return query(statement, binds);
178 }
179
180 private:
181 sqlite_t conn_ = nullptr;
182};
183
184} // namespace sqlite
185} // namespace ngen
186
187#endif // NGEN_GEOPACKAGE_SQLITE_H
Definition ngen_sqlite.hpp:24
iterator query(const std::string &statement, const std::span< const std::string > binds={}) const
Query the SQLite Database and get the result.
Definition ngen_sqlite.cpp:220
sqlite3 * connection() const noexcept
Return the originating sqlite3 database pointer.
Definition ngen_sqlite.cpp:193
bool contains(const std::string &table) const
Check if SQLite database contains a given table.
Definition ngen_sqlite.cpp:198
std::unique_ptr< sqlite3_stmt, deleter > stmt_t
Definition ngen_sqlite.hpp:40
std::unique_ptr< sqlite3, deleter > sqlite_t
Definition ngen_sqlite.hpp:39
sqlite_t conn_
Definition ngen_sqlite.hpp:181
iterator query(const std::string &statement, const Ts &... params) const
Query the SQLite Database with a bound statement and get the result.
Definition ngen_sqlite.hpp:174
conjunction< std::is_convertible< From, To >::value... > all_is_convertible
Checks that all types {From} are convertible to {T}.
Definition traits.hpp:41
Definition DomainLayer.hpp:13
STL namespace.
Deleter used to provide smart pointer support for sqlite3 structs.
Definition ngen_sqlite.hpp:27
void operator()(sqlite3_stmt *stmt)
Definition ngen_sqlite.hpp:33
void operator()(sqlite3 *db)
Definition ngen_sqlite.hpp:28
Definition ngen_sqlite.hpp:44
const std::span< const int > types() const noexcept
Get a span of column types.
Definition ngen_sqlite.cpp:120
int ncol_
Definition ngen_sqlite.hpp:120
int find(const std::string &name) const noexcept
Return the column index for a named column.
Definition ngen_sqlite.cpp:107
stmt_t stmt_
Definition ngen_sqlite.hpp:116
const std::span< const std::string > columns() const noexcept
Get a span of column names.
Definition ngen_sqlite.cpp:114
bool done() const noexcept
Check if a row iterator is finished.
Definition ngen_sqlite.cpp:51
int num_columns() const noexcept
Get the number of columns within this iterator.
Definition ngen_sqlite.cpp:102
sqlite3_stmt * ptr_() const noexcept
Definition ngen_sqlite.cpp:46
int current_row() const noexcept
Get the current row index for the iterator.
Definition ngen_sqlite.cpp:97
iterator & next()
Step into the next row of a SQLite query.
Definition ngen_sqlite.cpp:56
std::vector< int > types_
Definition ngen_sqlite.hpp:123
Tp get(int col) const
Get a column value from a row iterator by index.
std::vector< std::string > names_
Definition ngen_sqlite.hpp:122
int done_
Definition ngen_sqlite.hpp:119
void handle_get_index_(int col) const
Definition ngen_sqlite.cpp:126
iterator & restart()
Restart an iteration to its initial state.
Definition ngen_sqlite.cpp:89
int step_
Definition ngen_sqlite.hpp:118
Definition ngen_sqlite.hpp:17
sqlite_error(const std::string &origin_func, int code, const std::string &extra="")
error codes: https://www.sqlite.org/rescode.html
Definition ngen_sqlite.cpp:12