has many generator progress
This commit is contained in:
parent
1a22c70320
commit
96c6c78cb8
|
|
@ -56,7 +56,7 @@ private:
|
||||||
struct insert_step {
|
struct insert_step {
|
||||||
sql::query_context ctx;
|
sql::query_context ctx;
|
||||||
|
|
||||||
utils::result<sql::statement, utils::error> acquire_and_bind(sql::statement_cache &cache) const;
|
utils::result<void, utils::error> acquire_and_bind(sql::statement &stmt) const;
|
||||||
|
|
||||||
// Session uses these to handle manual/sequence/table pre-insert PKs
|
// Session uses these to handle manual/sequence/table pre-insert PKs
|
||||||
utils::generator_type pk_generator{utils::generator_type::Manual};
|
utils::generator_type pk_generator{utils::generator_type::Manual};
|
||||||
|
|
@ -300,7 +300,9 @@ private:
|
||||||
return utils::failure(utils::error{error_code::UnknownType, "Unknown type"});
|
return utils::failure(utils::error{error_code::UnknownType, "Unknown type"});
|
||||||
}
|
}
|
||||||
step.ctx = cit->second.insert;
|
step.ctx = cit->second.insert;
|
||||||
step.bind_object = [ptr](sql::statement &stmt) { stmt.bind(*ptr); };
|
step.bind_object = [ptr](sql::statement &stmt) {
|
||||||
|
stmt.bind(*ptr);
|
||||||
|
};
|
||||||
if (info.has_primary_key() && step.pk_generator == utils::generator_type::Identity) {
|
if (info.has_primary_key() && step.pk_generator == utils::generator_type::Identity) {
|
||||||
const auto pk_name = info.primary_key_attribute()->name();
|
const auto pk_name = info.primary_key_attribute()->name();
|
||||||
const table_column pk_col(&it->second.table(), pk_name);
|
const table_column pk_col(&it->second.table(), pk_name);
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
|
||||||
return utils::failure(stmt.err());
|
return utils::failure(stmt.err());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = step.acquire_and_bind(cache_);
|
if (auto result = step.acquire_and_bind(*stmt); !result.is_ok()) {
|
||||||
if (!result.is_ok()) {
|
|
||||||
return utils::failure(result.err());
|
return utils::failure(result.err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,7 +126,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
|
||||||
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to insert object and retrieve identity."));
|
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to insert object and retrieve identity."));
|
||||||
}
|
}
|
||||||
step.apply_returning(*record.value());
|
step.apply_returning(*record.value());
|
||||||
} else if (const auto exec_result = result->execute(); !exec_result.is_ok()) {
|
} else if (const auto exec_result = stmt->execute(); !exec_result.is_ok()) {
|
||||||
return utils::failure(exec_result.err());
|
return utils::failure(exec_result.err());
|
||||||
}
|
}
|
||||||
step.make_object_persistent();
|
step.make_object_persistent();
|
||||||
|
|
@ -157,9 +156,7 @@ public:
|
||||||
stmt_.bind(binding_position_, x);
|
stmt_.bind(binding_position_, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {
|
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
|
||||||
}
|
|
||||||
|
|
||||||
template<class Type>
|
template<class Type>
|
||||||
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/) {}
|
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/) {}
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
|
|
@ -257,8 +254,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
||||||
select_query_builder eqb(schema_);
|
select_query_builder eqb(schema_);
|
||||||
auto data = eqb.build<Type>(*it->second.table().primary_key_column() == pk);
|
auto data = eqb.build<Type>(*it->second.table().primary_key_column() == pk);
|
||||||
if (!data.is_ok()) {
|
if (!data.is_ok()) {
|
||||||
return utils::failure(make_error(error_code::FailedToBuildQuery,
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + it->second.name() + "."));
|
||||||
"Failed to build query for type " + it->second.name() + "."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ctx = data->compile(dialect_);
|
auto ctx = data->compile(dialect_);
|
||||||
|
|
@ -272,7 +268,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<sql::query_result<Type>, utils::error> session::find(query::criteria_ptr clause) {
|
utils::result<sql::query_result<Type>, utils::error> session::find(criteria_ptr clause) {
|
||||||
const auto it = schema_.find(typeid(Type));
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (it == schema_.end()) {
|
if (it == schema_.end()) {
|
||||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ public:
|
||||||
static void on_base(const BaseType&) {}
|
static void on_base(const BaseType&) {}
|
||||||
template<typename PrimaryKeyType>
|
template<typename PrimaryKeyType>
|
||||||
void on_primary_key(const char * /*id*/, PrimaryKeyType &pk, const primary_key_attribute & = DefaultPkAttributes) {
|
void on_primary_key(const char * /*id*/, PrimaryKeyType &pk, const primary_key_attribute & = DefaultPkAttributes) {
|
||||||
const auto value = pk_.as<PrimaryKeyType>();
|
const auto value = pk_.convert<PrimaryKeyType>();
|
||||||
if (!value) {
|
if (!value) {
|
||||||
// Todo: throw error
|
// Todo: throw error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,10 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
const primary_key_attribute DefaultPkAttributes {};
|
const primary_key_attribute DefaultPkAttributes {};
|
||||||
|
const primary_key_attribute Manual {generator_type::Manual};
|
||||||
const primary_key_attribute Identity {generator_type::Identity};
|
const primary_key_attribute Identity {generator_type::Identity};
|
||||||
|
const primary_key_attribute Sequence {generator_type::Sequence};
|
||||||
|
const primary_key_attribute Table {generator_type::Table};
|
||||||
const primary_key_attribute ManualVarChar63 {63};
|
const primary_key_attribute ManualVarChar63 {63};
|
||||||
const primary_key_attribute ManualVarChar127 {127};
|
const primary_key_attribute ManualVarChar127 {127};
|
||||||
const primary_key_attribute ManualVarChar255 {255};
|
const primary_key_attribute ManualVarChar255 {255};
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,9 @@
|
||||||
#include "matador/sql/statement_cache.hpp"
|
#include "matador/sql/statement_cache.hpp"
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
utils::result<sql::statement, utils::error> insert_step::acquire_and_bind(sql::statement_cache &cache) const {
|
utils::result<void, utils::error> insert_step::acquire_and_bind(sql::statement &stmt) const {
|
||||||
auto result = cache.acquire(ctx);
|
bind_object(stmt);
|
||||||
if (!result.is_ok()) {
|
|
||||||
return utils::failure(result.err());
|
|
||||||
}
|
|
||||||
|
|
||||||
bind_object(*result);
|
return utils::ok<void>();
|
||||||
|
|
||||||
return utils::ok(std::move(*result));
|
|
||||||
}
|
}
|
||||||
} // namespace matador::query
|
} // namespace matador::query
|
||||||
|
|
@ -55,10 +55,18 @@ session::session(session_context&& ctx, const basic_schema &scm)
|
||||||
}
|
}
|
||||||
columns.push_back(col);
|
columns.push_back(col);
|
||||||
}
|
}
|
||||||
queries.insert = query::insert()
|
if (node.pk_generator().type() == utils::generator_type::Identity) {
|
||||||
.into(node.name(), columns)
|
queries.insert = query::insert()
|
||||||
.values(generator::placeholders(columns.size()))
|
.into(node.name(), columns)
|
||||||
.compile(dialect_);
|
.values(generator::placeholders(columns.size()))
|
||||||
|
.returning(node.table().primary_key_column()->as(node.table().primary_key_column()->column_name()))
|
||||||
|
.compile(dialect_);
|
||||||
|
} else {
|
||||||
|
queries.insert = query::insert()
|
||||||
|
.into(node.name(), columns)
|
||||||
|
.values(generator::placeholders(columns.size()))
|
||||||
|
.compile(dialect_);
|
||||||
|
}
|
||||||
|
|
||||||
queries.insert.resolver = resolver_service_;
|
queries.insert.resolver = resolver_service_;
|
||||||
queries.update_one.resolver = resolver_service_;
|
queries.update_one.resolver = resolver_service_;
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,14 @@ using namespace matador::object;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
||||||
namespace matador::test {
|
namespace matador::test {
|
||||||
|
template<const utils::primary_key_attribute &PkAttribute>
|
||||||
struct book_identity;
|
struct book_identity;
|
||||||
|
|
||||||
|
template<const utils::primary_key_attribute &PkAttribute = utils::Identity>
|
||||||
struct author_identity {
|
struct author_identity {
|
||||||
unsigned int id{};
|
unsigned int id{};
|
||||||
std::string name;
|
std::string name;
|
||||||
collection<object_ptr<book_identity> > books;
|
collection<object_ptr<book_identity<PkAttribute>>> books;
|
||||||
|
|
||||||
author_identity() = default;
|
author_identity() = default;
|
||||||
explicit author_identity(std::string name)
|
explicit author_identity(std::string name)
|
||||||
|
|
@ -28,31 +31,38 @@ struct author_identity {
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process(Operator &op) {
|
void process(Operator &op) {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
field::primary_key(op, "id", id, utils::Identity);
|
field::primary_key(op, "id", id, PkAttribute);
|
||||||
field::attribute(op, "name", name, VarChar63);
|
field::attribute(op, "name", name, VarChar63);
|
||||||
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<const utils::primary_key_attribute &PkAttribute = utils::Identity>
|
||||||
struct book_identity {
|
struct book_identity {
|
||||||
unsigned int id{};
|
unsigned int id{};
|
||||||
std::string title;
|
std::string title;
|
||||||
object_ptr<author_identity> book_author;
|
object_ptr<author_identity<PkAttribute>> book_author;
|
||||||
unsigned short published_in{};
|
unsigned short published_in{};
|
||||||
|
|
||||||
book_identity() = default;
|
book_identity() = default;
|
||||||
book_identity(std::string title, object_ptr<author_identity> author, const unsigned short published_in)
|
book_identity(std::string title, object_ptr<author_identity<PkAttribute>> author, const unsigned short published_in)
|
||||||
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
|
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process(Operator &op) {
|
void process(Operator &op) {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
field::primary_key(op, "id", id, utils::Identity);
|
field::primary_key(op, "id", id, PkAttribute);
|
||||||
field::attribute(op, "title", title, VarChar511);
|
field::attribute(op, "title", title, VarChar511);
|
||||||
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
|
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
|
||||||
field::attribute(op, "published_in", published_in);
|
field::attribute(op, "published_in", published_in);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using author_sequence = author_identity<utils::Sequence>;
|
||||||
|
using author_table = author_identity<utils::Table>;
|
||||||
|
using book_sequence = book_identity<utils::Sequence>;
|
||||||
|
using book_table = book_identity<utils::Table>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") {
|
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") {
|
||||||
|
|
@ -76,20 +86,40 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") {
|
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") {
|
||||||
const auto result = schema.attach<book_identity>("books")
|
const auto result = schema.attach<book_identity<>>("books")
|
||||||
.and_then( [this] { return schema.attach<author_identity>("authors"); } )
|
.and_then( [this] { return schema.attach<author_identity<>>("authors"); } )
|
||||||
.and_then([this] { return schema.create(db); } );
|
.and_then([this] { return schema.create(db); } );
|
||||||
REQUIRE(result.is_ok());
|
REQUIRE(result.is_ok());
|
||||||
|
|
||||||
session ses({bus, connection::dns, 4}, schema);
|
session ses({bus, connection::dns, 4}, schema);
|
||||||
|
|
||||||
auto s_king = make_object<author_identity>("Steven King");
|
auto s_king = make_object<author_identity<>>("Steven King");
|
||||||
|
|
||||||
s_king->books.push_back(make_object<book_identity>("Carrie", nullobj, 1974));
|
s_king->books.push_back(make_object<book_identity<>>("Carrie", nullobj, 1974));
|
||||||
s_king->books.push_back(make_object<book_identity>("The Shining", nullobj, 1977));
|
s_king->books.push_back(make_object<book_identity<>>("The Shining", nullobj, 1977));
|
||||||
s_king->books.push_back(make_object<book_identity>("It", nullobj, 1986));
|
s_king->books.push_back(make_object<book_identity<>>("It", nullobj, 1986));
|
||||||
s_king->books.push_back(make_object<book_identity>("Misery", nullobj, 1987));
|
s_king->books.push_back(make_object<book_identity<>>("Misery", nullobj, 1987));
|
||||||
s_king->books.push_back(make_object<book_identity>("The Dark Tower: The Gunslinger", nullobj, 1982));
|
s_king->books.push_back(make_object<book_identity<>>("The Dark Tower: The Gunslinger", nullobj, 1982));
|
||||||
|
|
||||||
|
auto res = ses.insert(s_king);
|
||||||
|
REQUIRE(res.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with sequence", "[session][insert][has_many][sequence]") {
|
||||||
|
const auto result = schema.attach<book_sequence>("books")
|
||||||
|
.and_then( [this] { return schema.attach<author_sequence>("authors"); } )
|
||||||
|
.and_then([this] { return schema.create(db); } );
|
||||||
|
REQUIRE(result.is_ok());
|
||||||
|
|
||||||
|
session ses({bus, connection::dns, 4}, schema);
|
||||||
|
|
||||||
|
auto s_king = make_object<author_sequence>("Steven King");
|
||||||
|
|
||||||
|
s_king->books.push_back(make_object<book_sequence>("Carrie", nullobj, 1974));
|
||||||
|
s_king->books.push_back(make_object<book_sequence>("The Shining", nullobj, 1977));
|
||||||
|
s_king->books.push_back(make_object<book_sequence>("It", nullobj, 1986));
|
||||||
|
s_king->books.push_back(make_object<book_sequence>("Misery", nullobj, 1987));
|
||||||
|
s_king->books.push_back(make_object<book_sequence>("The Dark Tower: The Gunslinger", nullobj, 1982));
|
||||||
|
|
||||||
auto res = ses.insert(s_king);
|
auto res = ses.insert(s_king);
|
||||||
REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue