115 lines
2.7 KiB
C++
115 lines
2.7 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;
|
|
|
|
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
|
|
|
|
// 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, record> 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<Type>(info->name).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
|