diff --git a/CMakeLists.txt b/CMakeLists.txt index b697f02..1d7ed0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,10 @@ 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_POSITION_INDEPENDENT_CODE ON) diff --git a/backends/mysql/test/CMakeLists.txt b/backends/mysql/test/CMakeLists.txt index 7834f2a..d535b46 100644 --- a/backends/mysql/test/CMakeLists.txt +++ b/backends/mysql/test/CMakeLists.txt @@ -25,7 +25,8 @@ set(TEST_SOURCES ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp - ../../tests/StatementCacheTest.cpp) + ../../tests/StatementCacheTest.cpp + ../../tests/SessionTest.cpp) set(LIBRARY_TEST_TARGET mysql_tests) diff --git a/backends/postgres/test/CMakeLists.txt b/backends/postgres/test/CMakeLists.txt index a07d07f..efa28ee 100644 --- a/backends/postgres/test/CMakeLists.txt +++ b/backends/postgres/test/CMakeLists.txt @@ -12,7 +12,7 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) include(CTest) 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) @@ -25,7 +25,8 @@ set(TEST_SOURCES ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp - ../../tests/StatementCacheTest.cpp) + ../../tests/StatementCacheTest.cpp + ../../tests/SessionTest.cpp) set(LIBRARY_TEST_TARGET postgres_tests) diff --git a/backends/sqlite/test/CMakeLists.txt b/backends/sqlite/test/CMakeLists.txt index 8be1d13..e3d0f3a 100644 --- a/backends/sqlite/test/CMakeLists.txt +++ b/backends/sqlite/test/CMakeLists.txt @@ -25,7 +25,8 @@ set(TEST_SOURCES ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp - ../../tests/StatementCacheTest.cpp) + ../../tests/StatementCacheTest.cpp + ../../tests/SessionTest.cpp) set(LIBRARY_TEST_TARGET sqlite_tests) diff --git a/backends/tests/SessionTest.cpp b/backends/tests/SessionTest.cpp new file mode 100644 index 0000000..d46e86d --- /dev/null +++ b/backends/tests/SessionTest.cpp @@ -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 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("airplane"); + ses.insert(1, "Boeing", "A380"); + + REQUIRE(true); +} \ No newline at end of file diff --git a/include/matador/sql/session.hpp b/include/matador/sql/session.hpp index 4e54976..291d7f0 100644 --- a/include/matador/sql/session.hpp +++ b/include/matador/sql/session.hpp @@ -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 &pool); + template + void attach(const std::string &table_name); + + template + entity insert(Type *obj); + + template< class Type, typename... Args > + entity insert(Args&&... args) { + return insert(new Type(std::forward(args)...)); + } + + template + void drop_table(); + void drop_table(const std::string &table_name); + [[nodiscard]] query_result fetch(const query_context &q) const; // [[nodiscard]] query_result 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 - void attach(const std::string &table_name) - { - table_repository_.attach(table_name); - } - [[nodiscard]] const table_repository& tables() const; const class dialect& dialect() const; @@ -51,5 +58,33 @@ private: mutable std::unordered_map prototypes_; }; +template +void session::attach(const std::string &table_name) +{ + table_repository_.attach(table_name); +} + +template +entity session::insert(Type *obj) +{ + auto c = pool_.acquire(); + auto info = table_repository_.info(); + if (!info) { + return {}; + } + c->insert().into(info->name).values(*obj).execute(); + + return entity{obj}; +} + +template +void session::drop_table() +{ + auto info = table_repository_.info(); + if (info) { + return drop_table(info.name); + } + +} } #endif //QUERY_SESSION_HPP diff --git a/src/sql/session.cpp b/src/sql/session.cpp index d9b9366..fdd3462 100644 --- a/src/sql/session.cpp +++ b/src/sql/session.cpp @@ -10,6 +10,16 @@ session::session(connection_pool &pool) : pool_(pool) , 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 session::fetch(const query_context &q) const { auto c = pool_.acquire(); diff --git a/test/models/flight.hpp b/test/models/flight.hpp index 88ed0ea..fda82b4 100644 --- a/test/models/flight.hpp +++ b/test/models/flight.hpp @@ -10,14 +10,15 @@ #include "matador/sql/entity.hpp" #include +#include namespace matador::test { struct flight { flight() = default; - flight(unsigned long id, const sql::entity &plane, const std::string &name) - : id(id), airplane(plane), pilot_name(name) {} + flight(unsigned long id, const sql::entity &plane, std::string name) + : id(id), airplane(plane), pilot_name(std::move(name)) {} unsigned long id{}; sql::entity airplane;