record refactoring
This commit is contained in:
@@ -108,8 +108,6 @@ private:
|
||||
using data_type_index = std::vector<data_type_t>;
|
||||
|
||||
private:
|
||||
friend class record;
|
||||
|
||||
static const data_type_index data_type_index_;
|
||||
|
||||
std::string name_;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "matador/sql/any_type.hpp"
|
||||
#include "matador/sql/any_type_to_visitor.hpp"
|
||||
#include "matador/sql/data_type_traits.hpp"
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
|
||||
@@ -64,11 +65,13 @@ class field
|
||||
public:
|
||||
explicit field(std::string name);
|
||||
template<typename Type>
|
||||
field(std::string name, Type value, int index = -1)
|
||||
field(std::string name, Type value, size_t size = 0, int index = -1)
|
||||
: name_(std::move(name))
|
||||
, size_(size)
|
||||
, index_(index)
|
||||
, value_(value)
|
||||
, type_(field_traits<Type>::type()) {}
|
||||
field(std::string name, data_type_t data_type, size_t size = 0, int index = -1);
|
||||
field(const field &x) = default;
|
||||
field& operator=(const field &x) = default;
|
||||
field(field &&x) noexcept;
|
||||
@@ -82,6 +85,7 @@ public:
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] int index() const;
|
||||
|
||||
template<class Type>
|
||||
@@ -104,9 +108,21 @@ public:
|
||||
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
||||
|
||||
private:
|
||||
template<class Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
op.on_attribute(name_.c_str(), value_, field_type_to_data_type(type_), size_);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class record;
|
||||
|
||||
static data_type_t field_type_to_data_type(field_type type);
|
||||
|
||||
std::string name_;
|
||||
int index_{-1};
|
||||
any_type value_;
|
||||
size_t size_{};
|
||||
field_type type_;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef QUERY_QUERY_CONTEXT_HPP
|
||||
#define QUERY_QUERY_CONTEXT_HPP
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
@@ -67,7 +67,7 @@ public:
|
||||
template<typename Type>
|
||||
Type fetch_value()
|
||||
{
|
||||
return fetch_one().at(0).as<Type>();
|
||||
return fetch_one().at(0).as<Type>().value();
|
||||
}
|
||||
|
||||
statement prepare();
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class record;
|
||||
|
||||
template < typename Type >
|
||||
class query_result;
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "matador/sql/any_type.hpp"
|
||||
#include "matador/sql/query_result_reader.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/data_type_traits.hpp"
|
||||
|
||||
#include <memory>
|
||||
@@ -73,6 +73,7 @@ public:
|
||||
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr = utils::null_attributes);
|
||||
// void on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr = utils::null_attributes);
|
||||
|
||||
template < class Pointer >
|
||||
void on_belongs_to(const char * /*id*/, Pointer &x, const utils::foreign_attributes &/*attr*/)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/field.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
@@ -15,17 +15,17 @@ struct column;
|
||||
class record
|
||||
{
|
||||
private:
|
||||
using column_by_index = std::vector<column_definition>;
|
||||
using column_index_pair = std::pair<std::reference_wrapper<column_definition>, size_t>;
|
||||
using column_by_name_map = std::unordered_map<std::string, column_index_pair>;
|
||||
using field_by_index = std::vector<field>;
|
||||
using field_index_pair = std::pair<std::reference_wrapper<field>, field_by_index::difference_type>;
|
||||
using field_by_name_map = std::unordered_map<std::string, field_index_pair>;
|
||||
|
||||
public:
|
||||
using iterator = column_by_index::iterator;
|
||||
using const_iterator = column_by_index::const_iterator;
|
||||
using iterator = field_by_index::iterator;
|
||||
using const_iterator = field_by_index::const_iterator;
|
||||
|
||||
record() = default;
|
||||
record(std::initializer_list<column_definition> columns);
|
||||
explicit record(const std::vector<column_definition> &columns);
|
||||
record(std::initializer_list<field> columns);
|
||||
explicit record(const std::vector<field> &columns);
|
||||
record(const record &x);
|
||||
record& operator=(const record &x);
|
||||
record(record&&) noexcept = default;
|
||||
@@ -35,25 +35,17 @@ public:
|
||||
template<class Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
for(auto &col : columns_) {
|
||||
col.process(op);
|
||||
for(auto &f : fields_) {
|
||||
f.process(op);
|
||||
}
|
||||
}
|
||||
template < typename Type >
|
||||
void append(const std::string &name, long size = -1)
|
||||
{
|
||||
append(make_column<Type>(name, size));
|
||||
}
|
||||
|
||||
void append(column_definition col);
|
||||
void append(field col);
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] std::optional<column_definition> primary_key() const;
|
||||
[[nodiscard]] const std::vector<field>& columns() const;
|
||||
|
||||
[[nodiscard]] const std::vector<column_definition>& columns() const;
|
||||
|
||||
[[nodiscard]] const column_definition& at(const column &col) const;
|
||||
[[nodiscard]] const column_definition& at(size_t index) const;
|
||||
[[nodiscard]] const field& at(const column &col) const;
|
||||
[[nodiscard]] const field& at(size_t index) const;
|
||||
|
||||
iterator find(const std::string &column_name);
|
||||
[[nodiscard]] const_iterator find(const std::string &column_name) const;
|
||||
@@ -70,17 +62,17 @@ public:
|
||||
[[nodiscard]] bool empty() const;
|
||||
void clear();
|
||||
|
||||
[[nodiscard]] bool unknown() const;
|
||||
// [[nodiscard]] bool unknown() const;
|
||||
|
||||
private:
|
||||
void init();
|
||||
void add_to_map(column_definition &col, size_t index);
|
||||
void add_to_map(field &col, size_t index);
|
||||
|
||||
private:
|
||||
column_by_index columns_;
|
||||
column_by_name_map columns_by_name_;
|
||||
field_by_index fields_;
|
||||
field_by_name_map fields_by_name_;
|
||||
|
||||
int pk_index_{-1};
|
||||
// int pk_index_{-1};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#define QUERY_SCHEMA_HPP
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/table_definition.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -16,7 +16,7 @@ class connection;
|
||||
struct table_info
|
||||
{
|
||||
std::string name;
|
||||
record prototype;
|
||||
table_definition prototype;
|
||||
};
|
||||
|
||||
class schema
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
template<typename Type>
|
||||
const table_info& attach(const std::string &table_name)
|
||||
{
|
||||
return attach(std::type_index(typeid(Type)), table_info{table_name, record{column_generator::generate<Type>(*this)}});
|
||||
return attach(std::type_index(typeid(Type)), table_info{table_name, table_definition{column_generator::generate<Type>(*this)}});
|
||||
}
|
||||
|
||||
const table_info& attach(std::type_index ti, const table_info& table);
|
||||
|
||||
@@ -79,7 +79,7 @@ private:
|
||||
const class dialect &dialect_;
|
||||
|
||||
std::unique_ptr<schema> schema_;
|
||||
mutable std::unordered_map<std::string, record> prototypes_;
|
||||
mutable std::unordered_map<std::string, table_definition> prototypes_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef QUERY_TABLE_DEFINITION_HPP
|
||||
#define QUERY_TABLE_DEFINITION_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class table_definition final
|
||||
{
|
||||
private:
|
||||
using column_by_index = std::vector<column_definition>;
|
||||
using column_index_pair = std::pair<std::reference_wrapper<column_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;
|
||||
|
||||
table_definition() = default;
|
||||
table_definition(std::initializer_list<column_definition> columns);
|
||||
explicit table_definition(const std::vector<column_definition> &columns);
|
||||
table_definition(const table_definition &x);
|
||||
table_definition& operator=(const table_definition &x);
|
||||
table_definition(table_definition&&) noexcept = default;
|
||||
table_definition& operator=(table_definition&&) noexcept = default;
|
||||
~table_definition() = default;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] std::optional<column_definition> primary_key() const;
|
||||
|
||||
template < typename Type >
|
||||
void append(const std::string &name, long size = -1)
|
||||
{
|
||||
append(make_column<Type>(name, size));
|
||||
}
|
||||
void append(column_definition col);
|
||||
|
||||
[[nodiscard]] const std::vector<column_definition>& columns() const;
|
||||
|
||||
[[nodiscard]] const column_definition& at(const column &col) const;
|
||||
[[nodiscard]] const column_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(column_definition &col, size_t index);
|
||||
|
||||
private:
|
||||
column_by_index columns_;
|
||||
column_by_name_map columns_by_name_;
|
||||
|
||||
int pk_index_{-1};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_TABLE_DEFINITION_HPP
|
||||
Reference in New Issue
Block a user