renamed foreign class to entity, introduced interface class query_result_reader

This commit is contained in:
2023-11-24 17:40:57 +01:00
parent 700cbc0652
commit c2a96f4cbd
18 changed files with 285 additions and 207 deletions
+26 -24
View File
@@ -1,6 +1,6 @@
#include "sqlite_connection.hpp"
#include "sqlite_error.hpp"
#include "sqlite_query_result.hpp"
#include "sqlite_result_reader.hpp"
#include "matador/sql/record.hpp"
@@ -37,17 +37,11 @@ bool sqlite_connection::is_open()
return sqlite_db_ != nullptr;
}
struct fetch_context
{
sql::record prototype;
sqlite_query_result::rows rows;
};
int sqlite_connection::parse_result(void* param, int column_count, char** values, char** columns)
{
auto *context = static_cast<fetch_context*>(param);
sqlite_query_result::columns column;
sqlite_result_reader::columns column;
for(int i = 0; i < column_count; ++i) {
// copy and store column data;
if (values[i] == nullptr) {
@@ -73,6 +67,17 @@ int sqlite_connection::parse_result(void* param, int column_count, char** values
return 0;
}
sqlite_connection::fetch_context sqlite_connection::fetch_internal(const std::string &stmt)
{
fetch_context context;
char *errmsg = nullptr;
const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, &context, &errmsg);
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
return context;
}
size_t sqlite_connection::execute(const std::string &stmt)
{
char *errmsg = nullptr;
@@ -85,13 +90,9 @@ size_t sqlite_connection::execute(const std::string &stmt)
std::unique_ptr<sql::query_result_impl> sqlite_connection::fetch(const std::string &stmt)
{
fetch_context context;
char *errmsg = nullptr;
const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, &context, &errmsg);
auto context = fetch_internal(stmt);
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
return std::move(std::make_unique<sqlite_query_result>(std::move(context.prototype), std::move(context.rows)));
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)
@@ -135,21 +136,21 @@ sql::data_type_t string2type(const char *type)
sql::record sqlite_connection::describe(const std::string& table)
{
std::string stmt("PRAGMA table_info(" + table + ")");
const auto result = fetch("PRAGMA table_info(" + table + ")");
const auto result = fetch_internal("PRAGMA table_info(" + table + ")");
sqlite_result_reader reader(std::move(result.rows), result.prototype.size());
sql::record prototype;
while (result->fetch()) {
while (reader.fetch()) {
char *end = nullptr;
// Todo: add index to column
auto index = strtoul(result->column(0), &end, 10);
std::string name = result->column(1);
auto index = strtoul(reader.column(0), &end, 10);
std::string name = reader.column(1);
// Todo: extract size
auto type = (string2type(result->column(2)));
auto type = (string2type(reader.column(2)));
end = nullptr;
utils::constraints options{};
if (strtoul(result->column(3), &end, 10) == 0) {
if (strtoul(reader.column(3), &end, 10) == 0) {
options = utils::constraints::NOT_NULL;
}
// f.default_value(res->column(4));
@@ -161,15 +162,16 @@ sql::record sqlite_connection::describe(const std::string& table)
bool sqlite_connection::exists(const std::string &table_name)
{
const auto result = fetch("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='" + table_name + "' LIMIT 1");
const auto result = fetch_internal("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='" + table_name + "' LIMIT 1");
sqlite_result_reader reader(std::move(result.rows), result.prototype.size());
if (!result->fetch()) {
if (!reader.fetch()) {
// Todo: throw an exception?
return false;
}
int v{};
result->read_value(nullptr, 0, v);
reader.read_value(nullptr, 0, v);
return v == 1;
}