Add a ChannelOptions table in the DB
And a way to retrieve its values, defaulting on the ServerOptions for unset values.
This commit is contained in:
@@ -14,12 +14,30 @@
|
|||||||
<field name="realname" type="string" length="1024" default=""/>
|
<field name="realname" type="string" length="1024" default=""/>
|
||||||
<field name="verifyCert" type="boolean" default="true"/>
|
<field name="verifyCert" type="boolean" default="true"/>
|
||||||
|
|
||||||
|
<field name="encodingOut" type="string" default="utf-8"/>
|
||||||
|
<field name="encodingIn" type="string" default="utf-8"/>
|
||||||
|
|
||||||
<index unique="true">
|
<index unique="true">
|
||||||
<indexfield name="owner"/>
|
<indexfield name="owner"/>
|
||||||
<indexfield name="server"/>
|
<indexfield name="server"/>
|
||||||
</index>
|
</index>
|
||||||
</object>
|
</object>
|
||||||
|
|
||||||
|
<object name="IrcChannelOptions">
|
||||||
|
<field name="owner" type="string" length="3071"/>
|
||||||
|
<field name="server" type="string" length="3071"/>
|
||||||
|
<field name="channel" type="string" length="1024"/>
|
||||||
|
|
||||||
|
<field name="encodingOut" type="string"/>
|
||||||
|
<field name="encodingIn" type="string"/>
|
||||||
|
|
||||||
|
<index unique="true">
|
||||||
|
<indexfield name="owner"/>
|
||||||
|
<indexfield name="server"/>
|
||||||
|
<indexfield name="channel"/>
|
||||||
|
</index>
|
||||||
|
</object>
|
||||||
|
|
||||||
<object name="LogLine">
|
<object name="LogLine">
|
||||||
<field name="date" type="date" />
|
<field name="date" type="date" />
|
||||||
<field name="body" type="string" length="4096"/>
|
<field name="body" type="string" length="4096"/>
|
||||||
|
|||||||
@@ -49,6 +49,39 @@ db::IrcServerOptions Database::get_irc_server_options(const std::string& owner,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner,
|
||||||
|
const std::string& server,
|
||||||
|
const std::string& channel)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
auto options = litesql::select<db::IrcChannelOptions>(Database::get_db(),
|
||||||
|
db::IrcChannelOptions::Owner == owner &&
|
||||||
|
db::IrcChannelOptions::Server == server &&
|
||||||
|
db::IrcChannelOptions::Channel == channel).one();
|
||||||
|
return options;
|
||||||
|
} catch (const litesql::NotFound& e) {
|
||||||
|
db::IrcChannelOptions options(Database::get_db());
|
||||||
|
options.owner = owner;
|
||||||
|
options.server = server;
|
||||||
|
options.channel = channel;
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
db::IrcChannelOptions Database::get_irc_channel_options_with_server_default(const std::string& owner,
|
||||||
|
const std::string& server,
|
||||||
|
const std::string& channel)
|
||||||
|
{
|
||||||
|
auto coptions = Database::get_irc_channel_options(owner, server, channel);
|
||||||
|
auto soptions = Database::get_irc_server_options(owner, server);
|
||||||
|
if (coptions.encodingIn.value().empty())
|
||||||
|
coptions.encodingIn = soptions.encodingIn;
|
||||||
|
if (coptions.encodingOut.value().empty())
|
||||||
|
coptions.encodingOut = soptions.encodingOut;
|
||||||
|
|
||||||
|
return coptions;
|
||||||
|
}
|
||||||
|
|
||||||
void Database::close()
|
void Database::close()
|
||||||
{
|
{
|
||||||
Database::db.reset(nullptr);
|
Database::db.reset(nullptr);
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ public:
|
|||||||
*/
|
*/
|
||||||
static db::IrcServerOptions get_irc_server_options(const std::string& owner,
|
static db::IrcServerOptions get_irc_server_options(const std::string& owner,
|
||||||
const std::string& server);
|
const std::string& server);
|
||||||
|
static db::IrcChannelOptions get_irc_channel_options(const std::string& owner,
|
||||||
|
const std::string& server,
|
||||||
|
const std::string& channel);
|
||||||
|
static db::IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner,
|
||||||
|
const std::string& server,
|
||||||
|
const std::string& channel);
|
||||||
|
|
||||||
static void close();
|
static void close();
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ TEST_CASE("Database")
|
|||||||
#ifdef USE_DATABASE
|
#ifdef USE_DATABASE
|
||||||
Config::set("db_name", ":memory:");
|
Config::set("db_name", ":memory:");
|
||||||
Database::set_verbose(false);
|
Database::set_verbose(false);
|
||||||
|
|
||||||
|
SECTION("Basic retrieve and update")
|
||||||
|
{
|
||||||
auto o = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
auto o = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
||||||
o.update();
|
o.update();
|
||||||
auto a = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
auto a = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
||||||
@@ -23,6 +26,65 @@ TEST_CASE("Database")
|
|||||||
|
|
||||||
CHECK(b.pass == "");
|
CHECK(b.pass == "");
|
||||||
CHECK(b.pass.value() == "");
|
CHECK(b.pass.value() == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("channel options")
|
||||||
|
{
|
||||||
|
Config::set("db_name", ":memory:");
|
||||||
|
auto o = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
|
||||||
|
|
||||||
|
CHECK(o.encodingIn == "");
|
||||||
|
o.encodingIn = "ISO-8859-1";
|
||||||
|
o.update();
|
||||||
|
auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
|
||||||
|
CHECK(o.encodingIn == "ISO-8859-1");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Channel options with server default")
|
||||||
|
{
|
||||||
|
const std::string owner{"zouzou@example.com"};
|
||||||
|
const std::string server{"irc.example.com"};
|
||||||
|
const std::string chan1{"#foo"};
|
||||||
|
|
||||||
|
auto c = Database::get_irc_channel_options(owner, server, chan1);
|
||||||
|
auto s = Database::get_irc_server_options(owner, server);
|
||||||
|
|
||||||
|
GIVEN("An option defined for the channel but not the server")
|
||||||
|
{
|
||||||
|
c.encodingIn = "channelEncoding";
|
||||||
|
c.update();
|
||||||
|
WHEN("we fetch that option")
|
||||||
|
{
|
||||||
|
auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
|
||||||
|
THEN("we get the channel option")
|
||||||
|
CHECK(r.encodingIn == "channelEncoding");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("An option defined for the server but not the channel")
|
||||||
|
{
|
||||||
|
s.encodingIn = "serverEncoding";
|
||||||
|
s.update();
|
||||||
|
WHEN("we fetch that option")
|
||||||
|
{
|
||||||
|
auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
|
||||||
|
THEN("we get the server option")
|
||||||
|
CHECK(r.encodingIn == "serverEncoding");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("An option defined for both the server and the channel")
|
||||||
|
{
|
||||||
|
s.encodingIn = "serverEncoding";
|
||||||
|
s.update();
|
||||||
|
c.encodingIn = "channelEncoding";
|
||||||
|
c.update();
|
||||||
|
WHEN("we fetch that option")
|
||||||
|
{
|
||||||
|
auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
|
||||||
|
THEN("we get the channel option")
|
||||||
|
CHECK(r.encodingIn == "channelEncoding");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Database::close();
|
Database::close();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user