173 lines
4.8 KiB
C++
173 lines
4.8 KiB
C++
#ifndef QUERY_SESSION_HPP
|
|
#define QUERY_SESSION_HPP
|
|
|
|
#include "matador/orm/session_query_builder.hpp"
|
|
|
|
#include "matador/sql/column_generator.hpp"
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/connection_pool.hpp"
|
|
#include "matador/sql/statement.hpp"
|
|
|
|
#include "matador/object/object_ptr.hpp"
|
|
#include "matador/object/object_definition.hpp"
|
|
#include "matador/object/schema.hpp"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace matador::orm {
|
|
|
|
enum class session_error {
|
|
Ok = 0,
|
|
NoConnectionAvailable,
|
|
UnknownType,
|
|
FailedToBuildQuery,
|
|
FailedToFindObject
|
|
};
|
|
|
|
class session
|
|
{
|
|
public:
|
|
explicit session(sql::connection_pool<sql::connection> &pool);
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &table_name);
|
|
|
|
utils::result<void, utils::error> create_schema() const;
|
|
|
|
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);
|
|
}
|
|
|
|
session_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);
|
|
}
|
|
|
|
session_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);
|
|
}
|
|
|
|
session_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]] utils::result<sql::query_result<sql::record>, utils::error> fetch(const sql::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(const sql::query_context& q) 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;
|
|
|
|
[[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;
|
|
|
|
static query::fetchable_query build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data);
|
|
|
|
private:
|
|
sql::connection_pool<sql::connection> &pool_;
|
|
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)
|
|
{
|
|
return 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 {};
|
|
}
|
|
|
|
query::query::insert()
|
|
.into(info->name, sql::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
|