128 lines
3.0 KiB
C++
128 lines
3.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/entity_query_builder.hpp"
|
|
#include "matador/sql/statement.hpp"
|
|
#include "matador/sql/schema.hpp"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace matador::sql {
|
|
|
|
class dialect;
|
|
|
|
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>
|
|
std::optional<entity<Type>> find(const PrimaryKeyType &pk) {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return {};
|
|
}
|
|
|
|
entity_query_builder<PrimaryKeyType> eqb(pk, *schema_);
|
|
auto data = eqb.template build<Type>();
|
|
|
|
auto q = c->query(*schema_)
|
|
.select(data->columns)
|
|
.from(data->root_table_name);
|
|
|
|
for (auto &jd : data->joins) {
|
|
q.join_left(jd.join_table)
|
|
.on(std::move(jd.condition));
|
|
}
|
|
auto e = q
|
|
.where(std::move(data->where_clause))
|
|
.template fetch_one<Type>();
|
|
|
|
if (!e) {
|
|
return std::nullopt;
|
|
}
|
|
return entity<Type>{ e.release() };
|
|
}
|
|
|
|
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;
|
|
|
|
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
|