query preparations for std::optional

This commit is contained in:
Sascha Kuehl 2024-01-16 22:09:19 +01:00
parent 92eb30767e
commit 2060883d59
8 changed files with 69 additions and 36 deletions

View File

@ -130,10 +130,10 @@ private:
*/
column operator "" _col(const char *name, size_t len);
column make_column(const std::string &name, data_type_t type, utils::field_attributes attr = utils::null_attributes);
column make_column(const std::string &name, data_type_t type, utils::field_attributes attr = utils::not_null_attributes);
template < typename Type >
column make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes)
column make_column(const std::string &name, utils::field_attributes attr = utils::not_null_attributes)
{
return make_column(name, data_type_traits<Type>::builtin_type(0), attr);
}

View File

@ -30,6 +30,7 @@ private:
};
const field_attributes null_attributes {};
const field_attributes not_null_attributes { constraints::NOT_NULL };
}
#endif //QUERY_FIELD_ATTRIBUTES_HPP

View File

@ -5,33 +5,26 @@
namespace matador::sql {
column::column(sql_function_t func, std::string name)
: name_(std::move(name))
, type_(data_type_t::type_int)
, attributes_(utils::null_attributes)
, function_(func) {}
: name_(std::move(name)), type_(data_type_t::type_int), attributes_(utils::null_attributes), function_(func)
{}
column::column(const char *name, std::string alias)
: name_(name)
, attributes_(utils::null_attributes)
, alias_(std::move(alias)) {}
: name_(name), attributes_(utils::null_attributes), alias_(std::move(alias))
{}
column::column(std::string name, std::string alias)
: name_(std::move(name))
, attributes_(utils::null_attributes)
, alias_(std::move(alias)) {}
: name_(std::move(name)), attributes_(utils::null_attributes), alias_(std::move(alias))
{}
column::column(std::string name, data_type_t type, utils::field_attributes attr)
: name_(std::move(name))
, type_(type)
, attributes_(attr) {}
: name_(std::move(name)), type_(type), attributes_(attr)
{}
column::column(std::string name, data_type_t type, size_t index, std::string ref_table, std::string ref_column, utils::field_attributes attr)
: name_(std::move(name))
, index_(index)
, type_(type)
, attributes_(attr)
, ref_table_(std::move(ref_table))
, ref_column_(std::move(ref_column)) {}
column::column(std::string name, data_type_t type, size_t index, std::string ref_table, std::string ref_column,
utils::field_attributes attr)
: name_(std::move(name)), index_(index), type_(type), attributes_(attr), ref_table_(std::move(ref_table)),
ref_column_(std::move(ref_column))
{}
const std::string &column::name() const
{
@ -108,6 +101,9 @@ column make_column( const std::string& name, data_type_t type, utils::field_attr
template<>
column make_column<std::string>(const std::string &name, utils::field_attributes attr)
{
if (attr.options() == utils::constraints::NONE) {
return make_column(name, data_type_traits<std::string>::builtin_type(attr.size()), { attr.size(), utils::constraints::NOT_NULL});
}
return make_column(name, data_type_traits<std::string>::builtin_type(attr.size()), attr);
}
@ -118,7 +114,8 @@ column make_pk_column<std::string>( const std::string& name, size_t size )
}
template<>
[[maybe_unused]] column make_fk_column<std::string>(const std::string& name, size_t size, const std::string &ref_table, const std::string &ref_column)
[[maybe_unused]] column make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table,
const std::string &ref_column)
{
return {name, data_type_traits<std::string>::builtin_type(size), 0, ref_table, ref_column, {size, utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL}};
}

View File

@ -476,6 +476,9 @@ std::string build_create_column(const column &col, const dialect &d, column_cont
if (is_constraint_set(col.attributes().options(), utils::constraints::NOT_NULL)) {
result.append(" NOT NULL");
}
if (is_constraint_set(col.attributes().options(), utils::constraints::UNIQUE)) {
result.append(" UNIQUE");
}
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
context.primary_keys.emplace_back(col.name());
}

View File

@ -31,7 +31,8 @@ add_executable(tests QueryBuilderTest.cpp
models/coordinate.hpp
models/location.hpp
TypeTraitsTest.cpp
Databases.hpp)
Databases.hpp
models/optional.hpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
matador

View File

@ -6,6 +6,7 @@
#include <matador/sql/query_builder.hpp>
using namespace matador::sql;
using namespace matador::utils;
TEST_CASE("Create table sql statement string", "[query]") {
dialect d = dialect_builder::builder().create().build();
@ -16,17 +17,17 @@ TEST_CASE("Create table sql statement string", "[query]") {
make_column<unsigned short>("age")
}).compile();
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))##");
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "age" INTEGER NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
REQUIRE(q.table_name == "person");
q = query.create().table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("name", { 255, constraints::UNIQUE | constraints::NOT_NULL }),
make_column<unsigned short>("age"),
make_fk_column<unsigned long>("address", "address", "id")
}).compile();
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, "address" BIGINT, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
REQUIRE(q.table_name == "person");
}

View File

@ -48,7 +48,7 @@ private:
};
};
TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]", Sqlite, Postgres)
TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]", Sqlite, Postgres, MySql)
{
auto &s = StatementTestFixture<TestType>::session();
auto &planes = StatementTestFixture<TestType>::planes();

30
test/models/optional.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef QUERY_OPTIONAL_HPP
#define QUERY_OPTIONAL_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/field_attributes.hpp"
#include <optional>
#include <string>
namespace matador::test {
struct optional
{
unsigned long id{};
std::optional<std::string> name;
std::optional<unsigned int> age{};
template<class Operator>
void process(Operator &op) {
namespace field = matador::utils::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::attribute(op, "age", age);
}
};
}
#endif //QUERY_OPTIONAL_HPP