1#include "SQLiteDatabaseModel.h"
3#include "Exceptions/SQLite3Exception.h"
18 static constexpr string_view symbols =
"0123456789-.,";
20 if (all_of(source.begin(), source.end(), [](
char c) { return find(symbols.begin(), symbols.end(), c) != symbols.end(); }))
24 for (
const auto& i : source)
26 if (i ==
'.' || i ==
',')
50 sqlite3_stmt* result =
nullptr;
51 vector<unordered_map<string, string>> output;
54 sqlite3_prepare_v2(**database, query.data(), -1, &result,
nullptr);
56 while ((code = sqlite3_step(result)) == SQLITE_ROW)
58 unordered_map<string, string>& row = output.emplace_back();
59 int columnCount = sqlite3_column_count(result);
61 for (
int i = 0; i < columnCount; i++)
65 sqlite3_column_name(result, i),
66 reinterpret_cast<const char*
>(sqlite3_column_text(result, i))
71 if (code != SQLITE_DONE)
76 sqlite3_finalize(result);
81 SQLiteDatabaseModel::SQLiteDatabaseModel() :
82 database(databaseConstructor)
96 for (
const auto& [key, value] : attributes)
98 fields += format(
"{} {}, ", key, value);
101 fields.replace(fields.end() - 2, fields.end(),
")");
103 this->
raw(format(
"CREATE TABLE IF NOT EXISTS {} {}", this->getTableName(), fields));
108 this->
raw(format(
"DROP TABLE IF EXISTS {}", this->getTableName()));
123 for (
const auto& [key, value] : attributes)
125 keys += format(
"{}, ", key);
129 return this->
raw(format(
"INSERT INTO {} ({}) VALUES ({})", this->getTableName(),
string(keys.begin(), keys.end() - 2),
string(values.begin(), values.end() - 2)));
134 string query = format(
"UPDATE {} SET ", this->getTableName());
136 for (
const auto& [key, value] : attributes)
153 string query = format(
"DELETE FROM {} WHERE ", this->getTableName());
155 for (
const auto& [fieldName, fieldValue] : attributes)
160 query.resize(query.size() - 5);
167 return this->
raw(format(
"SELECT * FROM {}", this->getTableName()));
177 string query = format(
"SELECT * FROM {} WHERE ", this->getTableName());
179 for (
const auto& [fieldName, fieldValue] : attributes)
184 query.resize(query.size() - 5);
186 return this->
raw(query);
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.
virtual void dropTable()
Delete table.
Contains result of SQL request.