From 0168b96b79db2627fdba77a8712956408aa081d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 4 Oct 2017 21:28:18 +0200 Subject: Add postgresql support --- src/database/insert_query.hpp | 103 ++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 49 deletions(-) (limited to 'src/database/insert_query.hpp') diff --git a/src/database/insert_query.hpp b/src/database/insert_query.hpp index 2ece69d..2fc0512 100644 --- a/src/database/insert_query.hpp +++ b/src/database/insert_query.hpp @@ -12,62 +12,63 @@ #include -template -typename std::enable_if, Id>::value, void>::type -actual_bind(Statement& statement, std::vector& params, const std::tuple&) +template +typename std::enable_if::type +update_autoincrement_id(std::tuple& columns, Statement& statement) { - const auto value = params.front(); - params.erase(params.begin()); - if (sqlite3_bind_text(statement.get(), N + 1, value.data(), static_cast(value.size()), SQLITE_TRANSIENT) != SQLITE_OK) - log_error("Failed to bind ", value, " to param ", N); -} - -template -typename std::enable_if, Id>::value, void>::type -actual_bind(Statement& statement, std::vector&, const std::tuple& columns) -{ - auto&& column = std::get(columns); - if (column.value != 0) + using ColumnType = typename std::decay(columns))>::type; + if (std::is_same::value) { - if (sqlite3_bind_int64(statement.get(), N + 1, static_cast(column.value)) != SQLITE_OK) - log_error("Failed to bind ", column.value, " to id."); + log_debug("EXTRACTING LAST ID"); + auto&& column = std::get(columns); } - else if (sqlite3_bind_null(statement.get(), N + 1) != SQLITE_OK) - log_error("Failed to bind NULL to param ", N); + update_autoincrement_id(columns, statement); } +template +typename std::enable_if::type +update_autoincrement_id(std::tuple&, Statement& statement) +{} + struct InsertQuery: public Query { - InsertQuery(const std::string& name): - Query("INSERT OR REPLACE INTO ") + template + InsertQuery(const std::string& name, const std::tuple& columns): + Query("INSERT INTO ") { this->body += name; + this->insert_col_names(columns); + this->insert_values(columns); } template - void execute(const std::tuple& columns, sqlite3* db) + void execute(DatabaseEngine& db, std::tuple& columns) { - auto statement = this->prepare(db); - { - this->bind_param(columns, statement); - if (sqlite3_step(statement.get()) != SQLITE_DONE) - log_error("Failed to execute query: ", sqlite3_errmsg(db)); - } + auto statement = db.prepare(this->body); + this->bind_param(columns, *statement); + + if (statement->step() != StepResult::Error) + db.extract_last_insert_rowid(*statement); + else + log_error("Failed to extract the rowid from the last INSERT"); } template typename std::enable_if::type - bind_param(const std::tuple& columns, Statement& statement) + bind_param(const std::tuple& columns, Statement& statement, int index=1) { - using ColumnType = typename std::remove_reference(columns))>::type; + auto&& column = std::get(columns); + using ColumnType = std::decay_t; + + if (!std::is_same::value) + actual_bind(statement, column.value, index++); - actual_bind(statement, this->params, columns); - this->bind_param(columns, statement); + this->bind_param(columns, statement, index); } template typename std::enable_if::type - bind_param(const std::tuple&, Statement&) + bind_param(const std::tuple&, Statement&, int) {} template @@ -80,18 +81,21 @@ struct InsertQuery: public Query template typename std::enable_if::type - insert_value(const std::tuple& columns) + insert_value(const std::tuple& columns, int index=1) { - this->body += "?"; - if (N != sizeof...(T) - 1) - this->body += ","; - this->body += " "; - add_param(*this, std::get(columns)); - this->insert_value(columns); + using ColumnType = std::decay_t(columns))>; + + if (!std::is_same::value) + { + this->body += "$" + std::to_string(index++); + if (N != sizeof...(T) - 1) + this->body += ", "; + } + this->insert_value(columns, index); } template typename std::enable_if::type - insert_value(const std::tuple&) + insert_value(const std::tuple&, const int) { } template @@ -99,27 +103,28 @@ struct InsertQuery: public Query { this->body += " ("; this->insert_col_name(columns); - this->body += ")\n"; + this->body += ")"; } template typename std::enable_if::type insert_col_name(const std::tuple& columns) { - using ColumnType = typename std::remove_reference(columns))>::type; + using ColumnType = std::decay_t(columns))>; - this->body += ColumnType::name; + if (!std::is_same::value) + { + this->body += ColumnType::name; - if (N < (sizeof...(T) - 1)) - this->body += ", "; + if (N < (sizeof...(T) - 1)) + this->body += ", "; + } this->insert_col_name(columns); } + template typename std::enable_if::type insert_col_name(const std::tuple&) {} - - - private: }; -- cgit v1.2.3