From f3c502a2ce487d6d1b5a4ffe8cde6f761c4258a3 Mon Sep 17 00:00:00 2001 From: Sascha Kuehl Date: Sun, 26 Nov 2023 22:17:59 +0100 Subject: [PATCH] added describe table and table exists to session --- include/matador/sql/session.hpp | 3 +++ src/sql/session.cpp | 18 ++++++++++++++++++ test/CMakeLists.txt | 1 - test/ConnectionTest.cpp | 6 +++++- test/SessionRecordTest.cpp | 8 +++++++- test/query.cpp | 24 ------------------------ 6 files changed, 33 insertions(+), 27 deletions(-) delete mode 100644 test/query.cpp diff --git a/include/matador/sql/session.hpp b/include/matador/sql/session.hpp index 44b1bf5..c70e5c6 100644 --- a/include/matador/sql/session.hpp +++ b/include/matador/sql/session.hpp @@ -35,6 +35,9 @@ public: // [[nodiscard]] query_result fetch(const std::string &sql) const; [[nodiscard]] std::pair execute(const std::string &sql) const; + 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) { diff --git a/src/sql/session.cpp b/src/sql/session.cpp index 66db828..832f121 100644 --- a/src/sql/session.cpp +++ b/src/sql/session.cpp @@ -73,6 +73,24 @@ std::pair session::execute(const std::string &sql) const { return c->execute(sql); } +record session::describe_table(const std::string &table_name) const +{ + auto c = pool_.acquire(); + if (!c.valid()) { + throw std::logic_error("no database connection available"); + } + return c->describe(table_name); +} + +bool session::table_exists(const std::string &table_name) const +{ + auto c = pool_.acquire(); + if (!c.valid()) { + throw std::logic_error("no database connection available"); + } + return c->exists(table_name); +} + const table_repository& session::tables() const { return table_repository_; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f7910f2..582a676 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,7 +12,6 @@ add_executable(tests QueryBuilderTest.cpp SessionTest.cpp RecordTest.cpp ConnectionPoolTest.cpp - query.cpp BackendProviderTest.cpp ConnectionTest.cpp models/product.hpp diff --git a/test/ConnectionTest.cpp b/test/ConnectionTest.cpp index de0ed26..3e2a034 100644 --- a/test/ConnectionTest.cpp +++ b/test/ConnectionTest.cpp @@ -6,7 +6,11 @@ using namespace matador::sql; TEST_CASE("Create connection", "[connection]") { - connection c("sqlite://test.db"); + auto dns = GENERATE(as{}, + "sqlite://sqlite.db", + "postgres://test:test123@127.0.0.1:5432/matador_test" ); + + connection c(dns); REQUIRE(!c.is_open()); c.open(); diff --git a/test/SessionRecordTest.cpp b/test/SessionRecordTest.cpp index c298081..e84a57a 100644 --- a/test/SessionRecordTest.cpp +++ b/test/SessionRecordTest.cpp @@ -9,9 +9,13 @@ using namespace matador::sql; TEST_CASE("Create and drop table statement", "[session record]") { - connection_pool pool("sqlite://sqlite.db", 4); + auto dns = GENERATE(as{}, + "sqlite://sqlite.db", + "postgres://test:test123@127.0.0.1:5432/matador_test" ); + connection_pool pool(dns, 4); session s(pool); + REQUIRE(!s.table_exists("person")); auto res = s.create() .table("person", { make_pk_column("id"), @@ -22,6 +26,8 @@ TEST_CASE("Create and drop table statement", "[session record]") { REQUIRE(res.second == R"(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))"); + REQUIRE(s.table_exists("person")); + res = s.drop() .table("person") .execute(); diff --git a/test/query.cpp b/test/query.cpp deleted file mode 100644 index 2e039b2..0000000 --- a/test/query.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -#include "matador/sql/connection.hpp" - -using namespace matador; - -TEST_CASE("Query test", "[query]") { - auto dns = GENERATE(as{}, - "sqlite://sqlite.db", - "postgres://test:test123@127.0.0.1:5432/matador_test" ); - - sql::connection c(dns); - REQUIRE(!c.is_open()); - - c.open(); - REQUIRE(c.is_open()); - - c.close(); - REQUIRE(!c.is_open()); - - INFO(dns); - REQUIRE(!dns.empty()); -}