From c2a96f4cbd775bc7401eed321bb0132f992e5900 Mon Sep 17 00:00:00 2001 From: Sascha Kuehl Date: Fri, 24 Nov 2023 17:40:57 +0100 Subject: [PATCH] renamed foreign class to entity, introduced interface class query_result_reader --- Todo.md | 2 +- backends/sqlite/CMakeLists.txt | 4 +- backends/sqlite/include/sqlite_connection.hpp | 11 ++ ...ry_result.hpp => sqlite_result_reader.hpp} | 29 ++--- backends/sqlite/src/sqlite_connection.cpp | 50 +++++---- ...ry_result.cpp => sqlite_result_reader.cpp} | 92 ++++++--------- include/matador/sql/entity.hpp | 47 ++++++++ include/matador/sql/foreign.hpp | 42 ------- include/matador/sql/query_result_impl.hpp | 105 ++++++++++++------ include/matador/sql/query_result_reader.hpp | 40 +++++++ src/CMakeLists.txt | 5 +- src/sql/query_result_impl.cpp | 28 +++-- src/utils/logger.cpp | 2 +- test/SessionTest.cpp | 15 ++- test/ValueGeneratorTest.cpp | 4 +- test/models/airplane.hpp | 4 +- test/models/flight.hpp | 6 +- test/models/product.hpp | 6 +- 18 files changed, 285 insertions(+), 207 deletions(-) rename backends/sqlite/include/{sqlite_query_result.hpp => sqlite_result_reader.hpp} (78%) rename backends/sqlite/src/{sqlite_query_result.cpp => sqlite_result_reader.cpp} (66%) create mode 100644 include/matador/sql/entity.hpp delete mode 100644 include/matador/sql/foreign.hpp create mode 100644 include/matador/sql/query_result_reader.hpp diff --git a/Todo.md b/Todo.md index bdbab93..bcd1054 100644 --- a/Todo.md +++ b/Todo.md @@ -1,7 +1,7 @@ # Todo - Add is_valid() method to connection & connection_impl -- Read in foreign fields +- Read in entity fields - Add special handling for update in backends - Add PostgreSQL backend - Add MySQL/MariaDB backend diff --git a/backends/sqlite/CMakeLists.txt b/backends/sqlite/CMakeLists.txt index c4b6717..9d85c11 100644 --- a/backends/sqlite/CMakeLists.txt +++ b/backends/sqlite/CMakeLists.txt @@ -2,14 +2,14 @@ set(HEADER include/sqlite_connection.hpp include/sqlite_error.hpp include/sqlite_dialect.hpp - include/sqlite_query_result.hpp + include/sqlite_result_reader.hpp ) set(SOURCES src/sqlite_connection.cpp src/sqlite_error.cpp src/sqlite_dialect.cpp - src/sqlite_query_result.cpp + src/sqlite_result_reader.cpp ) add_library(matador-sqlite SHARED ${SOURCES} ${HEADER}) diff --git a/backends/sqlite/include/sqlite_connection.hpp b/backends/sqlite/include/sqlite_connection.hpp index 5ead81d..d6ef698 100644 --- a/backends/sqlite/include/sqlite_connection.hpp +++ b/backends/sqlite/include/sqlite_connection.hpp @@ -14,6 +14,8 @@ #include "matador/sql/connection_impl.hpp" +#include "sqlite_result_reader.hpp" + #include namespace matador::backends::sqlite { @@ -35,9 +37,18 @@ public: bool exists(const std::string &table_name) override; +private: + struct fetch_context + { + sql::record prototype; + sqlite_result_reader::rows rows; + }; + private: static int parse_result(void* param, int column_count, char** values, char** columns); + fetch_context fetch_internal(const std::string &stmt); + private: sqlite3 *sqlite_db_{}; }; diff --git a/backends/sqlite/include/sqlite_query_result.hpp b/backends/sqlite/include/sqlite_result_reader.hpp similarity index 78% rename from backends/sqlite/include/sqlite_query_result.hpp rename to backends/sqlite/include/sqlite_result_reader.hpp index b609727..3ff1e5b 100644 --- a/backends/sqlite/include/sqlite_query_result.hpp +++ b/backends/sqlite/include/sqlite_result_reader.hpp @@ -1,24 +1,27 @@ -#ifndef QUERY_SQLITE_QUERY_RESULT_HPP -#define QUERY_SQLITE_QUERY_RESULT_HPP +#ifndef QUERY_SQLITE_RESULT_READER_HPP +#define QUERY_SQLITE_RESULT_READER_HPP -#include "matador/sql/query_result_impl.hpp" -#include "matador/sql/record.hpp" +#include "matador/sql/query_result_reader.hpp" #include namespace matador::backends::sqlite { -class sqlite_query_result : public sql::query_result_impl { +class sqlite_result_reader : public sql::query_result_reader +{ public: using columns = std::vector; using rows = std::vector; public: - sqlite_query_result(sql::record prototype, rows result); - ~sqlite_query_result() override; + sqlite_result_reader(rows result, size_t column_count); + ~sqlite_result_reader() override; size_t column_count() const override; + [[nodiscard]] const char* column(size_t index) const override; + [[nodiscard]] bool fetch() override; + void read_value(const char *id, size_t index, char &value) override; void read_value(const char *id, size_t index, short &value) override; void read_value(const char *id, size_t index, int &value) override; @@ -37,20 +40,12 @@ public: void read_value(const char *id, size_t index, std::string &value, size_t s) override; void read_value(const char *id, size_t index, sql::any_type &value, sql::data_type_t type, size_t size) override; - [[nodiscard]] const char* column(size_t index) const override; - [[nodiscard]] bool fetch() override; - -private: - friend class sqlite_connection; - -private: - void push_back(char **row_values, int column_count); - private: rows result_; long long row_index_ = -1; + size_t column_count_{}; }; } -#endif //QUERY_SQLITE_QUERY_RESULT_HPP +#endif //QUERY_SQLITE_RESULT_READER_HPP diff --git a/backends/sqlite/src/sqlite_connection.cpp b/backends/sqlite/src/sqlite_connection.cpp index 3585afd..f00219c 100644 --- a/backends/sqlite/src/sqlite_connection.cpp +++ b/backends/sqlite/src/sqlite_connection.cpp @@ -1,6 +1,6 @@ #include "sqlite_connection.hpp" #include "sqlite_error.hpp" -#include "sqlite_query_result.hpp" +#include "sqlite_result_reader.hpp" #include "matador/sql/record.hpp" @@ -37,17 +37,11 @@ bool sqlite_connection::is_open() return sqlite_db_ != nullptr; } -struct fetch_context -{ - sql::record prototype; - sqlite_query_result::rows rows; -}; - int sqlite_connection::parse_result(void* param, int column_count, char** values, char** columns) { auto *context = static_cast(param); - sqlite_query_result::columns column; + sqlite_result_reader::columns column; for(int i = 0; i < column_count; ++i) { // copy and store column data; if (values[i] == nullptr) { @@ -73,6 +67,17 @@ int sqlite_connection::parse_result(void* param, int column_count, char** values return 0; } +sqlite_connection::fetch_context sqlite_connection::fetch_internal(const std::string &stmt) +{ + fetch_context context; + char *errmsg = nullptr; + const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, &context, &errmsg); + + throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt); + + return context; +} + size_t sqlite_connection::execute(const std::string &stmt) { char *errmsg = nullptr; @@ -85,13 +90,9 @@ size_t sqlite_connection::execute(const std::string &stmt) std::unique_ptr sqlite_connection::fetch(const std::string &stmt) { - fetch_context context; - char *errmsg = nullptr; - const int ret = sqlite3_exec(sqlite_db_, stmt.c_str(), parse_result, &context, &errmsg); + auto context = fetch_internal(stmt); - throw_sqlite_error(ret, sqlite_db_, "sqlite", stmt); - - return std::move(std::make_unique(std::move(context.prototype), std::move(context.rows))); + return std::move(std::make_unique(std::make_unique(std::move(context.rows), context.prototype.size()), std::move(context.prototype))); } void sqlite_connection::prepare(const std::string &stmt) @@ -135,21 +136,21 @@ sql::data_type_t string2type(const char *type) sql::record sqlite_connection::describe(const std::string& table) { - std::string stmt("PRAGMA table_info(" + table + ")"); - const auto result = fetch("PRAGMA table_info(" + table + ")"); + const auto result = fetch_internal("PRAGMA table_info(" + table + ")"); + sqlite_result_reader reader(std::move(result.rows), result.prototype.size()); sql::record prototype; - while (result->fetch()) { + while (reader.fetch()) { char *end = nullptr; // Todo: add index to column - auto index = strtoul(result->column(0), &end, 10); - std::string name = result->column(1); + auto index = strtoul(reader.column(0), &end, 10); + std::string name = reader.column(1); // Todo: extract size - auto type = (string2type(result->column(2))); + auto type = (string2type(reader.column(2))); end = nullptr; utils::constraints options{}; - if (strtoul(result->column(3), &end, 10) == 0) { + if (strtoul(reader.column(3), &end, 10) == 0) { options = utils::constraints::NOT_NULL; } // f.default_value(res->column(4)); @@ -161,15 +162,16 @@ sql::record sqlite_connection::describe(const std::string& table) bool sqlite_connection::exists(const std::string &table_name) { - const auto result = fetch("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='" + table_name + "' LIMIT 1"); + const auto result = fetch_internal("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='" + table_name + "' LIMIT 1"); + sqlite_result_reader reader(std::move(result.rows), result.prototype.size()); - if (!result->fetch()) { + if (!reader.fetch()) { // Todo: throw an exception? return false; } int v{}; - result->read_value(nullptr, 0, v); + reader.read_value(nullptr, 0, v); return v == 1; } diff --git a/backends/sqlite/src/sqlite_query_result.cpp b/backends/sqlite/src/sqlite_result_reader.cpp similarity index 66% rename from backends/sqlite/src/sqlite_query_result.cpp rename to backends/sqlite/src/sqlite_result_reader.cpp index 8ff7e68..a202213 100644 --- a/backends/sqlite/src/sqlite_query_result.cpp +++ b/backends/sqlite/src/sqlite_result_reader.cpp @@ -1,11 +1,9 @@ -#include "sqlite_query_result.hpp" +#include "sqlite_result_reader.hpp" #include #include #include -#include - namespace matador::backends::sqlite { template < class Type > @@ -50,12 +48,11 @@ void read(Type &x, const char *val, typename std::enable_if void convert(const char *valstr, sql::any_type &value) { @@ -203,7 +179,7 @@ void convert(const char *valstr, sql::any_type &value) value = val; } -void sqlite_query_result::read_value(const char *id, size_t index, sql::any_type &value, sql::data_type_t type, size_t size) +void sqlite_result_reader::read_value(const char *id, size_t index, sql::any_type &value, sql::data_type_t type, size_t size) { switch (type) { case sql::data_type_t::type_char: diff --git a/include/matador/sql/entity.hpp b/include/matador/sql/entity.hpp new file mode 100644 index 0000000..dc199cb --- /dev/null +++ b/include/matador/sql/entity.hpp @@ -0,0 +1,47 @@ +#ifndef QUERY_ENTITY_HPP +#define QUERY_ENTITY_HPP + +#include + +namespace matador::sql { + +template < class Type > +class entity +{ +public: + using value_type = Type; + using pointer = value_type*; + using reference = value_type&; + + entity() = default; + explicit entity(Type *obj) + : obj_(obj) {} + entity(const entity&) = default; + entity& operator=(const entity&) = default; + entity(entity&&) noexcept = default; + entity& operator=(entity&&) noexcept = default; + ~entity() = default; + + void reset(Type *obj) { obj_.reset(obj); } + + pointer operator->() { return obj_.get(); } + const value_type* operator->() const { return obj_.get(); } + + reference operator*() { return *obj_; } + const value_type& operator*() const { return *obj_; } + + pointer get() { return obj_.get(); } + const value_type* get() const { return obj_.get(); } + +private: + std::shared_ptr obj_; +}; + +template +[[maybe_unused]] entity make_entity(Args&&... args) +{ + return entity(new Type(std::forward(args)...)); +} + +} +#endif //QUERY_ENTITY_HPP diff --git a/include/matador/sql/foreign.hpp b/include/matador/sql/foreign.hpp deleted file mode 100644 index ba96602..0000000 --- a/include/matador/sql/foreign.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef QUERY_FOREIGN_HPP -#define QUERY_FOREIGN_HPP - -#include - -namespace matador::sql { - -template < class Type > -class foreign -{ -public: - using value_type = Type; - using pointer = value_type*; - using reference = value_type&; - - foreign() = default; - explicit foreign(Type *obj) - : obj_(obj) {} - foreign(const foreign&) = default; - foreign& operator=(const foreign&) = default; - foreign(foreign&&) noexcept = default; - foreign& operator=(foreign&&) noexcept = default; - ~foreign() = default; - - pointer operator->() { return obj_.get(); } - const value_type* operator->() const { return obj_.get(); } - - reference operator*() { return *obj_; } - const value_type& operator*() const { return *obj_; } - -private: - std::shared_ptr obj_; -}; - -template -[[maybe_unused]] foreign make_foreign(Args&&... args) -{ - return foreign(new Type(std::forward(args)...)); -} - -} -#endif //QUERY_FOREIGN_HPP diff --git a/include/matador/sql/query_result_impl.hpp b/include/matador/sql/query_result_impl.hpp index edc71af..cbbe6fe 100644 --- a/include/matador/sql/query_result_impl.hpp +++ b/include/matador/sql/query_result_impl.hpp @@ -5,44 +5,61 @@ #include "matador/utils/field_attributes.hpp" #include "matador/sql/any_type.hpp" +#include "matador/sql/query_result_reader.hpp" #include "matador/sql/record.hpp" #include "matador/sql/types.hpp" +#include #include namespace matador::sql { +namespace detail { +class pk_reader +{ +public: + explicit pk_reader(query_result_reader &reader); + + template + void read(Type &obj, size_t column_index) + { + column_index_ = column_index; + utils::access::process(*this, obj); + } + + template + void on_primary_key(const char *id, ValueType &value, typename std::enable_if::value && !std::is_same::value>::type* = 0); + void on_primary_key(const char *id, std::string &value, size_t size); + void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {} + + template < class Type > + void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {} + template < class Pointer > + void on_belongs_to(const char *id, Pointer &x, utils::cascade_type) {} + template < class Pointer > + void on_has_one(const char *id, Pointer &x, utils::cascade_type) {} + + template + void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {} + template + void on_has_many(const char *, ContainerType &, utils::cascade_type) {} + +private: + size_t column_index_{}; + query_result_reader &reader_; +}; + +} + class query_result_impl { public: - virtual ~query_result_impl() = default; - - virtual size_t column_count() const = 0; - - virtual void read_value(const char *id, size_t index, char &value) = 0; - virtual void read_value(const char *id, size_t index, short &value) = 0; - virtual void read_value(const char *id, size_t index, int &value) = 0; - virtual void read_value(const char *id, size_t index, long &value) = 0; - virtual void read_value(const char *id, size_t index, long long &value) = 0; - virtual void read_value(const char *id, size_t index, unsigned char &value) = 0; - virtual void read_value(const char *id, size_t index, unsigned short &value) = 0; - virtual void read_value(const char *id, size_t index, unsigned int &value) = 0; - virtual void read_value(const char *id, size_t index, unsigned long &value) = 0; - virtual void read_value(const char *id, size_t index, unsigned long long &value) = 0; - virtual void read_value(const char *id, size_t index, bool &value) = 0; - virtual void read_value(const char *id, size_t index, float &value) = 0; - virtual void read_value(const char *id, size_t index, double &value) = 0; -// virtual void read_value(const char *id, size_t index, matador::time &value) = 0; -// virtual void read_value(const char *id, size_t index, matador::date &value) = 0; - virtual void read_value(const char *id, size_t index, char *value, size_t s) = 0; - virtual void read_value(const char *id, size_t index, std::string &value) = 0; - virtual void read_value(const char *id, size_t index, std::string &value, size_t s) = 0; - virtual void read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size) = 0; + query_result_impl(std::unique_ptr &&reader, record prototype); template void on_primary_key(const char *id, ValueType &value, typename std::enable_if::value && !std::is_same::value>::type* = 0) { - read_value(id, column_index_++, value); + reader_->read_value(id, column_index_++, value); } void on_primary_key(const char *id, std::string &value, size_t size); void on_revision(const char *id, unsigned long long &rev); @@ -50,16 +67,28 @@ public: template < class Type > void on_attribute(const char *id, Type &x, const utils::field_attributes &/*attr*/ = utils::null_attributes) { - read_value(id, column_index_++, x); + reader_->read_value(id, column_index_++, x); } void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes); void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes); void on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr = utils::null_attributes); template < class Pointer > - void on_belongs_to(const char *id, Pointer &x, utils::cascade_type) {} + void on_belongs_to(const char * /*id*/, Pointer &x, utils::cascade_type) + { + if (!x.get()) { + x.reset(new typename Pointer::value_type); + } + pk_reader_.read(*x, column_index_++); + } template < class Pointer > - void on_has_one(const char *id, Pointer &x, utils::cascade_type) {} + void on_has_one(const char * /*id*/, Pointer &x, utils::cascade_type) + { + if (!x.get()) { + x.reset(new typename Pointer::value_type); + } + pk_reader_.read(*x, column_index_++); + } template void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {} @@ -72,26 +101,32 @@ public: template bool fetch(Type &obj) { - if (!fetch()) { + column_index_ = 0; + if (!reader_->fetch()) { return false; } matador::utils::access::process(*this, obj); return true; } - [[nodiscard]] virtual const char* column(size_t index) const = 0; - [[nodiscard]] virtual bool fetch() = 0; - - const record& prototype() const; - -protected: - explicit query_result_impl(record prototype); + [[nodiscard]] const record& prototype() const; protected: size_t column_index_ = 0; record prototype_; + std::unique_ptr reader_; + detail::pk_reader pk_reader_; }; +namespace detail { + +template +void detail::pk_reader::on_primary_key(const char *id, ValueType &value, typename std::enable_if::value && !std::is_same::value>::type *) +{ + reader_.read_value(id, column_index_++, value); +} + +} } #endif //QUERY_QUERY_RESULT_IMPL_HPP diff --git a/include/matador/sql/query_result_reader.hpp b/include/matador/sql/query_result_reader.hpp new file mode 100644 index 0000000..983d3a5 --- /dev/null +++ b/include/matador/sql/query_result_reader.hpp @@ -0,0 +1,40 @@ +#ifndef QUERY_QUERY_RESULT_READER_HPP +#define QUERY_QUERY_RESULT_READER_HPP + +#include "matador/sql/any_type.hpp" +#include "matador/sql/types.hpp" + +namespace matador::sql { + +class query_result_reader +{ +public: + virtual ~query_result_reader() = default; + + [[nodiscard]] virtual size_t column_count() const = 0; + [[nodiscard]] virtual const char* column(size_t index) const = 0; + [[nodiscard]] virtual bool fetch() = 0; + + virtual void read_value(const char *id, size_t index, char &value) = 0; + virtual void read_value(const char *id, size_t index, short &value) = 0; + virtual void read_value(const char *id, size_t index, int &value) = 0; + virtual void read_value(const char *id, size_t index, long &value) = 0; + virtual void read_value(const char *id, size_t index, long long &value) = 0; + virtual void read_value(const char *id, size_t index, unsigned char &value) = 0; + virtual void read_value(const char *id, size_t index, unsigned short &value) = 0; + virtual void read_value(const char *id, size_t index, unsigned int &value) = 0; + virtual void read_value(const char *id, size_t index, unsigned long &value) = 0; + virtual void read_value(const char *id, size_t index, unsigned long long &value) = 0; + virtual void read_value(const char *id, size_t index, bool &value) = 0; + virtual void read_value(const char *id, size_t index, float &value) = 0; + virtual void read_value(const char *id, size_t index, double &value) = 0; +// virtual void read_value(const char *id, size_t index, matador::time &value) = 0; +// virtual void read_value(const char *id, size_t index, matador::date &value) = 0; + virtual void read_value(const char *id, size_t index, char *value, size_t s) = 0; + virtual void read_value(const char *id, size_t index, std::string &value) = 0; + virtual void read_value(const char *id, size_t index, std::string &value, size_t s) = 0; + virtual void read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size) = 0; +}; + +} +#endif //QUERY_QUERY_RESULT_READER_HPP diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aca6ad2..a30e22b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,10 +44,11 @@ set(SQL_HEADER ../include/matador/sql/value_extractor.hpp ../include/matador/sql/any_type.hpp ../include/matador/sql/key_value_generator.hpp - ../include/matador/sql/foreign.hpp + ../include/matador/sql/entity.hpp ../include/matador/sql/fk_value_extractor.hpp "../include/matador/sql/table_repository.hpp" - ../include/matador/sql/any_type_to_visitor.hpp) + ../include/matador/sql/any_type_to_visitor.hpp + ../include/matador/sql/query_result_reader.hpp) set(UTILS_HEADER ../include/matador/utils/field_attributes.hpp diff --git a/src/sql/query_result_impl.cpp b/src/sql/query_result_impl.cpp index e67ba1b..aa6635c 100644 --- a/src/sql/query_result_impl.cpp +++ b/src/sql/query_result_impl.cpp @@ -1,31 +1,45 @@ #include "matador/sql/query_result_impl.hpp" +#include "matador/sql/query_result_reader.hpp" namespace matador::sql { +detail::pk_reader::pk_reader(query_result_reader &reader) +: reader_(reader) {} + +void detail::pk_reader::on_primary_key(const char *id, std::string &value, size_t size) { + reader_.read_value(id, column_index_, value, size); +} + +query_result_impl::query_result_impl(std::unique_ptr &&reader, record prototype) +: prototype_(std::move(prototype)) +, reader_(std::move(reader)) +, pk_reader_(*reader_) +{} + void query_result_impl::on_primary_key(const char *id, std::string &value, size_t size) { - read_value(id, column_index_++, value, size); + reader_->read_value(id, column_index_++, value, size); } void query_result_impl::on_revision(const char *id, unsigned long long int &rev) { - read_value(id, column_index_++, rev); + reader_->read_value(id, column_index_++, rev); } void query_result_impl::on_attribute(const char *id, char *value, const utils::field_attributes &attr) { - read_value(id, column_index_++, value, attr.size()); + reader_->read_value(id, column_index_++, value, attr.size()); } void query_result_impl::on_attribute(const char *id, std::string &value, const utils::field_attributes &attr) { - read_value(id, column_index_++, value, attr.size()); + reader_->read_value(id, column_index_++, value, attr.size()); } void query_result_impl::on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr) { - read_value(id, column_index_++, value, type, attr.size()); + reader_->read_value(id, column_index_++, value, type, attr.size()); } const record& query_result_impl::prototype() const @@ -33,8 +47,4 @@ const record& query_result_impl::prototype() const return prototype_; } -query_result_impl::query_result_impl(record prototype) -: prototype_(std::move(prototype)) -{} - } \ No newline at end of file diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp index 8830b9a..8d36fa7 100644 --- a/src/utils/logger.cpp +++ b/src/utils/logger.cpp @@ -107,7 +107,7 @@ void logger::log(log_level lvl, const char *message) const std::snprintf(timestamp_buffer + std::strftime(timestamp_buffer, sizeof timestamp_buffer - 3, "%F %T.", - std::localtime(&coarse)), 4, "%03ld", fine.time_since_epoch().count() % 1000); + std::localtime(&coarse)), 4, "%03lld", fine.time_since_epoch().count() % 1000); char buffer[1024]; #ifdef _MSC_VER diff --git a/test/SessionTest.cpp b/test/SessionTest.cpp index 85b7f3d..a9c5192 100644 --- a/test/SessionTest.cpp +++ b/test/SessionTest.cpp @@ -122,10 +122,10 @@ TEST_CASE("Select statement with foreign key", "[session]") { REQUIRE(res.first == 0); REQUIRE(res.second == R"(CREATE TABLE "flight" ("id" BIGINT, "airplane_id" BIGINT, "pilot_name" VARCHAR(255), CONSTRAINT PK_flight PRIMARY KEY (id), CONSTRAINT FK_flight_airplane_id FOREIGN KEY (airplane_id) REFERENCES airplane(id)))"); - std::vector> planes { - make_foreign(1, "Airbus", "A380"), - make_foreign(2, "Boeing", "707"), - make_foreign(3, "Boeing", "747") + std::vector> planes { + make_entity(1, "Airbus", "A380"), + make_entity(2, "Boeing", "707"), + make_entity(3, "Boeing", "747") }; for (const auto &plane : planes) { @@ -136,13 +136,16 @@ TEST_CASE("Select statement with foreign key", "[session]") { auto count = s.select({count_all()}).from("airplane").fetch_value(); REQUIRE(count == 3); - flight f4711{4, *planes.begin(), "hans"}; + flight f4711{4, planes.at(1), "hans"}; res = s.insert().into("flight").values(f4711).execute(); REQUIRE(res.first == 1); auto f = *s.select().from("flight").fetch_all().begin(); - + REQUIRE(f.id == 4); + REQUIRE(f.pilot_name == "hans"); + REQUIRE(f.airplane.get() != nullptr); + REQUIRE(f.airplane->id == 2); s.drop().table("flight").execute(); s.drop().table("airplane").execute(); diff --git a/test/ValueGeneratorTest.cpp b/test/ValueGeneratorTest.cpp index 829f5ef..ffa4ee8 100644 --- a/test/ValueGeneratorTest.cpp +++ b/test/ValueGeneratorTest.cpp @@ -14,9 +14,9 @@ TEST_CASE("Extract values object", "[value extractor]") { p.units_in_stock = 100; p.unit_price = 49; p.quantity_per_unit = "pcs"; - p.category = make_foreign(); + p.category = make_entity(); p.category->id = 7; - p.supplier = make_foreign();; + p.supplier = make_entity();; p.supplier->id = 13; p.product_name = "candle"; diff --git a/test/models/airplane.hpp b/test/models/airplane.hpp index 642bc9d..10ad424 100644 --- a/test/models/airplane.hpp +++ b/test/models/airplane.hpp @@ -8,7 +8,7 @@ #include "matador/utils/cascade_type.hpp" #include "matador/utils/field_attributes.hpp" -#include "matador/sql/foreign.hpp" +#include "matador/sql/entity.hpp" #include @@ -21,7 +21,7 @@ struct airplane : id(id) , brand(std::move(b)) , model(std::move(m)) {} - unsigned long id; + unsigned long id{}; std::string brand; std::string model; diff --git a/test/models/flight.hpp b/test/models/flight.hpp index 3283c1f..4187691 100644 --- a/test/models/flight.hpp +++ b/test/models/flight.hpp @@ -7,15 +7,15 @@ #include "matador/utils/cascade_type.hpp" #include "matador/utils/field_attributes.hpp" -#include "matador/sql/foreign.hpp" +#include "matador/sql/entity.hpp" #include namespace matador::test { struct flight { - unsigned long id; - sql::foreign airplane; + unsigned long id{}; + sql::entity airplane; std::string pilot_name; template diff --git a/test/models/product.hpp b/test/models/product.hpp index e49b901..b0b0740 100644 --- a/test/models/product.hpp +++ b/test/models/product.hpp @@ -8,7 +8,7 @@ #include "matador/utils/cascade_type.hpp" #include "matador/utils/field_attributes.hpp" -#include "matador/sql/foreign.hpp" +#include "matador/sql/entity.hpp" #include @@ -17,8 +17,8 @@ namespace matador::test { struct product { std::string product_name; - sql::foreign supplier; - sql::foreign category; + sql::entity supplier; + sql::entity category; std::string quantity_per_unit; unsigned int unit_price; unsigned int units_in_stock;