query builder progress
This commit is contained in:
+180
-105
@@ -18,29 +18,34 @@ using namespace matador::object;
|
||||
using namespace matador::query;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record][data types]") {
|
||||
check_table_not_exists("types");
|
||||
|
||||
auto res = query::create()
|
||||
.table("types", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<int8_t>("val_char"),
|
||||
make_column<int16_t>("val_short"),
|
||||
make_column<int32_t>("val_int"),
|
||||
make_column<int64_t>("val_long_long"),
|
||||
make_column<uint8_t>("val_uchar"),
|
||||
make_column<uint16_t>("val_ushort"),
|
||||
make_column<uint32_t>("val_uint"),
|
||||
make_column<uint64_t>("val_ulong_long"),
|
||||
make_column<bool>("val_bool"),
|
||||
make_column<float>("val_float"),
|
||||
make_column<double>("val_double"),
|
||||
make_column<std::string>("val_string"),
|
||||
make_column<std::string>("val_varchar", 63),
|
||||
// make_column<matador::date>("val_date"),
|
||||
// make_column<matador::time>("val_time"),
|
||||
make_column<matador::utils::blob>("val_blob"),
|
||||
.table("types")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("val_char", basic_type::type_int8),
|
||||
attribute("val_short", basic_type::type_int16),
|
||||
attribute("val_int", basic_type::type_int32),
|
||||
attribute("val_long_long", basic_type::type_int64),
|
||||
attribute("val_uchar", basic_type::type_uint8),
|
||||
attribute("val_ushort", basic_type::type_uint16),
|
||||
attribute("val_uint", basic_type::type_uint32),
|
||||
attribute("val_ulong_long", basic_type::type_uint64),
|
||||
attribute("val_bool", basic_type::type_bool),
|
||||
attribute("val_float", basic_type::type_float),
|
||||
attribute("val_double", basic_type::type_double),
|
||||
attribute("val_string", basic_type::type_text),
|
||||
attribute("val_varchar", basic_type::type_varchar, 63),
|
||||
// attribute("val_date", basic_type::type_date),
|
||||
// attribute("val_time", basic_type::type_time),
|
||||
attribute("val_blob", basic_type::type_blob),
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_types").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -80,7 +85,7 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
|
||||
std::string varchar{"good day"};
|
||||
// auto md{matador::date()};
|
||||
// auto mt{matador::time::now()};
|
||||
matador::utils::blob bin{0x01,0x02,0x03,0x04};
|
||||
blob bin{0x01,0x02,0x03,0x04};
|
||||
|
||||
res = query::insert()
|
||||
.into("types", cols)
|
||||
@@ -112,17 +117,21 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
|
||||
REQUIRE(varchar == (*row)->at<std::string>("val_varchar"));
|
||||
// REQUIRE(md == (*row)->at<matador::date>("val_date"));
|
||||
// REQUIRE(mt == (*row)->at<matador::time>("val_time"));
|
||||
REQUIRE(bin == (*row)->at<matador::utils::blob>("val_blob"));
|
||||
REQUIRE(bin == (*row)->at<blob>("val_blob"));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Create and drop table statement", "[query][record]")
|
||||
{
|
||||
check_table_not_exists("person");
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -143,12 +152,16 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement", "[query][recor
|
||||
TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("airplane", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
})
|
||||
.execute(db);
|
||||
.table("airplane")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("brand", basic_type::type_varchar, 255),
|
||||
attribute("model", basic_type::type_varchar, 255),
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_airplane").primary_key("id"),
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
|
||||
@@ -156,11 +169,16 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key
|
||||
tables_to_drop.emplace("airplane");
|
||||
|
||||
res = query::create()
|
||||
.table("flight", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_fk_column<uint32_t>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.table("flight")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("airplane_id", basic_type::type_uint32),
|
||||
attribute("pilot_name", basic_type::type_varchar, 255)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_flight").primary_key("id"),
|
||||
constraint( "FK_flight_airplane_id").foreign_key("airplane_id").references("airplane", "id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
@@ -188,12 +206,16 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute insert record statement", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.execute(db);
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
tables_to_drop.emplace("person");
|
||||
@@ -227,10 +249,14 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement", "[query][recor
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("airplane", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
.table("airplane")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("brand", basic_type::type_varchar, 255),
|
||||
attribute("model", basic_type::type_varchar, 255),
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_airplane").primary_key("id"),
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -238,17 +264,22 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key
|
||||
tables_to_drop.emplace("airplane");
|
||||
|
||||
res = query::create()
|
||||
.table("flight", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_fk_column<uint32_t>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.execute(db);
|
||||
.table("flight")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("airplane_id", basic_type::type_uint32),
|
||||
attribute("pilot_name", basic_type::type_varchar, 255)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_flight").primary_key("id"),
|
||||
constraint( "FK_flight_airplane_id").foreign_key("airplane_id").references("airplane", "id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
tables_to_drop.emplace("flight");
|
||||
|
||||
std::vector<std::vector<matador::utils::database_type>> values_list{
|
||||
std::vector<std::vector<database_type>> values_list{
|
||||
{1, "Airbus", "A380"},
|
||||
{2, "Boeing", "707"},
|
||||
{3, "Boeing", "747"}
|
||||
@@ -281,10 +312,14 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute update record statement", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -330,17 +365,21 @@ TEST_CASE_METHOD(QueryFixture, "Execute update record statement", "[query][recor
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute select statement", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute(db);
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
tables_to_drop.emplace("person");
|
||||
|
||||
std::vector<std::vector<matador::utils::database_type>> values_list{
|
||||
std::vector<std::vector<database_type>> values_list{
|
||||
{1, "george", 45},
|
||||
{2, "jane", 32},
|
||||
{3, "michael", 67},
|
||||
@@ -386,17 +425,21 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement", "[query][record]")
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute select statement with order by", "[query][record]")
|
||||
{
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res.value() == 0);
|
||||
tables_to_drop.emplace("person");
|
||||
|
||||
std::vector<std::vector<matador::utils::database_type>> values_list{
|
||||
std::vector<std::vector<database_type>> values_list{
|
||||
{1, "george", 45},
|
||||
{2, "jane", 32},
|
||||
{3, "michael", 67},
|
||||
@@ -428,17 +471,21 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with order by", "[query
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute select statement with group by and order by", "[query][record]") {
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute(db);
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 0);
|
||||
tables_to_drop.emplace("person");
|
||||
|
||||
std::vector<std::vector<matador::utils::database_type>> values_list{
|
||||
std::vector<std::vector<database_type>> values_list{
|
||||
{1, "george", 45},
|
||||
{2, "jane", 45},
|
||||
{3, "joe", 45},
|
||||
@@ -477,11 +524,16 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with group by and order
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Execute delete statement", "[query][record]") {
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute(db);
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 0);
|
||||
tables_to_drop.emplace("person");
|
||||
@@ -522,9 +574,10 @@ TEST_CASE_METHOD(QueryFixture, "Execute delete statement", "[query][record]") {
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test quoted identifier record", "[query][record]") {
|
||||
auto res = query::create()
|
||||
.table("quotes", {
|
||||
make_column<std::string>("from", 255),
|
||||
make_column<std::string>("to", 255)
|
||||
.table("quotes")
|
||||
.columns({
|
||||
attribute("from", basic_type::type_varchar, 255),
|
||||
attribute("to", basic_type::type_varchar, 255)
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -533,16 +586,18 @@ TEST_CASE_METHOD(QueryFixture, "Test quoted identifier record", "[query][record]
|
||||
|
||||
// check table description
|
||||
std::vector<std::string> columns = { "from", "to"};
|
||||
std::vector<matador::utils::basic_type> types = {
|
||||
matador::utils::basic_type::type_varchar,
|
||||
matador::utils::basic_type::type_varchar
|
||||
std::vector types = {
|
||||
basic_type::type_varchar,
|
||||
basic_type::type_varchar
|
||||
};
|
||||
auto fields = db.describe("quotes");
|
||||
REQUIRE(fields.is_ok());
|
||||
|
||||
size_t i = 0;
|
||||
for (const auto &field : *fields) {
|
||||
REQUIRE(field.name() == columns[field.index()]);
|
||||
REQUIRE(field.type() == types[field.index()]);
|
||||
REQUIRE(field.name() == columns[i]);
|
||||
REQUIRE(field.type() == types[i]);
|
||||
++i;
|
||||
}
|
||||
|
||||
res = query::insert()
|
||||
@@ -580,10 +635,14 @@ TEST_CASE_METHOD(QueryFixture, "Test quoted identifier record", "[query][record]
|
||||
TEST_CASE_METHOD(QueryFixture, "Test create record", "[query][record][create]") {
|
||||
check_table_not_exists("person");
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -603,10 +662,14 @@ TEST_CASE_METHOD(QueryFixture, "Test create record", "[query][record][create]")
|
||||
TEST_CASE_METHOD(QueryFixture, "Test insert record", "[query][record][insert]") {
|
||||
check_table_not_exists("person");
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -636,10 +699,14 @@ TEST_CASE_METHOD(QueryFixture, "Test insert record", "[query][record][insert]")
|
||||
TEST_CASE_METHOD(QueryFixture, "Test update record", "[query][record][update]") {
|
||||
check_table_not_exists("person");
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
@@ -686,10 +753,14 @@ TEST_CASE_METHOD(QueryFixture, "Test update record", "[query][record][update]")
|
||||
TEST_CASE_METHOD(QueryFixture, "Test prepared record statement", "[query][record][prepared]") {
|
||||
check_table_not_exists("person");
|
||||
auto stmt = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32),
|
||||
attribute("name", basic_type::type_varchar, 255),
|
||||
attribute("age", basic_type::type_uint16)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.prepare(db);
|
||||
REQUIRE(stmt.is_ok());
|
||||
@@ -712,8 +783,12 @@ TEST_CASE_METHOD(QueryFixture, "Test prepared record statement", "[query][record
|
||||
TEST_CASE_METHOD(QueryFixture, "Test scalar result", "[query][record][scalar][result]") {
|
||||
check_table_not_exists("person");
|
||||
auto res = query::create()
|
||||
.table("person", {
|
||||
make_pk_column<uint32_t>("id"),
|
||||
.table("person")
|
||||
.columns({
|
||||
attribute("id", basic_type::type_uint32)
|
||||
})
|
||||
.constraints({
|
||||
constraint("PK_person").primary_key("id")
|
||||
})
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
|
||||
Reference in New Issue
Block a user