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 <vector>
6
7#include <sqlite3.h>
8
9#include <boost/core/span.hpp>
10#include "traits.hpp"
11
12namespace ngen {
13namespace sqlite {
14
15struct sqlite_error : public std::runtime_error
16{
17 using std::runtime_error::runtime_error;
18
19 sqlite_error(const std::string& origin_func, int code, const std::string& extra = "");
20};
21
23{
25 struct deleter
26 {
27 void operator()(sqlite3* db)
28 {
29 sqlite3_close_v2(db);
30 }
31
32 void operator()(sqlite3_stmt* stmt)
33 {
34 sqlite3_finalize(stmt);
35 }
36 };
37
38 using sqlite_t = std::unique_ptr<sqlite3, deleter>;
39 using stmt_t = std::unique_ptr<sqlite3_stmt, deleter>;
40
41 public:
42 struct iterator
43 {
44 iterator() = delete;
45
46 explicit iterator(stmt_t&& stmt);
47
51 bool done() const noexcept;
52
58 iterator& next();
59
65
69 int current_row() const noexcept;
70
73 int num_columns() const noexcept;
74
79 int find(const std::string& name) const noexcept;
80
83 const boost::span<const std::string> columns() const noexcept;
84
90 const boost::span<const int> types() const noexcept;
91
92
98 template<typename Tp>
99 Tp get(int col) const;
100
106 template<typename Tp>
107 Tp get(const std::string& col) const
108 { return get<Tp>(find(col)); }
109
110 private:
111
112 sqlite3_stmt* ptr_() const noexcept;
113 void handle_get_index_(int col) const;
114
116
117 int step_ = -1;
118 int done_ = false;
119 int ncol_ = 0;
120
121 std::vector<std::string> names_;
122 std::vector<int> types_;
123 }; // struct iterator
124
125 // Prevent default construction and copy
126 // construction/assignment.
127 database() = delete;
128 database(const database&) = delete;
129 database& operator=(const database&) = delete;
130
131 // Allow move operations
132 database(database&&) = default;
133 database& operator=(database&&) = default;
134 ~database() = default;
135
138 explicit database(const std::string& path);
139
140
143 sqlite3* connection() const noexcept;
144
149 bool contains(const std::string& table);
150
155 iterator query(const std::string& statement, const boost::span<const std::string> binds = {});
156
161 template<
162 typename... Ts,
163 std::enable_if_t<
164 ngen::traits::all_is_convertible<std::string, Ts...>::value,
165 bool
166 > = true
167 >
168 iterator query(const std::string& statement, const Ts&... params)
169 {
170 std::array<std::string, sizeof...(params)> binds = { params... };
171 return query(statement, binds);
172 }
173
174 private:
175 sqlite_t conn_ = nullptr;
176};
177
178} // namespace sqlite
179} // namespace ngen
180
181#endif // NGEN_GEOPACKAGE_SQLITE_H
Definition ngen_sqlite.hpp:23
iterator query(const std::string &statement, const boost::span< const std::string > binds={})
Query the SQLite Database and get the result.
Definition ngen_sqlite.cpp:205
bool contains(const std::string &table)
Check if SQLite database contains a given table.
Definition ngen_sqlite.cpp:198
sqlite3 * connection() const noexcept
Return the originating sqlite3 database pointer.
Definition ngen_sqlite.cpp:193
std::unique_ptr< sqlite3_stmt, deleter > stmt_t
Definition ngen_sqlite.hpp:39
std::unique_ptr< sqlite3, deleter > sqlite_t
Definition ngen_sqlite.hpp:38
iterator query(const std::string &statement, const Ts &... params)
Query the SQLite Database with a bound statement and get the result.
Definition ngen_sqlite.hpp:168
sqlite_t conn_
Definition ngen_sqlite.hpp:175
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:9
Deleter used to provide smart pointer support for sqlite3 structs.
Definition ngen_sqlite.hpp:26
void operator()(sqlite3_stmt *stmt)
Definition ngen_sqlite.hpp:32
void operator()(sqlite3 *db)
Definition ngen_sqlite.hpp:27
Definition ngen_sqlite.hpp:43
const boost::span< const int > types() const noexcept
Get a span of column types.
Definition ngen_sqlite.cpp:120
int ncol_
Definition ngen_sqlite.hpp:119
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:115
const boost::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:122
Tp get(int col) const
Get a column value from a row iterator by index.
std::vector< std::string > names_
Definition ngen_sqlite.hpp:121
int done_
Definition ngen_sqlite.hpp:118
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:117
Definition ngen_sqlite.hpp:16
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