query progress
This commit is contained in:
+207
-155
@@ -10,190 +10,242 @@
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Create table sql statement string", "[query]") {
|
||||
TEST_CASE("Create table sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query qu(noop, scm);
|
||||
auto s = qu.create().table({"person"}, {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).str();
|
||||
query q(noop, scm);
|
||||
auto result = q.create().table({"person"}, {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).build();
|
||||
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
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(result.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "age" INTEGER NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(result.table.name == "person");
|
||||
|
||||
REQUIRE(s == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "age" INTEGER NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
// REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "age" INTEGER NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(q.table_name == "person");
|
||||
result = q.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", {255, constraints::UNIQUE}, null_option::NOT_NULL),
|
||||
make_column<unsigned short>("age"),
|
||||
make_fk_column<unsigned long>("address", "address", "id")
|
||||
}).build();
|
||||
|
||||
q = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", { 255, constraints::UNIQUE }, null_option::NOT_NULL),
|
||||
make_column<unsigned short>("age"),
|
||||
make_fk_column<unsigned long>("address", "address", "id")
|
||||
}).compile();
|
||||
|
||||
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Drop table sql statement string", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.drop().table("person").compile();
|
||||
TEST_CASE("Drop table sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.drop().table("person").build();
|
||||
|
||||
REQUIRE(q.sql == R"(DROP TABLE "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(DROP TABLE "person")");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"}).from("person").compile();
|
||||
TEST_CASE("Select sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.select({"id", "name", "age"}).from("person").build();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person")");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Insert sql statement string", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.insert().into("person", {
|
||||
"id", "name", "age"
|
||||
}).values({7UL, "george", 65U}).compile();
|
||||
|
||||
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 = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.update("person").set({
|
||||
{"id", 7UL},
|
||||
{"name", "george"},
|
||||
{"age", 65U}
|
||||
}).compile();
|
||||
|
||||
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 = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.remove().from("person").compile();
|
||||
|
||||
REQUIRE(q.sql == R"(DELETE FROM "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with where clause", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == 8 && "age"_col > 50)
|
||||
.compile();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = 8 AND "age" > 50))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
|
||||
q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == _ && "age"_col > 50)
|
||||
.compile();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = ? AND "age" > 50))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Insert sql statement with placeholder", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.insert().into("person", {
|
||||
TEST_CASE("Insert sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.insert().into("person", {
|
||||
"id", "name", "age"
|
||||
}).values({_, _, _}).compile();
|
||||
}).values({7UL, "george", 65U}).build();
|
||||
|
||||
REQUIRE(q.sql == R"(INSERT INTO "person" ("id", "name", "age") VALUES (?, ?, ?))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(q.bind_vars.size() == 3);
|
||||
REQUIRE(result.sql == R"(INSERT INTO "person" ("id", "name", "age") VALUES (7, 'george', 65))");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with order by", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.compile();
|
||||
TEST_CASE("Update sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.update("person").set({
|
||||
{"id", 7UL},
|
||||
{"name", "george"},
|
||||
{"age", 65U}
|
||||
}).build();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" ORDER BY "name" ASC)");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(UPDATE "person" SET "id"=7, "name"='george', "age"=65)");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with group by", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.compile();
|
||||
TEST_CASE("Update limit sql statement", "[query][update][limit]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.update("person")
|
||||
.set({{"id", 7UL}, {"name", "george"}, {"age", 65U}})
|
||||
.where("name"_col == "george")
|
||||
.order_by("id"_col).asc()
|
||||
.limit(2)
|
||||
.build();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" GROUP BY "age")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(UPDATE "person" SET "id"=7, "name"='george', "age"=65 WHERE "name" = 'george' ORDER BY "id" ASC LIMIT 2)");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with offset and limit", "[query]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
const auto q = query.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.offset(10)
|
||||
.limit(20)
|
||||
.compile();
|
||||
TEST_CASE("Delete sql statement string", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.remove().from("person").build();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" OFFSET 10 LIMIT 20)");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(DELETE FROM "person")");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Create, insert and select a blob column", "[query][blob]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
auto q = query.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<blob>("data")
|
||||
}).compile();
|
||||
TEST_CASE("Delete limit sql statement", "[query][delete][limit]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.remove()
|
||||
.from("person")
|
||||
.where("name"_col == "george")
|
||||
.order_by("id"_col).asc()
|
||||
.limit(2)
|
||||
.build();
|
||||
|
||||
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "data" BLOB NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(q.table_name == "person");
|
||||
|
||||
q = query.insert().into("person", {
|
||||
{ "id", "name", "data" }
|
||||
}).values({7UL, "george", blob{1, 'A', 3, 4}}).compile();
|
||||
|
||||
REQUIRE(q.sql == R"(INSERT INTO "person" ("id", "name", "data") VALUES (7, 'george', X'01410304'))");
|
||||
REQUIRE(q.table_name == "person");
|
||||
|
||||
q = query.select({"id", "name", "data"}).from("person").compile();
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "id", "name", "data" FROM "person")");
|
||||
REQUIRE(q.table_name == "person");
|
||||
REQUIRE(result.sql == R"(DELETE FROM "person" WHERE "name" = 'george' ORDER BY "id" ASC LIMIT 2)");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select statement with join_left", "[query][join_left]") {
|
||||
dialect d = dialect_builder::builder().create().build();
|
||||
query_builder query(d);
|
||||
TEST_CASE("Select sql statement string with where clause", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
auto result = q.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == 8 && "age"_col > 50)
|
||||
.build();
|
||||
|
||||
auto q = query.select({"f.id", "ap.brand", "f.pilot_name"}).from("flight", "f").join("airplane", join_type_t::INNER, "ap").on("f.airplane_id", "ap.id").compile();
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = 8 AND "age" > 50))");
|
||||
REQUIRE(result.table.name == "person");
|
||||
|
||||
REQUIRE(q.sql == R"(SELECT "f.id", "ap.brand", "f.pilot_name" FROM "flight" AS "f" INNER JOIN "airplane" AS "ap" ON "f.airplane_id"="ap.id")");
|
||||
REQUIRE(q.table_name == "flight");
|
||||
result = q.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.where("id"_col == _ && "age"_col > 50)
|
||||
.build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person" WHERE ("id" = ? AND "age" > 50))");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Insert sql statement with placeholder", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.insert().into("person", {
|
||||
"id", "name", "age"
|
||||
}).values({_, _, _}).build();
|
||||
|
||||
REQUIRE(result.sql == R"(INSERT INTO "person" ("id", "name", "age") VALUES (?, ?, ?))");
|
||||
REQUIRE(result.table.name == "person");
|
||||
REQUIRE(result.bind_vars.size() == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with order by", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person" ORDER BY "name" ASC)");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with group by", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person" GROUP BY "age")");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select sql statement string with offset and limit", "[query]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
const auto result = q.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("id"_col).asc()
|
||||
.limit(20)
|
||||
.offset(10)
|
||||
.build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "age" FROM "person" ORDER BY "id" ASC LIMIT 20 OFFSET 10)");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Create, insert and select a blob column", "[query][blob]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
auto result = q.create().table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<blob>("data")
|
||||
}).build();
|
||||
|
||||
REQUIRE(result.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "data" BLOB NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
|
||||
REQUIRE(result.table.name == "person");
|
||||
|
||||
result = q.insert().into("person", {
|
||||
"id", "name", "data"
|
||||
}).values({7UL, "george", blob{1, 'A', 3, 4}}).build();
|
||||
|
||||
REQUIRE(result.sql == R"(INSERT INTO "person" ("id", "name", "data") VALUES (7, 'george', X'01410304'))");
|
||||
REQUIRE(result.table.name == "person");
|
||||
|
||||
result = q.select({"id", "name", "data"}).from("person").build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "id", "name", "data" FROM "person")");
|
||||
REQUIRE(result.table.name == "person");
|
||||
}
|
||||
|
||||
TEST_CASE("Select statement with join_left", "[query][join_left]")
|
||||
{
|
||||
connection noop("noop://noop.db");
|
||||
auto scm = std::make_shared<schema>("noop");
|
||||
query q(noop, scm);
|
||||
auto result = q.select({"f.id", "ap.brand", "f.pilot_name"})
|
||||
.from({"flight", "f"})
|
||||
.join_left({"airplane", "ap"})
|
||||
.on("f.airplane_id"_col == "ap.id"_col)
|
||||
.build();
|
||||
|
||||
REQUIRE(result.sql == R"(SELECT "f"."id", "ap"."brand", "f"."pilot_name" FROM "flight" AS "f" INNER JOIN "airplane" AS "ap" ON "f"."airplane_id" = "ap"."id")");
|
||||
REQUIRE(result.table.name == "flight");
|
||||
}
|
||||
Reference in New Issue
Block a user