added more tests
This commit is contained in:
@@ -1,176 +0,0 @@
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column_definition::column_definition(const char *name)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name)
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name, const utils::basic_type type, const utils::field_attributes& attr, const null_option null_opt, const size_t index)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
{}
|
||||
|
||||
column_definition::column_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const size_t index,
|
||||
std::string ref_table,
|
||||
std::string ref_column,
|
||||
const utils::field_attributes& attr, const null_option null_opt)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
, ref_table_(std::move(ref_table))
|
||||
, ref_column_(std::move(ref_column))
|
||||
{}
|
||||
|
||||
const std::string &column_definition::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::string column_definition::full_name() const
|
||||
{
|
||||
return table_ + "." + name_;
|
||||
}
|
||||
|
||||
std::string column_definition::table_name() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
int column_definition::index() const
|
||||
{
|
||||
return index_;
|
||||
}
|
||||
|
||||
const utils::field_attributes &column_definition::attributes() const
|
||||
{
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
bool column_definition::is_nullable() const
|
||||
{
|
||||
return null_option_ == null_option::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type column_definition::type() const
|
||||
{
|
||||
return value_.type();
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_table() const
|
||||
{
|
||||
return ref_table_;
|
||||
}
|
||||
|
||||
const std::string &column_definition::ref_column() const
|
||||
{
|
||||
return ref_column_;
|
||||
}
|
||||
|
||||
bool column_definition::is_foreign_reference() const
|
||||
{
|
||||
return !ref_column_.empty() && !ref_table_.empty();
|
||||
}
|
||||
|
||||
bool column_definition::is_integer() const
|
||||
{
|
||||
return value_.is_integer();
|
||||
}
|
||||
|
||||
bool column_definition::is_floating_point() const
|
||||
{
|
||||
return value_.is_floating_point();
|
||||
}
|
||||
|
||||
bool column_definition::is_bool() const
|
||||
{
|
||||
return value_.is_bool();
|
||||
}
|
||||
|
||||
bool column_definition::is_string() const
|
||||
{
|
||||
return value_.is_string();
|
||||
}
|
||||
|
||||
bool column_definition::is_varchar() const
|
||||
{
|
||||
return value_.is_varchar();
|
||||
}
|
||||
|
||||
bool column_definition::is_date() const
|
||||
{
|
||||
return value_.is_date();
|
||||
}
|
||||
|
||||
bool column_definition::is_time() const
|
||||
{
|
||||
return value_.is_time();
|
||||
}
|
||||
|
||||
bool column_definition::is_blob() const
|
||||
{
|
||||
return value_.is_blob();
|
||||
}
|
||||
|
||||
bool column_definition::is_null() const
|
||||
{
|
||||
return value_.is_null();
|
||||
}
|
||||
|
||||
void column_definition::type(utils::basic_type /*type*/)
|
||||
{
|
||||
// type_ = type;
|
||||
// utils::initialize_by_utils::basic_type(type, value_);
|
||||
}
|
||||
|
||||
std::string column_definition::str() const
|
||||
{
|
||||
return value_.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const column_definition &col)
|
||||
{
|
||||
out << col.str();
|
||||
return out;
|
||||
}
|
||||
|
||||
column_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return {name, type, attr, null_opt};
|
||||
}
|
||||
|
||||
template<>
|
||||
column_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
template<>
|
||||
column_definition make_pk_column<std::string>(const std::string &name, size_t size)
|
||||
{
|
||||
return make_column<std::string>(name, {size, utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
template<>
|
||||
[[maybe_unused]] column_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table,
|
||||
const std::string &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<std::string>::type(size), 0, ref_table, ref_column, {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column_generator::column_generator(std::vector<column> &column_infos,
|
||||
const object::schema &scm,
|
||||
const std::string &table_name,
|
||||
bool force_lazy)
|
||||
: column_infos_(column_infos)
|
||||
, table_schema_(scm)
|
||||
, force_lazy_(force_lazy)
|
||||
{
|
||||
table_name_stack_.push(table_name);
|
||||
}
|
||||
|
||||
void column_generator::on_primary_key(const char *id, std::string &, size_t)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void column_generator::on_revision(const char *id, unsigned long long int &)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
void column_generator::push(const std::string &column_name)
|
||||
{
|
||||
char str[4];
|
||||
snprintf(str, 4, "c%02d", ++column_index);
|
||||
column_infos_.emplace_back(table{table_name_stack_.top()}, column_name, str);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -122,7 +122,7 @@ utils::result<void, utils::error> connection::rollback() const {
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
utils::result<std::vector<column_definition>, 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);
|
||||
}
|
||||
@@ -143,7 +143,7 @@ utils::result<size_t, utils::error> connection::execute(const std::string &sql)
|
||||
return connection_->execute(sql);
|
||||
}
|
||||
|
||||
bool has_unknown_columns(const std::vector<sql::column_definition> &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;
|
||||
});
|
||||
@@ -199,7 +199,7 @@ utils::result<statement, utils::error> connection::prepare(const query_context &
|
||||
return value.name() == col.name();
|
||||
});
|
||||
if (col.type() == utils::basic_type::type_null && rit != result->end()) {
|
||||
const_cast<column_definition &>(col).type(rit->type());
|
||||
const_cast<object::attribute_definition&>(col).type(rit->type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
namespace matador::sql::detail {
|
||||
|
||||
template<>
|
||||
record *create_prototype<record>(const std::vector<column_definition> &prototype)
|
||||
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype)
|
||||
{
|
||||
auto result = std::make_unique<record>();
|
||||
for (const auto &col: prototype) {
|
||||
|
||||
@@ -29,7 +29,7 @@ utils::result<size_t, utils::error> statement::execute() const
|
||||
return statement_->execute();
|
||||
}
|
||||
|
||||
//bool is_unknown(const std::vector<sql::column_definition> &columns) {
|
||||
//bool is_unknown(const std::vector<object::column_definition> &columns) {
|
||||
// return std::all_of(std::begin(columns), std::end(columns), [](const auto &col) {
|
||||
// return col.is_unknown();
|
||||
// });
|
||||
|
||||
Reference in New Issue
Block a user