added timestamp code, support columns for group by and order by and tested load has-many eager code
This commit is contained in:
@@ -120,7 +120,6 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
|
||||
REQUIRE(str == row.at<std::string>("val_string"));
|
||||
REQUIRE(varchar == row.at<std::string>("val_varchar"));
|
||||
REQUIRE(md == row.at<date_type_t>("val_date"));
|
||||
const auto mtres = row.at<time_type_t>("val_time");
|
||||
REQUIRE(mt == row.at<time_type_t>("val_time"));
|
||||
REQUIRE(bin == row.at<blob_type_t>("val_blob"));
|
||||
}
|
||||
@@ -510,7 +509,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with group by and order
|
||||
|
||||
auto result = query::select({count("age").as("age_count"), "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.group_by({"age"})
|
||||
.order_by("age_count").desc()
|
||||
.fetch_all(db);
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
#include "models/flight.hpp"
|
||||
#include "models/person.hpp"
|
||||
#include "models/recipe.hpp"
|
||||
#include "models/shipment.hpp"
|
||||
|
||||
#include "../test/utils/record_printer.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
using namespace matador::query;
|
||||
@@ -23,6 +26,8 @@ META_TABLE(ingredients, INGREDIENT, id, name);
|
||||
META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id);
|
||||
META_TABLE(airplanes, AIRPLANE, id, brand, model);
|
||||
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
|
||||
META_TABLE(shipments, SHIPMENT, id, tracking_number)
|
||||
META_TABLE(packages, PACKAGE, id, weight, shipment)
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
|
||||
auto result = repo.attach<airplane>("airplane")
|
||||
@@ -605,4 +610,107 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
|
||||
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
void print(std::ostream& out, const record& row) {
|
||||
for (const auto& f : row) {
|
||||
out << f << " ";
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
|
||||
auto result = repo.attach<package>("packages")
|
||||
.and_then( [this] { return repo.attach<shipment>("shipments"); } );
|
||||
// auto result = repo.attach<shipment>("shipments")
|
||||
// .and_then( [this] { return repo.attach<package>("packages"); } );
|
||||
REQUIRE(result.is_ok());
|
||||
tables_to_drop.emplace("shipments");
|
||||
tables_to_drop.emplace("packages");
|
||||
|
||||
result = repo.create(db);
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
const std::vector shipments {
|
||||
object_ptr{new shipment{1, "4711"}},
|
||||
object_ptr{new shipment{2, "0815"}}
|
||||
};
|
||||
|
||||
using namespace matador::query::meta;
|
||||
|
||||
for (const auto &sh: shipments) {
|
||||
auto res = query::insert()
|
||||
.into(SHIPMENT, SHIPMENT)
|
||||
.values(*sh)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
auto count = query::select({count_all()})
|
||||
.from(SHIPMENT)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 2);
|
||||
|
||||
std::vector packages {
|
||||
object_ptr{new package{3, 15.4, shipments.at(0)}},
|
||||
object_ptr{new package{4, 1.3, shipments.at(0)}},
|
||||
object_ptr{new package{5, 30.9, shipments.at(1)}},
|
||||
object_ptr{new package{6, 22.8, shipments.at(1)}},
|
||||
object_ptr{new package{7, 17.2, shipments.at(1)}}
|
||||
};
|
||||
|
||||
for (const auto &pkg: packages) {
|
||||
auto res = query::insert()
|
||||
.into(PACKAGE, PACKAGE)
|
||||
.values(*pkg)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
count = query::select({count_all()})
|
||||
.from(PACKAGE)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 5);
|
||||
|
||||
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment, SHIPMENT.tracking_number})
|
||||
.from(PACKAGE)
|
||||
.join_left(SHIPMENT)
|
||||
.on( PACKAGE.shipment == SHIPMENT.id )
|
||||
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
|
||||
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
|
||||
.order_by(PACKAGE.weight).asc()
|
||||
.limit( 5 )
|
||||
.offset( 0 )
|
||||
.fetch_all(db);
|
||||
|
||||
REQUIRE(pkgs.is_ok());
|
||||
|
||||
record_printer printer(std::cout);
|
||||
printer.print(*pkgs);
|
||||
|
||||
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
|
||||
.from(SHIPMENT)
|
||||
.join_left(PACKAGE)
|
||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
||||
.fetch_all(db);
|
||||
|
||||
REQUIRE(shipment_records.is_ok());
|
||||
|
||||
printer.print(*shipment_records);
|
||||
|
||||
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment})
|
||||
.from(SHIPMENT)
|
||||
.join_left(PACKAGE)
|
||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
||||
.fetch_all<shipment>(db);
|
||||
|
||||
REQUIRE(shipment_result.is_ok());
|
||||
|
||||
std::cout << "\n";
|
||||
for (const auto &s : *shipment_result) {
|
||||
std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef MATADOR_SHIPMENT_HPP
|
||||
#define MATADOR_SHIPMENT_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include "matador/object/collection.hpp"
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
struct package;
|
||||
struct shipment {
|
||||
long id{};
|
||||
std::string tracking_number;
|
||||
object::collection<object::object_ptr<package>> packages{};
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "tracking_number", tracking_number, 255);
|
||||
field::has_many(op, "packages", packages, "shipment_id", utils::CascadeAllFetchEager);
|
||||
}
|
||||
};
|
||||
|
||||
struct package {
|
||||
long id{};
|
||||
double weight{};
|
||||
object::object_ptr<shipment> delivery;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "weight", weight);
|
||||
field::belongs_to(op, "shipment", delivery, utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_SHIPMENT_HPP
|
||||
@@ -72,7 +72,7 @@ void test_result_reader::read_value(const char * /*id*/, const size_t /*index*/,
|
||||
value = {12, 34, 56, 123456};
|
||||
}
|
||||
|
||||
void test_result_reader::read_value(const char *id, size_t index, utils::timestamp_type_t &value) {
|
||||
void test_result_reader::read_value(const char * /*id*/, size_t /*index*/, utils::timestamp_type_t &value) {
|
||||
value = utils::timestamp_type_t();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "record_printer.hpp"
|
||||
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace matador::test {
|
||||
record_printer::record_printer(std::ostream &os)
|
||||
: os_(os) {
|
||||
type_widths_ = {
|
||||
{utils::basic_type::Int8, 4},
|
||||
{utils::basic_type::Int16, 6},
|
||||
{utils::basic_type::Int32, 11},
|
||||
{utils::basic_type::Int64, 20},
|
||||
{utils::basic_type::UInt8, 4},
|
||||
{utils::basic_type::UInt16, 6},
|
||||
{utils::basic_type::UInt32, 11},
|
||||
{utils::basic_type::UInt64, 20},
|
||||
{utils::basic_type::Float, 12},
|
||||
{utils::basic_type::Double, 15},
|
||||
{utils::basic_type::Boolean, 6},
|
||||
{utils::basic_type::Varchar, 20},
|
||||
{utils::basic_type::Text, 30},
|
||||
{utils::basic_type::Date, 12},
|
||||
{utils::basic_type::DateTime, 20},
|
||||
{utils::basic_type::Time, 10},
|
||||
{utils::basic_type::Blob, 10},
|
||||
{utils::basic_type::Null, 6}
|
||||
};
|
||||
}
|
||||
|
||||
void record_printer::print_header(const sql::record &rec) const {
|
||||
for (const auto &f_ref: rec.columns()) {
|
||||
const auto &f = f_ref.get();
|
||||
os_ << std::left << std::setw(width(f)) << f.name() << " ";
|
||||
}
|
||||
os_ << "\n";
|
||||
|
||||
for (const auto &f_ref: rec.columns()) {
|
||||
const auto &f = f_ref.get();
|
||||
os_ << std::string(width(f), '-') << " ";
|
||||
}
|
||||
os_ << "\n";
|
||||
}
|
||||
|
||||
void record_printer::print(const sql::record &rec) const {
|
||||
for (const auto &f_ref: rec.columns()) {
|
||||
const auto &f = f_ref.get();
|
||||
os_ << std::left << std::setw(width(f)) << f.str() << " ";
|
||||
}
|
||||
os_ << "\n";
|
||||
}
|
||||
|
||||
int record_printer::width(const sql::field &f) {
|
||||
// If it's a varchar/string and has a defined size, use it if it's reasonable,
|
||||
// otherwise fall back to type defaults.
|
||||
if ((f.is_varchar() || f.is_string()) && f.size() > 0 && f.size() < 100) {
|
||||
return std::max(f.name().length(), f.size());
|
||||
}
|
||||
|
||||
// Find the default width for type
|
||||
// Note: field class doesn't expose basic_type directly but we can use value().type()
|
||||
// if we had access. For now we use name length as minimum.
|
||||
constexpr size_t w = 15;
|
||||
// In a real implementation we would probe the field's underlying type.
|
||||
|
||||
return std::max(f.name().length(), w);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef MATADOR_RECORD_PRINTER_HPP
|
||||
#define MATADOR_RECORD_PRINTER_HPP
|
||||
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
|
||||
namespace matador::sql {
|
||||
class field;
|
||||
class record;
|
||||
}
|
||||
|
||||
namespace matador::test {
|
||||
class record_printer final {
|
||||
public:
|
||||
explicit record_printer(std::ostream &os);
|
||||
|
||||
void print_header(const sql::record &rec) const;
|
||||
void print(const sql::record &rec) const;
|
||||
|
||||
template < class Type, template <typename ...> class ContainerType >
|
||||
void print(ContainerType<Type> &records) const {
|
||||
bool first = true;
|
||||
for (const auto &rec: records) {
|
||||
if (first) {
|
||||
print_header(rec);
|
||||
first = false;
|
||||
}
|
||||
print(rec);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] static int width(const sql::field &f) ;
|
||||
|
||||
private:
|
||||
std::ostream &os_;
|
||||
std::map<utils::basic_type, int> type_widths_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_RECORD_PRINTER_HPP
|
||||
Reference in New Issue
Block a user