From 336b2a7342eefd12e1c07f6b1e8385db0d9b3c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Thu, 13 Feb 2025 16:13:20 +0100 Subject: [PATCH] fixed backend tests --- backends/postgres/src/postgres_connection.cpp | 8 ++- backends/postgres/src/postgres_error.cpp | 2 +- backends/postgres/test/CMakeLists.txt | 2 +- include/matador/query/query_compiler.hpp | 5 ++ include/matador/query/query_data.hpp | 18 +++-- include/matador/utils/value.hpp | 1 + source/core/object/attribute_definition.cpp | 5 +- source/core/utils/value.cpp | 65 ++++++++++++++++++- .../query/intermediates/executable_query.cpp | 3 + .../query/intermediates/fetchable_query.cpp | 5 ++ .../intermediates/query_into_intermediate.cpp | 12 ++-- source/orm/query/query_compiler.cpp | 29 +++++---- source/orm/sql/connection.cpp | 2 +- test/backends/ConnectionTest.cpp | 12 +++- test/backends/QueryFixture.cpp | 14 ++-- test/backends/QueryRecordTest.cpp | 3 +- test/backends/StatementCacheTest.cpp | 2 +- 17 files changed, 142 insertions(+), 46 deletions(-) diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index eabf5fe..69cff5c 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -20,7 +20,7 @@ postgres_connection::postgres_connection(const sql::connection_info &info) } utils::result postgres_connection::open() { - if (is_open()) { + if (conn_ != nullptr) { return utils::ok(); } @@ -221,7 +221,11 @@ utils::result postgres_connection::exists(const std::string return utils::failure(make_error(sql::error_code::TABLE_EXISTS_FAILED, res, conn_, "Failed check if table exists", stmt)); } - return utils::ok(utils::to(PQcmdTuples(res)) == 1); + const auto result = utils::to(PQcmdTuples(res)); + if (!result) { + return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to convert result value", stmt)); + } + return utils::ok(*result == 1); } std::string postgres_connection::to_escaped_string(const utils::blob& value) const diff --git a/backends/postgres/src/postgres_error.cpp b/backends/postgres/src/postgres_error.cpp index cb8dd1b..d4874af 100644 --- a/backends/postgres/src/postgres_error.cpp +++ b/backends/postgres/src/postgres_error.cpp @@ -27,7 +27,7 @@ bool is_result_error(const PGresult *res) { return true; } const auto status = PQresultStatus(res); - return status != PGRES_TUPLES_OK && status == PGRES_COMMAND_OK; + return status != PGRES_TUPLES_OK && status != PGRES_COMMAND_OK; } void throw_postgres_error(const char *what, const std::string &source) diff --git a/backends/postgres/test/CMakeLists.txt b/backends/postgres/test/CMakeLists.txt index 2cc6ac5..2edc12c 100644 --- a/backends/postgres/test/CMakeLists.txt +++ b/backends/postgres/test/CMakeLists.txt @@ -2,7 +2,7 @@ CPMAddPackage("gh:catchorg/Catch2@3.7.1") list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) -set(POSTGRES_CONNECTION_STRING "postgres://test:test123!@localhost:5432/matador") +set(POSTGRES_CONNECTION_STRING "postgres://news:news@localhost:15432/matador") configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE) diff --git a/include/matador/query/query_compiler.hpp b/include/matador/query/query_compiler.hpp index ecffc2a..5ee62d0 100644 --- a/include/matador/query/query_compiler.hpp +++ b/include/matador/query/query_compiler.hpp @@ -6,6 +6,8 @@ #include "matador/sql/query_context.hpp" +#include "matador/utils/placeholder.hpp" + namespace matador::sql { class connection_impl; class dialect; @@ -14,6 +16,7 @@ class dialect; namespace matador::query { struct query_data; +struct value_visitor; class query_compiler final : public query_part_visitor { @@ -52,6 +55,8 @@ protected: static std::string build_table_name(sql::dialect_token token, const sql::dialect &d, const sql::table& t); + std::string determine_value(value_visitor &visitor, const std::variant &val); + protected: const query_data *data_{}; sql::query_context query_; diff --git a/include/matador/query/query_data.hpp b/include/matador/query/query_data.hpp index 7ced617..f2409c3 100644 --- a/include/matador/query/query_data.hpp +++ b/include/matador/query/query_data.hpp @@ -5,17 +5,25 @@ #include #include "matador/object/attribute_definition.hpp" + #include "matador/sql/table.hpp" #include "matador/query/query_part.hpp" namespace matador::query { -struct query_data -{ - std::vector> parts{}; - std::vector columns{}; - std::unordered_map tables{}; + +enum struct query_mode { + Direct = 0, + Prepared = 1 }; + +struct query_data { + std::vector> parts{}; + std::vector columns{}; + std::unordered_map tables{}; + query_mode mode = query_mode::Direct; +}; + } #endif //QUERY_DATA_HPP diff --git a/include/matador/utils/value.hpp b/include/matador/utils/value.hpp index 0194bd3..dc0e185 100644 --- a/include/matador/utils/value.hpp +++ b/include/matador/utils/value.hpp @@ -68,6 +68,7 @@ public: [[nodiscard]] size_t size() const; [[nodiscard]] basic_type type() const; + void type(basic_type t); [[nodiscard]] bool is_integer() const; [[nodiscard]] bool is_floating_point() const; diff --git a/source/core/object/attribute_definition.cpp b/source/core/object/attribute_definition.cpp index 1442cbd..0f74f75 100644 --- a/source/core/object/attribute_definition.cpp +++ b/source/core/object/attribute_definition.cpp @@ -137,9 +137,8 @@ bool attribute_definition::is_null() const { return value_.is_null(); } -void attribute_definition::type(utils::basic_type /*type*/) { - // type_ = type; - // utils::initialize_by_utils::basic_type(type, value_); +void attribute_definition::type(const utils::basic_type type) { + value_.type(type); } std::string attribute_definition::str() const { diff --git a/source/core/utils/value.cpp b/source/core/utils/value.cpp index e5c1754..097ff09 100644 --- a/source/core/utils/value.cpp +++ b/source/core/utils/value.cpp @@ -5,6 +5,8 @@ namespace matador::utils { namespace detail { +void initialize_by_basic_type(basic_type type, database_type &val); + size_t determine_size(const std::string &val) { return val.size(); @@ -56,11 +58,15 @@ size_t value::size() const return size_; } -basic_type value::type() const -{ +basic_type value::type() const { return type_; } +void value::type(const basic_type t) { + type_ = t; + detail::initialize_by_basic_type(type_, value_); +} + bool value::is_integer() const { return type_ >= basic_type::type_int8 && type_ <= basic_type::type_uint64; @@ -106,4 +112,59 @@ bool value::is_null() const return type_ == basic_type::type_null; } +namespace detail { +void initialize_by_basic_type(const basic_type type, database_type &val) { + switch (type) { + case basic_type::type_int8: + val.emplace(); + break; + case basic_type::type_int16: + val.emplace(); + break; + case basic_type::type_int32: + val.emplace(); + break; + case basic_type::type_int64: + val.emplace(); + break; + case basic_type::type_uint8: + val.emplace(); + break; + case basic_type::type_uint16: + val.emplace(); + break; + case basic_type::type_uint32: + val.emplace(); + break; + case basic_type::type_uint64: + val.emplace(); + break; + case basic_type::type_bool: + val.emplace(); + break; + case basic_type::type_float: + val.emplace(); + break; + case basic_type::type_double: + val.emplace(); + break; + case basic_type::type_varchar: + case basic_type::type_text: + val.emplace(); + break; + // case basic_type::type_date: + // val.emplace(); + // break; + // case basic_type::type_time: + // val.emplace