query builder progress

This commit is contained in:
Sascha Kühl
2025-12-02 16:19:13 +01:00
parent c156ab5e74
commit bfbbd4c589
31 changed files with 348 additions and 164 deletions
+12 -12
View File
@@ -22,15 +22,15 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
auto columns = attribute_generator::generate<matador::test::product>(repo, obj);
const std::vector expected_columns = {
attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NOT_NULL },
attribute{"supplier_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NOT_NULL },
attribute{"category_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NOT_NULL },
attribute{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL },
attribute{"unit_price", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
attribute{"units_in_stock", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
attribute{"units_in_order", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
attribute{"reorder_level", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
attribute{"discontinued", basic_type::type_bool, null_attributes, null_option_type::NOT_NULL }
attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NotNull },
attribute{"supplier_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NotNull },
attribute{"category_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NotNull },
attribute{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option_type::NotNull },
attribute{"unit_price", basic_type::type_uint32, null_attributes, null_option_type::NotNull },
attribute{"units_in_stock", basic_type::type_uint32, null_attributes, null_option_type::NotNull },
attribute{"units_in_order", basic_type::type_uint32, null_attributes, null_option_type::NotNull },
attribute{"reorder_level", basic_type::type_uint32, null_attributes, null_option_type::NotNull },
attribute{"discontinued", basic_type::type_bool, null_attributes, null_option_type::NotNull }
};
REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size());
@@ -49,9 +49,9 @@ TEST_CASE("Generate columns from object with nullable columns", "[column generat
auto columns = attribute_generator::generate<matador::test::optional>(repo, obj);
const std::vector expected_columns = {
attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NOT_NULL },
attribute{"name", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL },
attribute{"age", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }
attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NotNull },
attribute{"name", basic_type::type_varchar, null_attributes, null_option_type::NotNull },
attribute{"age", basic_type::type_uint32, null_attributes, null_option_type::NotNull }
};
REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size());
+30 -17
View File
@@ -6,8 +6,6 @@
#include <matador/query/query.hpp>
#include "matador/query/table.hpp"
#include "matador/object/attribute_generator.hpp"
#include "matador/sql/connection.hpp"
#include "matador/utils/placeholder.hpp"
@@ -37,21 +35,32 @@ TEST_CASE_METHOD(QueryFixture, "Test alter table sql statement", "[query][alter]
TEST_CASE_METHOD(QueryFixture, "Test create table sql statement string", "[query]") {
auto result = query::create()
.table({"person"}, {
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
}).str(*db);
.table({"person"})
.columns({
attribute("id", basic_type::type_uint32),
attribute("name", basic_type::type_varchar, 255),
attribute("age", basic_type::type_uint16)
})
.constraints({
constraint("PK_person").primary_key({"id"})
})
.str(*db);
REQUIRE(result == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "age" INTEGER NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
auto ctx = query::create()
.table("person", {
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", {255, constraints::Unique}, null_option_type::NOT_NULL),
make_column<unsigned short>("age"),
make_fk_column<uint32_t>("address", "address", "id")
}).compile(*db);
.table("person")
.columns({
attribute("id", basic_type::type_uint32),
attribute("name", basic_type::type_varchar, 255),
attribute("age", basic_type::type_uint16),
attribute("address", basic_type::type_uint32)
})
.constraints({
constraint("PK_person").primary_key({"id"}),
constraint("FK_person_address").foreign_key({"address"}).references("address", {"id"})
})
.compile(*db);
REQUIRE(ctx.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
REQUIRE(ctx.additional_commands.size() == 1);
@@ -210,10 +219,14 @@ TEST_CASE_METHOD(QueryFixture, "Test select sql statement string with offset and
TEST_CASE_METHOD(QueryFixture, "Test create, insert and select a blob column", "[query][blob]") {
auto result = query::create()
.table("person", {
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<blob>("data")
.table("person")
.columns({
attribute("id", basic_type::type_uint32),
attribute("name", basic_type::type_varchar, 255),
attribute("data", basic_type::type_blob)
})
.constraints({
constraint("PK_person").primary_key({"id"})
})
.str(*db);
+17 -19
View File
@@ -9,9 +9,7 @@ TEST_CASE("Test create empty column", "[column]") {
attribute c("name");
REQUIRE(c.name() == "name");
REQUIRE(c.index() == -1);
REQUIRE(c.type() == basic_type::type_null);
REQUIRE(!c.reference_column());
c.change_type<std::string>(255);
REQUIRE(c.type() == basic_type::type_varchar);
@@ -24,39 +22,39 @@ TEST_CASE("Test copy and move column", "[column]") {
attribute c(
"name",
basic_type::type_varchar,
2,
std::make_shared<attribute>("author", basic_type::type_uint32, "books", attribute_options{constraints::ForeignKey}),
// 2,
// std::make_shared<attribute>("author", basic_type::type_uint32, "books", attribute_options{constraints::ForeignKey}),
{255, constraints::ForeignKey},
null_option_type::NOT_NULL
null_option_type::NotNull
);
REQUIRE(c.name() == "name");
REQUIRE(c.index() == 2);
REQUIRE(c.reference_column());
REQUIRE(c.reference_column()->name() == "author");
REQUIRE(c.reference_column()->table_name() == "books");
// REQUIRE(c.index() == 2);
// REQUIRE(c.reference_column());
// REQUIRE(c.reference_column()->name() == "author");
// REQUIRE(c.reference_column()->table_name() == "books");
REQUIRE(c.type() == basic_type::type_varchar);
REQUIRE(c.attributes().size() == 255);
auto c2 = c;
REQUIRE(c2.name() == "name");
REQUIRE(c2.index() == 2);
REQUIRE(c2.reference_column());
REQUIRE(c2.reference_column()->name() == "author");
REQUIRE(c2.reference_column()->table_name() == "books");
// REQUIRE(c2.index() == 2);
// REQUIRE(c2.reference_column());
// REQUIRE(c2.reference_column()->name() == "author");
// REQUIRE(c2.reference_column()->table_name() == "books");
REQUIRE(c2.type() == basic_type::type_varchar);
REQUIRE(c2.attributes().size() == 255);
auto c3 = std::move(c2);
REQUIRE(c3.name() == "name");
REQUIRE(c3.index() == 2);
REQUIRE(c3.reference_column());
REQUIRE(c3.reference_column()->name() == "author");
REQUIRE(c3.reference_column()->table_name() == "books");
// REQUIRE(c3.index() == 2);
// REQUIRE(c3.reference_column());
// REQUIRE(c3.reference_column()->name() == "author");
// REQUIRE(c3.reference_column()->table_name() == "books");
REQUIRE(c3.type() == basic_type::type_varchar);
REQUIRE(c3.attributes().size() == 255);
REQUIRE(c2.name().empty());
REQUIRE(c2.index() == 2);
REQUIRE(!c2.reference_column());
// REQUIRE(c2.index() == 2);
// REQUIRE(!c2.reference_column());
REQUIRE(c2.attributes().size() == 255);
}