From 01c1e69eb3da86dfe0306716c6b3205019dd124b Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 21 Jul 2024 10:06:30 +0200 Subject: [PATCH] More tests --- include/sqlpp11/parameter.h | 21 +++++++--- include/sqlpp11/type_traits.h | 24 +++++++++-- include/sqlpp11/value.h | 1 - tests/core/serialize/CMakeLists.txt | 7 ++-- tests/core/serialize/DynamicWhere.cpp | 5 ++- tests/core/serialize/ForUpdate.cpp | 24 ----------- tests/core/serialize/Parameter.cpp | 41 +++++++++++++++++++ .../core/serialize/ParameterizedVerbatim.cpp | 2 +- 8 files changed, 85 insertions(+), 40 deletions(-) create mode 100644 tests/core/serialize/Parameter.cpp diff --git a/include/sqlpp11/parameter.h b/include/sqlpp11/parameter.h index 002f6a5b..714b206b 100644 --- a/include/sqlpp11/parameter.h +++ b/include/sqlpp11/parameter.h @@ -26,21 +26,19 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include -#include #include namespace sqlpp { template - struct parameter_t : public expression_operators, ValueType> + struct parameter_t : public enable_comparison> { using _traits = make_traits; - using _nodes = detail::type_vector<>; using _parameters = detail::type_vector; - using _can_be_null = std::true_type; using _is_literal_expression = std::true_type; using _instance_t = member_t>; @@ -54,6 +52,17 @@ namespace sqlpp ~parameter_t() = default; }; + template + struct value_type_of> + { + using type = ValueType; + }; + + template + struct name_tag_of> : public name_tag_of + { + }; + template Context& serialize(Context& context, const parameter_t&) { @@ -70,9 +79,9 @@ namespace sqlpp template auto parameter(const ValueType& /*unused*/, const AliasProvider & /*unused*/) - -> parameter_t + -> parameter_t, AliasProvider> { - static_assert(is_value_type_t::value, "first argument is not a value type"); + static_assert(has_value_type::value, "first argument is not a value type"); static_assert(has_name::value, "second argument does not have a name"); return {}; } diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index c2636768..31d8cae7 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -153,10 +153,14 @@ namespace sqlpp // struct boolean{}; template<> + struct value_type_of { using type = boolean; }; + template<> struct value_type_of { using type = boolean; }; struct integral{}; template<> + struct value_type_of { using type = integral; }; + template<> struct value_type_of { using type = integral; }; template<> struct value_type_of { using type = integral; }; @@ -167,6 +171,8 @@ namespace sqlpp struct unsigned_integral{}; template<> + struct value_type_of { using type = unsigned_integral; }; + template<> struct value_type_of { using type = unsigned_integral; }; template<> struct value_type_of { using type = unsigned_integral; }; @@ -177,6 +183,8 @@ namespace sqlpp struct floating_point{}; template<> + struct value_type_of { using type = floating_point; }; + template<> struct value_type_of { using type = floating_point; }; template<> struct value_type_of { using type = floating_point; }; @@ -185,6 +193,8 @@ namespace sqlpp struct text{}; template <> + struct value_type_of { using type = text; }; + template <> struct value_type_of { using type = text; }; template <> struct value_type_of { using type = text; }; @@ -194,24 +204,30 @@ namespace sqlpp struct value_type_of { using type = text; }; struct blob{}; + template <> + struct value_type_of { using type = blob; }; template struct value_type_of> { using type = blob; }; - template <> struct value_type_of> { using type = blob; }; - template <> struct value_type_of> { using type = blob; }; struct day_point{}; template <> + struct value_type_of { using type = day_point; }; + template <> struct value_type_of> { using type = day_point; }; struct time_of_day{}; + template <> + struct value_type_of { using type = time_of_day; }; template struct value_type_of> { using type = time_of_day; }; struct time_point{}; + template <> + struct value_type_of { using type = time_point; }; template struct value_type_of> { using type = time_point; }; @@ -726,8 +742,8 @@ namespace sqlpp using _contains_aggregate_function = std::true_type; }; - template - using member_t = typename NameProvider::_alias_t::template _member_t; + template + using member_t = typename name_tag_of_t::template _member_t; template using derived_statement_t = typename Policies::_statement_t; diff --git a/include/sqlpp11/value.h b/include/sqlpp11/value.h index b4db9d76..b9961f3c 100644 --- a/include/sqlpp11/value.h +++ b/include/sqlpp11/value.h @@ -27,7 +27,6 @@ */ #include -#include #include namespace sqlpp diff --git a/tests/core/serialize/CMakeLists.txt b/tests/core/serialize/CMakeLists.txt index eab643df..63f20580 100644 --- a/tests/core/serialize/CMakeLists.txt +++ b/tests/core/serialize/CMakeLists.txt @@ -30,10 +30,10 @@ set(test_files Blob.cpp Count.cpp #CustomQuery.cpp - #DynamicWhere.cpp + DynamicWhere.cpp Exists.cpp Float.cpp - #ForUpdate.cpp + ForUpdate.cpp From.cpp In.cpp Insert.cpp @@ -44,6 +44,8 @@ set(test_files Min.cpp Operator.cpp Over.cpp + Parameter.cpp + ParameterizedVerbatim.cpp SelectAs.cpp SelectColumns.cpp SelectFlags.cpp @@ -52,7 +54,6 @@ set(test_files Trim.cpp Upper.cpp Where.cpp - #ParameterizedVerbatim.cpp ) create_test_sourcelist(test_sources test_serializer_main.cpp ${test_files}) diff --git a/tests/core/serialize/DynamicWhere.cpp b/tests/core/serialize/DynamicWhere.cpp index 5225ec89..8d21a091 100644 --- a/tests/core/serialize/DynamicWhere.cpp +++ b/tests/core/serialize/DynamicWhere.cpp @@ -31,12 +31,15 @@ int DynamicWhere(int, char*[]) { + const auto foo = test::TabFoo{}; const auto bar = test::TabBar{}; auto db = MockDb{}; compare(__LINE__, sqlpp::unconditionally(), ""); compare(__LINE__, where(bar.boolNn), " WHERE tab_bar.bool_nn"); -#warning add tests with optional expressions + compare(__LINE__, where(bar.boolNn and dynamic(true, foo.boolN)), " WHERE (tab_bar.bool_nn AND tab_foo.bool_n)"); + compare(__LINE__, where(bar.boolNn and dynamic(false, foo.boolN)), " WHERE tab_bar.bool_nn"); + return 0; } diff --git a/tests/core/serialize/ForUpdate.cpp b/tests/core/serialize/ForUpdate.cpp index 1c06d3fd..0b1b5262 100644 --- a/tests/core/serialize/ForUpdate.cpp +++ b/tests/core/serialize/ForUpdate.cpp @@ -27,36 +27,12 @@ #include "Sample.h" #include -#include - -namespace -{ - /* - auto getTrue() -> std::string - { - MockDb::_serializer_context_t printer = {}; - return serialize(sqlpp::value(true), printer).str(); - } - */ - - auto getFalse() -> std::string - { - MockDb::_serializer_context_t printer = {}; - return serialize(sqlpp::value(false), printer).str(); - } -} - int ForUpdate(int, char* []) { const auto foo = test::TabFoo{}; - // const auto bar = test::TabBar{}; - // Unconditionally compare(__LINE__, select(foo.doubleN).from(foo).unconditionally().for_update(), "SELECT tab_foo.double_n FROM tab_foo FOR UPDATE "); - // Never - compare(__LINE__, where(sqlpp::value(false)), " WHERE " + getFalse()); - return 0; } diff --git a/tests/core/serialize/Parameter.cpp b/tests/core/serialize/Parameter.cpp new file mode 100644 index 00000000..8d206023 --- /dev/null +++ b/tests/core/serialize/Parameter.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "compare.h" +#include "Sample.h" +#include + +int Parameter(int, char*[]) +{ + const auto foo = test::TabFoo{}; + const auto bar = test::TabBar{}; + + compare(__LINE__, parameter(foo.doubleN), "?"); + compare(__LINE__, bar.id > parameter(foo.doubleN), "(tab_bar.id > ?)"); + +#warning: Need type tests for parameter, too (do they have the right value type and the right paramter type?) + + return 0; +} diff --git a/tests/core/serialize/ParameterizedVerbatim.cpp b/tests/core/serialize/ParameterizedVerbatim.cpp index e55459cd..e8aae6ba 100644 --- a/tests/core/serialize/ParameterizedVerbatim.cpp +++ b/tests/core/serialize/ParameterizedVerbatim.cpp @@ -35,7 +35,7 @@ int ParameterizedVerbatim(int, char* []) { // An example thing that needs parameterized verbatim (as it's database specific) auto checking_value_in_range = sqlpp::parameterized_verbatim( - "(quests.spawn_level_range @> CAST(", parameter(sqlpp::integer(), quester_player_level), " AS integer))"); + "(quests.spawn_level_range @> CAST(", parameter(sqlpp::integral(), quester_player_level), " AS integer))"); compare(__LINE__, checking_value_in_range, "(quests.spawn_level_range @> CAST(? AS integer))");