Using OptionalBool, add RecordHistoryOptional col into IrcChannelOptions table

ref #3269
This commit is contained in:
louiz’
2017-06-16 09:54:36 +02:00
parent 4a963cc480
commit 40db183e37
8 changed files with 68 additions and 3 deletions
+6 -1
View File
@@ -7,6 +7,8 @@
#include <database/column.hpp>
#include <database/count_query.hpp>
#include <utils/optional_bool.hpp>
#include <chrono>
#include <string>
@@ -82,6 +84,9 @@ class Database
static constexpr auto options = "";
RecordHistory(): Column<bool>(true) {}};
struct RecordHistoryOptional: Column<OptionalBool> { static constexpr auto name = "recordHistory_";
static constexpr auto options = ""; };
struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
static constexpr auto options = "";
VerifyCert(): Column<bool>(true) {} };
@@ -99,7 +104,7 @@ class Database
using IrcServerOptionsTable = Table<Id, Owner, Server, Pass, AfterConnectionCommand, TlsPorts, Ports, Username, Realname, VerifyCert, TrustedFingerprint, EncodingOut, EncodingIn, MaxHistoryLength>;
using IrcServerOptions = IrcServerOptionsTable::RowType;
using IrcChannelOptionsTable = Table<Id, Owner, Server, Channel, EncodingOut, EncodingIn, MaxHistoryLength, Persistent>;
using IrcChannelOptionsTable = Table<Id, Owner, Server, Channel, EncodingOut, EncodingIn, MaxHistoryLength, Persistent, RecordHistoryOptional>;
using IrcChannelOptions = IrcChannelOptionsTable::RowType;
Database() = default;
+10
View File
@@ -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");
}
+2
View File
@@ -1,5 +1,6 @@
#pragma once
#include <utils/optional_bool.hpp>
#include <database/statement.hpp>
#include <database/column.hpp>
@@ -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);
+16 -1
View File
@@ -5,6 +5,8 @@
#include <logger/logger.hpp>
#include <database/row.hpp>
#include <utils/optional_bool.hpp>
#include <vector>
#include <string>
@@ -20,7 +22,7 @@ extract_row_value(Statement& statement, const int i)
}
template <typename T>
typename std::enable_if<std::is_same<std::string, T>::value, std::string>::type
typename std::enable_if<std::is_same<std::string, T>::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 T>
typename std::enable_if<std::is_same<OptionalBool, T>::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 <std::size_t N=0, typename... T>
typename std::enable_if<N < sizeof...(T), void>::type
extract_row_values(Row<T...>& row, Statement& statement)
+1
View File
@@ -6,3 +6,4 @@ template <> const std::string TypeToSQLType<long>::type = "INTEGER";
template <> const std::string TypeToSQLType<long long>::type = "INTEGER";
template <> const std::string TypeToSQLType<bool>::type = "INTEGER";
template <> const std::string TypeToSQLType<std::string>::type = "TEXT";
template <> const std::string TypeToSQLType<OptionalBool>::type = "INTEGER";
+4 -1
View File
@@ -1,5 +1,7 @@
#pragma once
#include <utils/optional_bool.hpp>
#include <string>
template <typename T>
@@ -10,4 +12,5 @@ template <> const std::string TypeToSQLType<std::size_t>::type;
template <> const std::string TypeToSQLType<long>::type;
template <> const std::string TypeToSQLType<long long>::type;
template <> const std::string TypeToSQLType<bool>::type;
template <> const std::string TypeToSQLType<std::string>::type;
template <> const std::string TypeToSQLType<std::string>::type;
template <> const std::string TypeToSQLType<OptionalBool>::type;
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <string>
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};
};