210 lines
7.3 KiB
C++
210 lines
7.3 KiB
C++
#ifndef QUERY_SESSION_HPP
|
|
#define QUERY_SESSION_HPP
|
|
|
|
#include "matador/orm/error_code.hpp"
|
|
#include "matador/orm/session_query_builder.hpp"
|
|
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include "matador/sql/column_generator.hpp"
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/connection_pool.hpp"
|
|
#include "matador/sql/executor.hpp"
|
|
#include "matador/sql/statement.hpp"
|
|
#include "matador/sql/statement_cache.hpp"
|
|
|
|
#include "matador/object/object_ptr.hpp"
|
|
#include "matador/object/object_definition.hpp"
|
|
#include "matador/object/schema.hpp"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace matador::orm {
|
|
|
|
utils::error make_error(error_code ec, const std::string &msg);
|
|
|
|
struct session_context {
|
|
utils::message_bus &bus;
|
|
sql::connection_pool &pool;
|
|
size_t cache_size{500};
|
|
};
|
|
|
|
template<typename Type>
|
|
class lazy_object_resolver final : public object::object_resolver<Type> {
|
|
public:
|
|
Type* resolve(const object::object_proxy<Type>& proxy) const override {
|
|
return proxy.resolve();
|
|
}
|
|
|
|
};
|
|
|
|
class prototype_builder final {
|
|
public:
|
|
explicit prototype_builder(const std::unordered_map<std::string, sql::statement> &statements_per_column)
|
|
: statements_per_column_(statements_per_column) {}
|
|
|
|
template < class V >
|
|
static void on_primary_key(const char * /*id*/, V &/*pk*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
|
|
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
|
template<typename Type>
|
|
static void on_attribute(const char * /*id*/, Type &/*obj*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
template<class Pointer>
|
|
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
|
|
|
}
|
|
template<class Pointer>
|
|
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
|
|
|
}
|
|
template<class ContainerType>
|
|
void on_has_many(const char * /*id*/, ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {
|
|
}
|
|
template<class ContainerType>
|
|
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr) {
|
|
|
|
}
|
|
template<class ContainerType>
|
|
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const utils::foreign_attributes &attr) {
|
|
|
|
}
|
|
|
|
private:
|
|
const std::unordered_map<std::string, sql::statement> &statements_per_column_;
|
|
};
|
|
class session final : public sql::executor {
|
|
public:
|
|
explicit session(session_context &&ctx);
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &table_name) const;
|
|
template<typename Type, typename SuperType>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &table_name) const;
|
|
|
|
utils::result<void, utils::error> create_schema() const;
|
|
|
|
template<typename Type>
|
|
utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
|
|
|
|
template< class Type, typename... Args >
|
|
utils::result<object::object_ptr<Type>, utils::error> insert(Args&&... args) {
|
|
return insert(new Type(std::forward<Args>(args)...));
|
|
}
|
|
|
|
template<typename Type, typename PrimaryKeyType>
|
|
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk) {
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
|
}
|
|
|
|
session_query_builder eqb(*schema_, *this);
|
|
auto data = eqb.build<Type>(pk);
|
|
if (!data.is_ok()) {
|
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
|
}
|
|
|
|
auto obj = build_select_query(data.release()).template fetch_one<Type>(*this);
|
|
|
|
if (!obj) {
|
|
return utils::failure(obj.err());
|
|
}
|
|
if (!obj->get()) {
|
|
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
|
|
}
|
|
return utils::ok(object::object_ptr<Type>{ obj->release() });
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<sql::query_result<Type>, utils::error> find() {
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
|
}
|
|
|
|
session_query_builder eqb(*schema_, *this);
|
|
auto data = eqb.build<Type>();
|
|
if (!data.is_ok()) {
|
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
|
}
|
|
|
|
const auto ctx = build_select_query(data.release()).compile(dialect_, query::query_mode::Prepared);
|
|
|
|
auto result = fetch(ctx);
|
|
if (!result.is_ok()) {
|
|
return utils::failure(result.err());
|
|
}
|
|
return utils::ok(sql::query_result<Type>(result.release(), []{ return new Type();}));
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<void, utils::error> drop_table();
|
|
utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
|
|
|
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::query_context &q) const;
|
|
[[nodiscard]] utils::result<size_t, utils::error> execute(const std::string &sql) const;
|
|
|
|
[[nodiscard]] std::vector<object::attribute_definition> describe_table(const std::string &table_name) const;
|
|
[[nodiscard]] bool table_exists(const std::string &table_name) const;
|
|
|
|
void dump_schema(std::ostream &os) const;
|
|
|
|
[[nodiscard]] utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &ctx) const override;
|
|
[[nodiscard]] utils::result<size_t, utils::error> execute(const sql::query_context &ctx) const override;
|
|
[[nodiscard]] utils::result<sql::statement, utils::error> prepare(const sql::query_context &ctx) override;
|
|
[[nodiscard]] std::string str( const sql::query_context& ctx ) const override;
|
|
[[nodiscard]] const sql::dialect& dialect() const override;
|
|
|
|
private:
|
|
friend class query_select;
|
|
|
|
static query::fetchable_query build_select_query(entity_query_data &&data);
|
|
|
|
private:
|
|
mutable sql::statement_cache cache_;
|
|
const sql::dialect &dialect_;
|
|
|
|
std::unique_ptr<object::schema> schema_;
|
|
mutable std::unordered_map<std::string, object::object_definition> prototypes_;
|
|
};
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> session::attach(const std::string &table_name) const {
|
|
return schema_->attach<Type>(table_name);
|
|
}
|
|
|
|
template<typename Type, typename SuperType>
|
|
utils::result<void, utils::error> session::attach( const std::string& table_name ) const {
|
|
return schema_->attach<Type, SuperType>(table_name);
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj) {
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(info.err());
|
|
}
|
|
|
|
auto res = query::query::insert()
|
|
.into(info->get().name(), sql::column_generator::generate<Type>(*schema_, true))
|
|
.values(*obj)
|
|
.execute(*this);
|
|
if (!res) {
|
|
return utils::failure(res.err());
|
|
}
|
|
|
|
return utils::ok(object::object_ptr{obj});
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<void, utils::error> session::drop_table() {
|
|
auto info = schema_->info<Type>();
|
|
if (info) {
|
|
return drop_table(info->get().name());
|
|
}
|
|
|
|
return utils::failure(info.err());
|
|
}
|
|
|
|
}
|
|
#endif //QUERY_SESSION_HPP
|