progress on record fetch
This commit is contained in:
@@ -37,14 +37,37 @@ 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 *result = static_cast<sqlite_query_result*>(param);
|
||||
result->push_back(values, column_count);
|
||||
auto *context = static_cast<fetch_context*>(param);
|
||||
|
||||
sql::record prototype;
|
||||
sqlite_query_result::columns column;
|
||||
for(int i = 0; i < column_count; ++i) {
|
||||
prototype.append(sql::column{columns[i]});
|
||||
// copy and store column data;
|
||||
if (values[i] == nullptr) {
|
||||
auto val = new char[1];
|
||||
val[0] = '\0';
|
||||
column.push_back(val);
|
||||
} else {
|
||||
size_t size = strlen(values[i]);
|
||||
auto val = new char[size + 1];
|
||||
std::memcpy(val, values[i], size);
|
||||
val[size] = '\0';
|
||||
column.push_back(val);
|
||||
}
|
||||
}
|
||||
context->rows.emplace_back(column);
|
||||
|
||||
if (context->prototype.empty()) {
|
||||
for(int i = 0; i < column_count; ++i) {
|
||||
context->prototype.append(sql::column{columns[i]});
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -62,13 +85,13 @@ size_t sqlite_connection::execute(const std::string &stmt)
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> sqlite_connection::fetch(const std::string &stmt)
|
||||
{
|
||||
auto result = std::make_unique<sqlite_query_result>();
|
||||
fetch_context context;
|
||||
char *errmsg = nullptr;
|
||||
const 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, &context, &errmsg);
|
||||
|
||||
throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt);
|
||||
|
||||
return std::move(result);
|
||||
return std::move(std::make_unique<sqlite_query_result>(std::move(context.prototype), std::move(context.rows)));
|
||||
}
|
||||
|
||||
void sqlite_connection::prepare(const std::string &stmt)
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
template < class Type >
|
||||
@@ -48,6 +50,11 @@ void read(Type &x, const char *val, typename std::enable_if<std::is_floating_poi
|
||||
}
|
||||
}
|
||||
|
||||
sqlite_query_result::sqlite_query_result(sql::record prototype, sqlite_query_result::rows result)
|
||||
: sql::query_result_impl(std::move(prototype))
|
||||
, result_(std::move(result))
|
||||
{}
|
||||
|
||||
sqlite_query_result::~sqlite_query_result()
|
||||
{
|
||||
std::for_each(result_.begin(), result_.end(), [](rows ::value_type& row) {
|
||||
@@ -57,6 +64,11 @@ sqlite_query_result::~sqlite_query_result()
|
||||
});
|
||||
}
|
||||
|
||||
size_t sqlite_query_result::column_count() const
|
||||
{
|
||||
return prototype_.size();
|
||||
}
|
||||
|
||||
void sqlite_query_result::read_value(const char *id, size_t index, char &value)
|
||||
{
|
||||
read(value, result_[row_index_][index]);
|
||||
@@ -265,5 +277,4 @@ void sqlite_query_result::read_value(const char *id, size_t index, sql::any_type
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user