From 40db183e3753486deaa43e950fff38579c5ced6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 16 Jun 2017 09:52:40 +0200 Subject: Using OptionalBool, add RecordHistoryOptional col into IrcChannelOptions table ref #3269 --- src/database/database.hpp | 7 ++++++- src/database/query.cpp | 10 ++++++++++ src/database/query.hpp | 2 ++ src/database/select_query.hpp | 17 ++++++++++++++++- src/database/type_to_sql.cpp | 1 + src/database/type_to_sql.hpp | 5 ++++- src/utils/optional_bool.hpp | 25 +++++++++++++++++++++++++ 7 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/utils/optional_bool.hpp (limited to 'src') diff --git a/src/database/database.hpp b/src/database/database.hpp index 1ad62fc..28b6b1b 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -7,6 +7,8 @@ #include #include +#include + #include #include @@ -82,6 +84,9 @@ class Database static constexpr auto options = ""; RecordHistory(): Column(true) {}}; + struct RecordHistoryOptional: Column { static constexpr auto name = "recordHistory_"; + static constexpr auto options = ""; }; + struct VerifyCert: Column { static constexpr auto name = "verifyCert_"; static constexpr auto options = ""; VerifyCert(): Column(true) {} }; @@ -99,7 +104,7 @@ class Database using IrcServerOptionsTable = Table; using IrcServerOptions = IrcServerOptionsTable::RowType; - using IrcChannelOptionsTable = Table; + using IrcChannelOptionsTable = Table; using IrcChannelOptions = IrcChannelOptionsTable::RowType; Database() = default; diff --git a/src/database/query.cpp b/src/database/query.cpp index fb8c055..e6cf072 100644 --- a/src/database/query.cpp +++ b/src/database/query.cpp @@ -9,3 +9,13 @@ void actual_add_param(Query& query, const std::string& val) { query.params.push_back(val); } + +void actual_add_param(Query& query, const OptionalBool& val) +{ + if (!val.is_set) + query.params.push_back("0"); + else if (val.value) + query.params.push_back("1"); + else + query.params.push_back("-1"); +} \ No newline at end of file diff --git a/src/database/query.hpp b/src/database/query.hpp index 42eeda2..d9638f7 100644 --- a/src/database/query.hpp +++ b/src/database/query.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -49,3 +50,4 @@ void actual_add_param(Query& query, const T& val) } void actual_add_param(Query& query, const std::string& val); +void actual_add_param(Query& query, const OptionalBool& val); diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp index 93d69ed..837b064 100644 --- a/src/database/select_query.hpp +++ b/src/database/select_query.hpp @@ -5,6 +5,8 @@ #include #include +#include + #include #include @@ -20,7 +22,7 @@ extract_row_value(Statement& statement, const int i) } template -typename std::enable_if::value, std::string>::type +typename std::enable_if::value, T>::type extract_row_value(Statement& statement, const int i) { const auto size = sqlite3_column_bytes(statement.get(), i); @@ -29,6 +31,19 @@ extract_row_value(Statement& statement, const int i) return result; } +template +typename std::enable_if::value, T>::type +extract_row_value(Statement& statement, const int i) +{ + const auto integer = sqlite3_column_int(statement.get(), i); + OptionalBool result; + if (integer > 0) + result.set_value(true); + else if (integer < 0) + result.set_value(false); + return result; +} + template typename std::enable_if::type extract_row_values(Row& row, Statement& statement) diff --git a/src/database/type_to_sql.cpp b/src/database/type_to_sql.cpp index 0b26185..bcd9daa 100644 --- a/src/database/type_to_sql.cpp +++ b/src/database/type_to_sql.cpp @@ -6,3 +6,4 @@ template <> const std::string TypeToSQLType::type = "INTEGER"; template <> const std::string TypeToSQLType::type = "INTEGER"; template <> const std::string TypeToSQLType::type = "INTEGER"; template <> const std::string TypeToSQLType::type = "TEXT"; +template <> const std::string TypeToSQLType::type = "INTEGER"; \ No newline at end of file diff --git a/src/database/type_to_sql.hpp b/src/database/type_to_sql.hpp index 1942268..ba806ab 100644 --- a/src/database/type_to_sql.hpp +++ b/src/database/type_to_sql.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include template @@ -10,4 +12,5 @@ template <> const std::string TypeToSQLType::type; template <> const std::string TypeToSQLType::type; template <> const std::string TypeToSQLType::type; template <> const std::string TypeToSQLType::type; -template <> const std::string TypeToSQLType::type; \ No newline at end of file +template <> const std::string TypeToSQLType::type; +template <> const std::string TypeToSQLType::type; \ No newline at end of file diff --git a/src/utils/optional_bool.hpp b/src/utils/optional_bool.hpp new file mode 100644 index 0000000..824e76d --- /dev/null +++ b/src/utils/optional_bool.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +struct OptionalBool +{ + OptionalBool() = default; + + OptionalBool(bool value): + is_set(true), value(value) {} + + void set_value(bool value) + { + this->is_set = true; + this->value = value; + } + + void unset() + { + this->is_set = false; + } + + bool is_set{false}; + bool value{false}; +}; -- cgit v1.2.3