added session test (progress)

This commit is contained in:
2024-02-04 22:24:50 +01:00
parent fa3ea28920
commit 0f13884f06
8 changed files with 115 additions and 16 deletions
+44 -9
View File
@@ -1,11 +1,9 @@
#ifndef QUERY_SESSION_HPP
#define QUERY_SESSION_HPP
#include "matador/sql/column_generator.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/query_builder.hpp"
#include "matador/sql/query_intermediates.hpp"
#include "matador/sql/entity.hpp"
#include "matador/sql/statement.hpp"
#include "matador/sql/table_repository.hpp"
@@ -20,6 +18,21 @@ class session
public:
explicit session(connection_pool<connection> &pool);
template<typename Type>
void attach(const std::string &table_name);
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;
@@ -28,12 +41,6 @@ public:
record describe_table(const std::string &table_name) const;
bool table_exists(const std::string &table_name) const;
template<typename Type>
void attach(const std::string &table_name)
{
table_repository_.attach<Type>(table_name);
}
[[nodiscard]] const table_repository& tables() const;
const class dialect& dialect() const;
@@ -51,5 +58,33 @@ private:
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