Using OptionalBool, add RecordHistoryOptional col into IrcChannelOptions table
ref #3269
This commit is contained in:
@@ -7,6 +7,8 @@
|
|||||||
#include <database/column.hpp>
|
#include <database/column.hpp>
|
||||||
#include <database/count_query.hpp>
|
#include <database/count_query.hpp>
|
||||||
|
|
||||||
|
#include <utils/optional_bool.hpp>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -82,6 +84,9 @@ class Database
|
|||||||
static constexpr auto options = "";
|
static constexpr auto options = "";
|
||||||
RecordHistory(): Column<bool>(true) {}};
|
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_";
|
struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
|
||||||
static constexpr auto options = "";
|
static constexpr auto options = "";
|
||||||
VerifyCert(): Column<bool>(true) {} };
|
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 IrcServerOptionsTable = Table<Id, Owner, Server, Pass, AfterConnectionCommand, TlsPorts, Ports, Username, Realname, VerifyCert, TrustedFingerprint, EncodingOut, EncodingIn, MaxHistoryLength>;
|
||||||
using IrcServerOptions = IrcServerOptionsTable::RowType;
|
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;
|
using IrcChannelOptions = IrcChannelOptionsTable::RowType;
|
||||||
|
|
||||||
Database() = default;
|
Database() = default;
|
||||||
|
|||||||
@@ -9,3 +9,13 @@ void actual_add_param(Query& query, const std::string& val)
|
|||||||
{
|
{
|
||||||
query.params.push_back(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");
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/optional_bool.hpp>
|
||||||
#include <database/statement.hpp>
|
#include <database/statement.hpp>
|
||||||
#include <database/column.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 std::string& val);
|
||||||
|
void actual_add_param(Query& query, const OptionalBool& val);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <logger/logger.hpp>
|
#include <logger/logger.hpp>
|
||||||
#include <database/row.hpp>
|
#include <database/row.hpp>
|
||||||
|
|
||||||
|
#include <utils/optional_bool.hpp>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -20,7 +22,7 @@ extract_row_value(Statement& statement, const int i)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
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)
|
extract_row_value(Statement& statement, const int i)
|
||||||
{
|
{
|
||||||
const auto size = sqlite3_column_bytes(statement.get(), i);
|
const auto size = sqlite3_column_bytes(statement.get(), i);
|
||||||
@@ -29,6 +31,19 @@ extract_row_value(Statement& statement, const int i)
|
|||||||
return result;
|
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>
|
template <std::size_t N=0, typename... T>
|
||||||
typename std::enable_if<N < sizeof...(T), void>::type
|
typename std::enable_if<N < sizeof...(T), void>::type
|
||||||
extract_row_values(Row<T...>& row, Statement& statement)
|
extract_row_values(Row<T...>& row, Statement& statement)
|
||||||
|
|||||||
@@ -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<long long>::type = "INTEGER";
|
||||||
template <> const std::string TypeToSQLType<bool>::type = "INTEGER";
|
template <> const std::string TypeToSQLType<bool>::type = "INTEGER";
|
||||||
template <> const std::string TypeToSQLType<std::string>::type = "TEXT";
|
template <> const std::string TypeToSQLType<std::string>::type = "TEXT";
|
||||||
|
template <> const std::string TypeToSQLType<OptionalBool>::type = "INTEGER";
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/optional_bool.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
template <typename T>
|
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>::type;
|
||||||
template <> const std::string TypeToSQLType<long long>::type;
|
template <> const std::string TypeToSQLType<long long>::type;
|
||||||
template <> const std::string TypeToSQLType<bool>::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;
|
||||||
@@ -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};
|
||||||
|
};
|
||||||
@@ -33,9 +33,13 @@ TEST_CASE("Database")
|
|||||||
|
|
||||||
CHECK(o.col<Database::EncodingIn>() == "");
|
CHECK(o.col<Database::EncodingIn>() == "");
|
||||||
o.col<Database::EncodingIn>() = "ISO-8859-1";
|
o.col<Database::EncodingIn>() = "ISO-8859-1";
|
||||||
|
CHECK(o.col<Database::RecordHistoryOptional>().is_set == false);
|
||||||
|
o.col<Database::RecordHistoryOptional>().set_value(false);
|
||||||
o.save(Database::db);
|
o.save(Database::db);
|
||||||
auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
|
auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
|
||||||
CHECK(o.col<Database::EncodingIn>() == "ISO-8859-1");
|
CHECK(o.col<Database::EncodingIn>() == "ISO-8859-1");
|
||||||
|
CHECK(o.col<Database::RecordHistoryOptional>().is_set == true);
|
||||||
|
CHECK(o.col<Database::RecordHistoryOptional>().value == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Channel options with server default")
|
SECTION("Channel options with server default")
|
||||||
|
|||||||
Reference in New Issue
Block a user