added column generator and tests
This commit is contained in:
+11
-4
@@ -11,7 +11,8 @@ set(SQL_SOURCES
|
||||
sql/connection_impl.cpp
|
||||
sql/session.cpp
|
||||
sql/backend_provider.cpp
|
||||
sql/query_result_impl.cpp)
|
||||
sql/query_result_impl.cpp
|
||||
sql/column_generator.cpp)
|
||||
|
||||
set(SQL_HEADER
|
||||
../include/matador/sql/dialect.hpp
|
||||
@@ -31,21 +32,27 @@ set(SQL_HEADER
|
||||
../include/matador/sql/connection_pool.hpp
|
||||
../include/matador/sql/session.hpp
|
||||
../include/matador/sql/backend_provider.hpp
|
||||
../include/matador/sql/query_result_impl.hpp)
|
||||
../include/matador/sql/query_result_impl.hpp
|
||||
../include/matador/sql/column_generator.hpp)
|
||||
|
||||
set(UTILS_HEADER
|
||||
../include/matador/utils/field_attributes.hpp
|
||||
../include/matador/utils/string.hpp
|
||||
../include/matador/utils/constraints.hpp
|
||||
../include/matador/utils/library.hpp
|
||||
../include/matador/utils/os.hpp)
|
||||
../include/matador/utils/os.hpp
|
||||
../include/matador/utils/access.hpp
|
||||
../include/matador/utils/identifiable.hpp
|
||||
../include/matador/utils/identifier.hpp
|
||||
../include/matador/utils/cascade_type.hpp)
|
||||
|
||||
set(UTILS_SOURCES
|
||||
utils/field_attributes.cpp
|
||||
utils/string.cpp
|
||||
sql/condition.cpp
|
||||
utils/library.cpp
|
||||
utils/os.cpp)
|
||||
utils/os.cpp
|
||||
utils/identifier.cpp)
|
||||
|
||||
add_library(matador STATIC ${SQL_SOURCES} ${SQL_HEADER} ${UTILS_SOURCES} ${UTILS_HEADER})
|
||||
target_include_directories(matador PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column_generator::column_generator(std::vector<column> &columns)
|
||||
: columns_(columns) {}
|
||||
|
||||
void column_generator::on_primary_key(const char *id, std::string &pk, size_t size)
|
||||
{
|
||||
on_attribute(id, pk, { size, utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
void column_generator::on_revision(const char *id, unsigned long long int &x)
|
||||
{
|
||||
on_attribute(id, x);
|
||||
}
|
||||
|
||||
void column_generator::on_belongs_to(const char *id, utils::identifiable &x, utils::cascade_type)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void column_generator::on_has_one(const char *id, utils::identifiable &x, utils::cascade_type)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+15
-10
@@ -143,7 +143,12 @@ query_builder& query_builder::remove() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
query_builder& query_builder::table(const std::string &table, std::initializer_list<column> columns) {
|
||||
query_builder& query_builder::table(const std::string &table, std::initializer_list<column> columns)
|
||||
{
|
||||
return this->table(table, std::vector<column>{columns});
|
||||
}
|
||||
|
||||
query_builder &query_builder::table(const std::string &table, const std::vector<column> &columns) {
|
||||
transition_to(state_t::QUERY_TABLE_CREATE);
|
||||
|
||||
query_parts_.emplace_back(" " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " ");
|
||||
@@ -151,16 +156,16 @@ query_builder& query_builder::table(const std::string &table, std::initializer_l
|
||||
std::string result = "(";
|
||||
|
||||
if (columns.size() < 2) {
|
||||
for (const auto &col : columns) {
|
||||
result.append(build_create_column(col, dialect_));
|
||||
}
|
||||
for (const auto &col : columns) {
|
||||
result.append(build_create_column(col, dialect_));
|
||||
}
|
||||
} else {
|
||||
auto it = columns.begin();
|
||||
result.append(build_create_column(*it++, dialect_));
|
||||
for (; it != columns.end(); ++it) {
|
||||
result.append(", ");
|
||||
result.append(build_create_column(*it, dialect_));
|
||||
}
|
||||
auto it = columns.begin();
|
||||
result.append(build_create_column(*it++, dialect_));
|
||||
for (; it != columns.end(); ++it) {
|
||||
result.append(", ");
|
||||
result.append(build_create_column(*it, dialect_));
|
||||
}
|
||||
}
|
||||
|
||||
result += ")";
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <ostream>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
identifier::base::base(const std::type_index &ti, detail::identifier_type_t id_type)
|
||||
: type_index_(ti)
|
||||
, identifier_type_(id_type)
|
||||
{}
|
||||
|
||||
bool identifier::base::is_similar_type(const identifier::base &x) const
|
||||
{
|
||||
return identifier_type_ == x.type();
|
||||
}
|
||||
|
||||
detail::identifier_type_t identifier::base::type() const
|
||||
{
|
||||
return identifier_type_;
|
||||
}
|
||||
|
||||
identifier::null_pk::null_pk()
|
||||
: base(std::type_index(typeid(null_type_t)), detail::identifier_type_traits<null_type_t>::type())
|
||||
{}
|
||||
|
||||
identifier::base* identifier::null_pk::copy() const
|
||||
{
|
||||
return new null_pk;
|
||||
}
|
||||
|
||||
bool identifier::null_pk::equal_to(const identifier::base &x) const
|
||||
{
|
||||
return type_index_ == x.type_index_;
|
||||
}
|
||||
|
||||
bool identifier::null_pk::less(const identifier::base &x) const
|
||||
{
|
||||
return type_index_ == x.type_index_;
|
||||
}
|
||||
|
||||
bool identifier::null_pk::is_valid() const
|
||||
{
|
||||
return detail::identifier_type_traits<null_type_t>::is_valid();
|
||||
}
|
||||
|
||||
std::string identifier::null_pk::str() const
|
||||
{
|
||||
return detail::identifier_type_traits<null_type_t>::to_string();
|
||||
}
|
||||
|
||||
void identifier::null_pk::serialize(identifier_serializer &s)
|
||||
{
|
||||
s.serialize(null_, {});
|
||||
}
|
||||
|
||||
size_t identifier::null_pk::hash() const
|
||||
{
|
||||
throw std::runtime_error("hash for null_pk not allowed");
|
||||
}
|
||||
|
||||
identifier::identifier()
|
||||
: id_(std::make_shared<null_pk>()) {}
|
||||
|
||||
identifier::identifier(const identifier &x)
|
||||
: id_(x.id_->copy()) {}
|
||||
|
||||
identifier &identifier::operator=(const identifier &x) {
|
||||
if (this == &x) {
|
||||
return *this;
|
||||
}
|
||||
id_.reset(x.id_->copy());
|
||||
return *this;
|
||||
}
|
||||
|
||||
identifier::identifier(identifier &&x) noexcept
|
||||
: id_(std::move(x.id_)) {}
|
||||
|
||||
identifier &identifier::operator=(identifier &&x) noexcept {
|
||||
id_ = std::move(x.id_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool identifier::operator==(const identifier &x) const {
|
||||
return id_->equal_to(*x.id_);
|
||||
}
|
||||
|
||||
bool identifier::operator!=(const identifier &x) const {
|
||||
return !operator==(x);
|
||||
}
|
||||
|
||||
bool identifier::operator<(const identifier &x) const {
|
||||
return id_->less(*x.id_);
|
||||
}
|
||||
|
||||
bool identifier::operator<=(const identifier &x) const {
|
||||
return operator==(x) || operator<(x);
|
||||
}
|
||||
|
||||
bool identifier::operator>(const identifier &x) const {
|
||||
return !operator<=(x);
|
||||
}
|
||||
|
||||
bool identifier::operator>=(const identifier &x) const {
|
||||
return !operator<(x);
|
||||
}
|
||||
|
||||
bool identifier::is_similar_type(const identifier &x) const
|
||||
{
|
||||
return id_->is_similar_type(*x.id_);
|
||||
}
|
||||
|
||||
std::string identifier::str() const {
|
||||
return id_->str();
|
||||
}
|
||||
|
||||
const std::type_index &identifier::type_index() const {
|
||||
return id_->type_index_;
|
||||
}
|
||||
|
||||
identifier identifier::share() const
|
||||
{
|
||||
return identifier(id_);
|
||||
}
|
||||
|
||||
size_t identifier::use_count() const
|
||||
{
|
||||
return id_.use_count();
|
||||
}
|
||||
|
||||
bool identifier::is_null() const
|
||||
{
|
||||
return type_index() == null_identifier.type_index();
|
||||
}
|
||||
|
||||
bool identifier::is_valid() const
|
||||
{
|
||||
return id_->is_valid();
|
||||
}
|
||||
|
||||
void identifier::clear()
|
||||
{
|
||||
id_ = std::make_unique<null_pk>();
|
||||
}
|
||||
|
||||
void identifier::serialize(identifier_serializer &s)
|
||||
{
|
||||
id_->serialize(s);
|
||||
}
|
||||
|
||||
size_t identifier::hash() const
|
||||
{
|
||||
return id_->hash();
|
||||
}
|
||||
|
||||
identifier::identifier(const std::shared_ptr<base> &id)
|
||||
: id_(id)
|
||||
{}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const identifier &id)
|
||||
{
|
||||
out << id.str();
|
||||
return out;
|
||||
}
|
||||
|
||||
size_t id_pk_hash::operator()(const identifier &id) const
|
||||
{
|
||||
return id.hash();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user