93 lines
2.1 KiB
C++
93 lines
2.1 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/table_repository.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>
|
|
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;
|
|
|
|
record describe_table(const std::string &table_name) const;
|
|
bool table_exists(const std::string &table_name) const;
|
|
|
|
[[nodiscard]] const table_repository& 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_;
|
|
|
|
table_repository table_repository_;
|
|
mutable std::unordered_map<std::string, record> prototypes_;
|
|
};
|
|
|
|
template<typename Type>
|
|
void session::attach(const std::string &table_name)
|
|
{
|
|
table_repository_.attach<Type>(table_name);
|
|
}
|
|
|
|
template<typename Type>
|
|
entity<Type> session::insert(Type *obj)
|
|
{
|
|
auto c = pool_.acquire();
|
|
auto info = table_repository_.info<Type>();
|
|
if (!info) {
|
|
return {};
|
|
}
|
|
c->insert().into<Type>(info->name).values(*obj).execute();
|
|
|
|
return entity{obj};
|
|
}
|
|
|
|
template<typename Type>
|
|
void session::drop_table()
|
|
{
|
|
auto info = table_repository_.info<Type>();
|
|
if (info) {
|
|
return drop_table(info.name);
|
|
}
|
|
|
|
}
|
|
}
|
|
#endif //QUERY_SESSION_HPP
|