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
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUD_SQLITE_HPP
#define FUD_SQLITE_HPP
#include <fud_result.hpp>
#include <fud_status.hpp>
#include <fud_string.hpp>
#include <sqlite3.h>
namespace fud {
class SqliteStatement;
// NOLINTNEXTLINE(performance-enum-size)
enum class SqliteOpenMode : int
{
ReadOnly = SQLITE_OPEN_READONLY,
ReadWrite = SQLITE_OPEN_READWRITE,
ReadWriteCreate = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
};
class SqliteErrorMsg;
class SqliteDb;
using SqliteDbResult = Result<SqliteDb, FudStatus>;
class SqliteDb {
private:
constexpr SqliteDb() = default;
public:
static SqliteDbResult make(const String& name, SqliteOpenMode mode, int extraFlags = 0);
static SqliteDbResult make(StringView name, SqliteOpenMode mode, int extraFlags = 0);
static SqliteDbResult make(const char* cStrName, SqliteOpenMode mode, int extraFlags = 0);
SqliteDb(const SqliteDb&) = delete;
SqliteDb(SqliteDb&& rhs) noexcept;
~SqliteDb();
SqliteDb& operator=(const SqliteDb&) = delete;
SqliteDb& operator=(SqliteDb&& rhs) noexcept;
[[nodiscard]] bool valid() const;
bool revalidate();
Result<SqliteStatement, FudStatus> prepare(const String& sql);
FudStatus exec(
const String& statement,
int (*callback)(void*, int, char**, char**),
void* context);
FudStatus exec(
const String& statement,
int (*callback)(void*, int, char**, char**),
void* context,
SqliteErrorMsg& errorMessage);
template <typename T>
FudStatus bind(SqliteStatement& statement, int index, T value);
FudStatus step(SqliteStatement& statement);
Result<int32_t, FudStatus> columnInt32(SqliteStatement& statement, int index);
Result<int64_t, FudStatus> columnInt64(SqliteStatement& statement, int index);
Result<double, FudStatus> columnDouble(SqliteStatement& statement, int index);
Result<StringView, FudStatus> columnText(SqliteStatement& statement, int index);
[[nodiscard]] constexpr int errorCode() const
{
return m_errorCode;
}
[[nodiscard]] constexpr sqlite3* handle() const
{
return m_dbHandle;
}
private:
static SqliteDbResult finishMake(SqliteOpenMode& mode, int& extraFlags, SqliteDb&& sqlDb);
// private methods
FudStatus initialize();
[[nodiscard]] int open();
// private data members
String m_name{};
bool m_nameValid{false};
sqlite3* m_dbHandle{nullptr};
int m_errorCode{0};
SqliteOpenMode m_mode{};
int m_extraFlags{};
};
class SqliteStatement {
public:
SqliteStatement(const SqliteDb& sqliteDb, const String& input);
SqliteStatement(const SqliteStatement&) = delete;
SqliteStatement(SqliteStatement&& rhs) noexcept;
~SqliteStatement();
SqliteStatement& operator=(const SqliteStatement&) = delete;
SqliteStatement& operator=(SqliteStatement&& rhs) noexcept = delete;
[[nodiscard]] bool valid() const;
sqlite3_stmt* statement();
FudStatus reset();
[[nodiscard]] constexpr int errorCode() const
{
return m_errorCode;
}
constexpr FudStatus status()
{
return m_status;
}
private:
const char* m_tail{nullptr};
FudStatus m_status{FudStatus::ObjectInvalid};
int m_errorCode{0};
sqlite3_stmt* m_preparedStatement{nullptr};
};
class SqliteErrorMsg {
public:
constexpr SqliteErrorMsg() = default;
explicit constexpr SqliteErrorMsg(char* errorMsg) : m_errorMsg{errorMsg}
{
}
SqliteErrorMsg(const SqliteErrorMsg&) = delete;
SqliteErrorMsg(SqliteErrorMsg&& rhs) noexcept;
~SqliteErrorMsg();
SqliteErrorMsg& operator=(const SqliteErrorMsg&) = delete;
SqliteErrorMsg& operator=(SqliteErrorMsg&& rhs) noexcept;
void setMessage(char* newMessage);
[[nodiscard]] const char* message() const
{
return m_errorMsg;
}
private:
char* m_errorMsg{nullptr};
};
template <typename T>
FudStatus SqliteDb::bind(SqliteStatement& statement, int index, T value)
{
static_assert(std::is_same_v<T, int32_t>|| std::is_same_v<T, int64_t> || std::is_same_v<T, double> || std::is_same_v<T, StringView>);
if (index < 0) {
return FudStatus::ArgumentInvalid;
}
if constexpr (std::is_same_v<T, int32_t>) {
m_errorCode = sqlite3_bind_int(statement.statement(), index, value);
} else if constexpr (std::is_same_v<T, int64_t>) {
m_errorCode = sqlite3_bind_int64(statement.statement(), index, value);
} else if constexpr (std::is_same_v<T, double>) {
m_errorCode = sqlite3_bind_double(statement.statement(), index, value);
} else if constexpr (std::is_same_v<T, StringView>) {
if (value.length() > std::numeric_limits<int>::max()) {
return FudStatus::ArgumentInvalid;
}
int stringLength = static_cast<int>(value.length());
m_errorCode = ::sqlite3_bind_text(statement.statement(), index, value.c_str(), stringLength, nullptr);
} else {
return FudStatus::Failure;
}
if (m_errorCode != SQLITE_OK) {
return FudStatus::Failure;
}
return FudStatus::Success;
}
} // namespace fud
#endif
|