Compare commits
No commits in common. "78abf16f282276e0cfd9db5496f39b19222d48be" and "3f3773dc71299ab03f848645cef2cabe02f3b335" have entirely different histories.
78abf16f28
...
3f3773dc71
|
|
@ -35,7 +35,7 @@ public:
|
|||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> describe(const std::string& table) override;
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string& table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
[[nodiscard]] std::string to_escaped_string( const utils::blob& value ) const override;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
#include "postgres_result_reader.hpp"
|
||||
#include "postgres_statement.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/error_code.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
|
@ -89,7 +89,7 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
|
|||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_, "Failed to fetch", context.sql));
|
||||
}
|
||||
|
||||
std::vector<object::attribute> prototype = context.prototype;
|
||||
std::vector<object::attribute_definition> prototype = context.prototype;
|
||||
|
||||
const int num_col = PQnfields(res);
|
||||
if (prototype.size() != static_cast<size_t>(num_col)) {
|
||||
|
|
@ -212,7 +212,7 @@ utils::basic_type string2type(const char *type) {
|
|||
}
|
||||
}
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> postgres_connection::describe(const std::string &table) {
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> postgres_connection::describe(const std::string &table) {
|
||||
const std::string stmt(
|
||||
"SELECT ordinal_position, column_name, udt_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_schema='public' AND table_name='"
|
||||
+ table + "'");
|
||||
|
|
@ -224,7 +224,7 @@ utils::result<std::vector<object::attribute>, utils::error> postgres_connection:
|
|||
}
|
||||
|
||||
postgres_result_reader reader(res);
|
||||
std::vector<object::attribute> prototype;
|
||||
std::vector<object::attribute_definition> prototype;
|
||||
while (auto fetched = reader.fetch()) {
|
||||
if (!fetched.is_ok()) {
|
||||
return utils::failure(fetched.release_error());
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
#include "matador/sql/query_macro.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
|
@ -144,26 +143,20 @@ struct matador::utils::data_type_traits<job::job_mode, void> {
|
|||
}
|
||||
};
|
||||
|
||||
QUERY_HELPER( authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
|
||||
QUERY_HELPER( authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
|
||||
|
||||
QUERY_HELPER( books, BOOK, id, author_id, title, published_in )
|
||||
QUERY_HELPER( books, id, author_id, title, published_in )
|
||||
|
||||
QUERY_HELPER( job, JOB, id, payload, type, description, state, mode )
|
||||
QUERY_HELPER( job, id, payload, type, description, state, mode )
|
||||
|
||||
QUERY_HELPER( payload, PAYLOAD, id )
|
||||
QUERY_HELPER( payload, id )
|
||||
|
||||
QUERY_HELPER( temporary_table, TEMPORARY_TABLE, id );
|
||||
|
||||
QUERY_HELPER(customer, CUSTOMER, id, name, email, address)
|
||||
QUERY_HELPER(product, PRODUCT, id, title, description, price, category)
|
||||
QUERY_HELPER(category, CATEGORY, id, title, description)
|
||||
QUERY_HELPER(cart, CART, id, items, owner)
|
||||
QUERY_HELPER( temporary_table, id );
|
||||
|
||||
int main() {
|
||||
using namespace matador::sql;
|
||||
using namespace matador::object;
|
||||
using namespace matador::utils;
|
||||
using namespace matador::query::meta;
|
||||
|
||||
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
||||
|
||||
|
|
@ -240,20 +233,19 @@ int main() {
|
|||
// .values( {3, "Misery", mc.id, 1984} )
|
||||
// .execute();
|
||||
//
|
||||
auto select_books_sql = matador::query::query::select( BOOK, {AUTHOR.last_name} )
|
||||
.from( BOOK )
|
||||
.join_left( AUTHOR )
|
||||
.on( BOOK.author_id == AUTHOR.id )
|
||||
.where( BOOK.published_in < 2008 && AUTHOR.last_name == "King" )
|
||||
.group_by( BOOK.published_in )
|
||||
.order_by( BOOK.title ).asc()
|
||||
.limit( 5 )
|
||||
.offset( 2 )
|
||||
.fetch_all(c);
|
||||
|
||||
for (const auto& r: *select_books_sql) {
|
||||
std::cout << "R: " << r.at( BOOK.title ) << ", " << r.at( AUTHOR.last_name ) << "\n";
|
||||
}
|
||||
// auto select_books_sql = c.query( s )
|
||||
// .select( qh::books.columns, {qh::authors.last_name} )
|
||||
// .from( qh::books )
|
||||
// .join_left( qh::authors )
|
||||
// .on( qh::books.author_id == qh::authors.id )
|
||||
// .where( qh::books.published_in < 2008 && qh::authors.last_name == "King" )
|
||||
// .group_by( qh::books.published_in )
|
||||
// .order_by( qh::books.title ).asc()
|
||||
// .limit( 5 )
|
||||
// .offset( 2 )
|
||||
// .fetch_all();
|
||||
//
|
||||
// for (const auto& r: select_books_sql) { std::cout << "R: " << r.at( qh::books.title ) << ", " << r.at( qh::authors.last_name ) << "\n"; }
|
||||
// // SELECT book.title, book.id, book.author_id, book.published_in, author.name
|
||||
// // FROM book
|
||||
// // INNER JOIN author ON book.author_id = author.id
|
||||
|
|
|
|||
|
|
@ -1,180 +0,0 @@
|
|||
#ifndef QUERY_COLUMN_DEFINITION_HPP
|
||||
#define QUERY_COLUMN_DEFINITION_HPP
|
||||
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
enum class null_option_type : uint8_t {
|
||||
NULLABLE, NOT_NULL
|
||||
};
|
||||
|
||||
struct attribute_options {
|
||||
utils::field_attributes attributes;
|
||||
null_option_type null_option{null_option_type::NOT_NULL};
|
||||
int index{-1};
|
||||
};
|
||||
|
||||
class attribute {
|
||||
public:
|
||||
explicit attribute(const char *name); // NOLINT(*-explicit-constructor)
|
||||
explicit attribute(std::string name); // NOLINT(*-explicit-constructor)
|
||||
|
||||
attribute(const attribute&) = default;
|
||||
attribute& operator=(const attribute&) = default;
|
||||
attribute(attribute&&) noexcept = default;
|
||||
attribute& operator=(attribute&&) noexcept = default;
|
||||
|
||||
attribute() = default;
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const utils::field_attributes&,
|
||||
null_option_type null_opt,
|
||||
int index = 0);
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
int index,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
const utils::field_attributes& attr,
|
||||
null_option_type null_opt);
|
||||
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const std::shared_ptr<attribute> &ref_column = {});
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute> &ref_column = {});
|
||||
|
||||
[[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]] 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) {
|
||||
type_ = utils::data_type_traits<Type>::type(attr.size());
|
||||
}
|
||||
[[nodiscard]] std::shared_ptr<attribute> reference_column() const;
|
||||
[[nodiscard]] bool is_foreign_reference() const;
|
||||
|
||||
[[nodiscard]] bool is_integer() const;
|
||||
[[nodiscard]] bool is_floating_point() const;
|
||||
[[nodiscard]] bool is_bool() const;
|
||||
[[nodiscard]] bool is_string() const;
|
||||
[[nodiscard]] bool is_varchar() const;
|
||||
[[nodiscard]] bool is_date() const;
|
||||
[[nodiscard]] bool is_time() const;
|
||||
[[nodiscard]] bool is_blob() const;
|
||||
[[nodiscard]] bool is_null() const;
|
||||
|
||||
template< typename Type >
|
||||
[[nodiscard]] bool is_type_of() const {
|
||||
return type() == utils::data_type_traits<Type>::type(attributes().size());
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string table_name_;
|
||||
attribute_options options_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
|
||||
std::shared_ptr<attribute> reference_column_;
|
||||
};
|
||||
|
||||
|
||||
class object {
|
||||
public:
|
||||
using iterator = std::vector<attribute>::iterator;
|
||||
using const_iterator = std::vector<attribute>::const_iterator;
|
||||
|
||||
private:
|
||||
std::vector<attribute> columns_;
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
};
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
private:
|
||||
std::shared_ptr<attribute> column_;
|
||||
};
|
||||
/**
|
||||
* User defined literal to have a shortcut creating a column object
|
||||
* @param name Name of the column
|
||||
* @param type
|
||||
* @param attr Length of the column name
|
||||
* @param null_opt
|
||||
* @return A column object with a given name
|
||||
*/
|
||||
attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
|
||||
}
|
||||
template <>
|
||||
attribute make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_pk_column(const std::string &name, size_t size = 0)
|
||||
{
|
||||
return make_column<Type>(name, { size, utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
template <>
|
||||
attribute make_pk_column<std::string>(const std::string &name, size_t size);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
|
||||
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::shared_ptr<attribute> &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column);
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(size), 0,
|
||||
std::make_shared<attribute>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::FOREIGN_KEY),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(0), 0,
|
||||
std::make_shared<attribute>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_COLUMN_DEFINITION_HPP
|
||||
|
|
@ -0,0 +1,183 @@
|
|||
#ifndef QUERY_COLUMN_DEFINITION_HPP
|
||||
#define QUERY_COLUMN_DEFINITION_HPP
|
||||
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
enum class null_option_type : uint8_t {
|
||||
NULLABLE, NOT_NULL
|
||||
};
|
||||
|
||||
struct attribute_options {
|
||||
utils::field_attributes attributes;
|
||||
null_option_type null_option{null_option_type::NOT_NULL};
|
||||
int index{-1};
|
||||
};
|
||||
|
||||
class attribute_definition {
|
||||
public:
|
||||
explicit attribute_definition(const char *name); // NOLINT(*-explicit-constructor)
|
||||
explicit attribute_definition(std::string name); // NOLINT(*-explicit-constructor)
|
||||
|
||||
attribute_definition(const attribute_definition&) = default;
|
||||
attribute_definition& operator=(const attribute_definition&) = default;
|
||||
attribute_definition(attribute_definition&&) noexcept = default;
|
||||
attribute_definition& operator=(attribute_definition&&) noexcept = default;
|
||||
|
||||
attribute_definition() = default;
|
||||
|
||||
// template<typename Type>
|
||||
// attribute_definition(std::string name, const utils::field_attributes& attr)
|
||||
// : attribute_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), attr)
|
||||
// {}
|
||||
//
|
||||
// template<size_t SIZE>
|
||||
// attribute_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option_type null_opt)
|
||||
// : attribute_definition(std::move(name), utils::data_type_traits<const char*>::type(attr.size()), attr, null_opt)
|
||||
// {}
|
||||
|
||||
attribute_definition(std::string name,
|
||||
utils::basic_type type,
|
||||
const utils::field_attributes&,
|
||||
null_option_type null_opt,
|
||||
int index = 0);
|
||||
|
||||
// template<typename Type>
|
||||
// attribute_definition(std::string name, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option_type null_opt)
|
||||
// : attribute_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), ref_column, attr, null_opt)
|
||||
// {}
|
||||
//
|
||||
attribute_definition(std::string name,
|
||||
utils::basic_type type,
|
||||
int index,
|
||||
const std::shared_ptr<attribute_definition> &ref_column,
|
||||
const utils::field_attributes& attr,
|
||||
null_option_type null_opt);
|
||||
|
||||
attribute_definition(std::string name,
|
||||
utils::basic_type type,
|
||||
const std::shared_ptr<attribute_definition> &ref_column = {});
|
||||
attribute_definition(std::string name,
|
||||
utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute_definition> &ref_column = {});
|
||||
|
||||
[[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]] 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) {
|
||||
type_ = utils::data_type_traits<Type>::type(attr.size());
|
||||
}
|
||||
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
|
||||
[[nodiscard]] bool is_foreign_reference() const;
|
||||
|
||||
[[nodiscard]] bool is_integer() const;
|
||||
[[nodiscard]] bool is_floating_point() const;
|
||||
[[nodiscard]] bool is_bool() const;
|
||||
[[nodiscard]] bool is_string() const;
|
||||
[[nodiscard]] bool is_varchar() const;
|
||||
[[nodiscard]] bool is_date() const;
|
||||
[[nodiscard]] bool is_time() const;
|
||||
[[nodiscard]] bool is_blob() const;
|
||||
[[nodiscard]] bool is_null() const;
|
||||
|
||||
template< typename Type >
|
||||
[[nodiscard]] bool is_type_of() const {
|
||||
return type() == utils::data_type_traits<Type>::type(attributes().size());
|
||||
}
|
||||
|
||||
private:
|
||||
friend class object_definition;
|
||||
|
||||
std::string name_;
|
||||
// std::shared_ptr<object_definition> object_{};
|
||||
std::string table_name_;
|
||||
attribute_options options_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
|
||||
std::shared_ptr<attribute_definition> reference_column_;
|
||||
};
|
||||
|
||||
/**
|
||||
* User defined literal to have a shortcut creating a column object
|
||||
* @param name Name of the column
|
||||
* @param type
|
||||
* @param attr Length of the column name
|
||||
* @param null_opt
|
||||
* @return A column object with a given name
|
||||
*/
|
||||
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL);
|
||||
|
||||
template < typename Type >
|
||||
attribute_definition make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
|
||||
}
|
||||
template <>
|
||||
attribute_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
|
||||
|
||||
template < typename Type >
|
||||
attribute_definition make_pk_column(const std::string &name, size_t size = 0)
|
||||
{
|
||||
return make_column<Type>(name, { size, utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
template <>
|
||||
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size);
|
||||
|
||||
template < typename Type >
|
||||
attribute_definition make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
|
||||
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, const std::shared_ptr<attribute_definition> &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column);
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(size), 0,
|
||||
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::FOREIGN_KEY),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(0), 0,
|
||||
std::make_shared<attribute_definition>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_COLUMN_DEFINITION_HPP
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef QUERY_COLUMN_DEFINITION_GENERATOR_HPP
|
||||
#define QUERY_COLUMN_DEFINITION_GENERATOR_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/data_type_traits.hpp"
|
||||
|
|
@ -24,9 +24,9 @@ public:
|
|||
fk_attribute_generator() = default;
|
||||
|
||||
template<class Type>
|
||||
attribute generate(const char *id, Type &x, const std::shared_ptr<attribute> &ref_column) {
|
||||
attribute_definition generate(const char *id, Type &x, const std::shared_ptr<attribute_definition> &ref_column) {
|
||||
access::process(*this, x);
|
||||
return attribute{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
|
|
@ -52,26 +52,27 @@ private:
|
|||
utils::basic_type type_{};
|
||||
};
|
||||
|
||||
class attribute_generator final {
|
||||
class attribute_definition_generator final {
|
||||
private:
|
||||
attribute_generator(std::vector<attribute> &columns, const repository &repo);
|
||||
attribute_definition_generator(std::vector<attribute_definition> &columns, const repository &repo);
|
||||
|
||||
public:
|
||||
~attribute_generator() = default;
|
||||
~attribute_definition_generator() = default;
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const repository &repo) {
|
||||
std::vector<attribute> columns;
|
||||
attribute_generator gen(columns, repo);
|
||||
static std::vector<attribute_definition> generate(const repository &repo)
|
||||
{
|
||||
std::vector<attribute_definition> columns;
|
||||
attribute_definition_generator gen(columns, repo);
|
||||
Type obj;
|
||||
access::process(gen, obj);
|
||||
return columns;
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const Type& obj, const repository &repo) {
|
||||
std::vector<attribute> columns;
|
||||
attribute_generator gen(columns, repo);
|
||||
static std::vector<attribute_definition> generate(const Type& obj, const repository &repo) {
|
||||
std::vector<attribute_definition> columns;
|
||||
attribute_definition_generator gen(columns, repo);
|
||||
access::process(gen, obj);
|
||||
return columns;
|
||||
}
|
||||
|
|
@ -97,12 +98,12 @@ public:
|
|||
|
||||
template <class Pointer>
|
||||
void on_foreign_key(const char *id, Pointer &x) {
|
||||
std::shared_ptr<attribute> ref_column;
|
||||
std::shared_ptr<attribute_definition> ref_column;
|
||||
std::type_index ti = typeid(typename Pointer::value_type);
|
||||
if (const auto result = determine_foreign_ref(std::type_index(ti))) {
|
||||
ref_column = *result;
|
||||
} else {
|
||||
ref_column = std::make_shared<attribute>();
|
||||
ref_column = std::make_shared<attribute_definition>();
|
||||
insert_missing_reference_column(ti, ref_column);
|
||||
}
|
||||
if (x.empty()) {
|
||||
|
|
@ -120,29 +121,29 @@ public:
|
|||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> determine_foreign_ref(const std::type_index &ti) const;
|
||||
void insert_missing_reference_column(const std::type_index &ti, const std::shared_ptr<attribute>& ref_column) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> determine_foreign_ref(const std::type_index &ti) const;
|
||||
void insert_missing_reference_column(const std::type_index &ti, const std::shared_ptr<attribute_definition>& ref_column) const;
|
||||
|
||||
private:
|
||||
size_t index_ = 0;
|
||||
std::vector<attribute> &columns_;
|
||||
std::vector<attribute_definition> &columns_;
|
||||
const repository &repo_;
|
||||
|
||||
fk_attribute_generator fk_column_generator_;
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
void attribute_definition_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
on_attribute(id, x, { attr.size(), utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr) {
|
||||
void attribute_definition_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr) {
|
||||
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NOT_NULL);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr)
|
||||
void attribute_definition_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr)
|
||||
{
|
||||
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NULLABLE);
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef BASIC_PROTOTYPE_INFO_HPP
|
||||
#define BASIC_PROTOTYPE_INFO_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/object_definition.hpp"
|
||||
#include "matador/object/relation_endpoint.hpp"
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
|
@ -24,8 +24,8 @@ public:
|
|||
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
[[nodiscard]] const std::vector<attribute>& attributes() const;
|
||||
[[nodiscard]] std::shared_ptr<attribute> reference_column() const;
|
||||
[[nodiscard]] const std::vector<attribute_definition>& attributes() const;
|
||||
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
|
|
@ -49,14 +49,16 @@ public:
|
|||
[[nodiscard]] bool endpoints_empty() const;
|
||||
|
||||
protected:
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes, utils::identifier &&pk, const std::shared_ptr<attribute> &pk_as_fk_column);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes);
|
||||
// basic_object_info(std::shared_ptr<repository_node> node, utils::identifier &&pk, const std::shared_ptr<attribute_definition> &pk_column, const std::shared_ptr<object_definition> &definition);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute_definition> &attributes, utils::identifier &&pk, const std::shared_ptr<attribute_definition> &pk_as_fk_column);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute_definition> &attributes);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<repository_node> node_; /**< prototype node of the represented object type */
|
||||
std::vector<attribute> attributes_;
|
||||
std::vector<attribute_definition> attributes_;
|
||||
// std::shared_ptr<object_definition> definition_;
|
||||
std::optional<utils::identifier> identifier_;
|
||||
std::shared_ptr<attribute> pk_as_fk_column_;
|
||||
std::shared_ptr<attribute_definition> pk_as_fk_column_;
|
||||
t_endpoint_map relation_endpoints_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ private:
|
|||
using node_ptr = std::shared_ptr<repository_node>;
|
||||
|
||||
public:
|
||||
explicit shadow_repository(repository& s);
|
||||
explicit shadow_repository(object::repository& s);
|
||||
|
||||
[[nodiscard]] repository& repo() const;
|
||||
[[nodiscard]] object::repository& repo() const;
|
||||
[[nodiscard]] bool contains(const std::type_index& ti) const;
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
||||
|
|
@ -28,7 +28,7 @@ public:
|
|||
[[nodiscard]] utils::result<void, utils::error> detach_node(const std::shared_ptr<repository_node> &node) const;
|
||||
|
||||
private:
|
||||
repository& repo_;
|
||||
object::repository& schema_;
|
||||
};
|
||||
}
|
||||
#endif //SHADOW_SCHEMA_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
#ifndef QUERY_TABLE_DEFINITION_HPP
|
||||
#define QUERY_TABLE_DEFINITION_HPP
|
||||
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class object_definition final {
|
||||
private:
|
||||
using column_by_index = std::vector<attribute_definition>;
|
||||
using column_index_pair = std::pair<std::reference_wrapper<attribute_definition>, column_by_index::difference_type>;
|
||||
using column_by_name_map = std::unordered_map<std::string, column_index_pair>;
|
||||
|
||||
public:
|
||||
using iterator = column_by_index::iterator;
|
||||
using const_iterator = column_by_index::const_iterator;
|
||||
|
||||
object_definition() = default;
|
||||
explicit object_definition(std::string name);
|
||||
object_definition(std::string name, std::initializer_list<attribute_definition> columns);
|
||||
explicit object_definition(std::string name, const std::vector<attribute_definition> &columns);
|
||||
object_definition(const object_definition &x);
|
||||
object_definition& operator=(const object_definition &x);
|
||||
object_definition(object_definition&&) noexcept = default;
|
||||
object_definition& operator=(object_definition&&) noexcept = default;
|
||||
~object_definition() = default;
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] std::optional<attribute_definition> primary_key() const;
|
||||
|
||||
template < typename Type >
|
||||
void append(const std::string &name, long size = -1) {
|
||||
append(make_column<Type>(name, size));
|
||||
}
|
||||
void append(attribute_definition&& col);
|
||||
|
||||
[[nodiscard]] const std::vector<attribute_definition>& columns() const;
|
||||
|
||||
[[nodiscard]] const attribute_definition& at(const std::string &name) const;
|
||||
[[nodiscard]] const attribute_definition& at(size_t index) const;
|
||||
|
||||
iterator find(const std::string &column_name);
|
||||
[[nodiscard]] const_iterator find(const std::string &column_name) const;
|
||||
|
||||
iterator begin();
|
||||
[[nodiscard]] const_iterator begin() const;
|
||||
[[nodiscard]] const_iterator cbegin() const;
|
||||
|
||||
iterator end();
|
||||
[[nodiscard]] const_iterator end() const;
|
||||
[[nodiscard]] const_iterator cend() const;
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
void clear();
|
||||
|
||||
private:
|
||||
void init();
|
||||
void add_to_map(attribute_definition &col, size_t index);
|
||||
|
||||
private:
|
||||
friend class repository_node;
|
||||
|
||||
std::string name_;
|
||||
column_by_index columns_;
|
||||
column_by_name_map columns_by_name_;
|
||||
|
||||
int pk_index_{-1};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_TABLE_DEFINITION_HPP
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
#define OBJECT_INFO_HPP
|
||||
|
||||
#include "matador/object/basic_object_info.hpp"
|
||||
// #include "matador/object/object_definition.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
class repository_node;
|
||||
|
|
@ -11,16 +12,30 @@ class object_info final : public basic_object_info {
|
|||
public:
|
||||
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||
|
||||
// object_info(const std::shared_ptr<repository_node>& node,
|
||||
// const std::shared_ptr<attribute_definition> &ref_column)
|
||||
// : basic_object_info(node, {}, ref_column, {})
|
||||
// , creator_([]{return std::make_unique<Type>(); }){
|
||||
// }
|
||||
// object_info(const std::shared_ptr<repository_node>& node,
|
||||
// utils::identifier &&pk,
|
||||
// const std::shared_ptr<attribute_definition> &ref_column,
|
||||
// object_definition &&definition)
|
||||
// : basic_object_info(node, std::move(pk), ref_column, std::move(definition))
|
||||
// , creator_([]{return std::make_unique<Type>(); }){
|
||||
// }
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
const std::vector<attribute> &attributes,
|
||||
const std::vector<attribute_definition> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
const std::shared_ptr<attribute_definition> &ref_column,
|
||||
// object_definition &&definition,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, attributes, std::move(pk), ref_column)
|
||||
: basic_object_info(node, attributes, std::move(pk), ref_column/*, std::move(definition)*/)
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
const std::vector<attribute> &attributes,
|
||||
const std::vector<attribute_definition> &attributes,
|
||||
// object_definition &&definition,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, attributes)
|
||||
, creator_(std::move(creator)){
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ public:
|
|||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> reference_column(
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference_column(
|
||||
const std::type_index &type_index) const;
|
||||
|
||||
void dump(std::ostream &os) const;
|
||||
|
|
@ -178,7 +178,7 @@ private:
|
|||
private:
|
||||
friend class internal::shadow_repository;
|
||||
friend class repository_node;
|
||||
friend class attribute_generator;
|
||||
friend class attribute_definition_generator;
|
||||
|
||||
std::string name_;
|
||||
std::shared_ptr<repository_node> root_;
|
||||
|
|
@ -187,7 +187,7 @@ private:
|
|||
t_type_index_node_map nodes_by_type_;
|
||||
logger::logger log_;
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<attribute> > missing_references_;
|
||||
std::unordered_map<std::type_index, std::shared_ptr<attribute_definition> > missing_references_;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SCHEMA_NODE_HPP
|
||||
#define SCHEMA_NODE_HPP
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/object_info.hpp"
|
||||
#include "matador/object/primary_key_resolver.hpp"
|
||||
|
||||
|
|
@ -18,12 +18,16 @@ public:
|
|||
|
||||
template < typename Type >
|
||||
static std::shared_ptr<repository_node> make_node(repository& repo, const std::string& name) {
|
||||
auto node = std::make_shared<repository_node>( repo, name, typeid( Type ) );
|
||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, typeid(Type)));
|
||||
|
||||
primary_key_resolver resolver;
|
||||
// auto obj = std::make_shared<object_definition>(name);
|
||||
auto pk_info = resolver.resolve<Type>();
|
||||
auto ref_column = determine_reference_column(typeid(Type), name, pk_info, repo);
|
||||
const auto attributes = attribute_generator::generate<Type>(repo);
|
||||
const auto attributes = attribute_definition_generator::generate<Type>(repo);
|
||||
// for (auto&& attr : attributes) {
|
||||
// obj->append(std::move(attr));
|
||||
// }
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
node,
|
||||
attributes,
|
||||
|
|
@ -47,7 +51,7 @@ public:
|
|||
auto obj = creator();
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
result.value(),
|
||||
attribute_generator::generate(*obj, repo),
|
||||
attribute_definition_generator::generate(*obj, repo),
|
||||
std::move(creator)
|
||||
);
|
||||
result.value()->info_ = std::move(info);
|
||||
|
|
@ -74,7 +78,7 @@ public:
|
|||
void update_name(const std::string& name);
|
||||
|
||||
template <typename Type>
|
||||
[[nodiscard]] object_info_ref<Type> info() const {
|
||||
object_info_ref<Type> info() const {
|
||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||
}
|
||||
|
||||
|
|
@ -90,7 +94,7 @@ private:
|
|||
void unlink();
|
||||
|
||||
static utils::result<node_ptr, utils::error> make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti);
|
||||
static std::shared_ptr<attribute> determine_reference_column(const std::type_index& ti,
|
||||
static std::shared_ptr<attribute_definition> determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ public:
|
|||
[[nodiscard]] utils::result<void, utils::error> drop_table();
|
||||
[[nodiscard]] utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
||||
|
||||
[[nodiscard]] utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<std::vector<object::attribute_definition>, utils::error> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name) const;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
#include "matador/sql/statement_cache.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/object_definition.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
|
@ -129,7 +130,7 @@ public:
|
|||
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::query_context &q) const;
|
||||
[[nodiscard]] utils::result<size_t, utils::error> execute(const std::string &sql) const;
|
||||
|
||||
[[nodiscard]] std::vector<object::attribute> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] std::vector<object::attribute_definition> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] bool table_exists(const std::string &table_name) const;
|
||||
|
||||
void dump_schema(std::ostream &os) const;
|
||||
|
|
@ -150,7 +151,7 @@ private:
|
|||
const sql::dialect &dialect_;
|
||||
|
||||
std::unique_ptr<object::repository> schema_;
|
||||
mutable std::unordered_map<std::string, std::vector<object::attribute>> prototypes_;
|
||||
mutable std::unordered_map<std::string, std::vector<object::attribute_definition>> prototypes_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
|
|
@ -245,7 +246,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::update( const obj
|
|||
using namespace matador::utils;
|
||||
using namespace matador::query;
|
||||
|
||||
const auto col = column(info.value().get().reference_column()->name());
|
||||
const auto col = column(info.value().get().definition().primary_key()->name());
|
||||
auto res = matador::query::query::update(info->get().name())
|
||||
.set(generator::column_value_pairs<Type>())
|
||||
.where(col == _)
|
||||
|
|
@ -271,7 +272,7 @@ utils::result<void, utils::error> session::remove( const object::object_ptr<Type
|
|||
using namespace matador::utils;
|
||||
using namespace matador::query;
|
||||
|
||||
const auto col = column(info.value().get().reference_column()->name());
|
||||
const auto col = column(info.value().get().definition().primary_key()->name());
|
||||
auto res = matador::query::query::remove()
|
||||
.from( info->get().name() )
|
||||
.where(col == _)
|
||||
|
|
@ -293,7 +294,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
|||
if (!info) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
if (!info.value().get().has_primary_key()) {
|
||||
if (!info.value().get().definition().has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NoPrimaryKey, "Type hasn't primary key."));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#ifndef MATADOR_BUILDER_HPP
|
||||
#define MATADOR_BUILDER_HPP
|
||||
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/query/column.hpp"
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
#include "matador/utils/constraints.hpp"
|
||||
#include "matador/utils/data_type_traits.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
|
@ -24,25 +24,26 @@ private:
|
|||
std::string alias_;
|
||||
};
|
||||
|
||||
class data_type {
|
||||
class attribute {
|
||||
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) {}
|
||||
attribute(std::string name, const utils::basic_type type)
|
||||
: name_(std::move(name))
|
||||
, type_(type) {}
|
||||
|
||||
[[nodiscard]] const utils::basic_type& type() const { return type_; }
|
||||
[[nodiscard]] size_t size() const { return size_; }
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
[[nodiscard]] const attribute_options& options() const;
|
||||
|
||||
private:
|
||||
utils::basic_type type_{};
|
||||
size_t size_{0};
|
||||
};
|
||||
friend class entity;
|
||||
|
||||
// using BigInt = data_type<int64_t>();
|
||||
// using Real = data_type<float>;
|
||||
// using double_ = data_type<double>;
|
||||
// using string = data_type<std::string>;
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
utils::basic_type type_{};
|
||||
attribute_options options_;
|
||||
|
||||
entity* owner_{nullptr};
|
||||
};
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
|
|
@ -70,17 +71,13 @@ namespace matador::query {
|
|||
|
||||
class column_builder {
|
||||
public:
|
||||
explicit column_builder(std::string column_name, utils::basic_type type, size_t size = 0);
|
||||
explicit column_builder(std::string column_name);
|
||||
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
operator query::column() const; // NOLINT(*-explicit-constructor)
|
||||
|
||||
column_builder& not_null();
|
||||
|
||||
private:
|
||||
std::string column_name_;
|
||||
utils::basic_type type_{};
|
||||
utils::constraints constraints_{};
|
||||
std::string column_name;
|
||||
};
|
||||
|
||||
class table_builder {
|
||||
|
|
@ -117,7 +114,7 @@ private:
|
|||
|
||||
constraint_builder constraint(std::string name);
|
||||
table_builder table(std::string name);
|
||||
column_builder column(std::string name, utils::basic_type type, size_t size = 0);
|
||||
column_builder column(std::string name);
|
||||
|
||||
}
|
||||
#endif //MATADOR_BUILDER_HPP
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "matador/sql/sql_functions.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
|
|
@ -33,13 +34,10 @@ public:
|
|||
[[nodiscard]] std::shared_ptr<table> table() const;
|
||||
void table(const std::shared_ptr<query::table>& t);
|
||||
|
||||
operator const std::string&() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<query::table> table_;
|
||||
std::string name;
|
||||
std::string alias_;
|
||||
|
||||
sql::sql_function_t function_{sql::sql_function_t::None};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "matador/query/intermediates/executable_query.hpp"
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
|
|
@ -13,11 +13,11 @@ 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);
|
||||
executable_query table(const query::table &tab, std::initializer_list<object::attribute_definition> columns);
|
||||
executable_query table(const query::table &tab, const std::vector<object::attribute_definition> &columns);
|
||||
template<class Type>
|
||||
executable_query table(const matador::query::table &tab, const object::repository &schema) {
|
||||
return this->table(tab, object::attribute_generator::generate<Type>(schema));
|
||||
executable_query table(const query::table &tab, const object::repository &schema) {
|
||||
return this->table(tab, object::attribute_definition_generator::generate<Type>(schema));
|
||||
}
|
||||
executable_query schema(const std::string &schema_name);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
#include "matador/query/column.hpp"
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
|
||||
|
|
@ -364,17 +364,17 @@ private:
|
|||
class query_create_table_part final : public query_part
|
||||
{
|
||||
public:
|
||||
query_create_table_part(table table, std::vector<object::attribute> columns);
|
||||
query_create_table_part(table table, std::vector<object::attribute_definition> columns);
|
||||
|
||||
[[nodiscard]] const table& table() const;
|
||||
[[nodiscard]] const std::vector<object::attribute>& columns() const;
|
||||
[[nodiscard]] const std::vector<object::attribute_definition>& columns() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
class table table_;
|
||||
std::vector<object::attribute> columns_;
|
||||
std::vector<object::attribute_definition> columns_;
|
||||
};
|
||||
|
||||
class query_create_schema_part final : public query_part {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "matador/query/fk_value_extractor.hpp"
|
||||
#include "matador/query/internal/column_value_pair.hpp"
|
||||
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
|
|
|||
|
|
@ -4,18 +4,16 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/query_part.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
struct query_data {
|
||||
std::vector<std::unique_ptr<query_part>> parts{};
|
||||
std::vector<object::attribute> columns{};
|
||||
std::vector<object::attribute_definition> columns{};
|
||||
std::unordered_map<std::string, table> tables{};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ public:
|
|||
table() = default;
|
||||
table(const char *name); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name, std::string as);
|
||||
table(const char *name, std::string as); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name, std::string as); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name, std::string as, const std::vector<column> &columns);
|
||||
|
||||
table& as(const std::string &a);
|
||||
|
|
@ -29,10 +30,6 @@ public:
|
|||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
[[nodiscard]] const std::vector<column>& columns() const;
|
||||
|
||||
[[nodiscard]] column column(const std::string &name) const;
|
||||
|
||||
operator const std::vector<query::column>&() const;
|
||||
private:
|
||||
friend class column;
|
||||
|
||||
|
|
@ -40,7 +37,7 @@ private:
|
|||
std::string alias_;
|
||||
|
||||
std::string schema_name_;
|
||||
std::vector<query::column> columns_;
|
||||
std::vector<column> columns_;
|
||||
};
|
||||
|
||||
table operator ""_tab(const char *name, size_t len);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef QUERY_CONNECTION_HPP
|
||||
#define QUERY_CONNECTION_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/abstract_sql_logger.hpp"
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
|
|
@ -126,7 +126,7 @@ public:
|
|||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> rollback() const;
|
||||
|
||||
[[nodiscard]] utils::result<std::vector<object::attribute>, utils::error> describe(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &table_name) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/query_context.hpp"
|
||||
|
|
@ -38,7 +38,7 @@ public:
|
|||
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const query_context &context) = 0;
|
||||
virtual utils::result<std::unique_ptr<statement_impl>, utils::error> prepare(const query_context &context) = 0;
|
||||
|
||||
virtual utils::result<std::vector<object::attribute>, utils::error> describe(const std::string &table) = 0;
|
||||
virtual utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table) = 0;
|
||||
virtual utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) = 0;
|
||||
|
||||
[[nodiscard]] const class dialect &dialect() const;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
#include "matador/sql/internal/query_result_pk_resolver.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
|
@ -73,9 +73,9 @@ private:
|
|||
class query_result_impl {
|
||||
public:
|
||||
query_result_impl(std::unique_ptr<query_result_reader> &&reader,
|
||||
std::vector<object::attribute> &&prototype, size_t column_index = 0);
|
||||
std::vector<object::attribute_definition> &&prototype, size_t column_index = 0);
|
||||
query_result_impl(std::unique_ptr<query_result_reader> &&reader,
|
||||
const std::vector<object::attribute> &prototype, size_t column_index = 0);
|
||||
const std::vector<object::attribute_definition> &prototype, size_t column_index = 0);
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
||||
|
|
@ -183,7 +183,7 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::vector<object::attribute> &prototype() const;
|
||||
[[nodiscard]] const std::vector<object::attribute_definition> &prototype() const;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
|
|
@ -209,7 +209,7 @@ private:
|
|||
|
||||
protected:
|
||||
size_t column_index_ = 0;
|
||||
std::vector<object::attribute> prototype_;
|
||||
std::vector<object::attribute_definition> prototype_;
|
||||
std::unique_ptr<query_result_reader> reader_;
|
||||
detail::pk_reader pk_reader_;
|
||||
std::stack<std::type_index> type_stack_;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
#ifndef QUERY_QUERY_DATA_HPP
|
||||
#define QUERY_QUERY_DATA_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
enum class sql_command {
|
||||
|
|
@ -37,7 +36,7 @@ struct query_context {
|
|||
std::string command_name{};
|
||||
std::string schema_name{};
|
||||
std::string table_name{};
|
||||
std::vector<object::attribute> prototype{};
|
||||
std::vector<object::attribute_definition> prototype{};
|
||||
std::vector<std::string> result_vars{};
|
||||
std::vector<std::string> bind_vars{};
|
||||
std::vector<utils::database_type> bind_types{};
|
||||
|
|
|
|||
|
|
@ -9,16 +9,16 @@
|
|||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
#define FIELD(x) const matador::query::column x{*this, #x, ""};
|
||||
#define FIELD(x) const sql::column x{*this, #x, ""};
|
||||
|
||||
#define QUERY_HELPER(C, V, ...) \
|
||||
namespace matador::query::meta { \
|
||||
#define QUERY_HELPER(C, ...) \
|
||||
namespace matador::qh { \
|
||||
namespace internal { \
|
||||
struct C##_query : table { \
|
||||
struct C##_query : sql::table { \
|
||||
C##_query() : table(#C) {} \
|
||||
MAP(FIELD, __VA_ARGS__) \
|
||||
}; } \
|
||||
static const internal:: C##_query V; \
|
||||
static const internal:: C##_query C; \
|
||||
}
|
||||
|
||||
#endif //QUERY_QUERY_HELPER_HPP
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef QUERY_QUERY_RESULT_HPP
|
||||
#define QUERY_QUERY_RESULT_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/internal/query_result_impl.hpp"
|
||||
|
||||
|
|
@ -102,12 +102,12 @@ private:
|
|||
namespace detail {
|
||||
|
||||
template < typename Type >
|
||||
Type* create_prototype(const std::vector<object::attribute> &/*prototype*/) {
|
||||
Type* create_prototype(const std::vector<object::attribute_definition> &/*prototype*/) {
|
||||
return new Type{};
|
||||
}
|
||||
|
||||
template <>
|
||||
record* create_prototype<record>(const std::vector<object::attribute> &prototype);
|
||||
record* create_prototype<record>(const std::vector<object::attribute_definition> &prototype);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,13 +13,14 @@ add_library(matador-core STATIC
|
|||
../../include/matador/net/reactor.hpp
|
||||
../../include/matador/net/select_fd_sets.hpp
|
||||
../../include/matador/net/socket_interrupter.hpp
|
||||
../../include/matador/object/attribute.hpp
|
||||
../../include/matador/object/attribute_generator.hpp
|
||||
../../include/matador/object/attribute_definition.hpp
|
||||
../../include/matador/object/attribute_definition_generator.hpp
|
||||
../../include/matador/object/basic_object_info.hpp
|
||||
../../include/matador/object/error_code.hpp
|
||||
../../include/matador/object/foreign_node_completer.hpp
|
||||
../../include/matador/object/internal/shadow_repository.hpp
|
||||
../../include/matador/object/many_to_many_relation.hpp
|
||||
../../include/matador/object/object_definition.hpp
|
||||
../../include/matador/object/object_info.hpp
|
||||
../../include/matador/object/object_proxy.hpp
|
||||
../../include/matador/object/object_ptr.hpp
|
||||
|
|
@ -74,12 +75,13 @@ add_library(matador-core STATIC
|
|||
logger/log_manager.cpp
|
||||
logger/logger.cpp
|
||||
logger/rotating_file_sink.cpp
|
||||
object/attribute.cpp
|
||||
object/attribute_generator.cpp
|
||||
object/attribute_definition.cpp
|
||||
object/attribute_definition_generator.cpp
|
||||
object/basic_object_info.cpp
|
||||
object/error_code.cpp
|
||||
object/foreign_node_completer.cpp
|
||||
object/internal/shadow_repository.cpp
|
||||
object/object_definition.cpp
|
||||
object/relation_endpoint.cpp
|
||||
object/repository.cpp
|
||||
object/repository_node.cpp
|
||||
|
|
|
|||
|
|
@ -1,178 +0,0 @@
|
|||
#include "matador/object/attribute.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::object {
|
||||
attribute::attribute(const char *name)
|
||||
: attribute(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute::attribute(std::string name)
|
||||
: attribute(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute::attribute(std::string name,
|
||||
const utils::basic_type type,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt,
|
||||
const int index)
|
||||
: attribute(std::move(name), type, "", {attr, null_opt, index}) {
|
||||
}
|
||||
|
||||
attribute::attribute(std::string name,
|
||||
const utils::basic_type type,
|
||||
const int index,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt)
|
||||
: attribute(std::move(name), type, "", {attr, null_opt, index}, ref_column) {
|
||||
}
|
||||
|
||||
attribute::attribute( std::string name, const utils::basic_type type, const std::shared_ptr<attribute>& ref_column )
|
||||
: attribute(std::move(name), type, {}, {}, ref_column) {
|
||||
}
|
||||
|
||||
attribute::attribute(std::string name,
|
||||
const utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute>& ref_column)
|
||||
: name_(std::move(name))
|
||||
, table_name_(std::move(table_name))
|
||||
, options_( options )
|
||||
, type_( type )
|
||||
, reference_column_( ref_column ) {
|
||||
}
|
||||
|
||||
const std::string &attribute::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
void attribute::name( const std::string& n ) {
|
||||
name_ = n;
|
||||
}
|
||||
|
||||
std::string attribute::full_name() const {
|
||||
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
|
||||
}
|
||||
|
||||
int attribute::index() const {
|
||||
return options_.index;
|
||||
}
|
||||
|
||||
const utils::field_attributes &attribute::attributes() const {
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
utils::field_attributes& attribute::attributes() {
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
bool attribute::is_nullable() const {
|
||||
return options_.null_option == null_option_type::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type attribute::type() const {
|
||||
return type_;
|
||||
}
|
||||
|
||||
const std::string& attribute::table_name() const {
|
||||
return table_name_;
|
||||
}
|
||||
|
||||
void attribute::table_name( const std::string& name ) {
|
||||
table_name_ = name;
|
||||
}
|
||||
|
||||
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||
options_.attributes = attr;
|
||||
type_ = type;
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute> attribute::reference_column() const {
|
||||
return reference_column_;
|
||||
}
|
||||
|
||||
bool attribute::is_foreign_reference() const {
|
||||
return reference_column_ != nullptr;
|
||||
}
|
||||
|
||||
bool attribute::is_integer() const {
|
||||
return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64;
|
||||
}
|
||||
|
||||
bool attribute::is_floating_point() const {
|
||||
return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double;
|
||||
}
|
||||
|
||||
bool attribute::is_bool() const {
|
||||
return type_ == utils::basic_type::type_bool;
|
||||
}
|
||||
|
||||
bool attribute::is_string() const {
|
||||
return type_ == utils::basic_type::type_text;
|
||||
}
|
||||
|
||||
bool attribute::is_varchar() const {
|
||||
return type_ == utils::basic_type::type_varchar;
|
||||
}
|
||||
|
||||
bool attribute::is_date() const {
|
||||
return type_ == utils::basic_type::type_date;
|
||||
}
|
||||
|
||||
bool attribute::is_time() const {
|
||||
return type_ == utils::basic_type::type_time;
|
||||
}
|
||||
|
||||
bool attribute::is_blob() const {
|
||||
return type_ == utils::basic_type::type_blob;
|
||||
}
|
||||
|
||||
bool attribute::is_null() const {
|
||||
return type_ == utils::basic_type::type_null;
|
||||
}
|
||||
|
||||
attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
|
||||
null_option_type null_opt) {
|
||||
return {name, type, attr, null_opt};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute make_column<std::string>(const std::string &name, utils::field_attributes attr,
|
||||
null_option_type null_opt) {
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute make_pk_column<std::string>(const std::string &name, size_t size) {
|
||||
return make_column<std::string>(name, {size, utils::constraints::PRIMARY_KEY});
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
|
||||
return {
|
||||
name, utils::basic_type::type_varchar, 0,
|
||||
std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
const auto ref_column = std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,188 @@
|
|||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::object {
|
||||
attribute_definition::attribute_definition(const char *name)
|
||||
: attribute_definition(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name)
|
||||
: attribute_definition(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt,
|
||||
const int index)
|
||||
: attribute_definition(std::move(name), type, "", {attr, null_opt, index}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const int index,
|
||||
const std::shared_ptr<attribute_definition> &ref_column,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt)
|
||||
: attribute_definition(std::move(name), type, "", {attr, null_opt, index}, ref_column) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition( std::string name, const utils::basic_type type, const std::shared_ptr<attribute_definition>& ref_column )
|
||||
: attribute_definition(std::move(name), type, {}, {}, ref_column) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute_definition>& ref_column)
|
||||
: name_(std::move(name))
|
||||
, table_name_(std::move(table_name))
|
||||
, options_( options )
|
||||
, type_( type )
|
||||
, reference_column_( ref_column ) {
|
||||
}
|
||||
|
||||
const std::string &attribute_definition::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
void attribute_definition::name( const std::string& n ) {
|
||||
name_ = n;
|
||||
}
|
||||
|
||||
std::string attribute_definition::full_name() const {
|
||||
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
|
||||
}
|
||||
|
||||
// std::shared_ptr<object_definition> attribute_definition::object() const {
|
||||
// return object_;
|
||||
// }
|
||||
//
|
||||
// void attribute_definition::object(const std::shared_ptr<object_definition>& obj) {
|
||||
// object_ = obj;
|
||||
// }
|
||||
//
|
||||
int attribute_definition::index() const {
|
||||
return options_.index;
|
||||
}
|
||||
|
||||
const utils::field_attributes &attribute_definition::attributes() const {
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
utils::field_attributes& attribute_definition::attributes() {
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_nullable() const {
|
||||
return options_.null_option == null_option_type::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type attribute_definition::type() const {
|
||||
return type_;
|
||||
}
|
||||
|
||||
const std::string& attribute_definition::table_name() const {
|
||||
return table_name_;
|
||||
}
|
||||
|
||||
void attribute_definition::table_name( const std::string& name ) {
|
||||
table_name_ = name;
|
||||
}
|
||||
|
||||
void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||
options_.attributes = attr;
|
||||
type_ = type;
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute_definition> attribute_definition::reference_column() const {
|
||||
return reference_column_;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_foreign_reference() const {
|
||||
return reference_column_ != nullptr;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_integer() const {
|
||||
return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_floating_point() const {
|
||||
return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_bool() const {
|
||||
return type_ == utils::basic_type::type_bool;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_string() const {
|
||||
return type_ == utils::basic_type::type_text;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_varchar() const {
|
||||
return type_ == utils::basic_type::type_varchar;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_date() const {
|
||||
return type_ == utils::basic_type::type_date;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_time() const {
|
||||
return type_ == utils::basic_type::type_time;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_blob() const {
|
||||
return type_ == utils::basic_type::type_blob;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_null() const {
|
||||
return type_ == utils::basic_type::type_null;
|
||||
}
|
||||
|
||||
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
|
||||
null_option_type null_opt) {
|
||||
return {name, type, attr, null_opt};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_column<std::string>(const std::string &name, utils::field_attributes attr,
|
||||
null_option_type null_opt) {
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size) {
|
||||
return make_column<std::string>(name, {size, utils::constraints::PRIMARY_KEY});
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
|
||||
return {
|
||||
name, utils::basic_type::type_varchar, 0,
|
||||
std::make_shared<attribute_definition>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
const auto ref_column = std::make_shared<attribute_definition>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_definition_generator::attribute_definition_generator(std::vector<attribute_definition> &columns, const repository &repo)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
{}
|
||||
|
||||
void attribute_definition_generator::on_revision(const char *id, uint64_t &rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<attribute_definition>, utils::error> attribute_definition_generator::determine_foreign_ref(const std::type_index &ti) const {
|
||||
return repo_.reference_column(ti);
|
||||
}
|
||||
|
||||
void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr<attribute_definition>& ref_column) const {
|
||||
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
{}
|
||||
|
||||
void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<attribute>, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
|
||||
return repo_.reference_column(ti);
|
||||
}
|
||||
|
||||
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr<attribute>& ref_column) const {
|
||||
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,10 +5,20 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// utils::identifier &&pk,
|
||||
// const std::shared_ptr<attribute_definition> &pk_column,
|
||||
// const std::shared_ptr<object_definition> &definition)
|
||||
// : node_(std::move(node))
|
||||
// , definition_(definition)
|
||||
// , identifier_(std::move(pk))
|
||||
// , pk_column_(pk_column) {
|
||||
// }
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::vector<attribute> &attributes,
|
||||
const std::vector<attribute_definition> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute> &pk_as_fk_column)
|
||||
const std::shared_ptr<attribute_definition> &pk_as_fk_column)
|
||||
: node_(std::move(node))
|
||||
, attributes_(attributes)
|
||||
, identifier_(std::move(pk))
|
||||
|
|
@ -16,7 +26,7 @@ basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
|||
}
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::vector<attribute> &attributes)
|
||||
const std::vector<attribute_definition> &attributes)
|
||||
: node_(std::move(node))
|
||||
, attributes_(attributes) {}
|
||||
|
||||
|
|
@ -28,11 +38,11 @@ std::string basic_object_info::name() const {
|
|||
return node_->name();
|
||||
}
|
||||
|
||||
const std::vector<attribute>& basic_object_info::attributes() const {
|
||||
const std::vector<attribute_definition>& basic_object_info::attributes() const {
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute> basic_object_info::reference_column() const {
|
||||
std::shared_ptr<attribute_definition> basic_object_info::reference_column() const {
|
||||
return pk_as_fk_column_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,30 +4,30 @@
|
|||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object::internal {
|
||||
shadow_repository::shadow_repository( repository& s )
|
||||
: repo_(s) {}
|
||||
shadow_repository::shadow_repository( object::repository& s )
|
||||
: schema_(s) {}
|
||||
|
||||
repository& shadow_repository::repo() const {
|
||||
return repo_;
|
||||
return schema_;
|
||||
}
|
||||
|
||||
bool shadow_repository::contains( const std::type_index& ti ) const {
|
||||
return repo_.contains(ti);
|
||||
return schema_.contains(ti);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
||||
return repo_.find_node(type_index);
|
||||
return schema_.find_node(type_index);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::string& name ) const {
|
||||
return repo_.find_node(name);
|
||||
return schema_.find_node(name);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::attach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||
return repo_.attach_node(node, "");
|
||||
return schema_.attach_node(node, "");
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> shadow_repository::detach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||
return repo_.detach(node);
|
||||
return schema_.detach(node);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
object_definition::object_definition(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
object_definition::object_definition(std::string name, const std::initializer_list<attribute_definition> columns)
|
||||
: name_(std::move(name))
|
||||
, columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(std::string name, const std::vector<attribute_definition> &columns)
|
||||
: name_(std::move(name))
|
||||
, columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const object_definition &x)
|
||||
: name_(x.name_)
|
||||
, columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_) {
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
}
|
||||
|
||||
object_definition &object_definition::operator=(const object_definition &x)
|
||||
{
|
||||
if (&x == this) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
name_ = x.name_;
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& object_definition::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool object_definition::has_primary_key() const {
|
||||
return pk_index_ > -1;
|
||||
}
|
||||
|
||||
std::optional<attribute_definition> object_definition::primary_key() const
|
||||
{
|
||||
if (!has_primary_key()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
void object_definition::append(attribute_definition&& col) {
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
}
|
||||
|
||||
const std::vector<object::attribute_definition> &object_definition::columns() const {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at(const std::string &name) const
|
||||
{
|
||||
return columns_by_name_.at(name).first;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at( const size_t index) const
|
||||
{
|
||||
return columns_.at(index);
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::find(const std::string &column_name)
|
||||
{
|
||||
const auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::find(const std::string &column_name) const {
|
||||
const auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::end()
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
}
|
||||
|
||||
size_t object_definition::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
bool object_definition::empty() const
|
||||
{
|
||||
return columns_.empty();
|
||||
}
|
||||
|
||||
void object_definition::clear()
|
||||
{
|
||||
columns_.clear();
|
||||
columns_by_name_.clear();
|
||||
}
|
||||
|
||||
void object_definition::init()
|
||||
{
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
add_to_map(col, index++);
|
||||
}
|
||||
}
|
||||
|
||||
void object_definition::add_to_map(attribute_definition &col, size_t index)
|
||||
{
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
pk_index_ = static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -62,7 +62,7 @@ utils::result<basic_object_info_ref, utils::error> repository::basic_info( const
|
|||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<attribute>, utils::error> repository::reference_column(const std::type_index &type_index) const {
|
||||
utils::result<std::shared_ptr<attribute_definition>, utils::error> repository::reference_column(const std::type_index &type_index) const {
|
||||
const auto result = find_node(type_index);
|
||||
if (result) {
|
||||
return utils::ok((*result)->info().reference_column());
|
||||
|
|
|
|||
|
|
@ -104,13 +104,13 @@ utils::result<repository_node::node_ptr, utils::error> repository_node::make_and
|
|||
return repo.attach_node(node, "");
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute> repository_node::determine_reference_column(const std::type_index& ti,
|
||||
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo) {
|
||||
const auto it = repo.missing_references_.find(ti);
|
||||
if (it == repo.missing_references_.end()) {
|
||||
return std::make_shared<attribute>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
return std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
auto ref_column = it->second;
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::drop_t
|
|||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
matador::utils::result<std::vector<matador::object::attribute>, matador::utils::error> matador::orm::schema::describe_table(const std::string& table_name) const {
|
||||
matador::utils::result<std::vector<matador::object::attribute_definition>, matador::utils::error> matador::orm::schema::describe_table(const std::string& table_name) const {
|
||||
const auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ utils::result<sql::query_result<sql::record>, utils::error> session::fetch_all(c
|
|||
if (const auto rit = std::find_if(it->second.begin(), it->second.end(), [&col](const auto &value) {
|
||||
return value.name() == col.name();
|
||||
}); rit != it->second.end()) {
|
||||
const_cast<object::attribute &>(col).change_type(rit->type());
|
||||
const_cast<object::attribute_definition &>(col).change_type(rit->type());
|
||||
}
|
||||
}
|
||||
auto res = fetch(q);
|
||||
|
|
@ -120,7 +120,7 @@ utils::result<size_t, utils::error> session::execute(const std::string &sql) con
|
|||
return execute(sql::query_context{sql});
|
||||
}
|
||||
|
||||
std::vector<object::attribute> session::describe_table(const std::string &table_name) const {
|
||||
std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const {
|
||||
const auto c = cache_.pool().acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
|
|
|
|||
|
|
@ -2,17 +2,11 @@
|
|||
|
||||
namespace matador::query {
|
||||
|
||||
column_builder::column_builder(std::string column_name, const utils::basic_type type, const size_t size)
|
||||
: column_name_(std::move(column_name))
|
||||
, type_(type){}
|
||||
column_builder::column_builder(std::string column_name)
|
||||
: column_name( std::move(column_name) ) {}
|
||||
|
||||
column_builder::operator class query::column() const {
|
||||
return {column_name_};
|
||||
}
|
||||
|
||||
column_builder& column_builder::not_null() {
|
||||
|
||||
return *this;
|
||||
return {column_name};
|
||||
}
|
||||
|
||||
table_builder::table_builder(std::string name)
|
||||
|
|
@ -24,7 +18,7 @@ table_builder& table_builder::as( std::string table_alias ) {
|
|||
}
|
||||
|
||||
table_builder::operator class query::table() const {
|
||||
return {table_name, alias, {}};
|
||||
return {table_name, alias};
|
||||
}
|
||||
|
||||
constraint_builder& constraint_builder::constraint( std::string name ) {
|
||||
|
|
@ -61,8 +55,8 @@ table_builder table( std::string name ) {
|
|||
return table_builder(std::move(name));
|
||||
}
|
||||
|
||||
column_builder column(std::string name, const utils::basic_type type, size_t size) {
|
||||
return column_builder(std::move(name), type, size);
|
||||
column_builder column( std::string name ) {
|
||||
return column_builder(std::move(name));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -98,9 +98,4 @@ std::shared_ptr<table> column::table() const {
|
|||
void column::table( const std::shared_ptr<query::table>& t ) {
|
||||
table_ = t;
|
||||
}
|
||||
|
||||
column::operator const std::string&() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
namespace matador::query {
|
||||
|
||||
namespace detail {
|
||||
sql::record *create_prototype(const std::vector<object::attribute> &prototype) {
|
||||
sql::record *create_prototype(const std::vector<object::attribute_definition> &prototype) {
|
||||
auto result = std::make_unique<sql::record>();
|
||||
for (const auto &col: prototype) {
|
||||
result->append({
|
||||
|
|
|
|||
|
|
@ -8,11 +8,11 @@ query_create_intermediate::query_create_intermediate() {
|
|||
context_->parts.push_back(std::make_unique<internal::query_create_part>());
|
||||
}
|
||||
|
||||
executable_query query_create_intermediate::table(const class table &tab, const std::initializer_list<object::attribute> columns) {
|
||||
return this->table(tab, std::vector<object::attribute>{columns});
|
||||
executable_query query_create_intermediate::table(const class table &tab, const std::initializer_list<object::attribute_definition> columns) {
|
||||
return this->table(tab, std::vector<object::attribute_definition>{columns});
|
||||
}
|
||||
|
||||
executable_query query_create_intermediate::table(const class table &tab, const std::vector<object::attribute> &columns) {
|
||||
executable_query query_create_intermediate::table(const class table &tab, const std::vector<object::attribute_definition> &columns) {
|
||||
context_->parts.push_back(std::make_unique<internal::query_create_table_part>(tab, columns));
|
||||
return {context_};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -344,7 +344,7 @@ void query_create_part::accept(query_part_visitor &visitor)
|
|||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_create_table_part::query_create_table_part(class table table, std::vector<object::attribute> columns)
|
||||
query_create_table_part::query_create_table_part(class table table, std::vector<object::attribute_definition> columns)
|
||||
: query_part(sql::dialect_token::Table)
|
||||
, table_(std::move(table))
|
||||
, columns_(std::move(columns)) {}
|
||||
|
|
@ -354,7 +354,7 @@ const table &query_create_table_part::table() const
|
|||
return table_;
|
||||
}
|
||||
|
||||
const std::vector<object::attribute> &query_create_table_part::columns() const
|
||||
const std::vector<object::attribute_definition> &query_create_table_part::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ namespace matador::sql {
|
|||
detail::pk_reader::pk_reader(query_result_reader &reader)
|
||||
: reader_(reader) {}
|
||||
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute> &&prototype, const size_t column_index)
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute_definition> &&prototype, const size_t column_index)
|
||||
: column_index_(column_index)
|
||||
, prototype_(std::move(prototype))
|
||||
, reader_(std::move(reader))
|
||||
, pk_reader_(*reader_)
|
||||
{}
|
||||
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute> &prototype, const size_t column_index)
|
||||
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute_definition> &prototype, const size_t column_index)
|
||||
: column_index_(column_index)
|
||||
, prototype_(prototype)
|
||||
, reader_(std::move(reader))
|
||||
|
|
@ -43,7 +43,7 @@ query_result_impl::on_attribute(const char *id, utils::value &val, const utils::
|
|||
reader_->read_value(id, column_index_++, val, attr.size());
|
||||
}
|
||||
|
||||
const std::vector<object::attribute>& query_result_impl::prototype() const
|
||||
const std::vector<object::attribute_definition>& query_result_impl::prototype() const
|
||||
{
|
||||
return prototype_;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#include "matador/query/query_compiler.hpp"
|
||||
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
#include "matador/query/attribute_string_writer.hpp"
|
||||
#include "matador/query/query_data.hpp"
|
||||
#include "matador/query/criteria_evaluator.hpp"
|
||||
|
|
@ -291,7 +293,7 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
|
|||
|
||||
struct fk_context {
|
||||
std::string column;
|
||||
std::shared_ptr<object::attribute> reference_column;
|
||||
std::shared_ptr<object::attribute_definition> reference_column;
|
||||
};
|
||||
|
||||
struct column_context
|
||||
|
|
@ -300,7 +302,7 @@ struct column_context
|
|||
std::vector<fk_context> foreign_contexts;
|
||||
};
|
||||
|
||||
std::string build_create_column(const object::attribute &col, const sql::dialect &d, column_context &context);
|
||||
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context);
|
||||
|
||||
void query_compiler::visit(internal::query_create_table_part &part)
|
||||
{
|
||||
|
|
@ -387,7 +389,7 @@ void query_compiler::visit(internal::query_drop_table_part &part)
|
|||
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
||||
}
|
||||
|
||||
std::string build_create_column(const object::attribute &col, const sql::dialect &d, column_context &context)
|
||||
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context)
|
||||
{
|
||||
std::string result = d.prepare_identifier_string(col.name()) + " " + d.data_type_at(col.type());
|
||||
if (col.attributes().size() > 0) {
|
||||
|
|
|
|||
|
|
@ -10,12 +10,17 @@ table::table(std::string name)
|
|||
: name_(std::move(name))
|
||||
{}
|
||||
|
||||
table::table(const char *name, std::string as)
|
||||
: name_(name)
|
||||
, alias_(std::move(as))
|
||||
{}
|
||||
|
||||
table::table(std::string name, std::string as)
|
||||
: name_(std::move(name))
|
||||
, alias_(std::move(as))
|
||||
{}
|
||||
|
||||
table::table( std::string name, std::string as, const std::vector<query::column>& columns )
|
||||
table::table( std::string name, std::string as, const std::vector<column>& columns )
|
||||
: name_(std::move(name))
|
||||
, alias_(std::move(as))
|
||||
, columns_(columns) {}
|
||||
|
|
@ -49,14 +54,6 @@ const std::vector<column>& table::columns() const {
|
|||
return columns_;
|
||||
}
|
||||
|
||||
column table::column( const std::string& name ) const {
|
||||
return {*this, name};
|
||||
}
|
||||
|
||||
table::operator const std::vector<query::column>&() const {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
table operator ""_tab(const char *name, size_t len) {
|
||||
return {{name, len}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ utils::result<void, utils::error> connection::rollback() const {
|
|||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> connection::describe(const std::string &table_name) const {
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> connection::describe(const std::string &table_name) const {
|
||||
return connection_->describe(table_name);
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +154,7 @@ utils::result<size_t, utils::error> connection::execute(const std::string &sql)
|
|||
return connection_->execute(sql);
|
||||
}
|
||||
|
||||
bool has_unknown_columns(const std::vector<object::attribute> &columns) {
|
||||
bool has_unknown_columns(const std::vector<object::attribute_definition> &columns) {
|
||||
return std::any_of(std::begin(columns), std::end(columns), [](const auto &col) {
|
||||
return col.type() == utils::basic_type::type_null;
|
||||
});
|
||||
|
|
@ -229,7 +229,7 @@ utils::result<std::unique_ptr<statement_impl>, utils::error> connection::perform
|
|||
[&col](const auto &value) { return value.name() == col.name(); }
|
||||
);
|
||||
if (col.type() == utils::basic_type::type_null && rit != result->end()) {
|
||||
const_cast<object::attribute&>(col).change_type(rit->type());
|
||||
const_cast<object::attribute_definition&>(col).change_type(rit->type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
namespace matador::sql::detail {
|
||||
|
||||
template<>
|
||||
record *create_prototype<record>(const std::vector<object::attribute> &prototype) {
|
||||
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype) {
|
||||
auto result = std::make_unique<record>();
|
||||
for (const auto &col: prototype) {
|
||||
result->append({
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include "catch2/catch_test_macros.hpp"
|
||||
#include "catch2/matchers/catch_matchers_string.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
|
@ -68,7 +68,7 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
|
|||
ucval, usval, uival, ullval,
|
||||
float_value, double_value,
|
||||
bval,
|
||||
// "Armer schwarzer Kater",
|
||||
"Armer schwarzer Kater",
|
||||
strval, varcharval,
|
||||
// date_val, time_val,
|
||||
blob_val
|
||||
|
|
@ -98,7 +98,7 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
|
|||
REQUIRE((*result)->unsigned_long64_ == ullval);
|
||||
REQUIRE((*result)->float_ == float_value);
|
||||
REQUIRE((*result)->double_ == double_value);
|
||||
// REQUIRE(strcmp((*result)->cstr_, cstr) == 0);
|
||||
REQUIRE(strcmp((*result)->cstr_, cstr) == 0);
|
||||
REQUIRE((*result)->bool_ == bval);
|
||||
REQUIRE((*result)->varchar_ == varcharval);
|
||||
REQUIRE((*result)->string_ == strval);
|
||||
|
|
@ -294,30 +294,30 @@ TEST_CASE_METHOD(QueryFixture, "Test describe table", "[query][describe][table]"
|
|||
"val_char", "val_float", "val_double", "val_short",
|
||||
"val_int", "val_long_long", "val_unsigned_char",
|
||||
"val_unsigned_short", "val_unsigned_int", "val_unsigned_long_long",
|
||||
"val_bool", /*"val_cstr", */"val_string", "val_varchar",
|
||||
"val_bool", "val_cstr", "val_string", "val_varchar",
|
||||
// "val_date", "val_time",
|
||||
"val_binary"};
|
||||
const std::vector<std::function<bool (const attribute&)>> type_check = {
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_floating_point(); },
|
||||
[](const attribute &cf) { return cf.is_floating_point(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
const std::vector<std::function<bool (const attribute_definition&)>> type_check = {
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_floating_point(); },
|
||||
[](const attribute_definition &cf) { return cf.is_floating_point(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
// [](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
// [](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_integer(); },
|
||||
[](const attribute &cf) { return cf.is_bool(); },
|
||||
// [](const attribute_definition &cf) { return cf.is_varchar(); },
|
||||
[](const attribute &cf) { return cf.is_string(); },
|
||||
[](const attribute &cf) { return cf.is_varchar(); },
|
||||
[](const attribute_definition &cf) { return cf.is_integer(); },
|
||||
[](const attribute_definition &cf) { return cf.is_bool(); },
|
||||
[](const attribute_definition &cf) { return cf.is_varchar(); },
|
||||
[](const attribute_definition &cf) { return cf.is_string(); },
|
||||
[](const attribute_definition &cf) { return cf.is_varchar(); },
|
||||
// [](const attribute_definition &cf) { return cf.is_date(); },
|
||||
// [](const attribute_definition &cf) { return cf.is_time(); },
|
||||
[](const attribute &cf) { return cf.is_blob(); }
|
||||
[](const attribute_definition &cf) { return cf.is_blob(); }
|
||||
};
|
||||
|
||||
const auto &cols = columns.value();
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ using namespace matador::test;
|
|||
TEST_CASE_METHOD(QueryFixture, "Test create statement", "[query][statement][create]") {
|
||||
REQUIRE(repo.attach<matador::test::person>("person"));
|
||||
auto stmt = query::create()
|
||||
.table<person>("person", repo)
|
||||
.table<person>("person"_tab, repo)
|
||||
.prepare(db);
|
||||
REQUIRE(stmt);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/generator.hpp"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "../test/models/product.hpp"
|
||||
|
|
@ -17,18 +17,18 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
|||
.and_then([&repo] { return repo.attach<matador::test::category>("categories"); });
|
||||
REQUIRE(result);
|
||||
|
||||
auto columns = attribute_generator::generate<matador::test::product>(repo);
|
||||
auto columns = attribute_definition_generator::generate<matador::test::product>(repo);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
attribute{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option_type::NOT_NULL },
|
||||
attribute{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL },
|
||||
attribute{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, 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_definition{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option_type::NOT_NULL },
|
||||
attribute_definition{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL },
|
||||
attribute_definition{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL },
|
||||
attribute_definition{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"unit_price", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"units_in_stock", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"units_in_order", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"reorder_level", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"discontinued", basic_type::type_bool, null_attributes, null_option_type::NOT_NULL }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
|
@ -43,12 +43,12 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
|||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||
repository repo("main");
|
||||
|
||||
auto columns = attribute_generator::generate<matador::test::optional>(repo);
|
||||
auto columns = attribute_definition_generator::generate<matador::test::optional>(repo);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
attribute{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, 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_definition{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, null_option_type::NOT_NULL },
|
||||
attribute_definition{"name", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL },
|
||||
attribute_definition{"age", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace matador::test {
|
|||
|
||||
struct types
|
||||
{
|
||||
// enum { CSTR_LEN=255 };
|
||||
enum { CSTR_LEN=255 };
|
||||
|
||||
unsigned int id_ = 0;
|
||||
int8_t char_ = 'c';
|
||||
|
|
@ -22,7 +22,7 @@ struct types
|
|||
float float_ = 3.1415f;
|
||||
double double_ = 1.1414;
|
||||
bool bool_ = true;
|
||||
// char cstr_[CSTR_LEN]{};
|
||||
char cstr_[CSTR_LEN]{};
|
||||
std::string string_ = "Welt";
|
||||
std::string varchar_ = "Erde";
|
||||
// matador::date date_;
|
||||
|
|
@ -46,7 +46,7 @@ struct types
|
|||
field::attribute(op, "val_unsigned_int", unsigned_int_);
|
||||
field::attribute(op, "val_unsigned_long_long", unsigned_long64_);
|
||||
field::attribute(op, "val_bool", bool_);
|
||||
// field::attribute(op, "val_cstr", cstr_, CSTR_LEN);
|
||||
field::attribute(op, "val_cstr", cstr_, CSTR_LEN);
|
||||
field::attribute(op, "val_string", string_);
|
||||
field::attribute(op, "val_varchar", varchar_, 63);
|
||||
// field::attribute(op, "val_date", date_);
|
||||
|
|
|
|||
|
|
@ -57,8 +57,8 @@ utils::result<std::unique_ptr<sql::statement_impl>, utils::error> test_connectio
|
|||
return utils::ok(std::move(s));
|
||||
}
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> test_connection::describe(const std::string &/*table*/) {
|
||||
return utils::ok(std::vector<object::attribute>{});
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> test_connection::describe(const std::string &/*table*/) {
|
||||
return utils::ok(std::vector<object::attribute_definition>{});
|
||||
}
|
||||
|
||||
utils::result<bool, utils::error> test_connection::exists(const std::string &/*schema_name*/, const std::string &/*table_name*/) {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public:
|
|||
utils::result<size_t, utils::error> execute(const std::string &stmt) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::vector<object::attribute>, utils::error> describe(const std::string &table) override;
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
[[nodiscard]] std::string to_escaped_string( const utils::blob& value ) const override;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <matador/query/query.hpp>
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Test create empty column", "[column]") {
|
||||
attribute c("name");
|
||||
attribute_definition c("name");
|
||||
|
||||
REQUIRE(c.name() == "name");
|
||||
REQUIRE(c.index() == -1);
|
||||
|
|
@ -21,11 +22,11 @@ TEST_CASE("Test create empty column", "[column]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Test copy and move column", "[column]") {
|
||||
attribute c(
|
||||
attribute_definition c(
|
||||
"name",
|
||||
basic_type::type_varchar,
|
||||
2,
|
||||
std::make_shared<attribute>("author", basic_type::type_uint32, "books", attribute_options{constraints::FOREIGN_KEY}),
|
||||
std::make_shared<attribute_definition>("author", basic_type::type_uint32, "books", attribute_options{constraints::FOREIGN_KEY}),
|
||||
{255, constraints::FOREIGN_KEY},
|
||||
null_option_type::NOT_NULL
|
||||
);
|
||||
|
|
|
|||
361
todo.md
361
todo.md
|
|
@ -20,245 +20,12 @@
|
|||
|
||||
## book class
|
||||
```cpp
|
||||
class Book : public matador::object::typed_object<Book> {
|
||||
public:
|
||||
using typed_object::as;
|
||||
|
||||
Book() : typed_object("book") {}
|
||||
explicit Book(std::string alias)
|
||||
: typed_object("book", std::move(alias)) {}
|
||||
|
||||
const matador::object::attribute id = create_attribute("id", *this);
|
||||
const matador::object::attribute title = create_attribute("title", *this);
|
||||
const matador::object::attribute year = create_attribute("year", *this);
|
||||
};
|
||||
```
|
||||
|
||||
## aliasable_table
|
||||
```cpp
|
||||
class object;
|
||||
|
||||
class attribute {
|
||||
public:
|
||||
explicit attribute(std::string name); // NOLINT(*-explicit-constructor)
|
||||
|
||||
attribute(const attribute&) = default;
|
||||
attribute& operator=(const attribute&) = default;
|
||||
attribute(attribute&&) noexcept = default;
|
||||
attribute& operator=(attribute&&) noexcept = default;
|
||||
|
||||
attribute() = default;
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const utils::field_attributes& attr = utils::null_attributes,
|
||||
utils::null_option_type null_opt = utils::null_option_type::NotNull);
|
||||
|
||||
[[nodiscard]] const std::string& name() const { return name_; }
|
||||
void name(const std::string& n);
|
||||
[[nodiscard]] const utils::field_attributes& attributes() const;
|
||||
[[nodiscard]] utils::field_attributes& attributes();
|
||||
|
||||
[[nodiscard]] bool is_nullable() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
|
||||
[[nodiscard]] bool is_integer() const;
|
||||
[[nodiscard]] bool is_floating_point() const;
|
||||
[[nodiscard]] bool is_bool() const;
|
||||
[[nodiscard]] bool is_string() const;
|
||||
[[nodiscard]] bool is_varchar() const;
|
||||
[[nodiscard]] bool is_date() const;
|
||||
[[nodiscard]] bool is_time() const;
|
||||
[[nodiscard]] bool is_blob() const;
|
||||
[[nodiscard]] bool is_null() const;
|
||||
|
||||
private:
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
utils::field_attributes attributes_;
|
||||
utils::null_option_type null_option_type_{utils::null_option_type::NotNull};
|
||||
|
||||
object* parent_{nullptr};
|
||||
};
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
constraint() = default;
|
||||
|
||||
explicit constraint(std::string name) : name_(std::move(name)) {}
|
||||
[[nodiscard]] const std::string& name() const { return name_; }
|
||||
|
||||
private:
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
std::string attribute_name_;
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
|
||||
object* parent_{nullptr};
|
||||
};
|
||||
|
||||
class object {
|
||||
public:
|
||||
using iterator = std::vector<attribute>::iterator;
|
||||
using const_iterator = std::vector<attribute>::const_iterator;
|
||||
|
||||
explicit object(std::string name, std::string alias = "");
|
||||
|
||||
void add_attribute(attribute attr) {
|
||||
auto &ref = attributes_.emplace_back(std::move(attr));
|
||||
ref.parent_ = this;
|
||||
}
|
||||
|
||||
static const attribute& create_attribute(std::string name, object& obj) {
|
||||
attribute attr{std::move(name)};
|
||||
attr.parent_ = &obj;
|
||||
return obj.attributes_.emplace_back(std::move(attr));
|
||||
}
|
||||
|
||||
iterator begin() { return attributes_.begin(); }
|
||||
iterator end() { return attributes_.end(); }
|
||||
|
||||
[[nodiscard]] bool empty() const { return attributes_.empty(); }
|
||||
[[nodiscard]] size_t size() const { return attributes_.size(); }
|
||||
|
||||
[[nodiscard]] const std::string& name() const { return name_; }
|
||||
[[nodiscard]] const std::string& alias() const { return alias_; }
|
||||
[[nodiscard]] const std::vector<attribute>& columns() const { return attributes_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
std::vector<attribute> attributes_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class typed_object : public object {
|
||||
public:
|
||||
using object::object;
|
||||
|
||||
Type as(std::string alias) { return Type{std::move(alias)}; }
|
||||
};
|
||||
```
|
||||
|
||||
```cpp
|
||||
struct column_builder {
|
||||
explicit column_builder(std::string column_name)
|
||||
: column_name( std::move(column_name) ) {
|
||||
}
|
||||
|
||||
column_builder& not_null() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
column_builder& primary_key() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator matador::sql::column() const {
|
||||
return matador::sql::column{column_name};
|
||||
}
|
||||
|
||||
std::string column_name;
|
||||
};
|
||||
|
||||
column_builder column(std::string name) {
|
||||
return column_builder(std::move(name));
|
||||
}
|
||||
|
||||
namespace matador::sql {
|
||||
struct constraint {
|
||||
std::string name;
|
||||
};
|
||||
}
|
||||
|
||||
struct constraint_builder {
|
||||
constraint_builder& constraint(std::string name) {
|
||||
constraint_name = std::move(name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder& primary_key(std::string name) {
|
||||
pk_column_name = std::move(name);
|
||||
return *this;
|
||||
}
|
||||
constraint_builder& foreign_key(std::string name) {
|
||||
fk_column_name = std::move(name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder& references(std::string table, std::string column) {
|
||||
this->table_name = std::move(table);
|
||||
this->column_name = std::move(column);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator matador::sql::constraint() const {
|
||||
return matador::sql::constraint{constraint_name};
|
||||
}
|
||||
|
||||
std::string constraint_name;
|
||||
std::string pk_column_name;
|
||||
std::string fk_column_name;
|
||||
std::string table_name;
|
||||
std::string column_name;
|
||||
};
|
||||
|
||||
constraint_builder constraint(std::string name) {
|
||||
constraint_builder builder;
|
||||
return builder.constraint(std::move(name));
|
||||
}
|
||||
void foo(const std::initializer_list<matador::sql::column> columns) {
|
||||
for (const auto& column : columns) {
|
||||
std::cout << column.name() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void foo(const std::vector<matador::sql::column>& columns) {
|
||||
for (const auto& column : columns) {
|
||||
std::cout << column.name() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::string column_prefix(matador::sql::aliasable_table<Type> *tab) {
|
||||
if (!tab || tab->empty()) {
|
||||
return "";
|
||||
}
|
||||
if (!tab->alias().empty()) {
|
||||
return tab->alias();
|
||||
}
|
||||
return tab->name();
|
||||
}
|
||||
struct table_builder {
|
||||
explicit table_builder(std::string name)
|
||||
: table_name( std::move(name) ) {}
|
||||
|
||||
table_builder& as(std::string table_alias) {
|
||||
this->alias = std::move(table_alias);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator matador::query::query_table() const {
|
||||
return {matador::sql::table{table_name}, alias};
|
||||
}
|
||||
std::string table_name;
|
||||
std::string alias;
|
||||
};
|
||||
|
||||
table_builder table(std::string name) {
|
||||
return table_builder(std::move(name));
|
||||
}
|
||||
|
||||
class Book : public matador::sql::aliasable_table<Book> {
|
||||
class book : public matador::sql::aliasable_table<Book> {
|
||||
public:
|
||||
using aliasable_table::as;
|
||||
|
||||
Book() : aliasable_table("book", "") {}
|
||||
|
||||
|
||||
private:
|
||||
friend class aliasable_table;
|
||||
|
||||
|
|
@ -270,100 +37,56 @@ public:
|
|||
matador::sql::column title = create_column("title", *this);
|
||||
matador::sql::column year = create_column("year", *this);
|
||||
};
|
||||
```
|
||||
|
||||
struct base_node {
|
||||
base_node() = delete;
|
||||
base_node(std::string name)
|
||||
: name(std::move(name)) {}
|
||||
virtual ~base_node() = default;
|
||||
## aliasable_table
|
||||
```cpp
|
||||
class table {
|
||||
public:
|
||||
using iterator = std::vector<column>::iterator;
|
||||
using const_iterator = std::vector<column>::const_iterator;
|
||||
|
||||
std::string name;
|
||||
};
|
||||
template < typename Type>
|
||||
struct node final {
|
||||
virtual ~node() = default;
|
||||
explicit table(std::string name) : name_(std::move(name)) {}
|
||||
table(std::string name, std::vector<column> columns)
|
||||
: name_(std::move(name)), columns_(std::move(columns)) {}
|
||||
|
||||
Type type;
|
||||
};
|
||||
void add_column(column column) {
|
||||
column.table_ = shared_from_this();
|
||||
columns_.emplace_back(std::move(column));
|
||||
}
|
||||
|
||||
template < template<typename> typename NodeType = node, typename BaseType = base_node >
|
||||
struct repository {
|
||||
template<typename Type>
|
||||
NodeType<Type> find(std::string name);
|
||||
static const column& create_column(std::string name, table& t) {
|
||||
column c{std::move(name)};
|
||||
c.table_ = &t;
|
||||
t.columns_.emplace_back(std::move(c));
|
||||
return t.columns_.back();
|
||||
}
|
||||
operator const std::vector<column>&() const { return columns_; }
|
||||
table as_table() const { return {name_, columns_}; }
|
||||
|
||||
std::unordered_map<std::string, BaseType> nodes;
|
||||
};
|
||||
iterator begin() { return columns_.begin(); }
|
||||
iterator end() { return columns_.end(); }
|
||||
|
||||
struct base_schema_node : base_node {
|
||||
base_schema_node() = delete;
|
||||
base_schema_node(std::string name, std::string table)
|
||||
: base_node(std::move(name)), table_name(std::move(table)) {}
|
||||
[[nodiscard]] bool empty() const { return columns_.empty(); }
|
||||
[[nodiscard]] size_t size() const { return columns_.size(); }
|
||||
|
||||
std::string table_name;
|
||||
[[nodiscard]] const std::string& name() const { return name_; }
|
||||
[[nodiscard]] const std::vector<column>& columns() const { return columns_; }
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<column> columns_;
|
||||
};
|
||||
|
||||
template < typename Type >
|
||||
struct schema_node final : base_schema_node {
|
||||
schema_node() = default;
|
||||
schema_node(std::string name, std::string table)
|
||||
: base_schema_node(std::move(name), std::move(table)) {}
|
||||
class aliasable_table : public table {
|
||||
public:
|
||||
aliasable_table(std::string name, std::string alias)
|
||||
: table(std::move(name))
|
||||
, alias_(std::move(alias)) {}
|
||||
|
||||
Type type;
|
||||
Type as(std::string alias) { return Type{std::move(alias)}; }
|
||||
|
||||
private:
|
||||
std::string alias_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
repository repo1;
|
||||
repository<schema_node, base_schema_node> repo;
|
||||
|
||||
Book BOOK;
|
||||
|
||||
const auto b = BOOK.as("b");
|
||||
|
||||
foo(b);
|
||||
foo(BOOK);
|
||||
foo({
|
||||
BOOK.id,
|
||||
b.title,
|
||||
column("col1").primary_key(),
|
||||
column("col2").not_null()
|
||||
});
|
||||
matador::sql::schema schema("test");
|
||||
|
||||
schema.add_table(BOOK.as_table());
|
||||
// query::create()
|
||||
// .table(BOOK)
|
||||
// .columns({
|
||||
// BOOK.id,
|
||||
// BOOK.title,
|
||||
// column("year"),
|
||||
// column("author_id")
|
||||
// })
|
||||
// .constraints({
|
||||
// constraint( "PK" ).primary_key("id"),
|
||||
// constraint( "FK" ).foreign_key("author_id").references("author", "id")
|
||||
// });
|
||||
//
|
||||
// query::create()
|
||||
// .table("book", {
|
||||
// column("id"),
|
||||
// column("title"),
|
||||
// column("year"),
|
||||
// column("author_id")
|
||||
// }, {
|
||||
// constraint( "PK" ).primary_key("id"),
|
||||
// constraint( "FK" ).foreign_key("author_id").references("author", "id")
|
||||
// });
|
||||
//
|
||||
// matador::query::query_table book = table("book").as("b");
|
||||
//
|
||||
// query::select({
|
||||
// column(book.column("id")),
|
||||
// column(book.column("title"))
|
||||
// })
|
||||
// .from(book);
|
||||
//
|
||||
// query::insert()
|
||||
// .into(b, {b.title, b.year})
|
||||
// .values({"title", 2021});
|
||||
}
|
||||
```
|
||||
Loading…
Reference in New Issue