From 50cadf3dac0d56ef8181d1800cc30f8dcb749141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 13 Jun 2017 10:38:39 +0200 Subject: Implement our own database ORM, and update the whole code to use it Entirely replace LiteSQL fix #3271 --- src/database/table.hpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/database/table.hpp (limited to 'src/database/table.hpp') diff --git a/src/database/table.hpp b/src/database/table.hpp new file mode 100644 index 0000000..163b97f --- /dev/null +++ b/src/database/table.hpp @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +template +class Table +{ + static_assert(sizeof...(T) > 0, "Table cannot be empty"); + using ColumnTypes = std::tuple; + + public: + using RowType = Row; + + Table(std::string name): + name(std::move(name)) + {} + + void create(sqlite3* db) + { + std::string res{"CREATE TABLE IF NOT EXISTS "}; + res += this->name; + res += " (\n"; + this->add_column_create<0>(res); + res += ")"; + + log_debug(res); + + char* error; + const auto result = sqlite3_exec(db, res.data(), nullptr, nullptr, &error); + log_debug("result: ", +result); + if (result != SQLITE_OK) + { + log_error("Error executing query: ", error); + sqlite3_free(error); + } + } + + RowType row() + { + return {this->name}; + } + + SelectQuery select() + { + SelectQuery select(this->name); + return select; + } + + const std::string& get_name() const + { + return this->name; + } + + private: + template + typename std::enable_if::type + add_column_create(std::string& str) + { + using ColumnType = typename std::remove_reference(std::declval()))>::type; + using RealType = typename ColumnType::real_type; + str += ColumnType::name; + str += " "; + str += TypeToSQLType::type; + str += " "s + ColumnType::options; + if (N != sizeof...(T) - 1) + str += ","; + str += "\n"; + + add_column_create(str); + } + + template + typename std::enable_if::type + add_column_create(std::string&) + { } + + const std::string name; +}; -- cgit v1.2.3