added session test (progress)
This commit is contained in:
parent
fa3ea28920
commit
0f13884f06
|
|
@ -1,5 +1,10 @@
|
||||||
cmake_minimum_required(VERSION 3.26)
|
cmake_minimum_required(VERSION 3.26)
|
||||||
project(query)
|
project(
|
||||||
|
query
|
||||||
|
VERSION 1.0.0
|
||||||
|
DESCRIPTION "SQL query fluent prototype for PostgreSQL, SQLite, MySQL and MSSQL"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,8 @@ set(TEST_SOURCES
|
||||||
../../tests/QueryRecordTest.cpp
|
../../tests/QueryRecordTest.cpp
|
||||||
../../tests/StatementTest.cpp
|
../../tests/StatementTest.cpp
|
||||||
../../tests/TypeTraitsTest.cpp
|
../../tests/TypeTraitsTest.cpp
|
||||||
../../tests/StatementCacheTest.cpp)
|
../../tests/StatementCacheTest.cpp
|
||||||
|
../../tests/SessionTest.cpp)
|
||||||
|
|
||||||
set(LIBRARY_TEST_TARGET mysql_tests)
|
set(LIBRARY_TEST_TARGET mysql_tests)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
||||||
include(CTest)
|
include(CTest)
|
||||||
include(Catch)
|
include(Catch)
|
||||||
|
|
||||||
set(POSTGRES_CONNECTION_STRING "postgres://test:test123@127.0.0.1:15432/test")
|
set(POSTGRES_CONNECTION_STRING "postgres://test:test123@127.0.0.1:5432/matador_test")
|
||||||
|
|
||||||
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
|
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
|
||||||
|
|
||||||
|
|
@ -25,7 +25,8 @@ set(TEST_SOURCES
|
||||||
../../tests/QueryRecordTest.cpp
|
../../tests/QueryRecordTest.cpp
|
||||||
../../tests/StatementTest.cpp
|
../../tests/StatementTest.cpp
|
||||||
../../tests/TypeTraitsTest.cpp
|
../../tests/TypeTraitsTest.cpp
|
||||||
../../tests/StatementCacheTest.cpp)
|
../../tests/StatementCacheTest.cpp
|
||||||
|
../../tests/SessionTest.cpp)
|
||||||
|
|
||||||
set(LIBRARY_TEST_TARGET postgres_tests)
|
set(LIBRARY_TEST_TARGET postgres_tests)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,8 @@ set(TEST_SOURCES
|
||||||
../../tests/QueryRecordTest.cpp
|
../../tests/QueryRecordTest.cpp
|
||||||
../../tests/StatementTest.cpp
|
../../tests/StatementTest.cpp
|
||||||
../../tests/TypeTraitsTest.cpp
|
../../tests/TypeTraitsTest.cpp
|
||||||
../../tests/StatementCacheTest.cpp)
|
../../tests/StatementCacheTest.cpp
|
||||||
|
../../tests/SessionTest.cpp)
|
||||||
|
|
||||||
set(LIBRARY_TEST_TARGET sqlite_tests)
|
set(LIBRARY_TEST_TARGET sqlite_tests)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "catch2/catch_test_macros.hpp"
|
||||||
|
|
||||||
|
#include "connection.hpp"
|
||||||
|
|
||||||
|
#include "matador/sql/session.hpp"
|
||||||
|
|
||||||
|
#include "models/airplane.hpp"
|
||||||
|
|
||||||
|
class SessionFixture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SessionFixture()
|
||||||
|
: pool(matador::test::connection::dns, 4)
|
||||||
|
, ses(pool)
|
||||||
|
{}
|
||||||
|
|
||||||
|
~SessionFixture()
|
||||||
|
{
|
||||||
|
drop_table_if_exists("flight");
|
||||||
|
drop_table_if_exists("airplane");
|
||||||
|
drop_table_if_exists("person");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
matador::sql::connection_pool<matador::sql::connection> pool;
|
||||||
|
matador::sql::session ses;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void drop_table_if_exists(const std::string &table_name)
|
||||||
|
{
|
||||||
|
if (ses.table_exists(table_name)) {
|
||||||
|
ses.drop_table(table_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using namespace matador;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||||
|
|
||||||
|
ses.attach<matador::test::airplane>("airplane");
|
||||||
|
ses.insert<test::airplane>(1, "Boeing", "A380");
|
||||||
|
|
||||||
|
REQUIRE(true);
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
#ifndef QUERY_SESSION_HPP
|
#ifndef QUERY_SESSION_HPP
|
||||||
#define QUERY_SESSION_HPP
|
#define QUERY_SESSION_HPP
|
||||||
|
|
||||||
#include "matador/sql/column_generator.hpp"
|
|
||||||
#include "matador/sql/connection.hpp"
|
#include "matador/sql/connection.hpp"
|
||||||
#include "matador/sql/connection_pool.hpp"
|
#include "matador/sql/connection_pool.hpp"
|
||||||
#include "matador/sql/query_builder.hpp"
|
#include "matador/sql/entity.hpp"
|
||||||
#include "matador/sql/query_intermediates.hpp"
|
|
||||||
#include "matador/sql/statement.hpp"
|
#include "matador/sql/statement.hpp"
|
||||||
#include "matador/sql/table_repository.hpp"
|
#include "matador/sql/table_repository.hpp"
|
||||||
|
|
||||||
|
|
@ -20,6 +18,21 @@ class session
|
||||||
public:
|
public:
|
||||||
explicit session(connection_pool<connection> &pool);
|
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 query_context &q) const;
|
||||||
// [[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
// [[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
||||||
[[nodiscard]] size_t execute(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;
|
record describe_table(const std::string &table_name) const;
|
||||||
bool table_exists(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;
|
[[nodiscard]] const table_repository& tables() const;
|
||||||
|
|
||||||
const class dialect& dialect() const;
|
const class dialect& dialect() const;
|
||||||
|
|
@ -51,5 +58,33 @@ private:
|
||||||
mutable std::unordered_map<std::string, record> prototypes_;
|
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
|
#endif //QUERY_SESSION_HPP
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,16 @@ session::session(connection_pool<connection> &pool)
|
||||||
: pool_(pool)
|
: pool_(pool)
|
||||||
, dialect_(backend_provider::instance().connection_dialect(pool_.info().type)) {}
|
, dialect_(backend_provider::instance().connection_dialect(pool_.info().type)) {}
|
||||||
|
|
||||||
|
void session::drop_table(const std::string &table_name)
|
||||||
|
{
|
||||||
|
auto c = pool_.acquire();
|
||||||
|
if (!c.valid()) {
|
||||||
|
throw std::logic_error("no database connection available");
|
||||||
|
}
|
||||||
|
|
||||||
|
c->drop().table(table_name).execute();
|
||||||
|
}
|
||||||
|
|
||||||
query_result<record> session::fetch(const query_context &q) const
|
query_result<record> session::fetch(const query_context &q) const
|
||||||
{
|
{
|
||||||
auto c = pool_.acquire();
|
auto c = pool_.acquire();
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,15 @@
|
||||||
#include "matador/sql/entity.hpp"
|
#include "matador/sql/entity.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace matador::test {
|
namespace matador::test {
|
||||||
|
|
||||||
struct flight
|
struct flight
|
||||||
{
|
{
|
||||||
flight() = default;
|
flight() = default;
|
||||||
flight(unsigned long id, const sql::entity<airplane> &plane, const std::string &name)
|
flight(unsigned long id, const sql::entity<airplane> &plane, std::string name)
|
||||||
: id(id), airplane(plane), pilot_name(name) {}
|
: id(id), airplane(plane), pilot_name(std::move(name)) {}
|
||||||
|
|
||||||
unsigned long id{};
|
unsigned long id{};
|
||||||
sql::entity<test::airplane> airplane;
|
sql::entity<test::airplane> airplane;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue