implemented sqlite fetch

This commit is contained in:
2023-11-15 22:54:58 +01:00
parent d76ac60278
commit 7c1e940814
24 changed files with 388 additions and 267 deletions
+72 -2
View File
@@ -2,9 +2,11 @@
#include "sqlite_error.hpp"
#include "sqlite_query_result.hpp"
#include <utility>
#include "matador/sql/record.hpp"
#include <cstring>
#include <memory>
#include <utility>
namespace matador::backends::sqlite {
@@ -40,6 +42,11 @@ int sqlite_connection::parse_result(void* param, int column_count, char** values
auto *result = static_cast<sqlite_query_result*>(param);
result->push_back(values, column_count);
sql::record prototype;
for(int i = 0; i < column_count; ++i) {
prototype.append(sql::column{columns[i]});
}
return 0;
}
@@ -57,7 +64,7 @@ std::unique_ptr<sql::query_result_impl> sqlite_connection::fetch(const std::stri
{
auto result = std::make_unique<sqlite_query_result>();
char *errmsg = nullptr;
int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, result.get(), &errmsg);
const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, result.get(), &errmsg);
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
@@ -68,6 +75,69 @@ void sqlite_connection::prepare(const std::string &stmt)
{
}
sql::data_type_t string2type(const char *type)
{
if (strncmp(type, "INTEGER", 7) == 0) {
return sql::data_type_t::type_int;
} else if (strncmp(type, "TINYINT", 7) == 0) {
return sql::data_type_t::type_char;
} else if (strncmp(type, "SMALLINT", 8) == 0) {
return sql::data_type_t::type_short;
} else if (strncmp(type, "BIGINT", 6) == 0) {
return sql::data_type_t::type_long_long;
} else if (strcmp(type, "BOOLEAN") == 0) {
return sql::data_type_t::type_bool;
} else if (strcmp(type, "REAL") == 0) {
return sql::data_type_t::type_double;
} else if (strcmp(type, "FLOAT") == 0) {
return sql::data_type_t::type_float;
} else if (strcmp(type, "DOUBLE") == 0) {
return sql::data_type_t::type_double;
} else if (strcmp(type, "BLOB") == 0) {
return sql::data_type_t::type_blob;
} else if (strcmp(type, "NULL") == 0) {
return sql::data_type_t::type_null;
} else if (strncmp(type, "VARCHAR", 7) == 0) {
return sql::data_type_t::type_varchar;
} else if (strcmp(type, "DATE") == 0) {
return sql::data_type_t::type_date;
} else if (strcmp(type, "DATETIME") == 0) {
return sql::data_type_t::type_time;
} else if (strcmp(type, "TEXT") == 0) {
return sql::data_type_t::type_text;
} else {
return sql::data_type_t::type_unknown;
}
}
sql::record sqlite_connection::describe(const std::string& table)
{
std::string stmt("PRAGMA table_info(" + table + ")");
const auto result = fetch("PRAGMA table_info(" + table + ")");
sql::record prototype;
while (result->fetch()) {
char *end = nullptr;
// Todo: add index to column
auto index = strtoul(result->column(0), &end, 10);
std::string name = result->column(1);
// Todo: extract size
auto type = (string2type(result->column(2)));
end = nullptr;
utils::constraints options{};
if (strtoul(result->column(3), &end, 10) == 0) {
options = utils::constraints::NOT_NULL;
}
// f.default_value(res->column(4));
// end = nullptr;
// f.is_primary_key(strtoul(res->column(3), &end, 10) == 0);
prototype.append({name, type, {options}});
}
return std::move(prototype);
}
}
extern "C"