From 937dd31a13a402a14fbdf863afe204dc2111367d Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Tue, 16 Jul 2024 07:37:03 +0200 Subject: [PATCH] Migrate column_t::as to using operator::as --- include/sqlpp11/column.h | 5 + include/sqlpp11/enable_as.h | 2 +- include/sqlpp11/eval.h | 3 +- include/sqlpp11/field_spec.h | 26 ++--- include/sqlpp11/having.h | 2 +- include/sqlpp11/on.h | 2 +- include/sqlpp11/operator/as_expression.h | 7 +- include/sqlpp11/type_traits.h | 123 +++++++++++------------ include/sqlpp11/where.h | 4 +- tests/core/types/result_row.cpp | 3 +- 10 files changed, 90 insertions(+), 87 deletions(-) diff --git a/include/sqlpp11/column.h b/include/sqlpp11/column.h index bd2a8671..a0cf4e7b 100644 --- a/include/sqlpp11/column.h +++ b/include/sqlpp11/column.h @@ -94,6 +94,11 @@ namespace sqlpp { }; + template + struct has_name> : std::true_type + { + }; + template Context& serialize(const column_t&, Context& context) { diff --git a/include/sqlpp11/enable_as.h b/include/sqlpp11/enable_as.h index c54f3152..419f17ce 100644 --- a/include/sqlpp11/enable_as.h +++ b/include/sqlpp11/enable_as.h @@ -35,7 +35,7 @@ namespace sqlpp template class enable_as { - constexpr decltype(auto) derived() const + constexpr auto derived() const -> const Expr& { return static_cast(*this); } diff --git a/include/sqlpp11/eval.h b/include/sqlpp11/eval.h index 29229057..c584ea94 100644 --- a/include/sqlpp11/eval.h +++ b/include/sqlpp11/eval.h @@ -43,7 +43,8 @@ namespace sqlpp "Expression cannot be used in eval because it requires tables"); using _name_type = alias::a_t::_alias_t; using _value_type = value_type_of_t; - using _field_spec = field_spec_t<_name_type, _value_type, true>; +#warning: Or do we expect users to provide the optional, too? + using _field_spec = field_spec_t<_name_type, sqlpp::compat::optional<_value_type>>; using type = typename _field_spec::cpp_type; }; diff --git a/include/sqlpp11/field_spec.h b/include/sqlpp11/field_spec.h index 96a19c6b..8c944cae 100644 --- a/include/sqlpp11/field_spec.h +++ b/include/sqlpp11/field_spec.h @@ -34,14 +34,13 @@ namespace sqlpp { - template + template struct field_spec_t { using _alias_t = NameType; - using cpp_type = typename std::conditional, - typename ValueType::_result_type>::type; +#warning: Maybe rename result_value in result_value_type? + using cpp_type = result_value_t; }; template @@ -52,18 +51,18 @@ namespace sqlpp template - struct is_field_compatible, - field_spec_t> + typename RightValue> + struct is_field_compatible, + field_spec_t> { + /* TODO reactivate static constexpr auto value = std::is_same::value and std::is_same::value and // Same value type (LeftCanBeNull or !RightCanBeNull); // The left hand side determines the result row and therefore must allow // NULL if the right hand side allows it + */ }; namespace detail @@ -71,16 +70,17 @@ namespace sqlpp template struct make_field_spec_impl { +#warning: required_tables_of and obtaining the alias should handle optional. using RawNamedExpr = remove_optional_t; - using ValueType = value_type_of_t; - static constexpr bool _can_be_null = is_optional::value or is_optional::value; static constexpr bool _depends_on_outer_table = detail::make_intersect_set_t, typename Select::_used_outer_tables>::size::value > 0; + using ValueType = typename std::conditional<_depends_on_outer_table, + sqlpp::force_optional_t>, + value_type_of_t>::type; using type = field_spec_t, - logic::any_t<_can_be_null, _depends_on_outer_table>::value>; + ValueType>; }; } // namespace detail diff --git a/include/sqlpp11/having.h b/include/sqlpp11/having.h index 8f4d69c3..2d612505 100644 --- a/include/sqlpp11/having.h +++ b/include/sqlpp11/having.h @@ -102,7 +102,7 @@ namespace sqlpp using type = static_combined_check_t::value, assert_having_not_cpp_bool_t>, static_check_t::value, assert_having_boolean_expression_t>, - static_check_t::value, assert_having_boolean_expression_t>>; + static_check_t::value, assert_having_boolean_expression_t>>; }; template diff --git a/include/sqlpp11/on.h b/include/sqlpp11/on.h index 4e4dd58d..3783ebab 100644 --- a/include/sqlpp11/on.h +++ b/include/sqlpp11/on.h @@ -39,7 +39,7 @@ namespace sqlpp struct check_on { using type = static_combined_check_t::value, assert_on_is_expression_t>, - static_check_t::value, assert_on_is_boolean_expression_t>>; + static_check_t::value, assert_on_is_boolean_expression_t>>; }; template diff --git a/include/sqlpp11/operator/as_expression.h b/include/sqlpp11/operator/as_expression.h index 5da2d55e..0e0c79f6 100644 --- a/include/sqlpp11/operator/as_expression.h +++ b/include/sqlpp11/operator/as_expression.h @@ -71,6 +71,11 @@ namespace sqlpp using type = value_type_of_t; }; + template + struct has_name> : std::true_type + { + }; + template Context& serialize(const as_expression& t, Context& context) { @@ -81,7 +86,7 @@ namespace sqlpp } template using check_as_args = std::enable_if_t< - is_expression_t::value and not is_alias_t::value and has_name::value + has_value_type::value and not is_alias_t::value and has_name::value >; template > diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index 2d401e80..808df0eb 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -138,6 +138,10 @@ namespace sqlpp { }; + template + struct can_be_null : public is_optional> {}; + +#warning: remove this template struct is_not_cpp_bool_t { @@ -145,11 +149,11 @@ namespace sqlpp }; // - struct boolean; + struct boolean{}; template<> struct value_type_of { using type = boolean; }; - struct integral; + struct integral{}; template<> struct value_type_of { using type = integral; }; template<> @@ -159,7 +163,7 @@ namespace sqlpp template<> struct value_type_of { using type = integral; }; - struct unsigned_integral; + struct unsigned_integral{}; template<> struct value_type_of { using type = unsigned_integral; }; template<> @@ -169,13 +173,13 @@ namespace sqlpp template<> struct value_type_of { using type = unsigned_integral; }; - struct floating_point; + struct floating_point{}; template<> struct value_type_of { using type = floating_point; }; template<> struct value_type_of { using type = floating_point; }; - struct text; + struct text{}; template <> struct value_type_of { using type = text; }; template <> @@ -185,22 +189,22 @@ namespace sqlpp template <> struct value_type_of { using type = text; }; - struct blob; + struct blob{}; template <> struct value_type_of> { using type = blob; }; template <> struct value_type_of> { using type = blob; }; - struct day_point; + struct day_point{}; template <> struct value_type_of> { using type = day_point; }; - struct time_of_day; + struct time_of_day{}; template struct value_type_of> { using type = time_of_day; }; - struct time_point; + struct time_point{}; template struct value_type_of> { using type = time_point; }; @@ -316,7 +320,45 @@ namespace sqlpp template - struct parameter_value; + struct result_value {}; + + template + struct result_value> + { + using type = sqlpp::compat::optional::type>; + }; + + template + using result_value_t = typename result_value::type; + + template<> + struct result_value { using type = sqlpp::compat::span; }; + + template<> + struct result_value { using type = bool; }; + + template<> + struct result_value { using type = int64_t; }; + + template<> + struct result_value { using type = uint64_t; }; + + template<> + struct result_value { using type = double; }; + + template<> + struct result_value { using type = sqlpp::compat::string_view; }; + + template<> + struct result_value { using type = std::chrono::time_point; }; + template<> + struct result_value { using type = std::chrono::microseconds; }; + + template<> + struct result_value { using type = std::chrono::time_point; }; + + template + struct parameter_value {}; template struct parameter_value> @@ -339,6 +381,9 @@ namespace sqlpp template<> struct parameter_value { using type = uint64_t; }; + template<> + struct parameter_value { using type = floating_point; }; + template<> struct parameter_value { using type = std::string; }; @@ -346,61 +391,10 @@ namespace sqlpp struct parameter_value { using type = std::chrono::time_point; }; template<> struct parameter_value { using type = std::chrono::microseconds; }; + template<> struct parameter_value { using type = std::chrono::time_point; }; -#warning: These something_t data type classifiers should be removed - // data types - struct blob; - template - using is_blob_t = std::is_same, blob>; - - template - using is_boolean_t = std::is_same, boolean>; - - struct day_point; - template - using is_day_point_t = std::is_same, day_point>; - - struct floating_point; - template - using is_floating_point_t = std::is_same, floating_point>; - - struct integral; - template - using is_integral_t = std::is_same, integral>; - - struct unsigned_integral; - template - using is_unsigned_integral_t = std::is_same, unsigned_integral>; - - struct text; - template - using is_text_t = std::is_same, text>; - - struct time_of_day; - template - using is_time_of_day_t = std::is_same, time_of_day>; - - struct time_point; - template - using is_time_point_t = std::is_same, time_point>; - - // joined data type - template - using is_numeric_t = - logic::any_t::value, is_unsigned_integral_t::value, is_floating_point_t::value>; - - template - using is_numeric_not_unsigned_t = - logic::any_t::value, not is_unsigned_integral_t::value, is_floating_point_t::value>; - - template - using is_day_or_time_point_t = logic::any_t::value, is_time_point_t::value>; - - template - struct can_be_null : public is_optional> {}; - #define SQLPP_VALUE_TRAIT_GENERATOR(name) \ namespace tag \ { \ @@ -678,11 +672,8 @@ namespace sqlpp template using name_of = typename T::_alias_t::_name_t; - template - struct has_name : public std::false_type {}; - template - struct has_name : public std::true_type {}; + struct has_name : public std::false_type {}; template struct make_traits diff --git a/include/sqlpp11/where.h b/include/sqlpp11/where.h index fa96091b..c13860eb 100644 --- a/include/sqlpp11/where.h +++ b/include/sqlpp11/where.h @@ -121,7 +121,7 @@ namespace sqlpp // assert_where_arg_is_not_cpp_bool_t>, // static_check_t::value...>::value, // assert_where_boolean_expressions_t>, - // static_check_t::value...>::value, + // static_check_t::value...>::value, // assert_where_arg_is_boolean_expression_t>, // static_check_t::value)...>::value, // assert_where_arg_contains_no_aggregate_functions_t>>; @@ -131,7 +131,7 @@ namespace sqlpp using type = static_combined_check_t< static_check_t::value, assert_where_arg_is_not_cpp_bool_t>, static_check_t::value, assert_where_arg_is_boolean_expression_t>, - static_check_t::value, assert_where_arg_is_boolean_expression_t>, + static_check_t::value, assert_where_arg_is_boolean_expression_t>, static_check_t::value, assert_where_arg_contains_no_aggregate_functions_t>>; }; diff --git a/tests/core/types/result_row.cpp b/tests/core/types/result_row.cpp index 9110476b..c7db35be 100644 --- a/tests/core/types/result_row.cpp +++ b/tests/core/types/result_row.cpp @@ -176,6 +176,8 @@ int main() static_assert(std::is_same, sqlpp::no_value_t>::value, ""); // as expressions retain the value type of the real thing + static_assert(sqlpp::has_name::value, ""); + sqlpp::as(bar.intN, bar.textN); static_assert(std::is_same, sqlpp::value_type_of_t>::value, ""); // max can yield NULL if there are no results. @@ -183,7 +185,6 @@ int main() static_assert(std::is_same, sqlpp::compat::optional>::value, ""); static_assert(std::is_same, sqlpp::compat::optional>::value, ""); - #if 0