diff --git a/backends/postgres/include/postgres_connection.hpp b/backends/postgres/include/postgres_connection.hpp index 32fac42..072edc8 100644 --- a/backends/postgres/include/postgres_connection.hpp +++ b/backends/postgres/include/postgres_connection.hpp @@ -33,7 +33,7 @@ public: utils::result execute(const sql::query_context &context) override; utils::result, utils::error> prepare(const sql::query_context &context) override; - utils::result, utils::error> fetch(const sql::query_context &context) override; + utils::result, utils::error> fetch(const sql::query_context &ctx) override; utils::result, utils::error> describe(const std::string& table) override; utils::result exists(const std::string &schema_name, const std::string &table_name) override; @@ -43,16 +43,16 @@ public: [[nodiscard]] std::string to_escaped_string( const utils::blob_type_t& value ) const override; private: - [[nodiscard]] static std::string generate_statement_name(const sql::query_context &query) ; + [[nodiscard]] static std::string generate_statement_name(const sql::query_context &ctx) ; - utils::result execute(const std::string &stmt) const; + [[nodiscard]] utils::result execute(const std::string &stmt) const; private: PGconn *conn_{nullptr}; - using string_to_int_map = std::unordered_map; + using hash_to_string_map = std::unordered_map; - static string_to_int_map statement_name_map_; + static hash_to_string_map statement_name_map_; }; } diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index 713ec01..084e335 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -7,14 +7,15 @@ #include "matador/sql/error_code.hpp" #include "matador/sql/record.hpp" - #include "matador/sql/internal/query_result_impl.hpp" +#include "matador/utils/string.hpp" + #include #include namespace matador::backends::postgres { -postgres_connection::string_to_int_map postgres_connection::statement_name_map_{}; +postgres_connection::hash_to_string_map postgres_connection::statement_name_map_{}; postgres_connection::postgres_connection(const sql::connection_info &info) : connection_impl(info) { @@ -82,20 +83,20 @@ utils::result postgres_connection::server_version( utils::basic_type oid2type(Oid oid); -utils::result, utils::error> postgres_connection::fetch(const sql::query_context &context) { - PGresult *res = PQexec(conn_, context.sql.c_str()); +utils::result, utils::error> postgres_connection::fetch(const sql::query_context &ctx) { + PGresult *res = PQexec(conn_, ctx.sql.c_str()); if (is_result_error(res)) { - const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", context.sql); + const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", ctx.sql); PQclear(res); return utils::failure(err); } - std::vector prototype = context.prototype; + std::vector prototype = ctx.prototype; const int num_col = PQnfields(res); if (prototype.size() != static_cast(num_col)) { - const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", context.sql); + const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", ctx.sql); PQclear(res); return utils::failure(err); } @@ -111,22 +112,18 @@ utils::result, utils::error> postgres_co return utils::ok(std::make_unique(std::make_unique(res), std::move(prototype), - context.resolver, - context.result_type)); + ctx.resolver, + ctx.result_type)); } -std::string postgres_connection::generate_statement_name(const sql::query_context &query) { - std::stringstream name; - name << query.table_name << "_" << query.command_name; - auto result = statement_name_map_.find(name.str()); +std::string postgres_connection::generate_statement_name(const sql::query_context &ctx) { + auto it = statement_name_map_.find(ctx.sql_hash); - if (result == statement_name_map_.end()) { - result = statement_name_map_.insert(std::make_pair(name.str(), 0)).first; + if (it == statement_name_map_.end()) { + it = statement_name_map_.insert(std::make_pair(ctx.sql_hash, utils::to_hex_string(ctx.sql_hash))).first; } - name << "_" << ++result->second; - - return name.str(); + return it->second; } utils::result postgres_connection::execute(const std::string& stmt) const { diff --git a/include/matador/query/schema.hpp b/include/matador/query/schema.hpp index 45721e8..385ef18 100644 --- a/include/matador/query/schema.hpp +++ b/include/matador/query/schema.hpp @@ -19,6 +19,28 @@ class executor; } namespace matador::query { +template +class query_object_resolver_producer : public sql::object_resolver_producer { +public: + query_object_resolver_producer() = default; + query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name) + : object_resolver_producer(typeid(Type)) + , repo_(repo) + , table_(tab) + , pk_name_(std::move(pk_name)) {} + + std::shared_ptr produce(sql::statement&& stmt) override { + return std::make_shared>(std::move(stmt)); + } + + utils::result build_query(const sql::dialect& d) override; + +private: + basic_schema& repo_; + const table& table_; + std::string pk_name_; +}; + template class query_collection_resolver_producer : public sql::collection_resolver_producer { public: @@ -77,9 +99,7 @@ public: return utils::ok(stmt); } - std::shared_ptr produce(sql::statement&& stmt, const sql::resolver_service& rs) override { - // const auto object_resolver = rs.object_resolver(); - + std::shared_ptr produce(sql::statement&& stmt, const sql::resolver_service& /*rs*/) override { return std::make_shared>(std::move(stmt), root_type(), collection_name()/*, object_resolver*/); } @@ -106,7 +126,18 @@ public: template static void on_belongs_to(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {} template - static void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {} + void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) { + const auto it = schema_.find(typeid(typename Pointer::value_type)); + if (it == schema_.end()) { + throw query_builder_exception{error_code::UnknownType, "Unknown type"}; + } + if (!it->second.node().info().has_primary_key()) { + throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"}; + } + + auto producer = std::make_unique>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name()); + schema_.resolver_producers_[typeid(typename Pointer::value_type)] = std::move(producer); + } template void on_has_many(const char * /*id*/, CollectionType &/*cont*/, const char *join_column, const utils::foreign_attributes &/*attr*/, std::enable_if_t::value> * = nullptr) { @@ -187,42 +218,6 @@ private: const std::type_index root_type_; }; - -template -class query_object_resolver_producer : public sql::object_resolver_producer { -public: - query_object_resolver_producer() = default; - query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name) - : object_resolver_producer(typeid(Type)) - , repo_(repo) - , table_(tab) - , pk_name_(std::move(pk_name)) {} - - std::shared_ptr produce(sql::statement&& stmt) override { - return std::make_shared>(std::move(stmt)); - } - - utils::result build_query(const sql::dialect& d) override { - producer_creator pc(repo_, typeid(Type)); - Type obj; - access::process(pc, obj); - - select_query_builder qb(repo_); - const auto *pk_column = table_[pk_name_]; - const auto result = qb.build(*pk_column == utils::_); - if (!result) { - return utils::failure(result.err()); - } - - return utils::ok(result->compile(d)); - } - -private: - basic_schema& repo_; - const table& table_; - std::string pk_name_; -}; - class schema; using schema_ref = std::reference_wrapper; @@ -311,6 +306,23 @@ utils::result schema::drop_table(const sql::connection &conn return utils::failure(info.err()); } +template +utils::result query_object_resolver_producer:: +build_query(const sql::dialect &d) { + producer_creator pc(repo_, typeid(Type)); + Type obj; + access::process(pc, obj); + + select_query_builder qb(repo_); + const auto *pk_column = table_[pk_name_]; + const auto result = qb.build(*pk_column == utils::_); + if (!result) { + return utils::failure(result.err()); + } + + return utils::ok(result->compile(d)); +} + template void schema_observer::on_attach(const object::repository_node &node, const Type &/*prototype*/) const { primary_key_generator_finder finder; diff --git a/include/matador/query/select_query_builder.hpp b/include/matador/query/select_query_builder.hpp index aee64a1..cc411c4 100644 --- a/include/matador/query/select_query_builder.hpp +++ b/include/matador/query/select_query_builder.hpp @@ -113,12 +113,12 @@ public: template void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) { - on_foreign_object(id, obj, attr); + on_foreign_object(id, obj, attr, true); } template void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) { - on_foreign_object(id, obj, attr); + on_foreign_object(id, obj, attr, false); } template @@ -258,7 +258,7 @@ public: private: template - void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr); + void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, bool add_column_on_lazy); void push(const std::string &column_name); static std::string build_alias(char prefix, unsigned int count); [[nodiscard]] bool is_root_entity() const; @@ -280,7 +280,7 @@ private: }; template -void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) { +void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, const bool add_column_on_lazy) { const auto it = schema_.find(typeid(typename Pointer::value_type)); if (it == schema_.end()) { throw query_builder_exception{error_code::UnknownType, "Unknown type"}; @@ -307,7 +307,7 @@ void select_query_builder::on_foreign_object(const char *id, Pointer &, const ut table_column{&table_info_stack_.top().table, id}, table_column{&next->second, info.primary_key_attribute()->name()} ); - } else { + } else if (add_column_on_lazy) { push(id); } } diff --git a/include/matador/sql/object_parameter_binder.hpp b/include/matador/sql/object_parameter_binder.hpp index dddf53c..9ef2707 100644 --- a/include/matador/sql/object_parameter_binder.hpp +++ b/include/matador/sql/object_parameter_binder.hpp @@ -38,9 +38,9 @@ public: pk_binder_.bind(*x, index_++, *binder_); } template class Pointer> - void on_has_one(const char * /*id*/, Pointer &x, const utils::foreign_attributes &/*attr*/) { - pk_binder_.bind(*x, index_++, *binder_); - } + static void on_has_one(const char * /*id*/, + Pointer &/*x*/, + const utils::foreign_attributes &/*attr*/) {} template static void on_has_many(const char * /*id*/, ContainerType &/*c*/, diff --git a/include/matador/sql/query_context.hpp b/include/matador/sql/query_context.hpp index 54e19da..771e30c 100644 --- a/include/matador/sql/query_context.hpp +++ b/include/matador/sql/query_context.hpp @@ -5,8 +5,6 @@ #include "matador/sql/resolver_service.hpp" -#include "matador/utils/types.hpp" - namespace matador::sql { enum class sql_command { Unknown, @@ -33,12 +31,10 @@ struct query_context { std::string sql; size_t sql_hash{}; sql_command command{}; - std::string command_name{}; std::string schema_name{}; std::string table_name{}; std::vector prototype{}; std::vector bind_vars{}; - // std::vector bind_types{}; // Data for resolving query result std::shared_ptr resolver{}; std::type_index result_type = typeid(void); diff --git a/include/matador/utils/string.hpp b/include/matador/utils/string.hpp index 95795f8..0c29883 100644 --- a/include/matador/utils/string.hpp +++ b/include/matador/utils/string.hpp @@ -22,6 +22,16 @@ MATADOR_UTILS_API std::string to_string(const blob_type_t &data); MATADOR_UTILS_API std::string to_string(const date_type_t &data); MATADOR_UTILS_API std::string to_string(const time_type_t &data); +template +std::string to_hex_string(IntegerType data, const size_t width = sizeof(IntegerType)<<1) { + static auto digits = "0123456789ABCDEF"; + std::string result(width,'0'); + for (size_t i=0, j=(width-1)*4 ; i>j) & 0x0f]; + } + return result; +} + /** * Splits a string by a delimiter and * add the string tokens to a vector. The diff --git a/source/orm/query/query_builder.cpp b/source/orm/query/query_builder.cpp index c89dcb8..28f0c33 100644 --- a/source/orm/query/query_builder.cpp +++ b/source/orm/query/query_builder.cpp @@ -12,8 +12,9 @@ #include "matador/query/internal/string_builder_utils.hpp" #include "matador/query/internal/query_parts.hpp" +#include "matador/sql/interface/connection_impl.hpp" + #include "matador/sql/query_context.hpp" -#include "matador/sql/connection.hpp" #include "matador/sql/dialect.hpp" namespace matador::query { diff --git a/test/backends/SessionInsertHasOne.cpp b/test/backends/SessionInsertHasOne.cpp index 7f870a4..1ebc7a1 100644 --- a/test/backends/SessionInsertHasOne.cpp +++ b/test/backends/SessionInsertHasOne.cpp @@ -13,10 +13,21 @@ using namespace matador::query; using namespace matador::object; TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[session][insert][has_one]") { - session sess{conn}; - user_pk_generator u; - u.name = "John Doe"; - sess.persist(u); + const auto result = schema.attach("users") + .and_then( [this] { return schema.attach("user_sessions"); } ) + .and_then([this] { return schema.create(db); } ); + REQUIRE(result.is_ok()); - REQUIRE(u.id > 0); + session ses({bus, connection::dns, 4}, schema); + + const auto u = make_object("user1", "password"); + const auto us = make_object("session1", u); + + REQUIRE(u.is_transient()); + REQUIRE(us.is_transient()); + REQUIRE(ses.insert(us).is_ok()); + REQUIRE(us.is_persistent()); + REQUIRE(u.is_persistent()); + + REQUIRE(u->session->session_token == "session1"); } \ No newline at end of file diff --git a/test/models/model_metas.hpp b/test/models/model_metas.hpp index 11b373d..a280b3e 100644 --- a/test/models/model_metas.hpp +++ b/test/models/model_metas.hpp @@ -17,5 +17,7 @@ META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_bi META_TABLE(books, BOOK, id, title, author_id, published_in) META_TABLE(orders, ORDER, order_id, order_date, required_date, shipped_date, ship_via, freight, ship_name, ship_address, ship_city, ship_region, ship_postal_code, ship_country) META_TABLE(courses, COURSE, id, title) +META_TABLE(users, USER, id, name, password, session_id) +META_TABLE(user_sessions, USER_SESSION, id, session_token, user_id) #endif //MATADOR_MODEL_METAS_HPP \ No newline at end of file diff --git a/test/models/user.hpp b/test/models/user.hpp index b42d061..52a9b88 100644 --- a/test/models/user.hpp +++ b/test/models/user.hpp @@ -15,14 +15,22 @@ template struct user_pk_generator { unsigned int id{}; std::string name; + std::string password; object::object_ptr> session; + user_pk_generator() = default; + user_pk_generator(std::string name, std::string password) + : name(std::move(name)), password(std::move(password)) {} + user_pk_generator(const unsigned int id, std::string name, std::string password) + : id(id), name(std::move(name)), password(std::move(password)) {} + template void process(Operator &op) { namespace field = matador::access; using namespace matador::utils; - field::primary_key(op, "id", id); + field::primary_key(op, "id", id, PkAttribute); field::attribute(op, "name", name, UniqueVarChar255); + field::attribute(op, "password", password, UniqueVarChar255); field::has_one(op, "session", session, CascadeAllFetchLazy); } }; @@ -33,11 +41,17 @@ struct user_session_pk_generator { std::string session_token; object::object_ptr> user_; + user_session_pk_generator() = default; + user_session_pk_generator(std::string session_token, object::object_ptr> user) + : session_token(std::move(session_token)), user_(std::move(user)) {} + user_session_pk_generator(const unsigned int id, std::string session_token, object::object_ptr> user) + : id(id), session_token(std::move(session_token)), user_(std::move(user)) {} + template void process(Operator &op) { namespace field = matador::access; using namespace matador::utils; - field::primary_key(op, "id", id); + field::primary_key(op, "id", id, PkAttribute); field::attribute(op, "session_token", session_token, VarChar255); field::belongs_to(op, "user_id", user_, CascadeAllFetchLazy); } diff --git a/test/orm/query/SessionQueryBuilderTest.cpp b/test/orm/query/SessionQueryBuilderTest.cpp index 9d16b23..5144adb 100644 --- a/test/orm/query/SessionQueryBuilderTest.cpp +++ b/test/orm/query/SessionQueryBuilderTest.cpp @@ -23,6 +23,7 @@ #include "../../models/recipe.hpp" #include "../../models/order.hpp" #include "../../models/student.hpp" +#include "../../models/user.hpp" #include "../../models/model_metas.hpp" using namespace matador::object; @@ -384,4 +385,22 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") { // .str(db); // // std::cout << ctx << std::endl; +} + +TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + + schema scm; + auto result = scm.attach("users") + .and_then( [&scm] { return scm.attach("user_sessions"); } ); + REQUIRE(result); + + select_query_builder eqb(scm); + + auto q = eqb.build(); + REQUIRE(q.is_ok()); + const auto sql = q->str(db); + } \ No newline at end of file diff --git a/test/orm/sql/StatementCacheTest.cpp b/test/orm/sql/StatementCacheTest.cpp index ca2683f..c5b52d3 100644 --- a/test/orm/sql/StatementCacheTest.cpp +++ b/test/orm/sql/StatementCacheTest.cpp @@ -8,6 +8,8 @@ #include "utils/RecordingObserver.hpp" +#include + using namespace matador::test; using namespace matador::sql; using namespace matador::utils;