entity query and record progress
This commit is contained in:
+14
-3
@@ -2,7 +2,9 @@
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
#include "matador/sql/schema.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
@@ -68,7 +70,7 @@ const connection_info &connection::info() const
|
||||
return connection_info_;
|
||||
}
|
||||
|
||||
record connection::describe(const std::string &table_name) const
|
||||
std::vector<sql::column_definition> connection::describe(const std::string &table_name) const
|
||||
{
|
||||
return std::move(connection_->describe(table_name));
|
||||
}
|
||||
@@ -94,12 +96,21 @@ sql::query connection::query(const sql::schema &schema) const
|
||||
return sql::query(*const_cast<connection*>(this), schema);
|
||||
}
|
||||
|
||||
bool is_unknown(const std::vector<sql::column_definition> &columns) {
|
||||
return std::all_of(std::begin(columns), std::end(columns), [](const auto &col) {
|
||||
return col.type() == data_type_t::type_unknown;
|
||||
});
|
||||
}
|
||||
|
||||
query_result<record> connection::fetch(const query_context &q) const
|
||||
{
|
||||
if (q.prototype.empty() || q.prototype.unknown()) {
|
||||
if (q.prototype.empty() || is_unknown(q.prototype)) {
|
||||
const auto table_prototype = describe(q.table.name);
|
||||
for (auto &col : q.prototype) {
|
||||
if (const auto rit = table_prototype.find(col.name()); col.type() == data_type_t::type_unknown && rit != table_prototype.end()) {
|
||||
const auto rit = std::find_if(std::begin(table_prototype), std::end(table_prototype), [&col](const auto &value) {
|
||||
return value.name() == col.name();
|
||||
});
|
||||
if (col.type() == data_type_t::type_unknown && rit != table_prototype.end()) {
|
||||
const_cast<column_definition&>(col).type(rit->type());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "matador/sql/field.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
const std::string &field::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const field &col)
|
||||
{
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,7 +39,7 @@ std::unique_ptr<statement_impl> noop_connection::prepare(query_context context)
|
||||
return {};
|
||||
}
|
||||
|
||||
record noop_connection::describe(const std::string &table)
|
||||
std::vector<sql::column_definition> noop_connection::describe(const std::string &table)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -141,18 +141,18 @@ query_builder &query_builder::select(const std::vector<column> &columns)
|
||||
for (const auto &col: columns) {
|
||||
result.append(dialect_.prepare_identifier(col));
|
||||
query_.result_vars.emplace_back(col.name);
|
||||
query_.prototype.append(column_definition{col.name});
|
||||
query_.prototype.emplace_back(col.name);
|
||||
}
|
||||
} else {
|
||||
auto it = columns.begin();
|
||||
result.append(dialect_.prepare_identifier(*it));
|
||||
query_.result_vars.emplace_back(it->name);
|
||||
query_.prototype.append(column_definition{(*it++).name});
|
||||
query_.prototype.emplace_back((*it++).name);
|
||||
for (; it != columns.end(); ++it) {
|
||||
result.append(", ");
|
||||
result.append(dialect_.prepare_identifier(*it));
|
||||
query_.result_vars.emplace_back(it->name);
|
||||
query_.prototype.append(column_definition{(*it).name});
|
||||
query_.prototype.emplace_back((*it).name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,18 +33,18 @@ void query_compiler::visit(query_select_part &select_part)
|
||||
for (const auto &col: columns) {
|
||||
result.append(dialect_.prepare_identifier(col));
|
||||
query_.result_vars.emplace_back(col.name);
|
||||
query_.prototype.append(column_definition{col.name});
|
||||
query_.prototype.emplace_back(col.name);
|
||||
}
|
||||
} else {
|
||||
auto it = columns.begin();
|
||||
result.append(dialect_.prepare_identifier(*it));
|
||||
query_.result_vars.emplace_back(it->name);
|
||||
query_.prototype.append(column_definition{(*it++).name});
|
||||
query_.prototype.emplace_back((*it++).name);
|
||||
for (; it != columns.end(); ++it) {
|
||||
result.append(", ");
|
||||
result.append(dialect_.prepare_identifier(*it));
|
||||
query_.result_vars.emplace_back(it->name);
|
||||
query_.prototype.append(column_definition{(*it).name});
|
||||
query_.prototype.emplace_back((*it).name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace matador::sql::detail {
|
||||
|
||||
template<>
|
||||
record *create_prototype<record>(const record &prototype)
|
||||
record *create_prototype<record>(const std::vector<column_definition> &prototype)
|
||||
{
|
||||
return new record{prototype};
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ void detail::pk_reader::on_primary_key(const char *id, std::string &value, size_
|
||||
data_type_traits<std::string>::read_value(reader_, id, column_index_++, value, size);
|
||||
}
|
||||
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, record prototype)
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<column_definition> prototype)
|
||||
: prototype_(std::move(prototype))
|
||||
, reader_(std::move(reader))
|
||||
, pk_reader_(*reader_)
|
||||
@@ -44,7 +44,7 @@ query_result_impl::on_attribute(const char *id, any_type &value, data_type_t typ
|
||||
reader_->read_value(id, column_index_++, value, type, attr.size());
|
||||
}
|
||||
|
||||
const record& query_result_impl::prototype() const
|
||||
const std::vector<column_definition>& query_result_impl::prototype() const
|
||||
{
|
||||
return prototype_;
|
||||
}
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ statement session::prepare(query_context q) const
|
||||
return c->prepare(std::move(q));
|
||||
}
|
||||
|
||||
record session::describe_table(const std::string &table_name) const
|
||||
std::vector<sql::column_definition> session::describe_table(const std::string &table_name) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
|
||||
Reference in New Issue
Block a user