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)
|
||||
|
||||
Reference in New Issue
Block a user