summaryrefslogtreecommitdiff
path: root/src/database/query.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/query.hpp')
-rw-r--r--src/database/query.hpp39
1 files changed, 6 insertions, 33 deletions
diff --git a/src/database/query.hpp b/src/database/query.hpp
index 6e1db12..7f8aecb 100644
--- a/src/database/query.hpp
+++ b/src/database/query.hpp
@@ -9,54 +9,27 @@
#include <vector>
#include <string>
-#include <sqlite3.h>
+void actual_bind(Statement& statement, const std::string& value, int index);
+void actual_bind(Statement& statement, const std::size_t value, int index);
+void actual_bind(Statement& statement, const OptionalBool& value, int index);
struct Query
{
std::string body;
std::vector<std::string> params;
+ int current_param{1};
Query(std::string str):
body(std::move(str))
{}
-
- Statement prepare(sqlite3* db)
- {
- sqlite3_stmt* stmt;
- auto res = sqlite3_prepare(db, this->body.data(), static_cast<int>(this->body.size()) + 1,
- &stmt, nullptr);
- if (res != SQLITE_OK)
- {
- log_error("Error preparing statement: ", sqlite3_errmsg(db));
- return nullptr;
- }
- 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_error("Failed to bind ", param, " to param ", i);
- i++;
- }
-
- return statement;
- }
-
- void execute(sqlite3* db)
- {
- auto statement = this->prepare(db);
- while (sqlite3_step(statement.get()) != SQLITE_DONE)
- ;
- }
};
template <typename ColumnType>
void add_param(Query& query, const ColumnType& column)
{
+ std::cout << "add_param<ColumnType>" << std::endl;
actual_add_param(query, column.value);
}
-template <>
-void add_param<Id>(Query& query, const Id& column);
template <typename T>
void actual_add_param(Query& query, const T& val)
@@ -81,7 +54,7 @@ template <typename Integer>
typename std::enable_if<std::is_integral<Integer>::value, Query&>::type
operator<<(Query& query, const Integer& i)
{
- query.body += "?";
+ query.body += "$" + std::to_string(query.current_param++);
actual_add_param(query, i);
return query;
}