added session class
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef BASIC_PROTOTYPE_INFO_HPP
|
||||
#define BASIC_PROTOTYPE_INFO_HPP
|
||||
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::object {
|
||||
@@ -12,6 +13,7 @@ public:
|
||||
virtual ~basic_object_info() = default;
|
||||
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
|
||||
protected:
|
||||
basic_object_info(schema_node &node, std::type_index type_index);
|
||||
@@ -27,8 +29,15 @@ public:
|
||||
explicit object_info(schema_node &node)
|
||||
: basic_object_info(node, typeid(Type)) {}
|
||||
|
||||
const Type &prototype() const { return prototype_; }
|
||||
|
||||
private:
|
||||
Type prototype_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
using object_info_ref = std::reference_wrapper<const object_info<Type>>;
|
||||
|
||||
namespace detail {
|
||||
struct null_type {};
|
||||
}
|
||||
|
||||
@@ -83,6 +83,16 @@ public:
|
||||
*/
|
||||
[[nodiscard]] std::string name() const;
|
||||
|
||||
template <typename Type>
|
||||
[[nodiscard]] utils::result<object_info_ref<Type>, utils::error> info() const {
|
||||
auto result = find_node(std::type_index(typeid(Type)));
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result.value()->info<Type>());
|
||||
}
|
||||
|
||||
private:
|
||||
using node_ptr = std::shared_ptr<schema_node>;
|
||||
using t_node_map = std::unordered_map<std::string, node_ptr>;
|
||||
|
||||
@@ -46,6 +46,11 @@ public:
|
||||
[[nodiscard]] node_ptr next() const;
|
||||
[[nodiscard]] node_ptr prev() const;
|
||||
|
||||
template <typename Type>
|
||||
object_info_ref<Type> info() const {
|
||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||
}
|
||||
|
||||
private:
|
||||
explicit schema_node(schema& tree);
|
||||
template < typename Type >
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
#ifndef QUERY_SESSION_HPP
|
||||
#define QUERY_SESSION_HPP
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
// #include "matador/sql/entity_query_builder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
class dialect;
|
||||
|
||||
enum class session_error {
|
||||
Ok = 0,
|
||||
NoConnectionAvailable,
|
||||
UnknownType,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject
|
||||
};
|
||||
|
||||
class session
|
||||
{
|
||||
public:
|
||||
explicit session(sql::connection_pool<sql::connection> &pool);
|
||||
|
||||
template<typename Type>
|
||||
void attach(const std::string &table_name);
|
||||
|
||||
void create_schema();
|
||||
|
||||
template<typename Type>
|
||||
object::object_ptr<Type> insert(Type *obj);
|
||||
|
||||
template< class Type, typename... Args >
|
||||
object::object_ptr<Type> insert(Args&&... args) {
|
||||
return insert(new Type(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template<typename Type, typename PrimaryKeyType>
|
||||
utils::result<object::object_ptr<Type>, session_error> find(const PrimaryKeyType &pk) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
}
|
||||
|
||||
entity_query_builder eqb(*schema_);
|
||||
auto data = eqb.build<Type>(pk);
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(session_error::FailedToBuildQuery);
|
||||
}
|
||||
|
||||
auto obj = build_select_query(c, data.release()).template fetch_one<Type>();
|
||||
|
||||
if (!obj) {
|
||||
return utils::failure(session_error::FailedToFindObject);
|
||||
}
|
||||
return utils::ok(object::object_ptr<Type>{ obj.release() });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<sql::query_result<Type>, session_error> find() {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
}
|
||||
|
||||
entity_query_builder eqb(*schema_);
|
||||
auto data = eqb.build<Type>();
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(session_error::FailedToBuildQuery);
|
||||
}
|
||||
|
||||
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<query::query_from_intermediate, session_error> select() {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
}
|
||||
|
||||
entity_query_builder eqb(*schema_);
|
||||
auto data = eqb.build<Type>();
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(session_error::FailedToBuildQuery);
|
||||
}
|
||||
|
||||
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void drop_table();
|
||||
void drop_table(const std::string &table_name);
|
||||
|
||||
[[nodiscard]] sql::query_result<sql::record> fetch(const query_context &q) const;
|
||||
// [[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
||||
[[nodiscard]] size_t execute(const std::string &sql) const;
|
||||
[[nodiscard]] sql::statement prepare(query_context q) const;
|
||||
|
||||
[[nodiscard]] std::vector<sql::column_definition> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] bool table_exists(const std::string &table_name) const;
|
||||
|
||||
[[nodiscard]] const sql::dialect& dialect() const;
|
||||
|
||||
private:
|
||||
friend class query_select;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<sql::query_result_impl> fetch(const std::string &sql) const;
|
||||
|
||||
query_select build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data) const;
|
||||
|
||||
private:
|
||||
sql::connection_pool<sql::connection> &pool_;
|
||||
const sql::dialect &dialect_;
|
||||
|
||||
std::unique_ptr<object::schema> schema_;
|
||||
mutable std::unordered_map<std::string, table_definition> prototypes_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
void session::attach(const std::string &table_name)
|
||||
{
|
||||
schema_->attach<Type>(table_name);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
object::object_ptr<Type> session::insert(Type *obj)
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return {};
|
||||
}
|
||||
c->query(*schema_)
|
||||
.insert()
|
||||
.into(info->name, column_generator::generate<Type>(*schema_, true))
|
||||
.values(*obj)
|
||||
.execute();
|
||||
|
||||
return object::object_ptr{obj};
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void session::drop_table()
|
||||
{
|
||||
auto info = schema_->info<Type>();
|
||||
if (info) {
|
||||
return drop_table(info.name);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif //QUERY_SESSION_HPP
|
||||
Reference in New Issue
Block a user