query builder progress
This commit is contained in:
@@ -10,14 +10,9 @@
|
||||
namespace matador::object {
|
||||
|
||||
enum class null_option_type : uint8_t {
|
||||
NULLABLE, NOT_NULL
|
||||
Nullable, NotNull
|
||||
};
|
||||
|
||||
struct attribute_options {
|
||||
utils::field_attributes attributes;
|
||||
null_option_type null_option{null_option_type::NOT_NULL};
|
||||
int index{-1};
|
||||
};
|
||||
class object;
|
||||
class attribute_generator;
|
||||
|
||||
@@ -33,20 +28,17 @@ public:
|
||||
attribute() = default;
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const utils::field_attributes&,
|
||||
null_option_type null_opt);
|
||||
const utils::field_attributes &attr = utils::null_attributes,
|
||||
null_option_type null_opt = null_option_type::NotNull);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
void name(const std::string& n);
|
||||
[[nodiscard]] std::string full_name() const;
|
||||
[[nodiscard]] int index() const;
|
||||
[[nodiscard]] const utils::field_attributes& attributes() const;
|
||||
[[nodiscard]] utils::field_attributes& attributes();
|
||||
[[nodiscard]] bool is_nullable() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
[[nodiscard]] object* owner() const;
|
||||
// [[nodiscard]] const std::string& table_name() const;
|
||||
// void table_name(const std::string& name);
|
||||
void change_type(utils::basic_type type, const utils::field_attributes &attr = utils::null_attributes);
|
||||
template < typename Type >
|
||||
void change_type(const utils::field_attributes &attr = utils::null_attributes) {
|
||||
@@ -75,8 +67,9 @@ private:
|
||||
|
||||
std::string name_;
|
||||
object *owner_{nullptr};
|
||||
attribute_options options_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
utils::field_attributes options_{};
|
||||
null_option_type null_option_{null_option_type::NotNull};
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ public:
|
||||
template<class Type>
|
||||
attribute generate(const char *id, Type &x) {
|
||||
access::process(*this, x);
|
||||
return attribute{id, type_, {utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
|
||||
return attribute{id, type_, {utils::constraints::ForeignKey }, null_option_type::NotNull};
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
@@ -139,18 +139,18 @@ private:
|
||||
|
||||
template<typename ValueType>
|
||||
void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NOT_NULL);
|
||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull);
|
||||
prepare_primary_key(ref, utils::identifier(x));
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NOT_NULL);
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NotNull);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NULLABLE);
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::Nullable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define BASIC_PROTOTYPE_INFO_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/relation_endpoint.hpp"
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
@@ -26,6 +27,7 @@ public:
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
[[nodiscard]] const std::list<attribute>& attributes() const;
|
||||
[[nodiscard]] const std::list<class constraint>& constraints() const;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "matador/utils/constraints.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
@@ -18,6 +19,14 @@ public:
|
||||
explicit constraint(std::string name);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const class attribute* attribute() const;
|
||||
[[nodiscard]] std::string column_name() const;
|
||||
[[nodiscard]] const object* owner() const;
|
||||
[[nodiscard]] bool is_primary_key_constraint() const;
|
||||
[[nodiscard]] bool is_foreign_key_constraint() const;
|
||||
[[nodiscard]] bool is_unique_constraint() const;
|
||||
[[nodiscard]] const std::string& ref_table_name() const;
|
||||
[[nodiscard]] const std::string& ref_column_name() const;
|
||||
|
||||
private:
|
||||
friend class constraint_builder;
|
||||
@@ -26,7 +35,8 @@ private:
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
attribute* attr_{nullptr};
|
||||
std::variant<class attribute*, std::string> attr_;
|
||||
// class attribute* attr_{nullptr};
|
||||
object *owner_{nullptr};
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
std::string ref_table_name_{};
|
||||
|
||||
@@ -88,7 +88,7 @@ public:
|
||||
template <class Pointer>
|
||||
void on_foreign_key(const char *id, Pointer &/*x*/) {
|
||||
const auto type = pk_type_determinator::determine<typename Pointer::value_type>();
|
||||
auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NOT_NULL);
|
||||
auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NotNull);
|
||||
ref.owner_ = object_.get();
|
||||
}
|
||||
template<class ContainerType>
|
||||
@@ -121,19 +121,19 @@ private:
|
||||
|
||||
template<typename ValueType>
|
||||
void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NOT_NULL);
|
||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull);
|
||||
prepare_primary_key(ref, utils::identifier(x));
|
||||
create_pk_constraint(id);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void object_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NOT_NULL);
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NotNull);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void object_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NULLABLE);
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::Nullable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
|
||||
|
||||
append_join(
|
||||
query::column{table_info_stack_.top().table, id},
|
||||
query::column{next->second, info->get().reference_column()->name()}
|
||||
query::column{next->second, info->get().primary_key_attribute()->name()}
|
||||
);
|
||||
} else {
|
||||
push(id);
|
||||
@@ -281,7 +281,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
|
||||
// create select query
|
||||
auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, generator::column_generator_options::ForceLazy))
|
||||
.from(*foreign_table)
|
||||
.where(column(foreign_table, info->get().reference_column()->name(), "") == _)
|
||||
.where(column(foreign_table, info->get().primary_key_attribute()->name(), "") == _)
|
||||
.prepare(executor_);
|
||||
if (!result) {
|
||||
throw query_builder_exception(query_build_error::QueryError, result.release_error());
|
||||
|
||||
@@ -26,10 +26,8 @@ private:
|
||||
|
||||
class data_type {
|
||||
public:
|
||||
template<typename Type>
|
||||
data_type() : type_(utils::data_type_traits<Type>()) {}
|
||||
template<typename Type>
|
||||
explicit data_type(const size_t size) : type_(utils::data_type_traits<Type>()), size_(size) {}
|
||||
explicit data_type(const utils::basic_type type, const size_t size = 0)
|
||||
: type_(type), size_(size) {}
|
||||
|
||||
[[nodiscard]] const utils::basic_type& type() const { return type_; }
|
||||
[[nodiscard]] size_t size() const { return size_; }
|
||||
@@ -39,10 +37,28 @@ private:
|
||||
size_t size_{0};
|
||||
};
|
||||
|
||||
// using BigInt = data_type<int64_t>();
|
||||
// using Real = data_type<float>;
|
||||
// using double_ = data_type<double>;
|
||||
// using string = data_type<std::string>;
|
||||
template<typename Type>
|
||||
class typed_data_type final : public data_type {
|
||||
public:
|
||||
typed_data_type()
|
||||
: data_type(utils::data_type_traits<Type>::type()) {}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class sized_typed_data_type final : public data_type {
|
||||
public:
|
||||
explicit sized_typed_data_type(size_t size)
|
||||
: data_type(utils::data_type_traits<Type>::type(size), size) {}
|
||||
};
|
||||
|
||||
using BigInt = typed_data_type<int64_t>;
|
||||
using Integer = typed_data_type<int64_t>;
|
||||
using Real = typed_data_type<float>;
|
||||
using Double_ = typed_data_type<double>;
|
||||
using String = typed_data_type<std::string>;
|
||||
using Boolean = typed_data_type<bool>;
|
||||
using Varchar = sized_typed_data_type<std::string>;
|
||||
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
@@ -50,7 +66,7 @@ public:
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& column_name() const;
|
||||
[[nodiscard]] std::string column_name() const;
|
||||
[[nodiscard]] const utils::constraints& type() const;
|
||||
[[nodiscard]] bool is_primary_key_constraint() const;
|
||||
[[nodiscard]] bool is_foreign_key_constraint() const;
|
||||
@@ -64,6 +80,7 @@ private:
|
||||
utils::constraints type_{};
|
||||
std::string referenced_table_;
|
||||
std::string referenced_column_;
|
||||
data_type dt = Varchar{255};
|
||||
};
|
||||
}
|
||||
namespace matador::query {
|
||||
|
||||
@@ -5,16 +5,32 @@
|
||||
|
||||
#include "matador/query/intermediates/executable_query.hpp"
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
class query_create_table_columns_intermediate : public executable_query {
|
||||
public:
|
||||
using executable_query::executable_query;
|
||||
|
||||
executable_query constraints(std::initializer_list<class object::constraint> constraints);
|
||||
executable_query constraints(const std::list<class object::constraint> &constraints);
|
||||
};
|
||||
|
||||
class query_create_table_intermediate : public query_intermediate {
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_create_table_columns_intermediate columns(std::initializer_list<object::attribute> columns);
|
||||
query_create_table_columns_intermediate columns(const std::list<object::attribute> &columns);
|
||||
};
|
||||
|
||||
class query_create_intermediate : public query_intermediate {
|
||||
public:
|
||||
query_create_intermediate();
|
||||
|
||||
executable_query table(const table &tab, std::initializer_list<object::attribute> columns);
|
||||
executable_query table(const query::table &tab, const std::vector<object::attribute> &columns);
|
||||
query_create_table_intermediate table(const table &tab);
|
||||
executable_query schema(const std::string &schema_name);
|
||||
};
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
|
||||
@@ -364,17 +365,41 @@ private:
|
||||
class query_create_table_part final : public query_part
|
||||
{
|
||||
public:
|
||||
query_create_table_part(class table tab, std::vector<object::attribute> columns);
|
||||
explicit query_create_table_part(class table tab);
|
||||
|
||||
[[nodiscard]] const class table& table() const;
|
||||
[[nodiscard]] const std::vector<object::attribute>& columns() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
class table table_;
|
||||
std::vector<object::attribute> columns_;
|
||||
};
|
||||
|
||||
class query_create_table_columns_part final : public query_part {
|
||||
public:
|
||||
explicit query_create_table_columns_part(const std::list<object::attribute> &columns);
|
||||
|
||||
[[nodiscard]] const std::list<object::attribute>& columns() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
std::list<object::attribute> columns_;
|
||||
};
|
||||
|
||||
class query_create_table_constraints_part final : public query_part {
|
||||
public:
|
||||
explicit query_create_table_constraints_part(const std::list<class object::constraint> &constraints);
|
||||
|
||||
[[nodiscard]] const std::list<class object::constraint>& constraints() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
std::list<class object::constraint> constraints_;
|
||||
};
|
||||
|
||||
class query_create_schema_part final : public query_part {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
namespace matador::sql {
|
||||
@@ -64,6 +65,8 @@ protected:
|
||||
|
||||
void visit(internal::query_create_part &part) override;
|
||||
void visit(internal::query_create_table_part &part) override;
|
||||
void visit(internal::query_create_table_columns_part& part) override;
|
||||
void visit(internal::query_create_table_constraints_part& part) override;
|
||||
void visit(internal::query_create_schema_part& part) override;
|
||||
|
||||
void visit(internal::query_drop_part &part) override;
|
||||
@@ -79,6 +82,8 @@ protected:
|
||||
size_t table_index{0};
|
||||
const sql::dialect *dialect_{nullptr};
|
||||
std::optional<std::reference_wrapper<const sql::connection_impl>> connection_{};
|
||||
|
||||
std::function<void(sql::query_context&)> finisher_ = [](sql::query_context&) {};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ class query_delete_part;
|
||||
class query_delete_from_part;
|
||||
class query_create_part;
|
||||
class query_create_table_part;
|
||||
class query_create_table_columns_part;
|
||||
class query_create_table_constraints_part;
|
||||
class query_create_schema_part;
|
||||
class query_drop_part;
|
||||
class query_drop_table_part;
|
||||
@@ -75,6 +77,8 @@ public:
|
||||
|
||||
virtual void visit(internal::query_create_part &part) = 0;
|
||||
virtual void visit(internal::query_create_table_part &part) = 0;
|
||||
virtual void visit(internal::query_create_table_columns_part &part) = 0;
|
||||
virtual void visit(internal::query_create_table_constraints_part &part) = 0;
|
||||
virtual void visit(internal::query_create_schema_part &part) = 0;
|
||||
|
||||
virtual void visit(internal::query_drop_part &part) = 0;
|
||||
|
||||
@@ -137,6 +137,7 @@ public:
|
||||
[[nodiscard]] const std::string& column() const;
|
||||
[[nodiscard]] const std::string& columns() const;
|
||||
[[nodiscard]] const std::string& commit() const;
|
||||
[[nodiscard]] const std::string& constraint() const;
|
||||
[[nodiscard]] const std::string& create() const;
|
||||
[[nodiscard]] const std::string& desc() const;
|
||||
[[nodiscard]] const std::string& distinct() const;
|
||||
@@ -201,6 +202,7 @@ private:
|
||||
{dialect_token::Column, "COLUMN"},
|
||||
{dialect_token::Columns, "COLUMNS"},
|
||||
{dialect_token::Commit, "COMMIT TRANSACTION"},
|
||||
{dialect_token::Constraint, "CONSTRAINT"},
|
||||
{dialect_token::Create, "CREATE"},
|
||||
{dialect_token::Desc, "DESC"},
|
||||
{dialect_token::Distinct, "DISTINCT"},
|
||||
|
||||
@@ -19,6 +19,7 @@ enum class dialect_token : uint8_t {
|
||||
Column,
|
||||
Columns,
|
||||
Commit,
|
||||
Constraint,
|
||||
Create,
|
||||
Database,
|
||||
Desc,
|
||||
|
||||
@@ -25,7 +25,8 @@ enum class basic_type : uint8_t {
|
||||
type_date, /*!< Data type date */
|
||||
type_time, /*!< Data type time */
|
||||
type_blob, /*!< Data type blob */
|
||||
type_null /*!< Data type null */
|
||||
type_null, /*!< Data type null */
|
||||
type_unknown /*!< Data type unknown */
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,11 @@ class attribute_writer;
|
||||
* for a data type
|
||||
*/
|
||||
template < class Type, class Enable = void >
|
||||
struct data_type_traits;
|
||||
struct data_type_traits {
|
||||
static basic_type type(std::size_t /*size*/) { return basic_type::type_unknown; }
|
||||
static void read_value(attribute_reader &/*reader*/, const char *id, size_t index, nullptr_t &/*value*/, size_t /*size*/ = 0) {}
|
||||
static void bind_value(attribute_writer &/*binder*/, size_t index, nullptr_t &/*value*/, size_t /*size*/ = 0) {}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //BASIC_TYPE_TRAITS_HPP
|
||||
|
||||
Reference in New Issue
Block a user