summaryrefslogtreecommitdiff
path: root/src/database/insert_query.hpp
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2017-06-13 10:38:39 +0200
committerlouiz’ <louiz@louiz.org>2017-06-14 00:19:15 +0200
commit50cadf3dac0d56ef8181d1800cc30f8dcb749141 (patch)
tree23e56307a6fba4f926d261f858c8df8b6b8d5ea7 /src/database/insert_query.hpp
parent7ca95a09740297ae9c041c5f8ae4caa0a57a149a (diff)
downloadbiboumi-50cadf3dac0d56ef8181d1800cc30f8dcb749141.tar.gz
biboumi-50cadf3dac0d56ef8181d1800cc30f8dcb749141.tar.bz2
biboumi-50cadf3dac0d56ef8181d1800cc30f8dcb749141.tar.xz
biboumi-50cadf3dac0d56ef8181d1800cc30f8dcb749141.zip
Implement our own database ORM, and update the whole code to use it
Entirely replace LiteSQL fix #3271
Diffstat (limited to 'src/database/insert_query.hpp')
-rw-r--r--src/database/insert_query.hpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/database/insert_query.hpp b/src/database/insert_query.hpp
new file mode 100644
index 0000000..00b77c5
--- /dev/null
+++ b/src/database/insert_query.hpp
@@ -0,0 +1,128 @@
+#pragma once
+
+#include <database/column.hpp>
+#include <database/query.hpp>
+#include <logger/logger.hpp>
+
+#include <type_traits>
+#include <vector>
+#include <string>
+#include <tuple>
+
+#include <sqlite3.h>
+
+template <int N, typename ColumnType, typename... T>
+typename std::enable_if<!std::is_same<std::decay_t<ColumnType>, Id>::value, void>::type
+actual_bind(sqlite3_stmt* statement, std::vector<std::string>& params, const std::tuple<T...>&)
+{
+ const auto value = params.front();
+ params.erase(params.begin());
+ if (sqlite3_bind_text(statement, N + 1, value.data(), static_cast<int>(value.size()), SQLITE_TRANSIENT) != SQLITE_OK)
+ log_error("Failed to bind ", value, " to param ", N);
+ else
+ log_debug("Bound (not id) [", value, "] to ", N);
+}
+
+template <int N, typename ColumnType, typename... T>
+typename std::enable_if<std::is_same<std::decay_t<ColumnType>, Id>::value, void>::type
+actual_bind(sqlite3_stmt* statement, std::vector<std::string>&, const std::tuple<T...>& columns)
+{
+ auto&& column = std::get<Id>(columns);
+ if (column.value != 0)
+ {
+ if (sqlite3_bind_int64(statement, N + 1, column.value) != SQLITE_OK)
+ log_error("Failed to bind ", column.value, " to id.");
+ }
+ else if (sqlite3_bind_null(statement, N + 1) != SQLITE_OK)
+ log_error("Failed to bind NULL to param ", N);
+ else
+ log_debug("Bound NULL to ", N);
+}
+
+struct InsertQuery: public Query
+{
+ InsertQuery(const std::string& name):
+ Query("INSERT OR REPLACE INTO ")
+ {
+ this->body += name;
+ }
+
+ template <typename... T>
+ void execute(const std::tuple<T...>& columns, sqlite3* db)
+ {
+ auto statement = this->prepare(db);
+ {
+ this->bind_param<0>(columns, statement);
+ if (sqlite3_step(statement) != SQLITE_DONE)
+ log_error("Failed to execute query: ", sqlite3_errmsg(db));
+ }
+ }
+
+ template <int N, typename... T>
+ typename std::enable_if<N < sizeof...(T), void>::type
+ bind_param(const std::tuple<T...>& columns, sqlite3_stmt* statement)
+ {
+ using ColumnType = typename std::remove_reference<decltype(std::get<N>(columns))>::type;
+
+ actual_bind<N, ColumnType>(statement, this->params, columns);
+ this->bind_param<N+1>(columns, statement);
+ }
+
+ template <int N, typename... T>
+ typename std::enable_if<N == sizeof...(T), void>::type
+ bind_param(const std::tuple<T...>&, sqlite3_stmt*)
+ {}
+
+ template <typename... T>
+ void insert_values(const std::tuple<T...>& columns)
+ {
+ this->body += "VALUES (";
+ this->insert_value<0>(columns);
+ this->body += ")";
+ }
+
+ template <int N, typename... T>
+ typename std::enable_if<N < sizeof...(T), void>::type
+ insert_value(const std::tuple<T...>& columns)
+ {
+ this->body += "?";
+ if (N != sizeof...(T) - 1)
+ this->body += ",";
+ this->body += " ";
+ add_param(*this, std::get<N>(columns));
+ this->insert_value<N+1>(columns);
+ }
+ template <int N, typename... T>
+ typename std::enable_if<N == sizeof...(T), void>::type
+ insert_value(const std::tuple<T...>&)
+ { }
+
+ template <typename... T>
+ void insert_col_names(const std::tuple<T...>& columns)
+ {
+ this->body += " (";
+ this->insert_col_name<0>(columns);
+ this->body += ")\n";
+ }
+
+ template <int N, typename... T>
+ typename std::enable_if<N < sizeof...(T), void>::type
+ insert_col_name(const std::tuple<T...>& columns)
+ {
+ auto value = std::get<N>(columns);
+
+ this->body += value.name;
+
+ if (N < (sizeof...(T) - 1))
+ this->body += ", ";
+
+ this->insert_col_name<N+1>(columns);
+ }
+ template <int N, typename... T>
+ typename std::enable_if<N == sizeof...(T), void>::type
+ insert_col_name(const std::tuple<T...>&)
+ {}
+
+
+ private:
+};