insert has one progress
This commit is contained in:
@@ -19,6 +19,28 @@ class executor;
|
||||
}
|
||||
|
||||
namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_object_resolver_producer : public sql::object_resolver_producer {
|
||||
public:
|
||||
query_object_resolver_producer() = default;
|
||||
query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: object_resolver_producer(typeid(Type))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::statement&& stmt) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(std::move(stmt));
|
||||
}
|
||||
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override;
|
||||
|
||||
private:
|
||||
basic_schema& repo_;
|
||||
const table& table_;
|
||||
std::string pk_name_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class query_collection_resolver_producer : public sql::collection_resolver_producer {
|
||||
public:
|
||||
@@ -77,9 +99,7 @@ public:
|
||||
return utils::ok(stmt);
|
||||
}
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::statement&& stmt, const sql::resolver_service& rs) override {
|
||||
// const auto object_resolver = rs.object_resolver<typename Type::value_type>();
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::statement&& stmt, const sql::resolver_service& /*rs*/) override {
|
||||
return std::make_shared<query_collection_primitive_resolver<Type>>(std::move(stmt), root_type(), collection_name()/*, object_resolver*/);
|
||||
}
|
||||
|
||||
@@ -106,7 +126,18 @@ public:
|
||||
template<class Pointer>
|
||||
static void on_belongs_to(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class Pointer>
|
||||
static void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
auto producer = std::make_unique<query_object_resolver_producer<typename Pointer::value_type>>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name());
|
||||
schema_.resolver_producers_[typeid(typename Pointer::value_type)] = std::move(producer);
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
void on_has_many(const char * /*id*/, CollectionType &/*cont*/, const char *join_column, const utils::foreign_attributes &/*attr*/, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
@@ -187,42 +218,6 @@ private:
|
||||
const std::type_index root_type_;
|
||||
};
|
||||
|
||||
|
||||
template<typename Type>
|
||||
class query_object_resolver_producer : public sql::object_resolver_producer {
|
||||
public:
|
||||
query_object_resolver_producer() = default;
|
||||
query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: object_resolver_producer(typeid(Type))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::statement&& stmt) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(std::move(stmt));
|
||||
}
|
||||
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override {
|
||||
producer_creator pc(repo_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
select_query_builder qb(repo_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto result = qb.build<Type>(*pk_column == utils::_);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result->compile(d));
|
||||
}
|
||||
|
||||
private:
|
||||
basic_schema& repo_;
|
||||
const table& table_;
|
||||
std::string pk_name_;
|
||||
};
|
||||
|
||||
class schema;
|
||||
|
||||
using schema_ref = std::reference_wrapper<schema>;
|
||||
@@ -311,6 +306,23 @@ utils::result<void, utils::error> schema::drop_table(const sql::connection &conn
|
||||
return utils::failure(info.err());
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<sql::query_context, utils::error> query_object_resolver_producer<Type>::
|
||||
build_query(const sql::dialect &d) {
|
||||
producer_creator pc(repo_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
select_query_builder qb(repo_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto result = qb.build<Type>(*pk_column == utils::_);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result->compile(d));
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
||||
primary_key_generator_finder finder;
|
||||
|
||||
@@ -113,12 +113,12 @@ public:
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
on_foreign_object(id, obj, attr, true);
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
on_foreign_object(id, obj, attr, false);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -258,7 +258,7 @@ public:
|
||||
|
||||
private:
|
||||
template<class Pointer>
|
||||
void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr);
|
||||
void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, bool add_column_on_lazy);
|
||||
void push(const std::string &column_name);
|
||||
static std::string build_alias(char prefix, unsigned int count);
|
||||
[[nodiscard]] bool is_root_entity() const;
|
||||
@@ -280,7 +280,7 @@ private:
|
||||
};
|
||||
|
||||
template<class Pointer>
|
||||
void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
||||
void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, const bool add_column_on_lazy) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
@@ -307,7 +307,7 @@ void select_query_builder::on_foreign_object(const char *id, Pointer &, const ut
|
||||
table_column{&table_info_stack_.top().table, id},
|
||||
table_column{&next->second, info.primary_key_attribute()->name()}
|
||||
);
|
||||
} else {
|
||||
} else if (add_column_on_lazy) {
|
||||
push(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,9 +38,9 @@ public:
|
||||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
template<class Type, template < class ... > class Pointer>
|
||||
void on_has_one(const char * /*id*/, Pointer<Type> &x, const utils::foreign_attributes &/*attr*/) {
|
||||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
static void on_has_one(const char * /*id*/,
|
||||
Pointer<Type> &/*x*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
#include "matador/sql/resolver_service.hpp"
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
enum class sql_command {
|
||||
Unknown,
|
||||
@@ -33,12 +31,10 @@ struct query_context {
|
||||
std::string sql;
|
||||
size_t sql_hash{};
|
||||
sql_command command{};
|
||||
std::string command_name{};
|
||||
std::string schema_name{};
|
||||
std::string table_name{};
|
||||
std::vector<object::attribute> prototype{};
|
||||
std::vector<std::string> bind_vars{};
|
||||
// std::vector<utils::database_type> bind_types{};
|
||||
// Data for resolving query result
|
||||
std::shared_ptr<resolver_service> resolver{};
|
||||
std::type_index result_type = typeid(void);
|
||||
|
||||
@@ -22,6 +22,16 @@ MATADOR_UTILS_API std::string to_string(const blob_type_t &data);
|
||||
MATADOR_UTILS_API std::string to_string(const date_type_t &data);
|
||||
MATADOR_UTILS_API std::string to_string(const time_type_t &data);
|
||||
|
||||
template <typename IntegerType>
|
||||
std::string to_hex_string(IntegerType data, const size_t width = sizeof(IntegerType)<<1) {
|
||||
static auto digits = "0123456789ABCDEF";
|
||||
std::string result(width,'0');
|
||||
for (size_t i=0, j=(width-1)*4 ; i<width; ++i,j-=4) {
|
||||
result[i] = digits[(data>>j) & 0x0f];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string by a delimiter and
|
||||
* add the string tokens to a vector. The
|
||||
|
||||
Reference in New Issue
Block a user