-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsqlite_storage.cpp
202 lines (148 loc) · 5.25 KB
/
sqlite_storage.cpp
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
#include <storage/sqlite_storage.h>
#include <optional>
#include <stdexcept>
#include <sqlite3.h>
using namespace std::string_literals;
using namespace Excess;
using namespace Excess::Storage;
class SqliteConflictError final : public std::exception { };
Models::Author ResultRowToAuthor(const ResultRow& row) {
try {
auto author = Models::Author(
row.at("name"),
row.at("password")
);
if (author.GetName().empty()) {
throw std::runtime_error("got author with incorrect name"s);
}
return author;
} catch (const std::out_of_range&) {
throw std::runtime_error("failed to convert row to author"s);
}
}
Models::Message ResultRowToMessage(const ResultRow& row) {
try {
auto message = Models::Message(
row.at("id"),
row.at("author"),
row.at("title"),
row.at("content")
);
if (message.GetId().empty()) {
throw std::runtime_error("got message with incorrect id"s);
}
return message;
} catch (const std::out_of_range&) {
throw std::runtime_error("failed to convert row to message"s);
}
}
SqliteStorage::SqliteStorage(const std::string& filename) {
sqlite3 *context = nullptr;
auto result = sqlite3_open(filename.c_str(), &context);
if (result != 0) {
throw std::runtime_error(
"failed to open sqlite3 database: "s + sqlite3_errmsg(context)
);
}
Context = context;
}
SqliteStorage::~SqliteStorage() {
if (Context != nullptr) {
auto context = reinterpret_cast<sqlite3*>(Context);
sqlite3_close_v2(context);
Context = nullptr;
}
}
void SqliteStorage::CreateAuthor(const Models::Author& author) {
auto sql = "insert into authors (name, password) values (?, ?)"s;
try {
ExecuteSql(sql, { author.GetName(), author.GetPassword() });
} catch (const SqliteConflictError&) {
throw AuthorAlreadyExistsError("author " + author.GetName() + " already exists"s);
}
}
std::optional<Models::Author> SqliteStorage::GetAuthorByName(const std::string& name) {
auto sql = "select name, password from authors where name = ?"s;
auto result = ExecuteSql(sql, { name });
if (result.size() == 0) {
return { };
}
return ResultRowToAuthor(result[0]);
}
void SqliteStorage::CreateMessage(const Models::Message& message) {
auto sql = "insert into messages (id, author, title, content) values (?, ?, ?, ?)"s;
try {
ExecuteSql(
sql, { message.GetId(), message.GetAuthor(), message.GetTitle(), message.GetContent() }
);
} catch (const SqliteConflictError&) {
throw MessageAlreadyExistsError("message " + message.GetId() + "already exists"s);
}
}
std::optional<Models::Message> SqliteStorage::GetMessageById(const std::string& id) {
auto sql = "select id, author, title, content from messages where id = ?"s;
auto result = ExecuteSql(sql, { id });
if (result.size() == 0) {
return { };
}
return ResultRowToMessage(result[0]);
}
std::vector<Models::Message> SqliteStorage::FindMessagesByAuthor(const std::string& author) {
auto sql = "select id, author, title, content from messages where author = ?"s;
auto result = ExecuteSql(sql, { author });
std::vector<Models::Message> messages;
for (auto& row : result) {
messages.emplace_back(ResultRowToMessage(row));
}
return messages;
}
ResultRows SqliteStorage::ExecuteSql(
const std::string& sql,
const std::vector<std::string>& arguments
) {
auto context = reinterpret_cast<sqlite3*>(Context);
int result;
sqlite3_stmt *stmt = nullptr;
result = sqlite3_prepare_v2(context, sql.c_str(), sql.size(), &stmt, nullptr);
if (result != SQLITE_OK) {
throw std::runtime_error(
"failed to prepare sqlite3 statement: "s + sqlite3_errmsg(context)
);
}
for (auto i = 0; i < arguments.size(); i += 1) {
auto argument = arguments[i];
result = sqlite3_bind_text(stmt, i + 1, argument.c_str(), argument.size(), SQLITE_TRANSIENT);
if (result != SQLITE_OK) {
sqlite3_finalize(stmt);
throw std::runtime_error(
"failed to bind text to sqlite3 statement: "s + sqlite3_errmsg(context)
);
}
}
std::vector<std::string> columns;
for (auto i = 0; i < sqlite3_column_count(stmt); i += 1) {
columns.emplace_back(sqlite3_column_name(stmt, i));
}
ResultRows rows;
while (true) {
result = sqlite3_step(stmt);
if (result == SQLITE_DONE || result == SQLITE_OK) {
break;
} else if (result == SQLITE_CONSTRAINT) {
sqlite3_finalize(stmt);
throw SqliteConflictError();
} else if (result != SQLITE_ROW) {
sqlite3_finalize(stmt);
throw std::runtime_error(
"failed to execute sqlite3 statement: "s + sqlite3_errmsg(context)
);
}
ResultRow row;
for (auto i = 0; i < columns.size(); i += 1) {
row[columns[i]] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
}
rows.push_back(row);
}
sqlite3_finalize(stmt);
return rows;
}