From 4bd7b6981bb49dd4111c908aaa34c34f677171f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 13 Apr 2018 23:35:06 +0200 Subject: Refactor that fixes a compilation issue in Release mode Some template specialization were not found, because they were not declared at the point they were used. We moved things around, things are less inter-dependant, and also now it works. --- src/database/column_escape.cpp | 46 --------------------------------- src/database/database.cpp | 57 +++++++++++++++++++---------------------- src/database/insert_query.cpp | 21 +++++++++++++++ src/database/insert_query.hpp | 21 +++++++++++++++ src/database/row.hpp | 49 ----------------------------------- src/database/save.hpp | 31 ++++++++++++++++++++++ src/database/select_query.cpp | 21 +++++++++++++++ src/database/select_query.hpp | 14 ++++++++++ src/database/sqlite3_engine.cpp | 1 + src/database/table.hpp | 10 ++------ src/database/update_query.hpp | 11 +++++++- 11 files changed, 147 insertions(+), 135 deletions(-) delete mode 100644 src/database/column_escape.cpp create mode 100644 src/database/insert_query.cpp create mode 100644 src/database/save.hpp create mode 100644 src/database/select_query.cpp (limited to 'src/database') diff --git a/src/database/column_escape.cpp b/src/database/column_escape.cpp deleted file mode 100644 index 0f1f611..0000000 --- a/src/database/column_escape.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include - -#include -#include - -template <> -std::string before_column() -{ - if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) - return "strftime(\"%Y-%m-%dT%H:%M:%SZ\", "; - else if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) - return "to_char("; - return {}; -} - -template <> -std::string after_column() -{ - if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) - return ")"; - else if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) - return R"(, 'YYYY-MM-DD"T"HH24:MM:SS"Z"'))"; - return {}; -} - -#include - -template <> -std::string before_value() -{ - if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) - return "julianday("; - if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) - return "to_timestamp("; - return {}; -} - -template <> -std::string after_value() -{ - if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) - return ", \"unixepoch\")"; - if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) - return ")"; - return {}; -} diff --git a/src/database/database.cpp b/src/database/database.cpp index 7cb0a45..c935139 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -1,6 +1,8 @@ #include "biboumi.h" #ifdef USE_DATABASE +#include +#include #include #include #include @@ -63,32 +65,28 @@ void Database::open(const std::string& filename) Database::GlobalOptions Database::get_global_options(const std::string& owner) { - auto request = Database::global_options.select(); + auto request = select(Database::global_options); request.where() << Owner{} << "=" << owner; - Database::GlobalOptions options{Database::global_options.get_name()}; auto result = request.execute(*Database::db); if (result.size() == 1) - options = result.front(); - else - options.col() = owner; + return result.front(); + Database::GlobalOptions options{Database::global_options.get_name()}; + options.col() = owner; return options; } Database::IrcServerOptions Database::get_irc_server_options(const std::string& owner, const std::string& server) { - auto request = Database::irc_server_options.select(); + auto request = select(Database::irc_server_options); request.where() << Owner{} << "=" << owner << " and " << Server{} << "=" << server; - Database::IrcServerOptions options{Database::irc_server_options.get_name()}; auto result = request.execute(*Database::db); if (result.size() == 1) - options = result.front(); - else - { - options.col() = owner; - options.col() = server; - } + return result.front(); + Database::IrcServerOptions options{Database::irc_server_options.get_name()}; + options.col() = owner; + options.col() = server; return options; } @@ -97,7 +95,7 @@ Database::AfterConnectionCommands Database::get_after_connection_commands(const const auto id = server_options.col(); if (id == Id::unset_value) return {}; - auto request = Database::after_connection_commands.select(); + auto request = select(Database::after_connection_commands); request.where() << ForeignKey{} << "=" << id; return request.execute(*Database::db); } @@ -116,26 +114,23 @@ void Database::set_after_connection_commands(const Database::IrcServerOptions& s for (auto& command: commands) { command.col() = server_options.col(); - command.save(Database::db); + save(command, *Database::db); } } Database::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner, const std::string& server, const std::string& channel) { - auto request = Database::irc_channel_options.select(); + auto request = select(Database::irc_channel_options); request.where() << Owner{} << "=" << owner <<\ " and " << Server{} << "=" << server <<\ " and " << Channel{} << "=" << channel; - Database::IrcChannelOptions options{Database::irc_channel_options.get_name()}; auto result = request.execute(*Database::db); if (result.size() == 1) - options = result.front(); - else - { - options.col() = owner; - options.col() = server; - options.col() = channel; - } + return result.front(); + Database::IrcChannelOptions options{Database::irc_channel_options.get_name()}; + options.col() = owner; + options.col() = server; + options.col() = channel; return options; } @@ -191,7 +186,7 @@ std::string Database::store_muc_message(const std::string& owner, const std::str line.col() = body; line.col() = nick; - line.save(Database::db); + save(line, *Database::db); return uuid; } @@ -202,7 +197,7 @@ std::vector Database::get_muc_logs(const std::string& owne if (limit == 0) return {}; - auto request = Database::muc_log_lines.select(); + auto request = select(Database::muc_log_lines); request.where() << Database::Owner{} << "=" << owner << \ " and " << Database::IrcChanName{} << "=" << chan_name << \ " and " << Database::IrcServerName{} << "=" << server; @@ -256,7 +251,7 @@ std::vector Database::get_muc_logs(const std::string& owne Database::MucLogLine Database::get_muc_log(const std::string& owner, const std::string& chan_name, const std::string& server, const std::string& uuid, const std::string& start, const std::string& end) { - auto request = Database::muc_log_lines.select(); + auto request = select(Database::muc_log_lines); request.where() << Database::Owner{} << "=" << owner << \ " and " << Database::IrcChanName{} << "=" << chan_name << \ " and " << Database::IrcServerName{} << "=" << server << \ @@ -297,7 +292,7 @@ void Database::add_roster_item(const std::string& local, const std::string& remo roster_item.col() = local; roster_item.col() = remote; - roster_item.save(Database::db); + save(roster_item, *Database::db); } void Database::delete_roster_item(const std::string& local, const std::string& remote) @@ -311,7 +306,7 @@ void Database::delete_roster_item(const std::string& local, const std::string& r bool Database::has_roster_item(const std::string& local, const std::string& remote) { - auto query = Database::roster.select(); + auto query = select(Database::roster); query.where() << Database::LocalJid{} << "=" << local << \ " and " << Database::RemoteJid{} << "=" << remote; @@ -322,7 +317,7 @@ bool Database::has_roster_item(const std::string& local, const std::string& remo std::vector Database::get_contact_list(const std::string& local) { - auto query = Database::roster.select(); + auto query = select(Database::roster); query.where() << Database::LocalJid{} << "=" << local; return query.execute(*Database::db); @@ -330,7 +325,7 @@ std::vector Database::get_contact_list(const std::string& std::vector Database::get_full_roster() { - auto query = Database::roster.select(); + auto query = select(Database::roster); return query.execute(*Database::db); } diff --git a/src/database/insert_query.cpp b/src/database/insert_query.cpp new file mode 100644 index 0000000..f72d67f --- /dev/null +++ b/src/database/insert_query.cpp @@ -0,0 +1,21 @@ +#include + +template <> +std::string before_value() +{ + if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) + return "julianday("; + if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) + return "to_timestamp("; + return {}; +} + +template <> +std::string after_value() +{ + if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) + return ", \"unixepoch\")"; + if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) + return ")"; + return {}; +} diff --git a/src/database/insert_query.hpp b/src/database/insert_query.hpp index e3a7e83..cd1942f 100644 --- a/src/database/insert_query.hpp +++ b/src/database/insert_query.hpp @@ -1,10 +1,15 @@ #pragma once #include +#include #include #include +#include + #include +#include + #include #include #include @@ -37,6 +42,12 @@ std::string after_value() return {}; } +template <> +std::string before_value(); + +template <> +std::string after_value(); + struct InsertQuery: public Query { template @@ -141,3 +152,13 @@ struct InsertQuery: public Query insert_col_name(const std::tuple&) {} }; + +template +void insert(Row& row, DatabaseEngine& db) +{ + InsertQuery query(row.table_name, row.columns); + // Ugly workaround for non portable stuff + if (is_one_of) + query.body += db.get_returning_id_sql_string(Id::name); + query.execute(db, row.columns); +} diff --git a/src/database/row.hpp b/src/database/row.hpp index 3703ff7..27caf43 100644 --- a/src/database/row.hpp +++ b/src/database/row.hpp @@ -1,9 +1,5 @@ #pragma once -#include -#include -#include - #include #include @@ -29,52 +25,7 @@ struct Row return col.value; } - template - void save(std::unique_ptr& db, typename std::enable_if && Coucou>::type* = nullptr) - { - this->insert(*db); - } - - template - void save(std::unique_ptr& db, typename std::enable_if && Coucou>::type* = nullptr) - { - const Id& id = std::get(this->columns); - if (id.value == Id::unset_value) - { - this->insert(*db); - if (db->last_inserted_rowid >= 0) - std::get(this->columns).value = static_cast(db->last_inserted_rowid); - } - else - this->update(*db); - } - - private: - template - void insert(DatabaseEngine& db, typename std::enable_if && Coucou>::type* = nullptr) - { - InsertQuery query(this->table_name, this->columns); - // Ugly workaround for non portable stuff - query.body += db.get_returning_id_sql_string(Id::name); - query.execute(db, this->columns); - } - - template - void insert(DatabaseEngine& db, typename std::enable_if && Coucou>::type* = nullptr) - { - InsertQuery query(this->table_name, this->columns); - query.execute(db, this->columns); - } - - void update(DatabaseEngine& db) - { - UpdateQuery query(this->table_name, this->columns); - - query.execute(db, this->columns); - } - public: std::tuple columns; std::string table_name; - }; diff --git a/src/database/save.hpp b/src/database/save.hpp new file mode 100644 index 0000000..4362110 --- /dev/null +++ b/src/database/save.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include + +#include + +#include + +template +void save(Row& row, DatabaseEngine& db, typename std::enable_if && Coucou>::type* = nullptr) +{ + insert(row, db); +} + +template +void save(Row& row, DatabaseEngine& db, typename std::enable_if && Coucou>::type* = nullptr) +{ + const Id& id = std::get(row.columns); + if (id.value == Id::unset_value) + { + insert(row, db); + if (db.last_inserted_rowid >= 0) + std::get(row.columns).value = static_cast(db.last_inserted_rowid); + } + else + update(row, db); +} + diff --git a/src/database/select_query.cpp b/src/database/select_query.cpp new file mode 100644 index 0000000..970b06c --- /dev/null +++ b/src/database/select_query.cpp @@ -0,0 +1,21 @@ +#include + +template <> +std::string before_column() +{ + if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) + return "strftime(\"%Y-%m-%dT%H:%M:%SZ\", "; + else if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) + return "to_char("; + return {}; +} + +template <> +std::string after_column() +{ + if (Database::engine_type() == DatabaseEngine::EngineType::Sqlite3) + return ")"; + else if (Database::engine_type() == DatabaseEngine::EngineType::Postgresql) + return R"(, 'YYYY-MM-DD"T"HH24:MM:SS"Z"'))"; + return {}; +} diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp index 3013dd8..0de4fe5 100644 --- a/src/database/select_query.hpp +++ b/src/database/select_query.hpp @@ -2,6 +2,8 @@ #include +#include +#include #include #include #include @@ -78,6 +80,12 @@ std::string after_column() return {}; } +template <> +std::string before_column(); + +template <> +std::string after_column(); + template struct SelectQuery: public Query { @@ -153,3 +161,9 @@ struct SelectQuery: public Query const std::string table_name; }; +template +auto select(const Table table) +{ + SelectQuery query(table.name); + return query; +} diff --git a/src/database/sqlite3_engine.cpp b/src/database/sqlite3_engine.cpp index 1fa6316..b6ac1a1 100644 --- a/src/database/sqlite3_engine.cpp +++ b/src/database/sqlite3_engine.cpp @@ -3,6 +3,7 @@ #ifdef SQLITE3_FOUND #include +#include #include #include diff --git a/src/database/table.hpp b/src/database/table.hpp index 31b92a7..4c96151 100644 --- a/src/database/table.hpp +++ b/src/database/table.hpp @@ -2,7 +2,6 @@ #include -#include #include #include @@ -82,12 +81,6 @@ class Table return {this->name}; } - auto select() - { - SelectQuery select(this->name); - return select; - } - auto del() { DeleteQuery query(this->name); @@ -99,6 +92,8 @@ class Table return this->name; } + const std::string name; + private: template @@ -133,5 +128,4 @@ class Table add_column_create(DatabaseEngine&, std::string&) { } - const std::string name; }; diff --git a/src/database/update_query.hpp b/src/database/update_query.hpp index a29ac3f..c2b819d 100644 --- a/src/database/update_query.hpp +++ b/src/database/update_query.hpp @@ -1,7 +1,8 @@ #pragma once -#include #include +#include +#include using namespace std::string_literals; @@ -102,3 +103,11 @@ struct UpdateQuery: public Query actual_bind(statement, value.value, sizeof...(T)); } }; + +template +void update(Row& row, DatabaseEngine& db) +{ + UpdateQuery query(row.table_name, row.columns); + + query.execute(db, row.columns); +} -- cgit v1.2.3