61 lines
1.9 KiB
C++
61 lines
1.9 KiB
C++
#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 "matador/query/table_pk_generator.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 = 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 = 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 = 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);
|
|
}
|
|
|
|
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 = 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);
|
|
} |