query/include/matador/sql/session.hpp

221 lines
6.0 KiB
C++

#ifndef QUERY_SESSION_HPP
#define QUERY_SESSION_HPP
#include "matador/sql/connection.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/entity.hpp"
#include "matador/sql/statement.hpp"
#include "matador/sql/schema.hpp"
#include <unordered_map>
namespace matador::sql {
class dialect;
struct join_data
{
table join_table;
std::unique_ptr<basic_condition> condition;
};
//class join_collector
//{
//private:
// explicit join_collector(const sql::schema &ts, std::vector<join_data> &joins)
// : table_schema_(ts)
// , joins_(joins) {}
//
//public:
// template < class Type >
// static std::vector<join_data> collect(const sql::schema &ts)
// {
// const auto info = ts.info<Type>();
// if (!info) {
// return {};
// }
// std::vector<join_data> joins;
// join_collector collector(ts, joins);
// Type obj;
// matador::utils::access::process(collector, obj);
// return joins;
// }
//
// template < class PrimaryKeyType >
// void on_primary_key(const char *id, PrimaryKeyType &, typename std::enable_if<std::is_integral<PrimaryKeyType>::value && !std::is_same<bool, PrimaryKeyType>::value>::type* = 0) {}
// void on_primary_key(const char *id, std::string &, size_t) {}
// void on_revision(const char *id, unsigned long long &/*rev*/) {}
// template<typename Type>
// void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
//
// template<class Pointer>
// void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
// {
// if (attr.fetch() == utils::fetch_type::LAZY) {
// push(id);
// } else {
// const auto info = table_schema_.info<typename Pointer::value_type>();
// if (!info) {
// return;
// }
// table_name_stack_.push(info.value().name);
// typename Pointer::value_type obj;
// matador::utils::access::process(*this, obj);
// table_name_stack_.pop();
// }
// }
// template<class Pointer>
// void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
// {
// if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
// push(id);
// } else {
// const auto info = table_schema_.info<typename Pointer::value_type>();
// if (!info) {
// return;
// }
// table_name_stack_.push(info.value().name);
// typename Pointer::value_type obj;
// matador::utils::access::process(*this, obj);
// table_name_stack_.pop();
// }
// }
// template<class ContainerType>
// void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
// {
// if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
// return;
// }
// const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
// if (!info) {
// return;
// }
//
// table_name_stack_.push(info.value().name);
// typename ContainerType::value_type::value_type obj;
// matador::utils::access::process(*this, obj);
// table_name_stack_.pop();
// }
// template<class ContainerType>
// void on_has_many(const char *id, ContainerType &c, const utils::foreign_attributes &attr)
// {
// on_has_many(id, c, "", "", attr);
// }
//
//private:
// const sql::schema &table_schema_;
// std::vector<join_data> &joins_;
//};
class session
{
public:
explicit session(connection_pool<connection> &pool);
template<typename Type>
void attach(const std::string &table_name);
void create_schema();
template<typename Type>
entity<Type> insert(Type *obj);
template< class Type, typename... Args >
entity<Type> insert(Args&&... args) {
return insert(new Type(std::forward<Args>(args)...));
}
template<typename Type, typename PrimaryKeyType>
entity<Type> find(const PrimaryKeyType &pk) {
auto c = pool_.acquire();
if (!c.valid()) {
throw std::logic_error("no database connection available");
}
// collect all columns
// - evaluate fetch::Eager flag for relations
auto info = schema_->info<Type>();
if (!info) {
return {};
}
auto columns = sql::column_generator::generate<Type>(*schema_);
// auto joins = sql
c->query(*schema_).select({}).from(info->name);
// build pk where condition
// - check if type has pk
// - check type
// pk_condition_builder<Type> builder;
// auto cond = builder.build(pk);
// create query with relations as requested
//
// c->query(*schema_).select<Type>().from("xyz").where().fetch_all();
return {};
}
template<typename Type>
void drop_table();
void drop_table(const std::string &table_name);
[[nodiscard]] query_result<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;
statement prepare(query_context q) const;
std::vector<sql::column_definition> describe_table(const std::string &table_name) const;
bool table_exists(const std::string &table_name) const;
[[nodiscard]] const schema& tables() const;
const class dialect& dialect() const;
private:
friend class query_select_finish;
[[nodiscard]] std::unique_ptr<query_result_impl> fetch(const std::string &sql) const;
private:
connection_pool<connection> &pool_;
const class dialect &dialect_;
std::unique_ptr<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>
entity<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_))
.values(*obj)
.execute();
return entity{obj};
}
template<typename Type>
void session::drop_table()
{
auto info = schema_->info<Type>();
if (info) {
return drop_table(info.name);
}
}
}
#endif //QUERY_SESSION_HPP