WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
SQLiteDatabaseModel.cpp
1#include "SQLiteDatabaseModel.h"
2
3#include "Exceptions/SQLite3Exception.h"
4
5using namespace std;
6
7namespace framework
8{
9 namespace sqlite
10 {
11 bool SQLiteDatabaseModel::isNumber(string_view source)
12 {
13 if (source.empty())
14 {
15 return false;
16 }
17
18 static constexpr string_view symbols = "0123456789-.,";
19
20 if (all_of(source.begin(), source.end(), [](char c) { return find(symbols.begin(), symbols.end(), c) != symbols.end(); }))
21 {
22 size_t check = 0;
23
24 for (const auto& i : source)
25 {
26 if (i == '.' || i == ',')
27 {
28 check++;
29 }
30 }
31
32 if (check > 1)
33 {
34 return false;
35 }
36
37 return true;
38 }
39
40 return false;
41 }
42
43 string SQLiteDatabaseModel::convertToValue(string_view source)
44 {
45 return SQLiteDatabaseModel::isNumber(source) ? string(source) : format("'{}'", source);
46 }
47
49 {
50 sqlite3_stmt* result = nullptr;
51 vector<unordered_map<string, string>> output;
52 int code;
53
54 sqlite3_prepare_v2(**database, query.data(), -1, &result, nullptr);
55
56 while ((code = sqlite3_step(result)) == SQLITE_ROW)
57 {
58 unordered_map<string, string>& row = output.emplace_back();
59 int columnCount = sqlite3_column_count(result);
60
61 for (int i = 0; i < columnCount; i++)
62 {
63 row.try_emplace
64 (
65 sqlite3_column_name(result, i),
66 reinterpret_cast<const char*>(sqlite3_column_text(result, i))
67 );
68 }
69 }
70
71 if (code != SQLITE_DONE)
72 {
73 throw exceptions::SQLite3Exception(sqlite3_errmsg(**database), query);
74 }
75
76 sqlite3_finalize(result);
77
78 return utility::SQLiteResult(move(output));
79 }
80
81 SQLiteDatabaseModel::SQLiteDatabaseModel() :
82 database(databaseConstructor) //-V670
83 {
84
85 }
86
88 {
89 return this->execute(query);
90 }
91
92 void SQLiteDatabaseModel::createTable(const vector<pair<string, string>>& attributes)
93 {
94 string fields = "(";
95
96 for (const auto& [key, value] : attributes)
97 {
98 fields += format("{} {}, ", key, value);
99 }
100
101 fields.replace(fields.end() - 2, fields.end(), ")");
102
103 this->raw(format("CREATE TABLE IF NOT EXISTS {} {}", this->getTableName(), fields));
104 }
105
107 {
108 this->raw(format("DROP TABLE IF EXISTS {}", this->getTableName()));
109 }
110
111 void SQLiteDatabaseModel::recreateTable(const vector<pair<string, string>>& attributes)
112 {
113 this->dropTable();
114
115 this->createTable(attributes);
116 }
117
118 utility::SQLiteResult SQLiteDatabaseModel::insert(const unordered_map<string, string>& attributes)
119 {
120 string keys;
121 string values;
122
123 for (const auto& [key, value] : attributes)
124 {
125 keys += format("{}, ", key);
126 values += format("{}, ", SQLiteDatabaseModel::convertToValue(value));
127 }
128
129 return this->raw(format("INSERT INTO {} ({}) VALUES ({})", this->getTableName(), string(keys.begin(), keys.end() - 2), string(values.begin(), values.end() - 2)));
130 }
131
132 void SQLiteDatabaseModel::update(const unordered_map<string, string>& attributes, const string& fieldName, const string& fieldValue)
133 {
134 string query = format("UPDATE {} SET ", this->getTableName());
135
136 for (const auto& [key, value] : attributes)
137 {
138 query += format("{} = {} ", key, SQLiteDatabaseModel::convertToValue(value));
139 }
140
141 query += format("WHERE {} = {}", fieldName, SQLiteDatabaseModel::convertToValue(fieldValue));
142
143 this->raw(query);
144 }
145
146 void SQLiteDatabaseModel::deleteQuery(const string& fieldName, const string& fieldValue)
147 {
148 this->raw(format("DELETE FROM {} WHERE {} = {}", this->getTableName(), fieldName, SQLiteDatabaseModel::convertToValue(fieldValue)));
149 }
150
151 void SQLiteDatabaseModel::deleteQuery(const unordered_map<string, string>& attributes)
152 {
153 string query = format("DELETE FROM {} WHERE ", this->getTableName());
154
155 for (const auto& [fieldName, fieldValue] : attributes)
156 {
157 query += format("{} = {} AND ", fieldName, SQLiteDatabaseModel::convertToValue(fieldValue));
158 }
159
160 query.resize(query.size() - 5);
161
162 this->raw(query);
163 }
164
166 {
167 return this->raw(format("SELECT * FROM {}", this->getTableName()));
168 }
169
170 utility::SQLiteResult SQLiteDatabaseModel::selectByField(const string& fieldName, const string& fieldValue)
171 {
172 return this->raw(format("SELECT * FROM {} WHERE {} = {}", this->getTableName(), fieldName, SQLiteDatabaseModel::convertToValue(fieldValue)));
173 }
174
175 utility::SQLiteResult SQLiteDatabaseModel::selectByField(const unordered_map<string, string>& attributes)
176 {
177 string query = format("SELECT * FROM {} WHERE ", this->getTableName());
178
179 for (const auto& [fieldName, fieldValue] : attributes)
180 {
181 query += format("{} = {} AND ", fieldName, SQLiteDatabaseModel::convertToValue(fieldValue));
182 }
183
184 query.resize(query.size() - 5);
185
186 return this->raw(query);
187 }
188 }
189}
Class for SQLite3 exceptions.
utility::SQLiteResult selectAll()
SELECT all.
virtual void createTable(const std::vector< std::pair< std::string, std::string > > &attributes={})
Create table.
static bool isNumber(std::string_view source)
Check that string represent number.
static std::string convertToValue(std::string_view source)
If source is string surrounds it with quotes.
virtual void deleteQuery(const std::string &fieldName, const std::string &fieldValue)
Delete from table.
virtual void recreateTable(const std::vector< std::pair< std::string, std::string > > &attributes={})
Delete and create table.
utility::SQLiteResult execute(const std::string &query)
Execute raw query.
utility::SQLiteResult raw(const std::string &query)
Raw SQL query.
virtual utility::SQLiteResult insert(const std::unordered_map< std::string, std::string > &attributes={})
INSERT row.
virtual utility::SQLiteResult selectByField(const std::string &fieldName, const std::string &fieldValue)
SELECT with condition.
virtual void update(const std::unordered_map< std::string, std::string > &attributes, const std::string &fieldName, const std::string &fieldValue)
UPDATE table.
Contains result of SQL request.