Compare commits

..

2 Commits

3 changed files with 58 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "catch2/catch_test_macros.hpp" #include "catch2/catch_test_macros.hpp"
#include "matador/query/query.hpp" #include "matador/query/query.hpp"
#include "matador/query/sequence_pk_generator.hpp"
#include "SequenceFixture.hpp" #include "SequenceFixture.hpp"
@ -34,3 +35,27 @@ TEST_CASE_METHOD(SequenceFixture, "test create and drop sequence", "[sequence][c
.execute(db); .execute(db);
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
} }
TEST_CASE_METHOD(SequenceFixture, "Test nextval and currval through sequence pk generator", "[sequence][nextval][currval]") {
auto result = query::create()
.sequence("person_seq")
.execute(db);
REQUIRE(result.is_ok());
sequences_to_drop.emplace("person_seq");
sequence_pk_generator generator("person_seq");
auto pk_result = generator.next_id(db);
REQUIRE(pk_result.is_ok());
REQUIRE(*pk_result == 1);
pk_result = generator.current_id(db);
REQUIRE(pk_result.is_ok());
REQUIRE(*pk_result == 1);
result = query::drop()
.sequence("person_seq")
.execute(db);
REQUIRE(result.is_ok());
}

View File

@ -4,6 +4,7 @@
#include "matador/query/expression/expression_operators.hpp" #include "matador/query/expression/expression_operators.hpp"
#include "matador/query/query.hpp" #include "matador/query/query.hpp"
#include "matador/query/table_column.hpp" #include "matador/query/table_column.hpp"
#include "matador/query/table_pk_generator.hpp"
#include "TableSequenceFixture.hpp" #include "TableSequenceFixture.hpp"
@ -38,3 +39,23 @@ TEST_CASE_METHOD(TableSequenceFixture, "test create and drop table sequence", "[
REQUIRE(id_result->has_value()); REQUIRE(id_result->has_value());
REQUIRE(id_result->value() == 2); REQUIRE(id_result->value() == 2);
} }
TEST_CASE_METHOD(TableSequenceFixture, "Test nextval and currval through table sequence pk generator", "[table_sequence][nextval][currval]" ) {
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_pk_generator generator(sequence_table_name, "test_seq");
auto pk_result = generator.next_id(db);
REQUIRE(pk_result.is_ok());
REQUIRE(*pk_result == 1);
pk_result = generator.current_id(db);
REQUIRE(pk_result.is_ok());
REQUIRE(*pk_result == 2);
}

12
todo.md
View File

@ -17,6 +17,18 @@ Order of next steps:
4. Finish `insert_query_builder` 4. Finish `insert_query_builder`
- collect all inserts for entities with relations - collect all inserts for entities with relations
- tests - tests
- cascade_type: `Insert`
- has_many <-> belongs_to: set root id into the join column of elements
- belongs_to <-> has_many: ?
- belongs_to <-> has_one: set root id into the foreign object
- has_one <-> belongs_to: set root id into the foreign object
- has_many_to_many: insert relation table entry
if generator type `manual` => extract pk before insert
=>
if generator type `sequence` or `table` => generate pk before insert
if generator type `identity` => extract pk after insert and before insert of relation objects
5. Finish `session::insert` method 5. Finish `session::insert` method
- use `insert_query_builder` - use `insert_query_builder`
- correct handling of pk generator - correct handling of pk generator