added more tests
This commit is contained in:
+20
-11
@@ -1,4 +1,14 @@
|
||||
add_library(matador-core STATIC
|
||||
../../include/matador/object/attribute_definition_generator.hpp
|
||||
../../include/matador/object/attribute_definition.hpp
|
||||
../../include/matador/object/basic_object_info.hpp
|
||||
../../include/matador/object/error_code.hpp
|
||||
../../include/matador/object/many_to_many_relation.hpp
|
||||
../../include/matador/object/object_definition.hpp
|
||||
../../include/matador/object/object_info.hpp
|
||||
../../include/matador/object/schema.hpp
|
||||
../../include/matador/object/schema_node.hpp
|
||||
../../include/matador/object/schema_node_iterator.hpp
|
||||
../../include/matador/utils/access.hpp
|
||||
../../include/matador/utils/attribute_reader.hpp
|
||||
../../include/matador/utils/attribute_writer.hpp
|
||||
@@ -29,9 +39,19 @@ add_library(matador-core STATIC
|
||||
../../include/matador/utils/types.hpp
|
||||
../../include/matador/utils/value.hpp
|
||||
../../include/matador/utils/version.hpp
|
||||
object/attribute_definition_generator.cpp
|
||||
object/attribute_definition.cpp
|
||||
object/basic_object_info.cpp
|
||||
object/error_code.cpp
|
||||
object/object_definition.cpp
|
||||
object/schema.cpp
|
||||
object/schema_node.cpp
|
||||
object/schema_node_iterator.cpp
|
||||
utils/default_type_traits.cpp
|
||||
utils/error.cpp
|
||||
utils/errors.cpp
|
||||
utils/field_attributes.cpp
|
||||
utils/foreign_attributes.cpp
|
||||
utils/identifier.cpp
|
||||
utils/library.cpp
|
||||
utils/os.cpp
|
||||
@@ -39,17 +59,6 @@ add_library(matador-core STATIC
|
||||
utils/types.cpp
|
||||
utils/value.cpp
|
||||
utils/version.cpp
|
||||
utils/errors.cpp
|
||||
../../include/matador/object/basic_object_info.hpp
|
||||
../../include/matador/object/error_code.hpp
|
||||
../../include/matador/object/schema.hpp
|
||||
../../include/matador/object/schema_node.hpp
|
||||
object/error_code.cpp
|
||||
object/schema.cpp
|
||||
object/schema_node.cpp
|
||||
object/basic_object_info.cpp
|
||||
../../include/matador/object/schema_node_iterator.hpp
|
||||
object/schema_node_iterator.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||
|
||||
+32
-32
@@ -1,21 +1,21 @@
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::sql {
|
||||
namespace matador::object {
|
||||
|
||||
column_definition::column_definition(const char *name)
|
||||
attribute_definition::attribute_definition(const char *name)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name)
|
||||
attribute_definition::attribute_definition(std::string name)
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name, const utils::basic_type type, const utils::field_attributes& attr, const null_option null_opt, const size_t index)
|
||||
attribute_definition::attribute_definition(std::string name, const utils::basic_type type, const utils::field_attributes& attr, const null_option null_opt, const size_t index)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
@@ -23,7 +23,7 @@ column_definition::column_definition(std::string name, const utils::basic_type t
|
||||
, value_(type, attr.size())
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name,
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const size_t index,
|
||||
std::string ref_table,
|
||||
@@ -38,137 +38,137 @@ column_definition::column_definition(std::string name,
|
||||
, ref_column_(std::move(ref_column))
|
||||
{}
|
||||
|
||||
const std::string &column_definition::name() const
|
||||
const std::string &attribute_definition::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::string column_definition::full_name() const
|
||||
std::string attribute_definition::full_name() const
|
||||
{
|
||||
return table_ + "." + name_;
|
||||
}
|
||||
|
||||
std::string column_definition::table_name() const
|
||||
std::string attribute_definition::table_name() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
int column_definition::index() const
|
||||
int attribute_definition::index() const
|
||||
{
|
||||
return index_;
|
||||
}
|
||||
|
||||
const utils::field_attributes &column_definition::attributes() const
|
||||
const utils::field_attributes &attribute_definition::attributes() const
|
||||
{
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
bool column_definition::is_nullable() const
|
||||
bool attribute_definition::is_nullable() const
|
||||
{
|
||||
return null_option_ == null_option::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type column_definition::type() const
|
||||
utils::basic_type attribute_definition::type() const
|
||||
{
|
||||
return value_.type();
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_table() const
|
||||
const std::string &attribute_definition::ref_table() const
|
||||
{
|
||||
return ref_table_;
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_column() const
|
||||
const std::string &attribute_definition::ref_column() const
|
||||
{
|
||||
return ref_column_;
|
||||
}
|
||||
|
||||
bool column_definition::is_foreign_reference() const
|
||||
bool attribute_definition::is_foreign_reference() const
|
||||
{
|
||||
return !ref_column_.empty() && !ref_table_.empty();
|
||||
}
|
||||
|
||||
bool column_definition::is_integer() const
|
||||
bool attribute_definition::is_integer() const
|
||||
{
|
||||
return value_.is_integer();
|
||||
}
|
||||
|
||||
bool column_definition::is_floating_point() const
|
||||
bool attribute_definition::is_floating_point() const
|
||||
{
|
||||
return value_.is_floating_point();
|
||||
}
|
||||
|
||||
bool column_definition::is_bool() const
|
||||
bool attribute_definition::is_bool() const
|
||||
{
|
||||
return value_.is_bool();
|
||||
}
|
||||
|
||||
bool column_definition::is_string() const
|
||||
bool attribute_definition::is_string() const
|
||||
{
|
||||
return value_.is_string();
|
||||
}
|
||||
|
||||
bool column_definition::is_varchar() const
|
||||
bool attribute_definition::is_varchar() const
|
||||
{
|
||||
return value_.is_varchar();
|
||||
}
|
||||
|
||||
bool column_definition::is_date() const
|
||||
bool attribute_definition::is_date() const
|
||||
{
|
||||
return value_.is_date();
|
||||
}
|
||||
|
||||
bool column_definition::is_time() const
|
||||
bool attribute_definition::is_time() const
|
||||
{
|
||||
return value_.is_time();
|
||||
}
|
||||
|
||||
bool column_definition::is_blob() const
|
||||
bool attribute_definition::is_blob() const
|
||||
{
|
||||
return value_.is_blob();
|
||||
}
|
||||
|
||||
bool column_definition::is_null() const
|
||||
bool attribute_definition::is_null() const
|
||||
{
|
||||
return value_.is_null();
|
||||
}
|
||||
|
||||
void column_definition::type(utils::basic_type /*type*/)
|
||||
void attribute_definition::type(utils::basic_type /*type*/)
|
||||
{
|
||||
// type_ = type;
|
||||
// utils::initialize_by_utils::basic_type(type, value_);
|
||||
}
|
||||
|
||||
std::string column_definition::str() const
|
||||
std::string attribute_definition::str() const
|
||||
{
|
||||
return value_.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const column_definition &col)
|
||||
std::ostream& operator<<(std::ostream &out, const attribute_definition &col)
|
||||
{
|
||||
out << col.str();
|
||||
return out;
|
||||
}
|
||||
|
||||
column_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, null_option null_opt)
|
||||
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return {name, type, attr, null_opt};
|
||||
}
|
||||
|
||||
template<>
|
||||
column_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt)
|
||||
attribute_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
template<>
|
||||
column_definition make_pk_column<std::string>(const std::string &name, size_t size)
|
||||
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size)
|
||||
{
|
||||
return make_column<std::string>(name, {size, utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
template<>
|
||||
[[maybe_unused]] column_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table,
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table,
|
||||
const std::string &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<std::string>::type(size), 0, ref_table, ref_column, {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL};
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_definition_generator::attribute_definition_generator(std::vector<object::attribute_definition> &columns, const schema &repo)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
{}
|
||||
|
||||
void attribute_definition_generator::on_primary_key(const char *id, std::string &pk, size_t size)
|
||||
{
|
||||
on_attribute(id, pk, { size, utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
void attribute_definition_generator::on_revision(const char *id, unsigned long long int &x)
|
||||
{
|
||||
on_attribute(id, x);
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> attribute_definition_generator::determine_foreign_ref(const std::type_index &ti)
|
||||
{
|
||||
return repo_.reference(ti).value();
|
||||
}
|
||||
|
||||
void fk_attribute_generator::on_primary_key(const char *, std::string &, const size_t size)
|
||||
{
|
||||
type_ = utils::data_type_traits<std::string>::type(size);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,9 +6,10 @@
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
basic_object_info::basic_object_info(schema_node &node, const std::type_index type_index)
|
||||
basic_object_info::basic_object_info(schema_node &node, const std::type_index type_index, object_definition &&definition)
|
||||
: node_(node)
|
||||
, type_index_(type_index) {}
|
||||
, type_index_(type_index)
|
||||
, definition_(std::move(definition)) {}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return type_index_;
|
||||
@@ -17,4 +18,9 @@ std::type_index basic_object_info::type_index() const {
|
||||
std::string basic_object_info::name() const {
|
||||
return node_.name();
|
||||
}
|
||||
|
||||
const object_definition& basic_object_info::definition() const {
|
||||
return definition_;
|
||||
}
|
||||
|
||||
} // namespace matador::object
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
object_definition::object_definition(std::initializer_list<object::attribute_definition> columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const std::vector<object::attribute_definition> &columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const object_definition &x)
|
||||
: columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_)
|
||||
{
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
}
|
||||
|
||||
object_definition &object_definition::operator=(const object_definition &x)
|
||||
{
|
||||
if (&x == this) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool object_definition::has_primary_key() const
|
||||
{
|
||||
return pk_index_ > -1;
|
||||
}
|
||||
|
||||
std::optional<object::attribute_definition> object_definition::primary_key() const
|
||||
{
|
||||
if (!has_primary_key()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
void object_definition::append(attribute_definition col)
|
||||
{
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
}
|
||||
|
||||
const std::vector<object::attribute_definition> &object_definition::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at(const std::string &name) const
|
||||
{
|
||||
return columns_by_name_.at(name).first;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at( const size_t index) const
|
||||
{
|
||||
return columns_.at(index);
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::find(const std::string &column_name) const {
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::end()
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
}
|
||||
|
||||
size_t object_definition::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
bool object_definition::empty() const
|
||||
{
|
||||
return columns_.empty();
|
||||
}
|
||||
|
||||
void object_definition::clear()
|
||||
{
|
||||
columns_.clear();
|
||||
columns_by_name_.clear();
|
||||
}
|
||||
|
||||
void object_definition::init()
|
||||
{
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
add_to_map(col, index++);
|
||||
}
|
||||
}
|
||||
|
||||
void object_definition::add_to_map(attribute_definition &col, size_t index)
|
||||
{
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
pk_index_ = static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,6 +35,10 @@ std::string schema::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
utils::result<std::pair<std::string, std::string>, utils::error> schema::reference( const std::type_index& type_index ) const {
|
||||
return utils::ok(std::pair<std::string, std::string>{});
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent) {
|
||||
if (has_node(node->type_index(), node->name())) {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
schema_node::schema_node(schema &tree)
|
||||
schema_node::schema_node(object::schema &tree)
|
||||
: schema_(tree)
|
||||
, info_(std::make_unique<null_info>(*this))
|
||||
, info_(std::make_unique<null_info>(*this, object_definition{}))
|
||||
{}
|
||||
|
||||
std::string schema_node::name() const {
|
||||
@@ -17,45 +17,8 @@ std::type_index schema_node::type_index() const {
|
||||
return info_->type_index();
|
||||
}
|
||||
|
||||
void schema_node::append(const std::shared_ptr<schema_node> &sibling) {
|
||||
sibling->parent_ = parent_;
|
||||
|
||||
// sibling->previous_sibling_ = this;
|
||||
// sibling->next_sibling_ = next_sibling_;
|
||||
// next_sibling_ = sibling;
|
||||
// sibling->next_sibling_->previous_sibling_ = sibling;
|
||||
// sibling->depth = depth;
|
||||
|
||||
// if (!parent_) {
|
||||
// sibling->op_first = new object_proxy();
|
||||
// sibling->op_last = sibling->op_marker = new object_proxy();
|
||||
// sibling->op_first->link(sibling->op_last);
|
||||
// } else {
|
||||
// throw object_exception("failed to add node as sibling: node has no parent");
|
||||
// }
|
||||
}
|
||||
|
||||
void schema_node::insert(const std::shared_ptr<schema_node> &child) {
|
||||
// child->parent_ = this;
|
||||
// child->previous_sibling_ = last_child_->previous_sibling_;
|
||||
// child->next_sibling_ = last_child_;
|
||||
// last_child_->previous_sibling_->next_sibling_ = child;
|
||||
// last_child_->previous_sibling_ = child;
|
||||
// set depth
|
||||
// child->depth = depth + 1;
|
||||
// set object proxy pointer
|
||||
// 1. first
|
||||
// if (op_first->next() == op_last) {
|
||||
// // node hasn't any serializable (proxy)
|
||||
// child->op_first = op_first;
|
||||
// } else {
|
||||
// // node has some objects (proxy)
|
||||
// child->op_first = op_last->prev();
|
||||
// }
|
||||
// // 2. marker
|
||||
// child->op_marker = op_last;
|
||||
// // 3. last
|
||||
// child->op_last = op_last;
|
||||
const basic_object_info& schema_node::basic_info() const {
|
||||
return *info_;
|
||||
}
|
||||
|
||||
schema_node::node_ptr schema_node::next() const {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
foreign_attributes::foreign_attributes(cascade_type cascade)
|
||||
: cascade_(cascade) {}
|
||||
|
||||
foreign_attributes::foreign_attributes(fetch_type fetch)
|
||||
: fetch_(fetch) {}
|
||||
|
||||
foreign_attributes::foreign_attributes(cascade_type cascade, fetch_type fetch)
|
||||
: cascade_(cascade)
|
||||
, fetch_(fetch ) {}
|
||||
|
||||
cascade_type foreign_attributes::cascade() const
|
||||
{
|
||||
return cascade_;
|
||||
}
|
||||
|
||||
fetch_type foreign_attributes::fetch() const
|
||||
{
|
||||
return fetch_;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,10 +41,11 @@ add_library(matador-orm STATIC
|
||||
../../include/matador/query/query_part.hpp
|
||||
../../include/matador/query/value_extractor.hpp
|
||||
../../include/matador/orm/session.hpp
|
||||
../../include/matador/orm/session_query_builder.hpp
|
||||
../../include/matador/sql/abstract_sql_logger.hpp
|
||||
../../include/matador/sql/backend_provider.hpp
|
||||
../../include/matador/sql/column.hpp
|
||||
../../include/matador/sql/column_definition.hpp
|
||||
../../include/matador/sql/column_generator.hpp
|
||||
../../include/matador/sql/connection.hpp
|
||||
../../include/matador/sql/connection_info.hpp
|
||||
../../include/matador/sql/dialect.hpp
|
||||
@@ -104,9 +105,10 @@ add_library(matador-orm STATIC
|
||||
query/query_update_intermediate.cpp
|
||||
query/value_extractor.cpp
|
||||
orm/session.cpp
|
||||
orm/session_query_builder.cpp
|
||||
sql/backend_provider.cpp
|
||||
sql/column.cpp
|
||||
sql/column_definition.cpp
|
||||
sql/column_generator.cpp
|
||||
sql/connection.cpp
|
||||
sql/connection_info.cpp
|
||||
sql/dialect.cpp
|
||||
|
||||
+35
-28
@@ -11,12 +11,17 @@ session::session(sql::connection_pool<sql::connection> &pool)
|
||||
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
|
||||
, schema_(std::make_unique<object::schema>(dialect_.default_schema_name())){}
|
||||
|
||||
void session::create_schema()
|
||||
{
|
||||
utils::result<void, utils::error> session::create_schema() const {
|
||||
auto c = pool_.acquire();
|
||||
for (const auto &t : *schema_) {
|
||||
c->query(*schema_).create().table(t.second.name, t.second.prototype.columns()).execute();
|
||||
auto result = query::query::create()
|
||||
.table(t.name(), t.basic_info().definition().columns())
|
||||
.execute(*c);
|
||||
if ( !result ) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
void session::drop_table(const std::string &table_name)
|
||||
@@ -26,10 +31,10 @@ void session::drop_table(const std::string &table_name)
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
|
||||
c->query(*schema_).drop().table(table_name).execute();
|
||||
// c->query(*schema_).drop().table(table_name).execute();
|
||||
}
|
||||
|
||||
sql::query_result<sql::record> session::fetch(const query_context &q) const
|
||||
utils::result<sql::query_result<sql::record>, utils::error> session::fetch(const sql::query_context &q) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
@@ -37,16 +42,20 @@ sql::query_result<sql::record> session::fetch(const query_context &q) const
|
||||
}
|
||||
auto it = prototypes_.find(q.table.name);
|
||||
if (it == prototypes_.end()) {
|
||||
it = prototypes_.emplace(q.table.name, c->describe(q.table.name)).first;
|
||||
auto result = c->describe(q.table.name);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
it = prototypes_.emplace(q.table.name, *result).first;
|
||||
}
|
||||
// adjust columns from given query
|
||||
for (auto &col : q.prototype) {
|
||||
if (const auto rit = it->second.find(col.name()); col.type() == utils::basic_type::type_unknown && rit != it->second.end()) {
|
||||
const_cast<sql::column_definition&>(col).type(rit->type());
|
||||
if (const auto rit = it->second.find(col.name()); /*col.type() == utils::basic_type::type_unknown && */rit != it->second.end()) {
|
||||
const_cast<object::attribute_definition&>(col).type(rit->type());
|
||||
}
|
||||
}
|
||||
auto res = c->fetch(q.sql);
|
||||
return sql::query_result<sql::record>{std::move(res), q.prototype};
|
||||
auto res = c->fetch(q);
|
||||
return utils::ok(sql::query_result<sql::record>{std::move(*res)/*, q.prototype*/});
|
||||
}
|
||||
|
||||
//query_result<record> session::fetch(const std::string &sql) const
|
||||
@@ -62,22 +71,22 @@ size_t session::execute(const std::string &sql) const {
|
||||
return c->execute(sql);
|
||||
}
|
||||
|
||||
sql::statement session::prepare(query_context q) const
|
||||
sql::statement session::prepare(const sql::query_context& q) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->prepare(std::move(q));
|
||||
return c->prepare(q).release();
|
||||
}
|
||||
|
||||
std::vector<sql::column_definition> session::describe_table(const std::string &table_name) const
|
||||
std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->describe(table_name);
|
||||
return c->describe(table_name).release();
|
||||
}
|
||||
|
||||
bool session::table_exists(const std::string &table_name) const
|
||||
@@ -89,28 +98,26 @@ bool session::table_exists(const std::string &table_name) const
|
||||
return c->exists(dialect_.default_schema_name(), table_name);
|
||||
}
|
||||
|
||||
const class dialect &session::dialect() const
|
||||
const class sql::dialect &session::dialect() const
|
||||
{
|
||||
return dialect_;
|
||||
}
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> session::fetch(const std::string &sql) const
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->fetch(sql);
|
||||
}
|
||||
// std::unique_ptr<sql::query_result_impl> session::fetch(const std::string &sql) const
|
||||
// {
|
||||
// auto c = pool_.acquire();
|
||||
// if (!c.valid()) {
|
||||
// throw std::logic_error("no database connection available");
|
||||
// }
|
||||
// return c->fetch(sql);
|
||||
// }
|
||||
|
||||
query_select session::build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data) const
|
||||
{
|
||||
return conn->query(*schema_)
|
||||
.select(data.columns)
|
||||
query::fetchable_query session::build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data) {
|
||||
return query::query::select(data.columns)
|
||||
.from(data.root_table_name)
|
||||
.join_left(data.joins)
|
||||
.where(std::move(data.where_clause))
|
||||
.order_by({data.root_table_name, data.pk_column_})
|
||||
.order_by(sql::column{data.root_table_name, data.pk_column_})
|
||||
.asc();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "matador/orm/session_query_builder.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
void session_query_builder::on_primary_key(const char *id, std::string &, size_t)
|
||||
{
|
||||
push(id);
|
||||
if (!is_root_entity()) {
|
||||
const auto b = pk_.is_varchar();
|
||||
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void session_query_builder::on_revision(const char *id, unsigned long long &/*rev*/)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void session_query_builder::push(const std::string &column_name)
|
||||
{
|
||||
char str[4];
|
||||
snprintf(str, 4, "c%02d", ++column_index);
|
||||
entity_query_data_.columns.emplace_back(table_info_stack_.top().get().name(), column_name, str);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool session_query_builder::is_root_entity() const {
|
||||
return table_info_stack_.size() == 1;
|
||||
}
|
||||
|
||||
void session_query_builder::append_join(const sql::column &left, const sql::column &right)
|
||||
{
|
||||
using namespace matador::query;
|
||||
entity_query_data_.joins.push_back({
|
||||
{ right.table_ },
|
||||
make_condition(left == right)
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,12 +9,12 @@ query_create_intermediate::query_create_intermediate()
|
||||
context_->parts.push_back(std::make_unique<internal::query_create_part>());
|
||||
}
|
||||
|
||||
executable_query query_create_intermediate::table(const sql::table &table, const std::initializer_list<sql::column_definition> columns)
|
||||
executable_query query_create_intermediate::table(const sql::table &table, const std::initializer_list<object::attribute_definition> columns)
|
||||
{
|
||||
return this->table(table, std::vector<sql::column_definition>{columns});
|
||||
return this->table(table, std::vector<object::attribute_definition>{columns});
|
||||
}
|
||||
|
||||
executable_query query_create_intermediate::table(const sql::table &table, const std::vector<sql::column_definition> &columns)
|
||||
executable_query query_create_intermediate::table(const sql::table &table, const std::vector<object::attribute_definition> &columns)
|
||||
{
|
||||
context_->parts.push_back(std::make_unique<internal::query_create_table_part>(table, columns));
|
||||
return {context_};
|
||||
|
||||
@@ -254,7 +254,7 @@ void query_create_part::accept(query_part_visitor &visitor)
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_create_table_part::query_create_table_part(sql::table table, std::vector<sql::column_definition> columns)
|
||||
query_create_table_part::query_create_table_part(sql::table table, std::vector<object::attribute_definition> columns)
|
||||
: query_part(sql::dialect_token::TABLE)
|
||||
, table_(std::move(table))
|
||||
, columns_(std::move(columns)) {}
|
||||
@@ -264,7 +264,7 @@ const sql::table &query_create_table_part::table() const
|
||||
return table_;
|
||||
}
|
||||
|
||||
const std::vector<sql::column_definition> &query_create_table_part::columns() const
|
||||
const std::vector<object::attribute_definition> &query_create_table_part::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ void detail::pk_reader::on_primary_key(const char *id, std::string &value, size_
|
||||
utils::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, std::vector<column_definition> &&prototype, const size_t column_index)
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute_definition> &&prototype, const size_t column_index)
|
||||
: column_index_(column_index)
|
||||
, prototype_(std::move(prototype))
|
||||
, reader_(std::move(reader))
|
||||
, pk_reader_(*reader_)
|
||||
{}
|
||||
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<column_definition> &prototype, const size_t column_index)
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute_definition> &prototype, const size_t column_index)
|
||||
: column_index_(column_index)
|
||||
, prototype_(prototype)
|
||||
, reader_(std::move(reader))
|
||||
@@ -53,7 +53,7 @@ query_result_impl::on_attribute(const char *id, utils::value &val, const utils::
|
||||
reader_->read_value(id, column_index_++, val, attr.size());
|
||||
}
|
||||
|
||||
const std::vector<column_definition>& query_result_impl::prototype() const
|
||||
const std::vector<object::attribute_definition>& query_result_impl::prototype() const
|
||||
{
|
||||
return prototype_;
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ struct column_context
|
||||
std::vector<fk_context> foreign_contexts;
|
||||
};
|
||||
|
||||
std::string build_create_column(const sql::column_definition &col, const sql::dialect &d, column_context &context);
|
||||
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context);
|
||||
|
||||
void query_compiler::visit(internal::query_create_table_part &create_table_part)
|
||||
{
|
||||
@@ -324,7 +324,7 @@ void query_compiler::visit(internal::query_drop_table_part &drop_table_part)
|
||||
query_.sql += " " + query_compiler::build_table_name(drop_table_part.token(), *dialect_, query_.table);
|
||||
}
|
||||
|
||||
std::string build_create_column(const sql::column_definition &col, const sql::dialect &d, column_context &context)
|
||||
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context)
|
||||
{
|
||||
std::string result = d.prepare_identifier_string(col.name()) + " " + d.data_type_at(col.type());
|
||||
if (col.attributes().size() > 0) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column_generator::column_generator(std::vector<column> &column_infos,
|
||||
const object::schema &scm,
|
||||
const std::string &table_name,
|
||||
bool force_lazy)
|
||||
: column_infos_(column_infos)
|
||||
, table_schema_(scm)
|
||||
, force_lazy_(force_lazy)
|
||||
{
|
||||
table_name_stack_.push(table_name);
|
||||
}
|
||||
|
||||
void column_generator::on_primary_key(const char *id, std::string &, size_t)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void column_generator::on_revision(const char *id, unsigned long long int &)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void column_generator::push(const std::string &column_name)
|
||||
{
|
||||
char str[4];
|
||||
snprintf(str, 4, "c%02d", ++column_index);
|
||||
column_infos_.emplace_back(table{table_name_stack_.top()}, column_name, str);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -122,7 +122,7 @@ utils::result<void, utils::error> connection::rollback() const {
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
utils::result<std::vector<column_definition>, utils::error> connection::describe(const std::string &table_name) const
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> connection::describe(const std::string &table_name) const
|
||||
{
|
||||
return connection_->describe(table_name);
|
||||
}
|
||||
@@ -143,7 +143,7 @@ utils::result<size_t, utils::error> connection::execute(const std::string &sql)
|
||||
return connection_->execute(sql);
|
||||
}
|
||||
|
||||
bool has_unknown_columns(const std::vector<sql::column_definition> &columns) {
|
||||
bool has_unknown_columns(const std::vector<object::attribute_definition> &columns) {
|
||||
return std::any_of(std::begin(columns), std::end(columns), [](const auto &col) {
|
||||
return col.type() == utils::basic_type::type_null;
|
||||
});
|
||||
@@ -199,7 +199,7 @@ utils::result<statement, utils::error> connection::prepare(const query_context &
|
||||
return value.name() == col.name();
|
||||
});
|
||||
if (col.type() == utils::basic_type::type_null && rit != result->end()) {
|
||||
const_cast<column_definition &>(col).type(rit->type());
|
||||
const_cast<object::attribute_definition&>(col).type(rit->type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
namespace matador::sql::detail {
|
||||
|
||||
template<>
|
||||
record *create_prototype<record>(const std::vector<column_definition> &prototype)
|
||||
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype)
|
||||
{
|
||||
auto result = std::make_unique<record>();
|
||||
for (const auto &col: prototype) {
|
||||
|
||||
@@ -29,7 +29,7 @@ utils::result<size_t, utils::error> statement::execute() const
|
||||
return statement_->execute();
|
||||
}
|
||||
|
||||
//bool is_unknown(const std::vector<sql::column_definition> &columns) {
|
||||
//bool is_unknown(const std::vector<object::column_definition> &columns) {
|
||||
// return std::all_of(std::begin(columns), std::end(columns), [](const auto &col) {
|
||||
// return col.is_unknown();
|
||||
// });
|
||||
|
||||
Reference in New Issue
Block a user