Repository navigation
Expand file tree
/
Copy pathConnection.hpp
More file actions
131 lines (100 loc) · 3.9 KB
/
Copy pathConnection.hpp
File metadata and controls
131 lines (100 loc) · 3.9 KB
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
#ifndef SQLGEN_SQLITE_CONNECTION_HPP_
#define SQLGEN_SQLITE_CONNECTION_HPP_
#include <sqlite3.h>
#include <memory>
#include <rfl.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include "../Iterator.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Session.hpp"
#include "../Transaction.hpp"
#include "../dynamic/SelectFrom.hpp"
#include "../dynamic/Union.hpp"
#include "../dynamic/Write.hpp"
#include "../internal/to_container.hpp"
#include "../internal/write_or_insert.hpp"
#include "../is_connection.hpp"
#include "../sqlgen_api.hpp"
#include "../transpilation/value_t.hpp"
#include "Iterator.hpp"
#include "to_sql.hpp"
namespace sqlgen::sqlite {
class SQLGEN_API Connection {
using ConnPtr = Ref<sqlite3>;
using StmtPtr = std::shared_ptr<sqlite3_stmt>;
public:
Connection(const std::string& _fname);
static rfl::Result<Ref<Connection>> make(const std::string& _fname) noexcept;
~Connection();
Result<Nothing> begin_transaction() noexcept;
Result<Nothing> commit() noexcept;
Result<Nothing> execute(const std::string& _sql) noexcept;
template <class ItBegin, class ItEnd>
Result<Nothing> insert(const dynamic::Insert& _stmt, ItBegin _begin,
ItEnd _end) noexcept {
return internal::write_or_insert(
[&](const auto& _data) { return insert_impl(_stmt, _data); }, _begin,
_end);
}
template <class ContainerType>
auto read(const rfl::Variant<dynamic::SelectFrom, dynamic::Union>& _query) {
using ValueType = transpilation::value_t<ContainerType>;
return internal::to_container<ContainerType>(
read_impl(_query).transform([](auto&& _it) {
return sqlgen::Iterator<ValueType, sqlite::Iterator>(std::move(_it));
}));
}
Result<Nothing> rollback() noexcept;
std::string to_sql(const dynamic::Statement& _stmt) noexcept;
Result<Nothing> start_write(const dynamic::Write& _stmt);
Result<Nothing> end_write();
template <class ItBegin, class ItEnd>
Result<Nothing> write(ItBegin _begin, ItEnd _end) {
return internal::write_or_insert(
[&](const auto& _data) { return write_impl(_data); }, _begin, _end);
}
private:
/// Generates the underlying connection.
static ConnPtr make_conn(const std::string& _fname);
/// Actually inserts data based on a prepared statement -
/// used by both .insert(...) and .write(...).
Result<Nothing> actual_insert(
const std::vector<std::vector<std::optional<std::string>>>& _data,
sqlite3_stmt* _stmt) const noexcept;
/// Implements the actual insert.
Result<Nothing> insert_impl(
const dynamic::Insert& _stmt,
const std::vector<std::vector<std::optional<std::string>>>&
_data) noexcept;
/// Generates a prepared statment, usually for inserts.
Result<StmtPtr> prepare_statement(const std::string& _sql) const noexcept;
/// Implements the actual read.
Result<Ref<Iterator>> read_impl(
const rfl::Variant<dynamic::SelectFrom, dynamic::Union>& _query);
/// Implements the actual write
Result<Nothing> write_impl(
const std::vector<std::vector<std::optional<std::string>>>& _data);
private:
/// A prepared statement - needed for the read and write operations. Note that
/// we have declared it before conn_, meaning it will be destroyed first.
StmtPtr stmt_;
/// The underlying sqlite3 connection.
ConnPtr conn_;
};
} // namespace sqlgen::sqlite
namespace sqlgen::internal {
template <class ValueType>
struct IteratorType<ValueType, sqlite::Connection> {
using Type = Iterator<ValueType, sqlite::Iterator>;
};
static_assert(is_connection<sqlite::Connection>,
"Must fulfill the is_connection concept.");
static_assert(is_connection<Session<sqlite::Connection>>,
"Must fulfill the is_connection concept.");
static_assert(is_connection<Transaction<sqlite::Connection>>,
"Must fulfill the is_connection concept.");
} // namespace sqlgen::internal
#endif