Add postgresql support
This commit is contained in:
+7
-32
@@ -11,52 +11,27 @@
|
||||
|
||||
#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 +56,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user