added describe table and table exists to session
This commit is contained in:
parent
a436af5293
commit
f3c502a2ce
|
|
@ -35,6 +35,9 @@ public:
|
|||
// [[nodiscard]] query_result<record> fetch(const std::string &sql) const;
|
||||
[[nodiscard]] std::pair<size_t, std::string> 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<typename Type>
|
||||
void attach(const std::string &table_name)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -73,6 +73,24 @@ std::pair<size_t, std::string> 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_;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ add_executable(tests QueryBuilderTest.cpp
|
|||
SessionTest.cpp
|
||||
RecordTest.cpp
|
||||
ConnectionPoolTest.cpp
|
||||
query.cpp
|
||||
BackendProviderTest.cpp
|
||||
ConnectionTest.cpp
|
||||
models/product.hpp
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@
|
|||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create connection", "[connection]") {
|
||||
connection c("sqlite://test.db");
|
||||
auto dns = GENERATE(as<std::string>{},
|
||||
"sqlite://sqlite.db",
|
||||
"postgres://test:test123@127.0.0.1:5432/matador_test" );
|
||||
|
||||
connection c(dns);
|
||||
REQUIRE(!c.is_open());
|
||||
|
||||
c.open();
|
||||
|
|
|
|||
|
|
@ -9,9 +9,13 @@
|
|||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create and drop table statement", "[session record]") {
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
auto dns = GENERATE(as<std::string>{},
|
||||
"sqlite://sqlite.db",
|
||||
"postgres://test:test123@127.0.0.1:5432/matador_test" );
|
||||
connection_pool<connection> pool(dns, 4);
|
||||
session s(pool);
|
||||
|
||||
REQUIRE(!s.table_exists("person"));
|
||||
auto res = s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("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();
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
using namespace matador;
|
||||
|
||||
TEST_CASE("Query test", "[query]") {
|
||||
auto dns = GENERATE(as<std::string>{},
|
||||
"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());
|
||||
}
|
||||
Loading…
Reference in New Issue