record refactoring
This commit is contained in:
@@ -36,6 +36,7 @@ set(SQL_SOURCES
|
||||
sql/query_part.cpp
|
||||
sql/any_type_to_string_visitor.cpp
|
||||
sql/field.cpp
|
||||
sql/table_definition.cpp
|
||||
)
|
||||
|
||||
set(SQL_HEADER
|
||||
@@ -89,6 +90,7 @@ set(SQL_HEADER
|
||||
../include/matador/sql/query_helper.hpp
|
||||
../include/matador/sql/field.hpp
|
||||
../include/matador/sql/entity_query_builder.hpp
|
||||
../include/matador/sql/table_definition.hpp
|
||||
)
|
||||
|
||||
set(QUERY_SOURCES
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "matador/sql/field.hpp"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
field::field(std::string name)
|
||||
@@ -8,6 +10,11 @@ field::field(std::string name)
|
||||
, type_(field_traits<nullptr_t>::type())
|
||||
{}
|
||||
|
||||
field::field(std::string name, data_type_t data_type, size_t size, int index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
field::field(field &&x) noexcept
|
||||
: name_(std::move(x.name_))
|
||||
, index_(x.index_)
|
||||
@@ -37,6 +44,11 @@ const std::string &field::name() const
|
||||
return name_;
|
||||
}
|
||||
|
||||
size_t field::size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
int field::index() const
|
||||
{
|
||||
return index_;
|
||||
@@ -83,4 +95,24 @@ bool field::is_null() const
|
||||
return type_ == field_type::Null;
|
||||
}
|
||||
|
||||
data_type_t field::field_type_to_data_type(field_type type)
|
||||
{
|
||||
switch (type) {
|
||||
case field_type::Integer:
|
||||
return data_type_t::type_long_long;
|
||||
case field_type::FloatingPoint:
|
||||
return data_type_t::type_double;
|
||||
case field_type::String:
|
||||
return data_type_t::type_varchar;
|
||||
case field_type::Boolean:
|
||||
return data_type_t::type_bool;
|
||||
case field_type::Blob:
|
||||
return data_type_t::type_blob;
|
||||
case field_type::Null:
|
||||
return data_type_t::type_null;
|
||||
default:
|
||||
return data_type_t::type_unknown;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
#include "matador/sql/query_result.hpp"
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
namespace matador::sql::detail {
|
||||
|
||||
template<>
|
||||
record *create_prototype<record>(const std::vector<column_definition> &prototype)
|
||||
{
|
||||
return new record{prototype};
|
||||
auto result = std::make_unique<record>();
|
||||
// for (const auto &col: prototype) {
|
||||
// result->append({})
|
||||
// }
|
||||
return result.release();
|
||||
}
|
||||
|
||||
}
|
||||
+60
-60
@@ -6,23 +6,23 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
record::record(std::initializer_list<column_definition> columns)
|
||||
: columns_(columns)
|
||||
record::record(std::initializer_list<field> columns)
|
||||
: fields_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
record::record(const std::vector<column_definition> &columns)
|
||||
: columns_(columns)
|
||||
record::record(const std::vector<field> &columns)
|
||||
: fields_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
record::record(const record &x)
|
||||
: columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_)
|
||||
: fields_(x.fields_)
|
||||
//, pk_index_(x.pk_index_)
|
||||
{
|
||||
for (auto& col : columns_) {
|
||||
for (auto& col : fields_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
}
|
||||
@@ -33,128 +33,128 @@ record &record::operator=(const record &x)
|
||||
return *this;
|
||||
}
|
||||
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
for (auto& col : columns_) {
|
||||
fields_ = x.fields_;
|
||||
fields_by_name_.clear();
|
||||
// pk_index_ = x.pk_index_;
|
||||
for (auto& col : fields_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::vector<column_definition> &record::columns() const
|
||||
const std::vector<field> &record::columns() const
|
||||
{
|
||||
return columns_;
|
||||
return fields_;
|
||||
}
|
||||
|
||||
bool record::has_primary_key() const
|
||||
//bool record::has_primary_key() const
|
||||
//{
|
||||
// return pk_index_ > -1;
|
||||
//}
|
||||
//
|
||||
//std::optional<field> record::primary_key() const
|
||||
//{
|
||||
// if (!has_primary_key()) {
|
||||
// return std::nullopt;
|
||||
// }
|
||||
//
|
||||
// return columns_[pk_index_];
|
||||
//}
|
||||
|
||||
const field &record::at(const column &col) const
|
||||
{
|
||||
return pk_index_ > -1;
|
||||
return fields_by_name_.at(col.name).first;
|
||||
}
|
||||
|
||||
std::optional<column_definition> record::primary_key() const
|
||||
const field &record::at(size_t index) const
|
||||
{
|
||||
if (!has_primary_key()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
const column_definition &record::at(const column &col) const
|
||||
{
|
||||
return columns_by_name_.at(col.name).first;
|
||||
}
|
||||
|
||||
const column_definition &record::at(size_t index) const
|
||||
{
|
||||
return columns_.at(index);
|
||||
return fields_.at(index);
|
||||
}
|
||||
|
||||
record::iterator record::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
auto it = fields_by_name_.find(column_name);
|
||||
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::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();
|
||||
auto it = fields_by_name_.find(column_name);
|
||||
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
|
||||
}
|
||||
|
||||
void record::append(column_definition col)
|
||||
void record::append(field col)
|
||||
{
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
auto &ref = fields_.emplace_back(std::move(col));
|
||||
add_to_map(ref, fields_.size() - 1);
|
||||
}
|
||||
|
||||
record::iterator record::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
return fields_.begin();
|
||||
}
|
||||
|
||||
record::const_iterator record::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
return fields_.begin();
|
||||
}
|
||||
|
||||
record::const_iterator record::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
return fields_.cbegin();
|
||||
}
|
||||
|
||||
record::iterator record::end()
|
||||
{
|
||||
return columns_.end();
|
||||
return fields_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
return fields_.end();
|
||||
}
|
||||
|
||||
record::const_iterator record::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
return fields_.cend();
|
||||
}
|
||||
|
||||
size_t record::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
return fields_.size();
|
||||
}
|
||||
|
||||
bool record::empty() const
|
||||
{
|
||||
return columns_.empty();
|
||||
return fields_.empty();
|
||||
}
|
||||
|
||||
void record::clear()
|
||||
{
|
||||
columns_.clear();
|
||||
columns_by_name_.clear();
|
||||
fields_.clear();
|
||||
fields_by_name_.clear();
|
||||
}
|
||||
|
||||
bool record::unknown() const
|
||||
{
|
||||
return std::all_of(std::begin(columns_), std::end(columns_), [](const auto &col) {
|
||||
return col.type() == data_type_t::type_unknown;
|
||||
});
|
||||
}
|
||||
//bool record::unknown() const
|
||||
//{
|
||||
// return std::all_of(std::begin(columns_), std::end(columns_), [](const auto &col) {
|
||||
// return col.type() == data_type_t::type_unknown;
|
||||
// });
|
||||
//}
|
||||
|
||||
void record::init()
|
||||
{
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
for(auto &col : fields_) {
|
||||
add_to_map(col, index++);
|
||||
}
|
||||
}
|
||||
|
||||
void record::add_to_map(column_definition &col, size_t index)
|
||||
void record::add_to_map(field &col, size_t index)
|
||||
{
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
||||
if (utils::is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
pk_index_ = static_cast<int>(index);
|
||||
}
|
||||
fields_by_name_.emplace(col.name(), field_index_pair {std::ref(col), index});
|
||||
// if (utils::is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
// pk_index_ = static_cast<int>(index);
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "matador/sql/table_definition.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
table_definition::table_definition(std::initializer_list<column_definition> columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
table_definition::table_definition(const std::vector<column_definition> &columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
table_definition::table_definition(const table_definition &x)
|
||||
: columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_)
|
||||
{
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
}
|
||||
|
||||
table_definition &table_definition::operator=(const table_definition &x)
|
||||
{
|
||||
if (&x == this) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool table_definition::has_primary_key() const
|
||||
{
|
||||
return pk_index_ > -1;
|
||||
}
|
||||
|
||||
std::optional<column_definition> table_definition::primary_key() const
|
||||
{
|
||||
if (!has_primary_key()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
void table_definition::append(column_definition col)
|
||||
{
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
}
|
||||
|
||||
const std::vector<column_definition> &table_definition::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const column_definition &table_definition::at(const column &col) const
|
||||
{
|
||||
return columns_by_name_.at(col.name).first;
|
||||
}
|
||||
|
||||
const column_definition &table_definition::at(size_t index) const
|
||||
{
|
||||
return columns_.at(index);
|
||||
}
|
||||
|
||||
table_definition::iterator table_definition::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
table_definition::const_iterator table_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();
|
||||
}
|
||||
|
||||
table_definition::iterator table_definition::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
table_definition::const_iterator table_definition::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
table_definition::const_iterator table_definition::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
}
|
||||
|
||||
table_definition::iterator table_definition::end()
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
table_definition::const_iterator table_definition::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
table_definition::const_iterator table_definition::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
}
|
||||
|
||||
size_t table_definition::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
bool table_definition::empty() const
|
||||
{
|
||||
return columns_.empty();
|
||||
}
|
||||
|
||||
void table_definition::clear()
|
||||
{
|
||||
columns_.clear();
|
||||
columns_by_name_.clear();
|
||||
}
|
||||
|
||||
void table_definition::init()
|
||||
{
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
add_to_map(col, index++);
|
||||
}
|
||||
}
|
||||
|
||||
void table_definition::add_to_map(column_definition &col, size_t index)
|
||||
{
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
||||
if (utils::is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
pk_index_ = static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user