query compiler progress
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
@@ -29,7 +30,7 @@ protected:
|
||||
private:
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (db.exists(table_name)) {
|
||||
db.drop().table(table_name).execute();
|
||||
db.query().drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -39,7 +40,7 @@ using namespace matador::sql;
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement", "[session][record]")
|
||||
{
|
||||
REQUIRE(!db.exists("person"));
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -49,7 +50,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement", "[sessi
|
||||
|
||||
REQUIRE(db.exists("person"));
|
||||
|
||||
db.drop()
|
||||
db.query().drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
|
||||
@@ -58,7 +59,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement", "[sessi
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement with foreign key", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
@@ -68,7 +69,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement with fore
|
||||
|
||||
REQUIRE(db.exists("airplane"));
|
||||
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
@@ -78,13 +79,13 @@ TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement with fore
|
||||
|
||||
REQUIRE(db.exists("flight"));
|
||||
|
||||
db.drop()
|
||||
db.query().drop()
|
||||
.table("flight")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!db.exists("flight"));
|
||||
|
||||
db.drop()
|
||||
db.query().drop()
|
||||
.table("airplane")
|
||||
.execute();
|
||||
|
||||
@@ -93,7 +94,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement with fore
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -101,14 +102,14 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement", "[sessi
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert()
|
||||
auto res = db.query().insert()
|
||||
.into("person", {"id", "name", "age"})
|
||||
.values({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select({"id", "name", "age"})
|
||||
auto result = db.query().select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
@@ -125,14 +126,14 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement", "[sessi
|
||||
REQUIRE(i.at(2).template as<int>() == 45);
|
||||
}
|
||||
|
||||
db.drop()
|
||||
db.query().drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement with foreign key", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
@@ -140,7 +141,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement with fore
|
||||
})
|
||||
.execute();
|
||||
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
@@ -148,23 +149,23 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement with fore
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
|
||||
auto res = db.query().insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = db.insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
|
||||
res = db.query().insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = db.insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
|
||||
res = db.query().insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
|
||||
auto count = db.query().select({count_all()}).from("airplane").fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
res = db.insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
|
||||
res = db.query().insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
db.drop().table("flight").execute();
|
||||
db.drop().table("airplane").execute();
|
||||
db.query().drop().table("flight").execute();
|
||||
db.query().drop().table("airplane").execute();
|
||||
|
||||
REQUIRE(!db.exists("flight"));
|
||||
REQUIRE(!db.exists("airplane"));
|
||||
@@ -172,7 +173,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement with fore
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute update record statement", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -180,14 +181,14 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute update record statement", "[sessi
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert()
|
||||
auto res = db.query().insert()
|
||||
.into("person", {"id", "name", "age"})
|
||||
.values({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = db.update("person")
|
||||
res = db.query().update("person")
|
||||
.set({{"id", 7},
|
||||
{"name", "jane"},
|
||||
{"age", 35}})
|
||||
@@ -196,7 +197,7 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute update record statement", "[sessi
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select({"id", "name", "age"})
|
||||
auto result = db.query().select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
@@ -213,12 +214,12 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute update record statement", "[sessi
|
||||
REQUIRE(i.at(2).as<int>() == 35);
|
||||
}
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -226,16 +227,16 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement", "[session][rec
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
auto res = db.query().insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select({"id", "name", "age"})
|
||||
auto result = db.query().select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
@@ -246,22 +247,22 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement", "[session][rec
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
auto rec = db.select({"id", "name", "age"})
|
||||
auto rec = db.query().select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_one();
|
||||
REQUIRE(rec.at(1).str() == "george");
|
||||
|
||||
auto name = db.select({"name"})
|
||||
auto name = db.query().select({"name"})
|
||||
.from("person")
|
||||
.fetch_value<std::string>();
|
||||
REQUIRE(name == "george");
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with order by", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -269,16 +270,16 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with order by",
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
auto res = db.query().insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select({"id", "name", "age"})
|
||||
auto result = db.query().select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.fetch_all();
|
||||
@@ -290,12 +291,12 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with order by",
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with group by and order by", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -303,18 +304,18 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with group by an
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
auto res = db.query().insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select({alias(count("age"), "age_count"), "age"})
|
||||
auto result = db.query().select({alias(count("age"), "age_count"), "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.order_by("age_count").desc()
|
||||
@@ -329,41 +330,41 @@ TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with group by an
|
||||
expected_values.pop_front();
|
||||
}
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Execute delete statement", "[session][record]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute();
|
||||
|
||||
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
auto res = db.query().insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
res = db.query().insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto count = db.select({count_all()}).from("person").fetch_value<int>();
|
||||
auto count = db.query().select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 2);
|
||||
|
||||
res = db.remove()
|
||||
res = db.query().remove()
|
||||
.from("person")
|
||||
.where("id"_col == 1)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
count = db.select({count_all()}).from("person").fetch_value<int>();
|
||||
count = db.query().select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 1);
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryRecordFixture, " Test quoted identifier", "[session][record]") {
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("quotes", {
|
||||
make_column<std::string>("from", 255),
|
||||
make_column<std::string>("to", 255)
|
||||
@@ -379,19 +380,19 @@ TEST_CASE_METHOD(QueryRecordFixture, " Test quoted identifier", "[session][recor
|
||||
REQUIRE(field.type() == types[field.index()]);
|
||||
}
|
||||
|
||||
db.insert().into("quotes", {"from", "to"}).values({"Berlin", "London"}).execute();
|
||||
db.query().insert().into("quotes", {"from", "to"}).values({"Berlin", "London"}).execute();
|
||||
|
||||
auto res = db.select({"from", "to"}).from("quotes").fetch_one();
|
||||
auto res = db.query().select({"from", "to"}).from("quotes").fetch_one();
|
||||
|
||||
REQUIRE("Berlin" == res.at("from").str());
|
||||
REQUIRE("London" == res.at("to").str());
|
||||
|
||||
db.update("quotes").set({{"from", "Hamburg"}, {"to", "New York"}}).where("from"_col == "Berlin").execute();
|
||||
db.query().update("quotes").set({{"from", "Hamburg"}, {"to", "New York"}}).where("from"_col == "Berlin").execute();
|
||||
|
||||
res = db.select({"from", "to"}).from("quotes").fetch_one();
|
||||
res = db.query().select({"from", "to"}).from("quotes").fetch_one();
|
||||
|
||||
REQUIRE("Hamburg" == res.at("from").str());
|
||||
REQUIRE("New York" == res.at("to").str());
|
||||
|
||||
db.drop().table("quotes").execute();
|
||||
db.query().drop().table("quotes").execute();
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
@@ -36,27 +37,27 @@ private:
|
||||
void drop_table_if_exists(const std::string &table_name)
|
||||
{
|
||||
if (db.exists(table_name)) {
|
||||
db.drop().table(table_name).execute();
|
||||
db.query().drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
REQUIRE(db.exists("airplane"));
|
||||
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<flight>("flight")
|
||||
.execute();
|
||||
|
||||
REQUIRE(db.exists("flight"));
|
||||
|
||||
db.drop().table("flight").execute();
|
||||
db.drop().table("airplane").execute();
|
||||
db.query().drop().table("flight").execute();
|
||||
db.query().drop().table("airplane").execute();
|
||||
|
||||
REQUIRE(!db.exists("flight"));
|
||||
REQUIRE(!db.exists("airplane"));
|
||||
@@ -64,20 +65,20 @@ TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[sess
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<person>("person")
|
||||
.execute();
|
||||
|
||||
person george{7, "george", 45};
|
||||
george.image.push_back(37);
|
||||
|
||||
auto res = db.insert()
|
||||
auto res = db.query().insert()
|
||||
.into("person", george)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
// fetch person as record
|
||||
auto result_record = db.select<person>()
|
||||
auto result_record = db.query().select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
@@ -96,7 +97,7 @@ TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[
|
||||
}
|
||||
|
||||
// fetch person as person
|
||||
auto result_person = db.select<person>()
|
||||
auto result_person = db.query().select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all<person>();
|
||||
@@ -107,12 +108,12 @@ TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[
|
||||
REQUIRE(i.age == 45);
|
||||
}
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
@@ -120,7 +121,7 @@ TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = db.insert()
|
||||
auto res = db.query().insert()
|
||||
.into("person", {{"", "id", ""}, {"", "name", ""}, {"", "color", ""}})
|
||||
.values({7, "george", "green"})
|
||||
.execute();
|
||||
@@ -128,7 +129,7 @@ TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]")
|
||||
REQUIRE(res == 1);
|
||||
|
||||
// fetch person as record
|
||||
auto result_record = db.select({"id", "name", "color"})
|
||||
auto result_record = db.query().select({"id", "name", "color"})
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
@@ -146,16 +147,16 @@ TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]")
|
||||
REQUIRE(i.at(2).as<std::string>() == "green");
|
||||
}
|
||||
|
||||
db.drop().table("person").execute();
|
||||
db.query().drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<flight>("flight")
|
||||
.execute();
|
||||
|
||||
@@ -166,35 +167,35 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]"
|
||||
};
|
||||
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query().insert().into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
|
||||
auto count = db.query().select({count_all()}).from("airplane").fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
flight f4711{4, planes.at(1), "hans"};
|
||||
|
||||
auto res = db.insert().into<flight>("flight").values(f4711).execute();
|
||||
auto res = db.query().insert().into<flight>("flight").values(f4711).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto f = *db.select<flight>().from("flight").fetch_all<flight>().begin();
|
||||
auto f = *db.query().select<flight>().from("flight").fetch_all<flight>().begin();
|
||||
REQUIRE(f.id == 4);
|
||||
REQUIRE(f.pilot_name == "hans");
|
||||
REQUIRE(f.airplane.get() != nullptr);
|
||||
REQUIRE(f.airplane->id == 2);
|
||||
|
||||
db.drop().table("flight").execute();
|
||||
db.drop().table("airplane").execute();
|
||||
db.query().drop().table("flight").execute();
|
||||
db.query().drop().table("airplane").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[session][join]")
|
||||
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join_left", "[session][join_left]")
|
||||
{
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<flight>("flight")
|
||||
.execute();
|
||||
|
||||
@@ -205,11 +206,11 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[
|
||||
};
|
||||
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query().insert().into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
|
||||
auto count = db.query().select({count_all()}).from("airplane").fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
std::vector<entity<flight>> flights{
|
||||
@@ -220,20 +221,20 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[
|
||||
};
|
||||
|
||||
for (const auto &f: flights) {
|
||||
auto res = db.insert().into<flight>("flight").values(*f).execute();
|
||||
auto res = db.query().insert().into<flight>("flight").values(*f).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto f = *db.select<flight>().from("flight").fetch_all<flight>().begin();
|
||||
auto f = *db.query().select<flight>().from("flight").fetch_all<flight>().begin();
|
||||
REQUIRE(f.id == 4);
|
||||
REQUIRE(f.pilot_name == "hans");
|
||||
REQUIRE(f.airplane.get() != nullptr);
|
||||
REQUIRE(f.airplane->id == 1);
|
||||
|
||||
auto result = db.select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
|
||||
.from("flight", "f")
|
||||
.join("airplane", "ap")
|
||||
.on("f.airplane_id", "ap.id")
|
||||
auto result = db.query().select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
|
||||
.from("flight", "f")
|
||||
.join_left("airplane", "ap")
|
||||
.on("f.airplane_id"_col == "ap.id"_col)
|
||||
.order_by("f.id").asc()
|
||||
.fetch_all();
|
||||
|
||||
@@ -250,7 +251,7 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[
|
||||
REQUIRE(r.at(3).as<std::string>() == expected_result[index++].second);
|
||||
}
|
||||
|
||||
db.drop().table("flight").execute();
|
||||
db.drop().table("airplane").execute();
|
||||
db.query().drop().table("flight").execute();
|
||||
db.query().drop().table("airplane").execute();
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
@@ -18,7 +18,7 @@ public:
|
||||
: db(matador::test::connection::dns)
|
||||
{
|
||||
db.open();
|
||||
db.create().table<airplane>("airplane").execute();
|
||||
db.query().create().table<airplane>("airplane").execute();
|
||||
}
|
||||
|
||||
~StatementTestFixture()
|
||||
@@ -38,7 +38,7 @@ protected:
|
||||
private:
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (db.exists(table_name)) {
|
||||
db.drop().table(table_name).execute();
|
||||
db.query().drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ private:
|
||||
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
|
||||
{
|
||||
SECTION("Insert with prepared statement and placeholder") {
|
||||
auto stmt = db.insert()
|
||||
auto stmt = db.query().insert()
|
||||
.into<airplane>("airplane")
|
||||
.values<airplane>().prepare();
|
||||
|
||||
@@ -57,7 +57,7 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
|
||||
stmt.reset();
|
||||
}
|
||||
|
||||
auto result = db.select<airplane>().from("airplane").fetch_all<airplane>();
|
||||
auto result = db.query().select<airplane>().from("airplane").fetch_all<airplane>();
|
||||
|
||||
size_t index{0};
|
||||
for (const auto &i: result) {
|
||||
@@ -69,11 +69,11 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
|
||||
|
||||
SECTION("Select with prepared statement") {
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query().insert().into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto stmt = db.select<airplane>().from("airplane").where("brand"_col == _).prepare();
|
||||
auto stmt = db.query().select<airplane>().from("airplane").where("brand"_col == _).prepare();
|
||||
|
||||
stmt.bind(0, "Airbus");
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ public:
|
||||
: db(matador::test::connection::dns)
|
||||
{
|
||||
db.open();
|
||||
db.create()
|
||||
db.query().create()
|
||||
.table<location>("location")
|
||||
.execute();
|
||||
}
|
||||
|
||||
~TypeTraitsTestFixture()
|
||||
{
|
||||
db.drop().table("location").execute();
|
||||
db.query().drop().table("location").execute();
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -33,7 +33,7 @@ protected:
|
||||
private:
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (db.exists(table_name)) {
|
||||
db.drop().table(table_name).execute();
|
||||
db.query().drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -85,10 +85,10 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||
SECTION("Insert and select with direct execution") {
|
||||
location loc{1, "center", {1, 2, 3}, Color::Black};
|
||||
|
||||
auto res = db.insert().into<location>("location").values(loc).execute();
|
||||
auto res = db.query().insert().into<location>("location").values(loc).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select<location>().from("location").fetch_all<location>();
|
||||
auto result = db.query().select<location>().from("location").fetch_all<location>();
|
||||
|
||||
for (const auto &l: result) {
|
||||
REQUIRE(l.name == "center");
|
||||
@@ -98,11 +98,11 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||
SECTION("Insert and select with prepared statement") {
|
||||
location loc{1, "center", {1, 2, 3}, Color::Black};
|
||||
|
||||
auto stmt = db.insert().into<location>("location").values<location>().prepare();
|
||||
auto stmt = db.query().insert().into<location>("location").values<location>().prepare();
|
||||
auto res = stmt.bind(loc).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db.select<location>().from("location").
|
||||
auto result = db.query().select<location>().from("location").
|
||||
template fetch_all<location>();
|
||||
|
||||
for (const auto &l: result) {
|
||||
|
||||
Reference in New Issue
Block a user