Compare commits
No commits in common. "905ee019d7465d89fcd23bea62c95eac542b1c14" and "6423bd505b2249293d8979da73a20af2219b968c" have entirely different histories.
905ee019d7
...
6423bd505b
|
|
@ -37,9 +37,6 @@ set(TEST_SOURCES
|
|||
../../../test/backends/SequenceFixture.hpp
|
||||
../../../test/backends/SequenceFixture.cpp
|
||||
../../../test/backends/SequenceTest.cpp
|
||||
../../../test/backends/TableSequenceFixture.hpp
|
||||
../../../test/backends/TableSequenceFixture.cpp
|
||||
../../../test/backends/TableSequenceTest.cpp
|
||||
)
|
||||
|
||||
set(LIBRARY_TEST_TARGET PostgresTests)
|
||||
|
|
|
|||
|
|
@ -52,7 +52,6 @@ struct LoginHistory : core::Model {
|
|||
template<typename Operator>
|
||||
void process( Operator& op ) {
|
||||
namespace field = matador::access;
|
||||
field::process_base<Model>( op, *this );
|
||||
field::process( op, *matador::base_class<Model>( this ) );
|
||||
field::belongs_to( op, "client", client, matador::utils::CascadeAllFetchLazy );
|
||||
field::belongs_to( op, "scenario", scenario, matador::utils::CascadeAllFetchLazy );
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
namespace matador::query {
|
||||
|
||||
class table;
|
||||
class column_expression;
|
||||
|
||||
// ReSharper disable CppNonExplicitConvertingConstructor
|
||||
class table_column {
|
||||
|
|
@ -36,7 +35,6 @@ public:
|
|||
|
||||
|
||||
table_column(const std::shared_ptr<abstract_column_expression>& expression) noexcept;
|
||||
table_column(column_expression&& expression) noexcept;
|
||||
table_column& operator=(const table_column& other);
|
||||
table_column(const table_column& other) = default;
|
||||
table_column(table_column&& other) noexcept = default;
|
||||
|
|
|
|||
|
|
@ -25,20 +25,6 @@ void process(Operator &op, const Type &object) {
|
|||
process(op, const_cast<Type &>(object));
|
||||
}
|
||||
|
||||
template<class Base, class Derived, class Operator>
|
||||
void process_base(Operator &op, const Derived &object) {
|
||||
static_assert(!std::is_same_v<Base, Derived>, "class Base must not be of same type as class Derived");
|
||||
static_assert(std::is_base_of_v<Base, Derived>, "class Base must be base of class Derived");
|
||||
process(op, static_cast<const Base&>(object));
|
||||
}
|
||||
|
||||
template<class Base, class Derived, class Operator>
|
||||
void process_base(Operator &op, Derived &object) {
|
||||
static_assert(!std::is_same_v<Base, Derived>, "class Base must not be of same type as class Derived");
|
||||
static_assert(std::is_base_of_v<Base, Derived>, "class Base must be base of class Derived");
|
||||
process(op, static_cast<Base&>(object));
|
||||
}
|
||||
|
||||
template< class Operator, class Type >
|
||||
void primary_key(Operator &op, const char *id, Type &value, const utils::primary_key_attribute &attr) {
|
||||
op.on_primary_key(id, value, attr);
|
||||
|
|
|
|||
|
|
@ -8,31 +8,33 @@ namespace matador {
|
|||
/**
|
||||
* @brief Safely casts a given derived class to its base class
|
||||
*
|
||||
* @tparam Base The base class type
|
||||
* @tparam Derived The class type of the derived class
|
||||
* @tparam B The base class type
|
||||
* @tparam D The class type of the derived class
|
||||
* @param derived The derived object
|
||||
* @return The cast object
|
||||
*/
|
||||
template < class Base, class Derived>
|
||||
const Base* base_class(const Derived *derived) {
|
||||
static_assert(!std::is_same_v<Base, Derived>, "class Base must not be of same type as class Derived");
|
||||
static_assert(std::is_base_of_v<Base, Derived>, "class Base must be base of class Derived");
|
||||
return static_cast<const Base*>(derived);
|
||||
template < class B, class D>
|
||||
const B* base_class(const D *derived)
|
||||
{
|
||||
static_assert(!std::is_same_v<B, D>, "class B must not be of same type as class D");
|
||||
static_assert(std::is_base_of_v<B, D>, "class B must be base of class D");
|
||||
return static_cast<const B*>(derived);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Safely casts a given derived class to its base class
|
||||
*
|
||||
* @tparam Base The base class type
|
||||
* @tparam Derived The class type of the derived class
|
||||
* @tparam B The base class type
|
||||
* @tparam D The class type of the derived class
|
||||
* @param derived The derived object
|
||||
* @return The cast object
|
||||
*/
|
||||
template < class Base, class Derived>
|
||||
Base* base_class(Derived *derived) {
|
||||
static_assert(!std::is_same_v<Base, Derived>, "class Base must not be of same type as class Derived");
|
||||
static_assert(std::is_base_of_v<Base, Derived>, "class Base must be base of class Derived");
|
||||
return static_cast<Base*>(derived);
|
||||
template < class B, class D>
|
||||
B* base_class(D *derived)
|
||||
{
|
||||
static_assert(!std::is_same_v<B, D>, "class B must not be of same type as class D");
|
||||
static_assert(std::is_base_of_v<B, D>, "class B must be base of class D");
|
||||
return static_cast<B*>(derived);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,105 +0,0 @@
|
|||
#ifndef MATADOR_PRIMARY_KEY_ACCESSOR_HPP
|
||||
#define MATADOR_PRIMARY_KEY_ACCESSOR_HPP
|
||||
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace matador::utils {
|
||||
class foreign_attributes;
|
||||
namespace detail {
|
||||
template < typename PrimaryKeyType >
|
||||
class primary_key_setter {
|
||||
public:
|
||||
explicit primary_key_setter(const std::string &name, const PrimaryKeyType& value)
|
||||
: name_(name)
|
||||
, value_(value){}
|
||||
|
||||
void on_primary_key(const char *id, PrimaryKeyType &pk, const utils::primary_key_attribute & = utils::default_pk_attributes) {
|
||||
if (id != nullptr && name_ == id) {
|
||||
pk = static_cast<PrimaryKeyType>(value_);
|
||||
}
|
||||
}
|
||||
template<typename ValueType>
|
||||
static void on_primary_key(const char * /*id*/, ValueType & /*pk*/, const utils::primary_key_attribute & = utils::default_pk_attributes) {}
|
||||
static void on_revision(const char * /*id*/, uint64_t & /*rev*/) {}
|
||||
template<typename T>
|
||||
static void on_attribute(const char * /*id*/, T &, const utils::field_attributes & = utils::null_attributes) {}
|
||||
template<class P>
|
||||
static void on_belongs_to(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class P>
|
||||
static void on_has_one(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many(const char * /*id*/, C &, const char * /*join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
|
||||
|
||||
private:
|
||||
const std::string &name_;
|
||||
PrimaryKeyType value_{};
|
||||
};
|
||||
|
||||
struct pk_unset_checker {
|
||||
const std::string &name;
|
||||
bool unset{true};
|
||||
|
||||
template<class V>
|
||||
void on_primary_key(const char *id, V &pk, const utils::primary_key_attribute & = utils::default_pk_attributes) {
|
||||
if (id != nullptr && name == id) {
|
||||
// Your convention: 0 means unset for integer PKs
|
||||
unset = (static_cast<std::uint64_t>(pk) == 0ULL);
|
||||
}
|
||||
}
|
||||
static void on_revision(const char * /*id*/, uint64_t & /*rev*/) {}
|
||||
template<typename T>
|
||||
static void on_attribute(const char * /*id*/, T &, const utils::field_attributes & = utils::null_attributes) {}
|
||||
template<class P>
|
||||
static void on_belongs_to(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class P>
|
||||
static void on_has_one(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many(const char * /*id*/, C &, const char * /*join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
|
||||
};
|
||||
|
||||
struct pk_value_extractor {
|
||||
std::uint64_t value{0};
|
||||
|
||||
template<class V>
|
||||
void on_primary_key(const char * /*id*/, V &pk, const utils::primary_key_attribute & = utils::default_pk_attributes) {
|
||||
value = static_cast<std::uint64_t>(pk);
|
||||
}
|
||||
static void on_revision(const char * /*id*/, uint64_t & /*rev*/) {}
|
||||
template<typename T>
|
||||
static void on_attribute(const char * /*id*/, T &, const utils::field_attributes & = utils::null_attributes) {}
|
||||
template<class P>
|
||||
static void on_belongs_to(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class P>
|
||||
static void on_has_one(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many(const char * /*id*/, C &, const char * /*join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes & ) {}
|
||||
template<class C>
|
||||
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename PrimaryKeyType>
|
||||
class primary_key_accessor {
|
||||
public:
|
||||
|
||||
PrimaryKeyType get() const;
|
||||
PrimaryKeyType
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_PRIMARY_KEY_ACCESSOR_HPP
|
||||
|
|
@ -124,7 +124,6 @@ add_library(matador-core STATIC
|
|||
object/collection_utils.cpp
|
||||
../../include/matador/object/pk_field_locator.hpp
|
||||
../../include/matador/object/object_cache.hpp
|
||||
../../include/matador/utils/primary_key_accessor.hpp
|
||||
)
|
||||
|
||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||
|
|
|
|||
|
|
@ -17,10 +17,11 @@
|
|||
#include "matador/sql/dialect.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
sql::query_context query_builder::compile(const query_data &data,
|
||||
const sql::dialect &d,
|
||||
const std::optional<std::reference_wrapper<const sql::connection_impl>>
|
||||
conn) {
|
||||
const std::optional<std::reference_wrapper<const sql::connection_impl>> conn)
|
||||
{
|
||||
data_ = &data;
|
||||
dialect_ = &d;
|
||||
connection_ = conn;
|
||||
|
|
@ -207,28 +208,23 @@ void query_builder::visit(internal::query_into_part& part) {
|
|||
query_.sql += ")"/* + result*/;
|
||||
}
|
||||
|
||||
struct value_visitor
|
||||
{
|
||||
struct value_visitor {
|
||||
value_visitor(attribute_string_writer &w, sql::query_context &ctx)
|
||||
: value_to_string_visitor(w, ctx) {
|
||||
}
|
||||
: value_to_string_visitor(w, ctx) {}
|
||||
|
||||
void operator()(const utils::database_type &val) {
|
||||
std::visit(value_to_string_visitor, val);
|
||||
}
|
||||
|
||||
}
|
||||
void operator()(const utils::placeholder &/*val*/) {
|
||||
value_to_string_visitor.query.bind_vars.emplace_back(
|
||||
std::string("value_") + std::to_string(value_to_string_visitor.query.bind_vars.size() + 1));
|
||||
value_to_string_visitor.result = value_to_string_visitor.writer->dialect().next_placeholder(
|
||||
value_to_string_visitor.query.bind_vars);
|
||||
value_to_string_visitor.query.bind_vars.emplace_back(std::string("value_") + std::to_string(value_to_string_visitor.query.bind_vars.size() + 1));
|
||||
value_to_string_visitor.result = value_to_string_visitor.writer->dialect().next_placeholder(value_to_string_visitor.query.bind_vars);
|
||||
}
|
||||
|
||||
internal::basic_type_to_string_visitor value_to_string_visitor;
|
||||
};
|
||||
|
||||
std::string query_builder::determine_value(const sql::dialect& d, sql::query_context& ctx,
|
||||
const abstract_column_expression& exp) {
|
||||
std::string query_builder::determine_value(const sql::dialect &d, sql::query_context& ctx, const abstract_column_expression &exp) {
|
||||
attribute_string_writer writer(d, {});
|
||||
expression_evaluator v(d, ctx);
|
||||
exp.accept(v);
|
||||
|
|
@ -236,8 +232,7 @@ std::string query_builder::determine_value(const sql::dialect& d, sql::query_con
|
|||
return v.result();
|
||||
}
|
||||
|
||||
std::string query_builder::determine_value(value_visitor& visitor,
|
||||
const std::variant<utils::placeholder, utils::database_type>& val) {
|
||||
std::string query_builder::determine_value(value_visitor &visitor, const std::variant<utils::placeholder, utils::database_type> &val) {
|
||||
std::visit(visitor, val);
|
||||
return visitor.value_to_string_visitor.result;
|
||||
}
|
||||
|
|
@ -286,12 +281,14 @@ void query_builder::visit(internal::query_delete_part&/*delete_part*/) {
|
|||
query_.sql = dialect_->remove();
|
||||
}
|
||||
|
||||
void query_builder::visit(internal::query_delete_from_part& part) {
|
||||
void query_builder::visit(internal::query_delete_from_part &part)
|
||||
{
|
||||
query_.table_name = part.table().name();
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
||||
}
|
||||
|
||||
void query_builder::visit(internal::query_create_part&/*create_part*/) {
|
||||
void query_builder::visit(internal::query_create_part &/*create_part*/)
|
||||
{
|
||||
query_.command = sql::sql_command::CreateTable;
|
||||
query_.sql = dialect_->create();
|
||||
}
|
||||
|
|
@ -299,7 +296,8 @@ void query_builder::visit(internal::query_create_part&/*create_part*/) {
|
|||
void build_create_column(std::string &out, const table_column &col, const sql::dialect &d);
|
||||
std::string build_constraint(const table_constraint &cons, const sql::dialect &d);
|
||||
|
||||
void query_builder::visit(internal::query_create_table_part& part) {
|
||||
void query_builder::visit(internal::query_create_table_part &part)
|
||||
{
|
||||
query_.sql += " " + dialect_->table() + " " + dialect_->prepare_identifier_string(part.table().name()) + " (";
|
||||
query_.table_name = part.table().name();
|
||||
|
||||
|
|
@ -498,9 +496,7 @@ std::string query_builder::build_add_constraint_string(const table_constraint& c
|
|||
if (c.is_primary_key_constraint()) {
|
||||
result.append(dialect_->primary_key()).append(" (").append(c.column_name()).append(")");
|
||||
} else if (c.is_foreign_key_constraint()) {
|
||||
result.append(dialect_->foreign_key()).append(" (").append(c.column_name()).append(") ").
|
||||
append(dialect_->references()).append(" ").append(c.referenced_table()).append(" (").append(
|
||||
c.referenced_column()).append(")");
|
||||
result.append(dialect_->foreign_key()).append(" (").append(c.column_name()).append(") ").append(dialect_->references()).append(" ").append(c.referenced_table()).append(" (").append(c.referenced_column()).append(")");
|
||||
} else if (c.is_unique_constraint()) {
|
||||
result.append(dialect_->unique()).append(" (").append(c.column_name()).append(")");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ void prepare_column(sql::query_context& ctx, const sql::dialect& d, const table_
|
|||
attribute_string_writer writer(d, {});
|
||||
expression_evaluator v(d, ctx);
|
||||
col.expression()->accept(v);
|
||||
ctx.sql += v.result();
|
||||
|
||||
if (col.has_alias()) {
|
||||
ctx.sql.append(" ").append(d.as()).append(" ").append(col.alias());
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@
|
|||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include "matador/query/expression/column_expression.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
table_column operator ""_col(const char *name, const size_t len) {
|
||||
|
|
@ -69,10 +67,6 @@ table_column::table_column(const class table* tab,
|
|||
table_column::table_column(const std::shared_ptr<abstract_column_expression>& expression) noexcept
|
||||
: table_column(nullptr, "", "", utils::basic_type::Unknown, {}, sql::sql_function_t::None, expression) {}
|
||||
|
||||
table_column::table_column(column_expression&& expression) noexcept
|
||||
: table_column(std::shared_ptr(expression.release())) {
|
||||
}
|
||||
|
||||
table_column & table_column::operator=(const table_column &other) {
|
||||
if (this == &other) {
|
||||
return *this;
|
||||
|
|
@ -101,7 +95,7 @@ bool table_column::equals(const table_column &x) const {
|
|||
}
|
||||
|
||||
table_column table_column::as(const std::string& alias) const {
|
||||
return {table_, column_name_, alias, type_, attributes_, function_, expression_};
|
||||
return {table_, column_name_, alias, type_, attributes_, function_};
|
||||
}
|
||||
|
||||
const std::string& table_column::name() const {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef MATADOR_SEQUENCE_FIXTURE_HPP
|
||||
#define MATADOR_SEQUENCE_FIXTURE_HPP
|
||||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include <stack>
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
#include "TableSequenceFixture.hpp"
|
||||
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/builder.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
TableSequenceFixture::TableSequenceFixture()
|
||||
: db(connection::dns) {
|
||||
REQUIRE(db.open());
|
||||
|
||||
REQUIRE(query::query::create()
|
||||
.table(sequence_table_name)
|
||||
.columns({
|
||||
query::column("name", utils::basic_type::Varchar, 255),
|
||||
query::column("next_id", utils::basic_type::Int64)
|
||||
})
|
||||
.execute(db));
|
||||
}
|
||||
|
||||
TableSequenceFixture::~TableSequenceFixture() {
|
||||
REQUIRE(query::query::drop()
|
||||
.table(sequence_table_name)
|
||||
.execute(db));
|
||||
REQUIRE(db.close());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
#ifndef MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
#define MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
class TableSequenceFixture {
|
||||
public:
|
||||
TableSequenceFixture();
|
||||
~TableSequenceFixture();
|
||||
|
||||
protected:
|
||||
sql::connection db;
|
||||
std::string sequence_table_name{"test_seq_table"};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/expression/expression_operators.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/table_column.hpp"
|
||||
|
||||
#include "TableSequenceFixture.hpp"
|
||||
|
||||
using namespace matador::query;
|
||||
using namespace matador::test;
|
||||
|
||||
TEST_CASE_METHOD(TableSequenceFixture, "test create and drop table sequence", "[table_sequence][create][drop]") {
|
||||
const auto next_id_col = "next_id"_col;
|
||||
auto result = query::query::insert()
|
||||
.into(sequence_table_name, { "name", next_id_col})
|
||||
.values({ "test_seq", 1 })
|
||||
.execute(db);
|
||||
REQUIRE(result);
|
||||
REQUIRE(result->affected_rows == 1);
|
||||
|
||||
table_column exp(next_id_col - 1);
|
||||
auto id_result = query::query::update(sequence_table_name)
|
||||
.set(next_id_col, next_id_col + 1)
|
||||
.where("name"_col == "test_seq")
|
||||
.returning((next_id_col - 1).as("id"))
|
||||
.fetch_value<int64_t>(db);
|
||||
|
||||
REQUIRE(id_result);
|
||||
REQUIRE(id_result->has_value());
|
||||
REQUIRE(id_result->value() == 1);
|
||||
|
||||
id_result = query::query::select({next_id_col})
|
||||
.from(sequence_table_name)
|
||||
.where("name"_col == "test_seq")
|
||||
.fetch_value<int64_t>(db);
|
||||
REQUIRE(id_result);
|
||||
REQUIRE(id_result->has_value());
|
||||
REQUIRE(id_result->value() == 2);
|
||||
}
|
||||
Loading…
Reference in New Issue