added more tests
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column_definition::column_definition(const char *name)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
column_definition::column_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)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const size_t index,
|
||||
std::string ref_table,
|
||||
std::string ref_column,
|
||||
const utils::field_attributes& attr, const null_option null_opt)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
, ref_table_(std::move(ref_table))
|
||||
, ref_column_(std::move(ref_column))
|
||||
{}
|
||||
|
||||
const std::string &column_definition::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::string column_definition::full_name() const
|
||||
{
|
||||
return table_ + "." + name_;
|
||||
}
|
||||
|
||||
std::string column_definition::table_name() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
int column_definition::index() const
|
||||
{
|
||||
return index_;
|
||||
}
|
||||
|
||||
const utils::field_attributes &column_definition::attributes() const
|
||||
{
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
bool column_definition::is_nullable() const
|
||||
{
|
||||
return null_option_ == null_option::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type column_definition::type() const
|
||||
{
|
||||
return value_.type();
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_table() const
|
||||
{
|
||||
return ref_table_;
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_column() const
|
||||
{
|
||||
return ref_column_;
|
||||
}
|
||||
|
||||
bool column_definition::is_foreign_reference() const
|
||||
{
|
||||
return !ref_column_.empty() && !ref_table_.empty();
|
||||
}
|
||||
|
||||
bool column_definition::is_integer() const
|
||||
{
|
||||
return value_.is_integer();
|
||||
}
|
||||
|
||||
bool column_definition::is_floating_point() const
|
||||
{
|
||||
return value_.is_floating_point();
|
||||
}
|
||||
|
||||
bool column_definition::is_bool() const
|
||||
{
|
||||
return value_.is_bool();
|
||||
}
|
||||
|
||||
bool column_definition::is_string() const
|
||||
{
|
||||
return value_.is_string();
|
||||
}
|
||||
|
||||
bool column_definition::is_varchar() const
|
||||
{
|
||||
return value_.is_varchar();
|
||||
}
|
||||
|
||||
bool column_definition::is_date() const
|
||||
{
|
||||
return value_.is_date();
|
||||
}
|
||||
|
||||
bool column_definition::is_time() const
|
||||
{
|
||||
return value_.is_time();
|
||||
}
|
||||
|
||||
bool column_definition::is_blob() const
|
||||
{
|
||||
return value_.is_blob();
|
||||
}
|
||||
|
||||
bool column_definition::is_null() const
|
||||
{
|
||||
return value_.is_null();
|
||||
}
|
||||
|
||||
void column_definition::type(utils::basic_type /*type*/)
|
||||
{
|
||||
// type_ = type;
|
||||
// utils::initialize_by_utils::basic_type(type, value_);
|
||||
}
|
||||
|
||||
std::string column_definition::str() const
|
||||
{
|
||||
return value_.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const column_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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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,
|
||||
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,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