added any type visitor and converter
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/any_type.hpp"
|
||||
#include "matador/sql/any_type_to_visitor.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Convert any type to string", "[any type visitor]") {
|
||||
any_type_to_visitor<std::string> to_string_visitor;
|
||||
|
||||
any_type value = 6;
|
||||
std::visit(to_string_visitor, value);
|
||||
REQUIRE(to_string_visitor.result == "6");
|
||||
|
||||
value = 2.5;
|
||||
std::visit(to_string_visitor, value);
|
||||
REQUIRE(to_string_visitor.result == "2.5");
|
||||
|
||||
value = true;
|
||||
std::visit(to_string_visitor, value);
|
||||
REQUIRE(to_string_visitor.result == "true");
|
||||
|
||||
value = "hello";
|
||||
std::visit(to_string_visitor, value);
|
||||
REQUIRE(to_string_visitor.result == "hello");
|
||||
|
||||
value = std::string{"world"};
|
||||
std::visit(to_string_visitor, value);
|
||||
REQUIRE(to_string_visitor.result == "world");
|
||||
}
|
||||
|
||||
TEST_CASE("Convert any type to integral", "[any type visitor]") {
|
||||
any_type_to_visitor<long> to_long_visitor;
|
||||
|
||||
any_type value = 6;
|
||||
std::visit(to_long_visitor, value);
|
||||
REQUIRE(to_long_visitor.result == 6);
|
||||
|
||||
value = 2.5;
|
||||
std::visit(to_long_visitor, value);
|
||||
REQUIRE(to_long_visitor.result == 2);
|
||||
|
||||
value = true;
|
||||
std::visit(to_long_visitor, value);
|
||||
REQUIRE(to_long_visitor.result == 1);
|
||||
|
||||
value = "hello";
|
||||
std::visit(to_long_visitor, value);
|
||||
REQUIRE(to_long_visitor.result == 0);
|
||||
|
||||
value = std::string{"world"};
|
||||
std::visit(to_long_visitor, value);
|
||||
REQUIRE(to_long_visitor.result == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Convert any type to floating point", "[any type visitor]") {
|
||||
any_type_to_visitor<double> to_double_visitor;
|
||||
|
||||
any_type value = 6;
|
||||
std::visit(to_double_visitor, value);
|
||||
REQUIRE(to_double_visitor.result == 6);
|
||||
|
||||
value = 2.5;
|
||||
std::visit(to_double_visitor, value);
|
||||
REQUIRE(to_double_visitor.result == 2.5);
|
||||
|
||||
value = true;
|
||||
std::visit(to_double_visitor, value);
|
||||
REQUIRE(to_double_visitor.result == 1);
|
||||
|
||||
value = "hello";
|
||||
std::visit(to_double_visitor, value);
|
||||
REQUIRE(to_double_visitor.result == 0);
|
||||
|
||||
value = std::string{"world"};
|
||||
std::visit(to_double_visitor, value);
|
||||
REQUIRE(to_double_visitor.result == 0);
|
||||
}
|
||||
+3
-1
@@ -23,7 +23,9 @@ add_executable(tests builder.cpp
|
||||
models/supplier.hpp
|
||||
models/airplane.hpp
|
||||
models/flight.hpp
|
||||
models/person.hpp)
|
||||
models/person.hpp
|
||||
AnyTypeToVisitorTest.cpp
|
||||
ColumnTest.cpp)
|
||||
target_link_libraries(tests PRIVATE
|
||||
Catch2::Catch2WithMain
|
||||
matador
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create empty column", "[column]") {
|
||||
column c("name");
|
||||
|
||||
REQUIRE(c.name() == "name");
|
||||
REQUIRE(c.index() == 0);
|
||||
REQUIRE(c.type() == data_type_t::type_unknown);
|
||||
REQUIRE(c.ref_table().empty());
|
||||
REQUIRE(c.ref_column().empty());
|
||||
|
||||
c.set(std::string{"george"}, 255);
|
||||
REQUIRE(c.type() == data_type_t::type_varchar);
|
||||
REQUIRE(c.as<std::string>() == "george");
|
||||
|
||||
c.set(7);
|
||||
REQUIRE(c.type() == data_type_t::type_int);
|
||||
REQUIRE(c.as<std::string>() == "7");
|
||||
REQUIRE(c.as<long>() == 7);
|
||||
REQUIRE(c.str() == "7");
|
||||
}
|
||||
|
||||
TEST_CASE("Copy and move column", "[column]") {
|
||||
column c("name");
|
||||
c.set(std::string{"george"}, 255);
|
||||
REQUIRE(c.name() == "name");
|
||||
REQUIRE(c.index() == 0);
|
||||
REQUIRE(c.ref_table().empty());
|
||||
REQUIRE(c.ref_column().empty());
|
||||
REQUIRE(c.type() == data_type_t::type_varchar);
|
||||
REQUIRE(c.as<std::string>() == "george");
|
||||
REQUIRE(c.attributes().size() == 255);
|
||||
|
||||
auto c2 = c;
|
||||
REQUIRE(c2.name() == "name");
|
||||
REQUIRE(c2.index() == 0);
|
||||
REQUIRE(c2.ref_table().empty());
|
||||
REQUIRE(c2.ref_column().empty());
|
||||
REQUIRE(c2.type() == data_type_t::type_varchar);
|
||||
REQUIRE(c2.as<std::string>() == "george");
|
||||
REQUIRE(c2.attributes().size() == 255);
|
||||
|
||||
auto c3 = std::move(c2);
|
||||
REQUIRE(c3.name() == "name");
|
||||
REQUIRE(c3.index() == 0);
|
||||
REQUIRE(c3.ref_table().empty());
|
||||
REQUIRE(c3.ref_column().empty());
|
||||
REQUIRE(c3.type() == data_type_t::type_varchar);
|
||||
REQUIRE(c3.as<std::string>() == "george");
|
||||
REQUIRE(c3.attributes().size() == 255);
|
||||
|
||||
REQUIRE(c2.name().empty());
|
||||
REQUIRE(c2.index() == 0);
|
||||
REQUIRE(c2.ref_table().empty());
|
||||
REQUIRE(c2.ref_column().empty());
|
||||
REQUIRE(c2.type() == data_type_t::type_varchar);
|
||||
REQUIRE(c2.as<std::string>().empty());
|
||||
REQUIRE(c2.attributes().size() == 255);
|
||||
}
|
||||
+65
-53
@@ -8,120 +8,132 @@
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create table sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
auto sql = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).compile();
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
auto q = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).compile();
|
||||
|
||||
REQUIRE(sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(q.table_name == "person");
|
||||
|
||||
sql = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age"),
|
||||
make_fk_column<unsigned long>("address", "address", "id")
|
||||
}).compile();
|
||||
q = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age"),
|
||||
make_fk_column<unsigned long>("address", "address", "id")
|
||||
}).compile();
|
||||
|
||||
REQUIRE(sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, "address" BIGINT, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
|
||||
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, "address" BIGINT, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Drop table sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.drop().table("person").compile();
|
||||
const auto q = query.drop().table("person").compile();
|
||||
|
||||
REQUIRE(sql == R"(DROP TABLE "person")");
|
||||
REQUIRE(q.sql == R"(DROP TABLE "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.select({"id", "name", "age"}).from("person").compile();
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"}).from("person").compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person")");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Insert sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.insert().into("person", {
|
||||
const auto q = query.insert().into("person", {
|
||||
"id", "name", "age"
|
||||
}).values({7UL, "george", 65U}).compile();
|
||||
|
||||
REQUIRE(sql == R"(INSERT INTO "person" ("id", "name", "age") VALUES (7, 'george', 65))");
|
||||
REQUIRE(q.sql == R"(INSERT INTO "person" ("id", "name", "age") VALUES (7, 'george', 65))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Update sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.update("person").set({
|
||||
const auto q = query.update("person").set({
|
||||
{"id", 7UL},
|
||||
{"name", "george"},
|
||||
{"age", 65U}
|
||||
}).compile();
|
||||
|
||||
REQUIRE(sql == R"(UPDATE "person" SET "id"=7, "name"='george', "age"=65)");
|
||||
REQUIRE(q.sql == R"(UPDATE "person" SET "id"=7, "name"='george', "age"=65)");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Delete sql statement string", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.remove().from("person").compile();
|
||||
const auto q = query.remove().from("person").compile();
|
||||
|
||||
REQUIRE(sql == R"(DELETE FROM "person")");
|
||||
REQUIRE(q.sql == R"(DELETE FROM "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with where clause", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
auto sql = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == 8 && "age"_col > 50)
|
||||
.compile();
|
||||
auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == 8 && "age"_col > 50)
|
||||
.compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = 8 AND "age" > 50))");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = 8 AND "age" > 50))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
|
||||
sql = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == _ && "age"_col > 50)
|
||||
.compile();
|
||||
q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == _ && "age"_col > 50)
|
||||
.compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = ? AND "age" > 50))");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = ? AND "age" > 50))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with order by", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.compile();
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person" ORDER BY "name" ASC)");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" ORDER BY "name" ASC)");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with group by", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.compile();
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person" GROUP BY "age")");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" GROUP BY "age")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with offset and limit", "[query]") {
|
||||
dialect d;
|
||||
query_builder query(d);
|
||||
const auto sql = query.select({"id", "name", "age"})
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.offset(10)
|
||||
.limit(20)
|
||||
.compile();
|
||||
|
||||
REQUIRE(sql == R"(SELECT "id", "name", "age" FROM "person" OFFSET 10 LIMIT 20)");
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" OFFSET 10 LIMIT 20)");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
+30
-12
@@ -79,13 +79,13 @@ TEST_CASE("Execute insert record statement", "[session]") {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).value<long long>() == 7);
|
||||
REQUIRE(i.at(0).as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).value<std::string>() == "george");
|
||||
REQUIRE(i.at(1).as<std::string>() == "george");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).value<int>() == 45);
|
||||
REQUIRE(i.at(2).as<int>() == 45);
|
||||
}
|
||||
|
||||
s.drop().table("person").execute();
|
||||
@@ -132,13 +132,13 @@ TEST_CASE("Execute update record statement", "[session]") {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).value<long long>() == 7);
|
||||
REQUIRE(i.at(0).as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).value<std::string>() == "jane");
|
||||
REQUIRE(i.at(1).as<std::string>() == "jane");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).value<int>() == 35);
|
||||
REQUIRE(i.at(2).as<int>() == 35);
|
||||
}
|
||||
|
||||
s.drop().table("person").execute();
|
||||
@@ -161,23 +161,41 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
|
||||
.execute();
|
||||
REQUIRE(res.first == 1);
|
||||
|
||||
// fetch person as record
|
||||
auto result_record = s.select<person>()
|
||||
// fetch specific columns as record
|
||||
auto result_record = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto& i : result_record) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).value<long long>() == george.id);
|
||||
REQUIRE(i.at(0).as<long long>() == george.id);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).value<std::string>() == george.name);
|
||||
REQUIRE(i.at(1).as<std::string>() == george.name);
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long);
|
||||
REQUIRE(i.at(2).value<long long>() == george.age);
|
||||
REQUIRE(i.at(2).as<long long>() == george.age);
|
||||
}
|
||||
|
||||
// fetch person as record
|
||||
result_record = s.select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto& i : result_record) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).as<long long>() == george.id);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).as<std::string>() == george.name);
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long);
|
||||
REQUIRE(i.at(2).as<long long>() == george.age);
|
||||
}
|
||||
|
||||
// fetch person as person
|
||||
|
||||
Reference in New Issue
Block a user