query preparations for std::optional
This commit is contained in:
parent
92eb30767e
commit
2060883d59
|
|
@ -130,10 +130,10 @@ private:
|
||||||
*/
|
*/
|
||||||
column operator "" _col(const char *name, size_t len);
|
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 >
|
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);
|
return make_column(name, data_type_traits<Type>::builtin_type(0), attr);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
const field_attributes null_attributes {};
|
const field_attributes null_attributes {};
|
||||||
|
const field_attributes not_null_attributes { constraints::NOT_NULL };
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //QUERY_FIELD_ATTRIBUTES_HPP
|
#endif //QUERY_FIELD_ATTRIBUTES_HPP
|
||||||
|
|
|
||||||
|
|
@ -5,33 +5,26 @@
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
column::column(sql_function_t func, std::string name)
|
column::column(sql_function_t func, std::string name)
|
||||||
: name_(std::move(name))
|
: name_(std::move(name)), type_(data_type_t::type_int), attributes_(utils::null_attributes), function_(func)
|
||||||
, type_(data_type_t::type_int)
|
{}
|
||||||
, attributes_(utils::null_attributes)
|
|
||||||
, function_(func) {}
|
|
||||||
|
|
||||||
column::column(const char *name, std::string alias)
|
column::column(const char *name, std::string alias)
|
||||||
: name_(name)
|
: name_(name), attributes_(utils::null_attributes), alias_(std::move(alias))
|
||||||
, attributes_(utils::null_attributes)
|
{}
|
||||||
, alias_(std::move(alias)) {}
|
|
||||||
|
|
||||||
column::column(std::string name, std::string alias)
|
column::column(std::string name, std::string alias)
|
||||||
: name_(std::move(name))
|
: name_(std::move(name)), attributes_(utils::null_attributes), alias_(std::move(alias))
|
||||||
, attributes_(utils::null_attributes)
|
{}
|
||||||
, alias_(std::move(alias)) {}
|
|
||||||
|
|
||||||
column::column(std::string name, data_type_t type, utils::field_attributes attr)
|
column::column(std::string name, data_type_t type, utils::field_attributes attr)
|
||||||
: name_(std::move(name))
|
: name_(std::move(name)), type_(type), attributes_(attr)
|
||||||
, 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)
|
column::column(std::string name, data_type_t type, size_t index, std::string ref_table, std::string ref_column,
|
||||||
: name_(std::move(name))
|
utils::field_attributes attr)
|
||||||
, index_(index)
|
: name_(std::move(name)), index_(index), type_(type), attributes_(attr), ref_table_(std::move(ref_table)),
|
||||||
, type_(type)
|
ref_column_(std::move(ref_column))
|
||||||
, attributes_(attr)
|
{}
|
||||||
, ref_table_(std::move(ref_table))
|
|
||||||
, ref_column_(std::move(ref_column)) {}
|
|
||||||
|
|
||||||
const std::string &column::name() const
|
const std::string &column::name() const
|
||||||
{
|
{
|
||||||
|
|
@ -81,7 +74,7 @@ void column::alias(const std::string &as)
|
||||||
std::string column::str() const
|
std::string column::str() const
|
||||||
{
|
{
|
||||||
any_type_to_visitor<std::string> visitor;
|
any_type_to_visitor<std::string> visitor;
|
||||||
std::visit(visitor, const_cast<any_type&>(value_));
|
std::visit(visitor, const_cast<any_type &>(value_));
|
||||||
return visitor.result;
|
return visitor.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,26 +93,30 @@ column operator "" _col(const char *name, size_t len)
|
||||||
return {std::string(name, len)};
|
return {std::string(name, len)};
|
||||||
}
|
}
|
||||||
|
|
||||||
column make_column( const std::string& name, data_type_t type, utils::field_attributes attr )
|
column make_column(const std::string &name, data_type_t type, utils::field_attributes attr)
|
||||||
{
|
{
|
||||||
return {name, type, attr};
|
return {name, type, attr};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
column make_column<std::string>( const std::string& name, utils::field_attributes attr )
|
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);
|
return make_column(name, data_type_traits<std::string>::builtin_type(attr.size()), attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
column make_pk_column<std::string>( const std::string& name, size_t size )
|
column make_pk_column<std::string>(const std::string &name, size_t size)
|
||||||
{
|
{
|
||||||
return make_column<std::string>(name, { size, utils::constraints::FOREIGN_KEY });
|
return make_column<std::string>(name, {size, utils::constraints::FOREIGN_KEY});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
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}};
|
return {name, data_type_traits<std::string>::builtin_type(size), 0, ref_table, ref_column, {size, utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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)) {
|
if (is_constraint_set(col.attributes().options(), utils::constraints::NOT_NULL)) {
|
||||||
result.append(" 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)) {
|
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||||
context.primary_keys.emplace_back(col.name());
|
context.primary_keys.emplace_back(col.name());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,8 @@ add_executable(tests QueryBuilderTest.cpp
|
||||||
models/coordinate.hpp
|
models/coordinate.hpp
|
||||||
models/location.hpp
|
models/location.hpp
|
||||||
TypeTraitsTest.cpp
|
TypeTraitsTest.cpp
|
||||||
Databases.hpp)
|
Databases.hpp
|
||||||
|
models/optional.hpp)
|
||||||
target_link_libraries(tests PRIVATE
|
target_link_libraries(tests PRIVATE
|
||||||
Catch2::Catch2WithMain
|
Catch2::Catch2WithMain
|
||||||
matador
|
matador
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <matador/sql/query_builder.hpp>
|
#include <matador/sql/query_builder.hpp>
|
||||||
|
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
|
using namespace matador::utils;
|
||||||
|
|
||||||
TEST_CASE("Create table sql statement string", "[query]") {
|
TEST_CASE("Create table sql statement string", "[query]") {
|
||||||
dialect d = dialect_builder::builder().create().build();
|
dialect d = dialect_builder::builder().create().build();
|
||||||
|
|
@ -16,17 +17,17 @@ TEST_CASE("Create table sql statement string", "[query]") {
|
||||||
make_column<unsigned short>("age")
|
make_column<unsigned short>("age")
|
||||||
}).compile();
|
}).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");
|
REQUIRE(q.table_name == "person");
|
||||||
|
|
||||||
q = query.create().table("person", {
|
q = query.create().table("person", {
|
||||||
make_pk_column<unsigned long>("id"),
|
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_column<unsigned short>("age"),
|
||||||
make_fk_column<unsigned long>("address", "address", "id")
|
make_fk_column<unsigned long>("address", "address", "id")
|
||||||
}).compile();
|
}).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");
|
REQUIRE(q.table_name == "person");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 &s = StatementTestFixture<TestType>::session();
|
||||||
auto &planes = StatementTestFixture<TestType>::planes();
|
auto &planes = StatementTestFixture<TestType>::planes();
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue