summaryrefslogtreecommitdiff
path: root/src/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/database')
-rw-r--r--src/database/database.cpp20
-rw-r--r--src/database/query.cpp15
-rw-r--r--src/database/query.hpp43
-rw-r--r--src/database/select_query.hpp41
4 files changed, 66 insertions, 53 deletions
diff --git a/src/database/database.cpp b/src/database/database.cpp
index 246cdbc..92f7682 100644
--- a/src/database/database.cpp
+++ b/src/database/database.cpp
@@ -41,7 +41,8 @@ void Database::open(const std::string& filename)
Database::GlobalOptions Database::get_global_options(const std::string& owner)
{
- auto request = Database::global_options.select().where() << Owner{} << "=" << owner;
+ auto request = Database::global_options.select();
+ request.where() << Owner{} << "=" << owner;
Database::GlobalOptions options{Database::global_options.get_name()};
auto result = request.execute(Database::db);
@@ -54,7 +55,8 @@ Database::GlobalOptions Database::get_global_options(const std::string& owner)
Database::IrcServerOptions Database::get_irc_server_options(const std::string& owner, const std::string& server)
{
- auto request = Database::irc_server_options.select().where() << Owner{} << "=" << owner << " and " << Server{} << "=" << server;
+ auto request = Database::irc_server_options.select();
+ request.where() << Owner{} << "=" << owner << " and " << Server{} << "=" << server;
Database::IrcServerOptions options{Database::irc_server_options.get_name()};
auto result = request.execute(Database::db);
@@ -70,9 +72,10 @@ Database::IrcServerOptions Database::get_irc_server_options(const std::string& o
Database::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner, const std::string& server, const std::string& channel)
{
- auto request = Database::irc_channel_options.select().where() << Owner{} << "=" << owner <<\
- " and " << Server{} << "=" << server <<\
- " and " << Channel{} << "=" << channel;
+ auto request = Database::irc_channel_options.select();
+ request.where() << Owner{} << "=" << owner <<\
+ " and " << Server{} << "=" << server <<\
+ " and " << Channel{} << "=" << channel;
Database::IrcChannelOptions options{Database::irc_channel_options.get_name()};
auto result = request.execute(Database::db);
if (result.size() == 1)
@@ -146,9 +149,10 @@ std::string Database::store_muc_message(const std::string& owner, const std::str
std::vector<Database::MucLogLine> Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server,
int limit, const std::string& start, const std::string& end)
{
- auto request = Database::muc_log_lines.select().where() << Database::Owner{} << "=" << owner << \
- " and " << Database::IrcChanName{} << "=" << chan_name << \
- " and " << Database::IrcServerName{} << "=" << server;
+ auto request = Database::muc_log_lines.select();
+ request.where() << Database::Owner{} << "=" << owner << \
+ " and " << Database::IrcChanName{} << "=" << chan_name << \
+ " and " << Database::IrcServerName{} << "=" << server;
if (!start.empty())
{
diff --git a/src/database/query.cpp b/src/database/query.cpp
index e6cf072..ba63a92 100644
--- a/src/database/query.cpp
+++ b/src/database/query.cpp
@@ -18,4 +18,17 @@ void actual_add_param(Query& query, const OptionalBool& val)
query.params.push_back("1");
else
query.params.push_back("-1");
-} \ No newline at end of file
+}
+
+Query& operator<<(Query& query, const char* str)
+{
+ query.body += str;
+ return query;
+}
+
+Query& operator<<(Query& query, const std::string& str)
+{
+ query.body += "?";
+ actual_add_param(query, str);
+ return query;
+}
diff --git a/src/database/query.hpp b/src/database/query.hpp
index d9638f7..f103fe9 100644
--- a/src/database/query.hpp
+++ b/src/database/query.hpp
@@ -22,16 +22,34 @@ struct Query
Statement prepare(sqlite3* db)
{
- sqlite3_stmt* statement;
+ sqlite3_stmt* stmt;
log_debug(this->body);
auto res = sqlite3_prepare(db, this->body.data(), static_cast<int>(this->body.size()) + 1,
- &statement, nullptr);
+ &stmt, nullptr);
if (res != SQLITE_OK)
{
log_error("Error preparing statement: ", sqlite3_errmsg(db));
return nullptr;
}
- return {statement};
+ Statement statement(stmt);
+ int i = 1;
+ for (const std::string& param: this->params)
+ {
+ if (sqlite3_bind_text(statement.get(), i, param.data(), static_cast<int>(param.size()), SQLITE_TRANSIENT) != SQLITE_OK)
+ log_debug("Failed to bind ", param, " to param ", i);
+ else
+ log_debug("Bound ", param, " to ", i);
+ i++;
+ }
+
+ return statement;
+ }
+
+ void execute(sqlite3* db)
+ {
+ auto statement = this->prepare(db);
+ while (sqlite3_step(statement.get()) != SQLITE_DONE)
+ ;
}
};
@@ -51,3 +69,22 @@ 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);
+
+template <typename T>
+typename std::enable_if<!std::is_integral<T>::value, Query&>::type
+operator<<(Query& query, const T&)
+{
+ query.body += T::name;
+ return query;
+}
+
+Query& operator<<(Query& query, const char* str);
+Query& operator<<(Query& query, const std::string& str);
+template <typename Integer>
+typename std::enable_if<std::is_integral<Integer>::value, Query&>::type
+operator<<(Query& query, const Integer& i)
+{
+ query.body += "?";
+ actual_add_param(query, i);
+ return query;
+}
diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp
index 837b064..f4d71af 100644
--- a/src/database/select_query.hpp
+++ b/src/database/select_query.hpp
@@ -112,16 +112,6 @@ struct SelectQuery: public Query
auto execute(sqlite3* db)
{
auto statement = this->prepare(db);
- int i = 1;
- for (const std::string& param: this->params)
- {
- if (sqlite3_bind_text(statement.get(), i, param.data(), static_cast<int>(param.size()), SQLITE_TRANSIENT) != SQLITE_OK)
- log_debug("Failed to bind ", param, " to param ", i);
- else
- log_debug("Bound ", param, " to ", i);
-
- i++;
- }
std::vector<Row<T...>> rows;
while (sqlite3_step(statement.get()) == SQLITE_ROW)
{
@@ -135,34 +125,3 @@ struct SelectQuery: public Query
const std::string table_name;
};
-template <typename T, typename... ColumnTypes>
-typename std::enable_if<!std::is_integral<T>::value, SelectQuery<ColumnTypes...>&>::type
-operator<<(SelectQuery<ColumnTypes...>& query, const T&)
-{
- query.body += T::name;
- return query;
-}
-
-template <typename... ColumnTypes>
-SelectQuery<ColumnTypes...>& operator<<(SelectQuery<ColumnTypes...>& query, const char* str)
-{
- query.body += str;
- return query;
-}
-
-template <typename... ColumnTypes>
-SelectQuery<ColumnTypes...>& operator<<(SelectQuery<ColumnTypes...>& query, const std::string& str)
-{
- query.body += "?";
- actual_add_param(query, str);
- return query;
-}
-
-template <typename Integer, typename... ColumnTypes>
-typename std::enable_if<std::is_integral<Integer>::value, SelectQuery<ColumnTypes...>&>::type
-operator<<(SelectQuery<ColumnTypes...>& query, const Integer& i)
-{
- query.body += "?";
- actual_add_param(query, i);
- return query;
-}