progress on record fetch
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/table_repository.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
|
||||
+13
-2
@@ -63,14 +63,25 @@ const connection_info &connection::info() const
|
||||
return connection_info_;
|
||||
}
|
||||
|
||||
query_result<record> connection::fetch(const std::string &sql)
|
||||
record connection::describe(const std::string &table_name) const
|
||||
{
|
||||
return std::move(connection_->describe(table_name));
|
||||
}
|
||||
|
||||
query_result<record> connection::fetch(const std::string &sql) const
|
||||
{
|
||||
auto rec = connection_->describe("person");
|
||||
return {connection_->fetch(sql), [rec](){ return new record(rec); }};
|
||||
}
|
||||
|
||||
std::pair<size_t, std::string> connection::execute(const std::string &sql)
|
||||
std::pair<size_t, std::string> connection::execute(const std::string &sql) const
|
||||
{
|
||||
return {connection_->execute(sql), sql};
|
||||
}
|
||||
|
||||
std::unique_ptr<query_result_impl> connection::call_fetch(const std::string &sql) const
|
||||
{
|
||||
return connection_->fetch(sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -108,13 +108,13 @@ query_builder& query_builder::select(const std::vector<std::string> &column_name
|
||||
|
||||
query_parts_.emplace_back(dialect_.token_at(dialect::token_t::SELECT) + " ");
|
||||
|
||||
prototype_.clear();
|
||||
query_.prototype.clear();
|
||||
|
||||
std::string result;
|
||||
if (column_names.size() < 2) {
|
||||
for (const auto &col : column_names) {
|
||||
result.append(dialect_.prepare_identifier(col));
|
||||
prototype_.append(column{col});
|
||||
query_.prototype.append(column{col});
|
||||
}
|
||||
} else {
|
||||
auto it = column_names.begin();
|
||||
@@ -122,7 +122,7 @@ query_builder& query_builder::select(const std::vector<std::string> &column_name
|
||||
for (; it != column_names.end(); ++it) {
|
||||
result.append(", ");
|
||||
result.append(dialect_.prepare_identifier(*it));
|
||||
prototype_.append(column{*it});
|
||||
query_.prototype.append(column{*it});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,17 +262,20 @@ query_builder& query_builder::values(const std::vector<any_type> &values) {
|
||||
std::string result{"("};
|
||||
if (values.size() < 2) {
|
||||
for (auto val : values) {
|
||||
query_.host_vars.push_back(val);
|
||||
std::visit(value_to_string_, val);
|
||||
result.append(value_to_string_.result);
|
||||
}
|
||||
} else {
|
||||
auto it = values.begin();
|
||||
auto val = *it++;
|
||||
query_.host_vars.push_back(val);
|
||||
std::visit(value_to_string_, val);
|
||||
result.append(value_to_string_.result);
|
||||
for (; it != values.end(); ++it) {
|
||||
result.append(", ");
|
||||
val = *it;
|
||||
query_.host_vars.push_back(val);
|
||||
std::visit(value_to_string_, val);
|
||||
result.append(value_to_string_.result);
|
||||
}
|
||||
@@ -403,11 +406,6 @@ std::string query_builder::compile() {
|
||||
return result;
|
||||
}
|
||||
|
||||
const record& query_builder::prototype() const
|
||||
{
|
||||
return prototype_;
|
||||
}
|
||||
|
||||
void query_builder::transition_to(query_builder::state_t next)
|
||||
{
|
||||
if (transitions_[state_].count(next) == 0) {
|
||||
@@ -419,6 +417,7 @@ void query_builder::transition_to(query_builder::state_t next)
|
||||
void query_builder::initialize(query_builder::command_t cmd, query_builder::state_t state)
|
||||
{
|
||||
command_ = cmd;
|
||||
query_ = {};
|
||||
state_ = state;
|
||||
query_parts_.clear();
|
||||
}
|
||||
@@ -432,11 +431,9 @@ std::string build_create_column(const column &col, const dialect &d, column_cont
|
||||
result.append(" NOT NULL");
|
||||
}
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
// result.append(" PRIMARY KEY");
|
||||
context.primary_keys.emplace_back(col.name());
|
||||
}
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::FOREIGN_KEY)) {
|
||||
// result.append(" FOREIGN KEY");
|
||||
context.foreign_contexts.push_back({col.name(), col.ref_table(), col.ref_column()});
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,11 @@ record query_select_finish::fetch_one()
|
||||
return *db().fetch(query().compile()).begin().get();
|
||||
}
|
||||
|
||||
std::unique_ptr<query_result_impl> query_select_finish::fetch()
|
||||
{
|
||||
return db().call_fetch(query().compile());
|
||||
}
|
||||
|
||||
query_intermediate::query_intermediate(session &db, query_builder &query)
|
||||
: db_(db), query_(query) {}
|
||||
|
||||
|
||||
@@ -28,4 +28,13 @@ query_result_impl::on_attribute(const char *id, any_type &value, data_type_t typ
|
||||
read_value(id, column_index_++, value, type, attr.size());
|
||||
}
|
||||
|
||||
const record& query_result_impl::prototype() const
|
||||
{
|
||||
return prototype_;
|
||||
}
|
||||
|
||||
query_result_impl::query_result_impl(record prototype)
|
||||
: prototype_(std::move(prototype))
|
||||
{}
|
||||
|
||||
}
|
||||
+5
-6
@@ -16,6 +16,11 @@ record::record(const std::vector<column> &columns)
|
||||
init();
|
||||
}
|
||||
|
||||
const std::vector<column> &record::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
bool record::has_primary_key() const
|
||||
{
|
||||
return pk_index_ > -1;
|
||||
@@ -30,11 +35,6 @@ const column &record::primary_key() const
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
const std::vector<column> &record::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const column &record::at(const std::string &name) const
|
||||
{
|
||||
return columns_by_name_.at(name).first;
|
||||
@@ -113,7 +113,6 @@ void record::init()
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
add_to_map(col, index++);
|
||||
// columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index++});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-7
@@ -22,6 +22,7 @@ query_drop_intermediate session::drop()
|
||||
|
||||
query_select_intermediate session::select(std::initializer_list<std::string> column_names)
|
||||
{
|
||||
|
||||
return query_select_intermediate{*this, query_.select(column_names)};
|
||||
}
|
||||
|
||||
@@ -40,15 +41,14 @@ query_delete_intermediate session::remove()
|
||||
return query_delete_intermediate{*this, query_.remove()};
|
||||
}
|
||||
|
||||
query_result<record> session::fetch(const std::string &sql) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->fetch(sql);
|
||||
query_result<record> session::fetch(const std::string &sql) const
|
||||
{
|
||||
auto res = call_fetch(sql);
|
||||
auto proto = res->prototype();
|
||||
return query_result<record>{std::move(res), [proto]() { return new record(proto); }};
|
||||
}
|
||||
|
||||
std::pair<size_t, std::string> session::execute(const std::string &sql) {
|
||||
std::pair<size_t, std::string> session::execute(const std::string &sql) const {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
@@ -60,4 +60,14 @@ const table_repository& session::tables() const
|
||||
{
|
||||
return table_repository_;
|
||||
}
|
||||
|
||||
std::unique_ptr<query_result_impl> session::call_fetch(const std::string &sql) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->call_fetch(sql);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,11 +4,6 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
const table_info &table_repository::attach(std::type_index ti, const std::string &table_name, const record &proto)
|
||||
{
|
||||
return attach(ti, table_info{table_name, proto});
|
||||
}
|
||||
|
||||
const table_info& table_repository::attach(const std::type_index ti, const table_info& table)
|
||||
{
|
||||
return repository_.try_emplace(ti, table).first->second;
|
||||
|
||||
Reference in New Issue
Block a user