implemented prepared statement for postgres and sqlite
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "sqlite_connection.hpp"
|
||||
#include "sqlite_error.hpp"
|
||||
#include "sqlite_result_reader.hpp"
|
||||
#include "sqlite_statement.hpp"
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
@@ -20,25 +21,25 @@ void sqlite_connection::open()
|
||||
return;
|
||||
}
|
||||
|
||||
const auto ret = sqlite3_open(info().database.c_str(), &sqlite_db_);
|
||||
const auto ret = sqlite3_open(info().database.c_str(), &db_);
|
||||
|
||||
if (ret != SQLITE_OK) {
|
||||
throw_sqlite_error(ret, sqlite_db_, "open");
|
||||
throw_sqlite_error(ret, db_, "open");
|
||||
}
|
||||
}
|
||||
|
||||
void sqlite_connection::close()
|
||||
{
|
||||
int ret = sqlite3_close(sqlite_db_);
|
||||
int ret = sqlite3_close(db_);
|
||||
|
||||
throw_sqlite_error(ret, sqlite_db_, "close");
|
||||
throw_sqlite_error(ret, db_, "close");
|
||||
|
||||
sqlite_db_ = nullptr;
|
||||
db_ = nullptr;
|
||||
}
|
||||
|
||||
bool sqlite_connection::is_open()
|
||||
{
|
||||
return sqlite_db_ != nullptr;
|
||||
return db_ != nullptr;
|
||||
}
|
||||
|
||||
int sqlite_connection::parse_result(void* param, int column_count, char** values, char** columns)
|
||||
@@ -75,9 +76,9 @@ sqlite_connection::fetch_context sqlite_connection::fetch_internal(const std::st
|
||||
{
|
||||
fetch_context context;
|
||||
char *errmsg = nullptr;
|
||||
const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, &context, &errmsg);
|
||||
const int ret = sqlite3_exec(db_, stmt.c_str(), parse_result, &context, &errmsg);
|
||||
|
||||
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
|
||||
throw_sqlite_error(ret, db_, "sqlite", stmt);
|
||||
|
||||
return context;
|
||||
}
|
||||
@@ -85,11 +86,11 @@ sqlite_connection::fetch_context sqlite_connection::fetch_internal(const std::st
|
||||
size_t sqlite_connection::execute(const std::string &stmt)
|
||||
{
|
||||
char *errmsg = nullptr;
|
||||
int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), nullptr, nullptr, &errmsg);
|
||||
int ret = sqlite3_exec(db_, stmt.c_str(), nullptr, nullptr, &errmsg);
|
||||
|
||||
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
|
||||
throw_sqlite_error(ret, db_, "sqlite", stmt);
|
||||
|
||||
return sqlite3_changes(sqlite_db_);
|
||||
return sqlite3_changes(db_);
|
||||
}
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> sqlite_connection::fetch(const std::string &stmt)
|
||||
@@ -99,8 +100,13 @@ std::unique_ptr<sql::query_result_impl> sqlite_connection::fetch(const std::stri
|
||||
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<sqlite_result_reader>(std::move(context.rows), context.prototype.size()), std::move(context.prototype)));
|
||||
}
|
||||
|
||||
void sqlite_connection::prepare(const std::string &stmt)
|
||||
std::unique_ptr<sql::statement_impl> sqlite_connection::prepare(sql::query_context query)
|
||||
{
|
||||
sqlite3_stmt *stmt{};
|
||||
int ret = sqlite3_prepare_v2(db_, query.sql.c_str(), static_cast<int>(query.sql.size()), &stmt, nullptr);
|
||||
throw_sqlite_error(ret, db_, "sqlite3_prepare_v2", query.sql);
|
||||
|
||||
return std::make_unique<sqlite_statement>(db_, stmt, query);
|
||||
}
|
||||
|
||||
sql::data_type_t string2type(const char *type)
|
||||
@@ -142,7 +148,7 @@ sql::record sqlite_connection::describe(const std::string& table)
|
||||
{
|
||||
const auto result = fetch_internal("PRAGMA table_info(" + table + ")");
|
||||
|
||||
sqlite_result_reader reader(std::move(result.rows), result.prototype.size());
|
||||
sqlite_result_reader reader(result.rows, result.prototype.size());
|
||||
sql::record prototype;
|
||||
while (reader.fetch()) {
|
||||
char *end = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user