From 8c483aa805eb3da25be685ee875555662cab1038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 17 Mar 2018 17:33:29 +0100 Subject: Re-apply "Use if constexpr to make things a lot more readable" This reverts commit d0e3c71b91f1a1c1780158789fd42b8ac7209495. --- src/database/index.hpp | 21 +++++----- src/database/insert_query.hpp | 91 +++++++++++++++++++------------------------ src/database/query.hpp | 41 +++++++------------ src/database/row.hpp | 52 ++++++++++++------------- src/database/select_query.hpp | 77 +++++++++++++++--------------------- src/database/table.hpp | 44 ++++++++++----------- src/database/update_query.hpp | 51 +++++++++++------------- 7 files changed, 164 insertions(+), 213 deletions(-) diff --git a/src/database/index.hpp b/src/database/index.hpp index 30766ab..094a36a 100644 --- a/src/database/index.hpp +++ b/src/database/index.hpp @@ -8,19 +8,16 @@ namespace { template -typename std::enable_if::type -add_column_name(std::string&) -{ } - -template -typename std::enable_if::type -add_column_name(std::string& out) +void add_column_name(std::string& out) { - using ColumnType = typename std::remove_reference(std::declval>()))>::type; - out += ColumnType::name; - if (N != sizeof...(T) - 1) - out += ","; - add_column_name(out); + if constexpr(N < sizeof...(T)) + { + using ColumnType = typename std::remove_reference(std::declval>()))>::type; + out += ColumnType::name; + if (N != sizeof...(T) - 1) + out += ","; + add_column_name(out); + } } } diff --git a/src/database/insert_query.hpp b/src/database/insert_query.hpp index 04c098c..ed1944f 100644 --- a/src/database/insert_query.hpp +++ b/src/database/insert_query.hpp @@ -11,20 +11,17 @@ #include template -typename std::enable_if::type -update_autoincrement_id(std::tuple& columns, Statement& statement) +void update_autoincrement_id(std::tuple& columns, Statement& statement) { - using ColumnType = typename std::decay(columns))>::type; - if (std::is_same::value) - auto&& column = std::get(columns); - update_autoincrement_id(columns, statement); + if constexpr(N < sizeof...(T)) + { + using ColumnType = typename std::decay(columns))>::type; + if (std::is_same::value) + auto&& column = std::get(columns); + update_autoincrement_id(columns, statement); + } } -template -typename std::enable_if::type -update_autoincrement_id(std::tuple&, Statement&) -{} - struct InsertQuery: public Query { template @@ -53,23 +50,20 @@ struct InsertQuery: public Query } template - typename std::enable_if::type - bind_param(const std::tuple& columns, Statement& statement, int index=1) + void bind_param(const std::tuple& columns, Statement& statement, int index=1) { - auto&& column = std::get(columns); - using ColumnType = std::decay_t; + if constexpr(N < sizeof...(T)) + { + auto&& column = std::get(columns); + using ColumnType = std::decay_t; - if (!std::is_same::value) - actual_bind(statement, column.value, index++); + if constexpr(!std::is_same::value) + actual_bind(statement, column.value, index++); - this->bind_param(columns, statement, index); + this->bind_param(columns, statement, index); + } } - template - typename std::enable_if::type - bind_param(const std::tuple&, Statement&, int) - {} - template void insert_values(const std::tuple& columns) { @@ -79,23 +73,21 @@ struct InsertQuery: public Query } template - typename std::enable_if::type - insert_value(const std::tuple& columns, int index=1) + void insert_value(const std::tuple& columns, int index=1) { - using ColumnType = std::decay_t(columns))>; - - if (!std::is_same::value) + if constexpr(N < sizeof...(T)) { - this->body += "$" + std::to_string(index++); - if (N != sizeof...(T) - 1) - this->body += ", "; + 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); } - this->insert_value(columns, index); } - template - typename std::enable_if::type - insert_value(const std::tuple&, const int) - { } template void insert_col_names(const std::tuple& columns) @@ -106,24 +98,21 @@ struct InsertQuery: public Query } template - typename std::enable_if::type - insert_col_name(const std::tuple& columns) + void insert_col_name(const std::tuple& columns) { - using ColumnType = std::decay_t(columns))>; - - if (!std::is_same::value) + if constexpr(N < sizeof...(T)) { - this->body += ColumnType::name; + using ColumnType = std::decay_t(columns))>; - if (N < (sizeof...(T) - 1)) - this->body += ", "; - } + if (!std::is_same::value) + { + this->body += ColumnType::name; - this->insert_col_name(columns); - } + if (N < (sizeof...(T) - 1)) + this->body += ", "; + } - template - typename std::enable_if::type - insert_col_name(const std::tuple&) - {} + this->insert_col_name(columns); + } + } }; diff --git a/src/database/query.hpp b/src/database/query.hpp index 1c4a5ff..2a2d2d4 100644 --- a/src/database/query.hpp +++ b/src/database/query.hpp @@ -13,12 +13,12 @@ void actual_bind(Statement& statement, const std::string& value, int index); void actual_bind(Statement& statement, const std::int64_t& value, int index); -template ::value>* = 0> +void actual_bind(Statement& statement, const std::optional& value, int index); +template void actual_bind(Statement& statement, const T& value, int index) { actual_bind(statement, static_cast(value), index); } -void actual_bind(Statement& statement, const std::optional& value, int index); #ifdef DEBUG_SQL_QUERIES #include @@ -57,38 +57,27 @@ struct Query #endif }; -template -void add_param(Query& query, const ColumnType& column) -{ - std::cout << "add_param" << std::endl; - actual_add_param(query, column.value); -} - +void actual_add_param(Query& query, const std::string& val); +void actual_add_param(Query& query, const std::optional& val); template void actual_add_param(Query& query, const T& val) { query.params.push_back(std::to_string(val)); } -void actual_add_param(Query& query, const std::string& val); - -template -typename std::enable_if::value, Query&>::type -operator<<(Query& query, const T&) -{ - query.body += T::name; - return query; -} -void actual_add_param(Query& query, const std::optional& val); - Query& operator<<(Query& query, const char* str); Query& operator<<(Query& query, const std::string& str); -template -typename std::enable_if::value, Query&>::type -operator<<(Query& query, const Integer& i) +template +Query& operator<<(Query& query, const T& i) { - query.body += "$" + std::to_string(query.current_param++); - actual_add_param(query, i); + if constexpr(std::is_integral::value) + { + query.body += "$" + std::to_string(query.current_param++); + actual_add_param(query, i); + } + else + { + query.body += T::name; + } return query; } - diff --git a/src/database/row.hpp b/src/database/row.hpp index 3703ff7..194a9c5 100644 --- a/src/database/row.hpp +++ b/src/database/row.hpp @@ -29,41 +29,39 @@ struct Row return col.value; } - template - void save(std::unique_ptr& db, typename std::enable_if && Coucou>::type* = nullptr) + void save(std::unique_ptr& db) { - 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) + if constexpr(is_one_of) { - this->insert(*db); - if (db->last_inserted_rowid >= 0) - std::get(this->columns).value = static_cast(db->last_inserted_rowid); + 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); } else - this->update(*db); + this->insert(*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) + void insert(DatabaseEngine& db) { - InsertQuery query(this->table_name, this->columns); - query.execute(db, this->columns); + if constexpr(is_one_of) + { + 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); + } + else + { + InsertQuery query(this->table_name, this->columns); + query.execute(db, this->columns); + } } void update(DatabaseEngine& db) diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp index cd9943c..f7496a4 100644 --- a/src/database/select_query.hpp +++ b/src/database/select_query.hpp @@ -15,48 +15,37 @@ using namespace std::string_literals; template -typename std::enable_if::value, std::int64_t>::type -extract_row_value(Statement& statement, const int i) +auto extract_row_value(Statement& statement, const int i) { - return statement.get_column_int64(i); -} - -template -typename std::enable_if::value, T>::type -extract_row_value(Statement& statement, const int i) -{ - return statement.get_column_text(i); -} - -template -typename std::enable_if, T>::value, T>::type -extract_row_value(Statement& statement, const int i) -{ - const auto integer = statement.get_column_int(i); - if (integer > 0) - return true; - else if (integer < 0) - return false; - return std::nullopt; + if constexpr(std::is_integral::value) + return statement.get_column_int64(i); + else if constexpr (std::is_same::value) + return statement.get_column_text(i); + else if (std::is_same, T>::value) + { + const auto integer = statement.get_column_int(i); + if (integer > 0) + return std::optional{true}; + else if (integer < 0) + return std::optional{false}; + return std::optional{}; + } } template -typename std::enable_if::type -extract_row_values(Row& row, Statement& statement) +void extract_row_values(Row& row, Statement& statement) { - using ColumnType = typename std::remove_reference(row.columns))>::type; + if constexpr(N < sizeof...(T)) + { + using ColumnType = typename std::remove_reference(row.columns))>::type; - auto&& column = std::get(row.columns); - column.value = static_cast(extract_row_value(statement, N)); + auto&& column = std::get(row.columns); + column.value = static_cast(extract_row_value(statement, N)); - extract_row_values(row, statement); + extract_row_values(row, statement); + } } -template -typename std::enable_if::type -extract_row_values(Row&, Statement&) -{} - template struct SelectQuery: public Query { @@ -69,23 +58,21 @@ struct SelectQuery: public Query } template - typename std::enable_if::type - insert_col_name() + void insert_col_name() { - using ColumnsType = std::tuple; - using ColumnType = typename std::remove_reference(std::declval()))>::type; + if constexpr(N < sizeof...(T)) + { + using ColumnsType = std::tuple; + using ColumnType = typename std::remove_reference(std::declval()))>::type; - this->body += " " + std::string{ColumnType::name}; + this->body += " " + std::string{ColumnType::name}; - if (N < (sizeof...(T) - 1)) - this->body += ", "; + if (N < (sizeof...(T) - 1)) + this->body += ", "; - this->insert_col_name(); + this->insert_col_name(); + } } - template - typename std::enable_if::type - insert_col_name() - {} SelectQuery& where() { diff --git a/src/database/table.hpp b/src/database/table.hpp index 680e7cc..eb68418 100644 --- a/src/database/table.hpp +++ b/src/database/table.hpp @@ -93,36 +93,32 @@ class Table private: template - typename std::enable_if::type - add_column_if_not_exists(DatabaseEngine& db, const std::set& existing_columns) + void add_column_if_not_exists(DatabaseEngine& db, const std::set& existing_columns) { - using ColumnType = typename std::remove_reference(std::declval()))>::type; - if (existing_columns.count(ColumnType::name) == 0) - add_column_to_table(db, this->name); - add_column_if_not_exists(db, existing_columns); + if constexpr(N < sizeof...(T)) + { + using ColumnType = typename std::remove_reference(std::declval()))>::type; + if (existing_columns.count(ColumnType::name) == 0) + add_column_to_table(db, this->name); + add_column_if_not_exists(db, existing_columns); + } } - template - typename std::enable_if::type - add_column_if_not_exists(DatabaseEngine&, const std::set&) - {} template - typename std::enable_if::type - add_column_create(DatabaseEngine& db, std::string& str) + void add_column_create(DatabaseEngine& db, std::string& str) { - using ColumnType = typename std::remove_reference(std::declval()))>::type; - str += ColumnType::name; - str += " "; - str += ToSQLType(db); - if (N != sizeof...(T) - 1) - str += ","; - - add_column_create(db, str); + if constexpr(N < sizeof...(T)) + { + using ColumnType = typename std::remove_reference(std::declval()))>::type; + str += ColumnType::name; + str += " "; + str += ToSQLType(db); + if (N != sizeof...(T) - 1) + str += ","; + + add_column_create(db, str); + } } - template - typename std::enable_if::type - 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..0ee2bd9 100644 --- a/src/database/update_query.hpp +++ b/src/database/update_query.hpp @@ -39,27 +39,25 @@ struct UpdateQuery: public Query } template - typename std::enable_if::type - insert_col_name_and_value(const std::tuple& columns) + void insert_col_name_and_value(const std::tuple& columns) { - using ColumnType = std::decay_t(columns))>; - - if (!std::is_same::value) + if constexpr(N < sizeof...(T)) { - this->body += ColumnType::name + "=$"s + std::to_string(this->current_param); - this->current_param++; + using ColumnType = std::decay_t(columns))>; - if (N < (sizeof...(T) - 1)) - this->body += ", "; - } + if (!std::is_same::value) + { + this->body += ColumnType::name + "=$"s + + std::to_string(this->current_param); + this->current_param++; - this->insert_col_name_and_value(columns); - } - template - typename std::enable_if::type - insert_col_name_and_value(const std::tuple&) - {} + if (N < (sizeof...(T) - 1)) + this->body += ", "; + } + this->insert_col_name_and_value(columns); + } + } template void execute(DatabaseEngine& db, const std::tuple& columns) @@ -76,23 +74,20 @@ struct UpdateQuery: public Query } template - typename std::enable_if::type - bind_param(const std::tuple& columns, Statement& statement, int index=1) + void bind_param(const std::tuple& columns, Statement& statement, int index=1) { - auto&& column = std::get(columns); - using ColumnType = std::decay_t; + if constexpr(N < sizeof...(T)) + { + auto&& column = std::get(columns); + using ColumnType = std::decay_t; - if (!std::is_same::value) - actual_bind(statement, column.value, index++); + if (!std::is_same::value) + actual_bind(statement, column.value, index++); - this->bind_param(columns, statement, index); + this->bind_param(columns, statement, index); + } } - template - typename std::enable_if::type - bind_param(const std::tuple&, Statement&, int) - {} - template void bind_id(const std::tuple& columns, Statement& statement) { -- cgit v1.2.3