0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 12:51:13 +08:00

Migrate column_t::as to using operator::as

This commit is contained in:
Roland Bock 2024-07-16 07:37:03 +02:00
parent bdd87b9e03
commit 937dd31a13
10 changed files with 90 additions and 87 deletions

View File

@ -94,6 +94,11 @@ namespace sqlpp
{ {
}; };
template<typename Table, typename ColumnSpec>
struct has_name<column_t<Table, ColumnSpec>> : std::true_type
{
};
template <typename Context, typename Table, typename ColumnSpec> template <typename Context, typename Table, typename ColumnSpec>
Context& serialize(const column_t<Table, ColumnSpec>&, Context& context) Context& serialize(const column_t<Table, ColumnSpec>&, Context& context)
{ {

View File

@ -35,7 +35,7 @@ namespace sqlpp
template <typename Expr> template <typename Expr>
class enable_as class enable_as
{ {
constexpr decltype(auto) derived() const constexpr auto derived() const -> const Expr&
{ {
return static_cast<const Expr&>(*this); return static_cast<const Expr&>(*this);
} }

View File

@ -43,7 +43,8 @@ namespace sqlpp
"Expression cannot be used in eval because it requires tables"); "Expression cannot be used in eval because it requires tables");
using _name_type = alias::a_t::_alias_t; using _name_type = alias::a_t::_alias_t;
using _value_type = value_type_of_t<Expr>; using _value_type = value_type_of_t<Expr>;
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; using type = typename _field_spec::cpp_type;
}; };

View File

@ -34,14 +34,13 @@
namespace sqlpp namespace sqlpp
{ {
template <typename NameType, typename ValueType, bool CanBeNull> template <typename NameType, typename ValueType>
struct field_spec_t struct field_spec_t
{ {
using _alias_t = NameType; using _alias_t = NameType;
using cpp_type = typename std::conditional<CanBeNull, #warning: Maybe rename result_value in result_value_type?
sqlpp::compat::optional<typename ValueType::_result_type>, using cpp_type = result_value_t<ValueType>;
typename ValueType::_result_type>::type;
}; };
template <typename Left, typename Right, typename Enable = void> template <typename Left, typename Right, typename Enable = void>
@ -52,18 +51,18 @@ namespace sqlpp
template <typename LeftName, template <typename LeftName,
typename LeftValue, typename LeftValue,
bool LeftCanBeNull,
typename RightName, typename RightName,
typename RightValue, typename RightValue>
bool RightCanBeNull> struct is_field_compatible<field_spec_t<LeftName, LeftValue>,
struct is_field_compatible<field_spec_t<LeftName, LeftValue, LeftCanBeNull>, field_spec_t<RightName, RightValue>>
field_spec_t<RightName, RightValue, RightCanBeNull>>
{ {
/* TODO reactivate
static constexpr auto value = static constexpr auto value =
std::is_same<typename LeftName::_name_t, typename RightName::_name_t>::value and std::is_same<typename LeftName::_name_t, typename RightName::_name_t>::value and
std::is_same<LeftValue, RightValue>::value and // Same value type std::is_same<LeftValue, RightValue>::value and // Same value type
(LeftCanBeNull or !RightCanBeNull); // The left hand side determines the result row and therefore must allow (LeftCanBeNull or !RightCanBeNull); // The left hand side determines the result row and therefore must allow
// NULL if the right hand side allows it // NULL if the right hand side allows it
*/
}; };
namespace detail namespace detail
@ -71,16 +70,17 @@ namespace sqlpp
template <typename Select, typename NamedExpr> template <typename Select, typename NamedExpr>
struct make_field_spec_impl struct make_field_spec_impl
{ {
#warning: required_tables_of and obtaining the alias should handle optional.
using RawNamedExpr = remove_optional_t<NamedExpr>; using RawNamedExpr = remove_optional_t<NamedExpr>;
using ValueType = value_type_of_t<RawNamedExpr>;
static constexpr bool _can_be_null = is_optional<ValueType>::value or is_optional<NamedExpr>::value;
static constexpr bool _depends_on_outer_table = static constexpr bool _depends_on_outer_table =
detail::make_intersect_set_t<required_tables_of<RawNamedExpr>, detail::make_intersect_set_t<required_tables_of<RawNamedExpr>,
typename Select::_used_outer_tables>::size::value > 0; 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<NamedExpr>>,
value_type_of_t<NamedExpr>>::type;
using type = field_spec_t<typename RawNamedExpr::_alias_t, using type = field_spec_t<typename RawNamedExpr::_alias_t,
remove_optional_t<ValueType>, ValueType>;
logic::any_t<_can_be_null, _depends_on_outer_table>::value>;
}; };
} // namespace detail } // namespace detail

View File

@ -102,7 +102,7 @@ namespace sqlpp
using type = using type =
static_combined_check_t<static_check_t<is_not_cpp_bool_t<Expression>::value, assert_having_not_cpp_bool_t>, static_combined_check_t<static_check_t<is_not_cpp_bool_t<Expression>::value, assert_having_not_cpp_bool_t>,
static_check_t<is_expression_t<Expression>::value, assert_having_boolean_expression_t>, static_check_t<is_expression_t<Expression>::value, assert_having_boolean_expression_t>,
static_check_t<is_boolean_t<Expression>::value, assert_having_boolean_expression_t>>; static_check_t<is_boolean<Expression>::value, assert_having_boolean_expression_t>>;
}; };
template <typename Expression> template <typename Expression>

View File

@ -39,7 +39,7 @@ namespace sqlpp
struct check_on struct check_on
{ {
using type = static_combined_check_t<static_check_t<is_expression_t<Expr>::value, assert_on_is_expression_t>, using type = static_combined_check_t<static_check_t<is_expression_t<Expr>::value, assert_on_is_expression_t>,
static_check_t<is_boolean_t<Expr>::value, assert_on_is_boolean_expression_t>>; static_check_t<is_boolean<Expr>::value, assert_on_is_boolean_expression_t>>;
}; };
template <typename Expr> template <typename Expr>

View File

@ -71,6 +71,11 @@ namespace sqlpp
using type = value_type_of_t<Expression>; using type = value_type_of_t<Expression>;
}; };
template <typename Expression, typename AliasProvider>
struct has_name<as_expression<Expression, AliasProvider>> : std::true_type
{
};
template <typename Context, typename Expression, typename AliasProvider> template <typename Context, typename Expression, typename AliasProvider>
Context& serialize(const as_expression<Expression, AliasProvider>& t, Context& context) Context& serialize(const as_expression<Expression, AliasProvider>& t, Context& context)
{ {
@ -81,7 +86,7 @@ namespace sqlpp
} }
template <typename Expr, typename AliasProvider> template <typename Expr, typename AliasProvider>
using check_as_args = std::enable_if_t< using check_as_args = std::enable_if_t<
is_expression_t<Expr>::value and not is_alias_t<Expr>::value and has_name<AliasProvider>::value has_value_type<Expr>::value and not is_alias_t<Expr>::value and has_name<AliasProvider>::value
>; >;
template <typename Expr, typename AliasProvider, typename = check_as_args<Expr, AliasProvider>> template <typename Expr, typename AliasProvider, typename = check_as_args<Expr, AliasProvider>>

View File

@ -138,6 +138,10 @@ namespace sqlpp
{ {
}; };
template <typename T>
struct can_be_null : public is_optional<value_type_of_t<T>> {};
#warning: remove this
template <typename T> template <typename T>
struct is_not_cpp_bool_t struct is_not_cpp_bool_t
{ {
@ -145,11 +149,11 @@ namespace sqlpp
}; };
// //
struct boolean; struct boolean{};
template<> template<>
struct value_type_of<bool> { using type = boolean; }; struct value_type_of<bool> { using type = boolean; };
struct integral; struct integral{};
template<> template<>
struct value_type_of<int8_t> { using type = integral; }; struct value_type_of<int8_t> { using type = integral; };
template<> template<>
@ -159,7 +163,7 @@ namespace sqlpp
template<> template<>
struct value_type_of<int64_t> { using type = integral; }; struct value_type_of<int64_t> { using type = integral; };
struct unsigned_integral; struct unsigned_integral{};
template<> template<>
struct value_type_of<uint8_t> { using type = unsigned_integral; }; struct value_type_of<uint8_t> { using type = unsigned_integral; };
template<> template<>
@ -169,13 +173,13 @@ namespace sqlpp
template<> template<>
struct value_type_of<uint64_t> { using type = unsigned_integral; }; struct value_type_of<uint64_t> { using type = unsigned_integral; };
struct floating_point; struct floating_point{};
template<> template<>
struct value_type_of<float> { using type = floating_point; }; struct value_type_of<float> { using type = floating_point; };
template<> template<>
struct value_type_of<double> { using type = floating_point; }; struct value_type_of<double> { using type = floating_point; };
struct text; struct text{};
template <> template <>
struct value_type_of<char> { using type = text; }; struct value_type_of<char> { using type = text; };
template <> template <>
@ -185,22 +189,22 @@ namespace sqlpp
template <> template <>
struct value_type_of<sqlpp::compat::string_view> { using type = text; }; struct value_type_of<sqlpp::compat::string_view> { using type = text; };
struct blob; struct blob{};
template <> template <>
struct value_type_of<std::vector<std::uint8_t>> { using type = blob; }; struct value_type_of<std::vector<std::uint8_t>> { using type = blob; };
template <> template <>
struct value_type_of<sqlpp::compat::span<std::uint8_t>> { using type = blob; }; struct value_type_of<sqlpp::compat::span<std::uint8_t>> { using type = blob; };
struct day_point; struct day_point{};
template <> template <>
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>> { using type = day_point; }; struct value_type_of<std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>> { using type = day_point; };
struct time_of_day; struct time_of_day{};
template <typename Rep, typename Period> template <typename Rep, typename Period>
struct value_type_of<std::chrono::duration<Rep, Period>> { using type = time_of_day; }; struct value_type_of<std::chrono::duration<Rep, Period>> { using type = time_of_day; };
struct time_point; struct time_point{};
template <typename Period> template <typename Period>
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, Period>> { using type = time_point; }; struct value_type_of<std::chrono::time_point<std::chrono::system_clock, Period>> { using type = time_point; };
@ -316,7 +320,45 @@ namespace sqlpp
template<typename T> template<typename T>
struct parameter_value; struct result_value {};
template<typename T>
struct result_value<sqlpp::compat::optional<T>>
{
using type = sqlpp::compat::optional<typename result_value<T>::type>;
};
template <typename T>
using result_value_t = typename result_value<T>::type;
template<>
struct result_value<blob> { using type = sqlpp::compat::span<uint8_t>; };
template<>
struct result_value<boolean> { using type = bool; };
template<>
struct result_value<integral> { using type = int64_t; };
template<>
struct result_value<unsigned_integral> { using type = uint64_t; };
template<>
struct result_value<floating_point> { using type = double; };
template<>
struct result_value<text> { using type = sqlpp::compat::string_view; };
template<>
struct result_value<day_point> { using type = std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>; };
template<>
struct result_value<time_of_day> { using type = std::chrono::microseconds; };
template<>
struct result_value<time_point> { using type = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>; };
template<typename T>
struct parameter_value {};
template<typename T> template<typename T>
struct parameter_value<sqlpp::compat::optional<T>> struct parameter_value<sqlpp::compat::optional<T>>
@ -339,6 +381,9 @@ namespace sqlpp
template<> template<>
struct parameter_value<unsigned_integral> { using type = uint64_t; }; struct parameter_value<unsigned_integral> { using type = uint64_t; };
template<>
struct parameter_value<floating_point> { using type = floating_point; };
template<> template<>
struct parameter_value<text> { using type = std::string; }; struct parameter_value<text> { using type = std::string; };
@ -346,61 +391,10 @@ namespace sqlpp
struct parameter_value<day_point> { using type = std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>; }; struct parameter_value<day_point> { using type = std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>; };
template<> template<>
struct parameter_value<time_of_day> { using type = std::chrono::microseconds; }; struct parameter_value<time_of_day> { using type = std::chrono::microseconds; };
template<> template<>
struct parameter_value<time_point> { using type = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>; }; struct parameter_value<time_point> { using type = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>; };
#warning: These something_t data type classifiers should be removed
// data types
struct blob;
template <typename T>
using is_blob_t = std::is_same<value_type_of_t<T>, blob>;
template <typename T>
using is_boolean_t = std::is_same<value_type_of_t<T>, boolean>;
struct day_point;
template <typename T>
using is_day_point_t = std::is_same<value_type_of_t<T>, day_point>;
struct floating_point;
template <typename T>
using is_floating_point_t = std::is_same<value_type_of_t<T>, floating_point>;
struct integral;
template <typename T>
using is_integral_t = std::is_same<value_type_of_t<T>, integral>;
struct unsigned_integral;
template <typename T>
using is_unsigned_integral_t = std::is_same<value_type_of_t<T>, unsigned_integral>;
struct text;
template <typename T>
using is_text_t = std::is_same<value_type_of_t<T>, text>;
struct time_of_day;
template <typename T>
using is_time_of_day_t = std::is_same<value_type_of_t<T>, time_of_day>;
struct time_point;
template <typename T>
using is_time_point_t = std::is_same<value_type_of_t<T>, time_point>;
// joined data type
template <typename T>
using is_numeric_t =
logic::any_t<is_integral_t<T>::value, is_unsigned_integral_t<T>::value, is_floating_point_t<T>::value>;
template <typename T>
using is_numeric_not_unsigned_t =
logic::any_t<is_integral_t<T>::value, not is_unsigned_integral_t<T>::value, is_floating_point_t<T>::value>;
template <typename T>
using is_day_or_time_point_t = logic::any_t<is_day_point_t<T>::value, is_time_point_t<T>::value>;
template <typename T>
struct can_be_null : public is_optional<value_type_of_t<T>> {};
#define SQLPP_VALUE_TRAIT_GENERATOR(name) \ #define SQLPP_VALUE_TRAIT_GENERATOR(name) \
namespace tag \ namespace tag \
{ \ { \
@ -678,11 +672,8 @@ namespace sqlpp
template <typename T> template <typename T>
using name_of = typename T::_alias_t::_name_t; using name_of = typename T::_alias_t::_name_t;
template<typename T, typename = void>
struct has_name : public std::false_type {};
template<typename T> template<typename T>
struct has_name<T, typename T::_alias_t> : public std::true_type {}; struct has_name : public std::false_type {};
template <typename ValueType, typename... Tags> template <typename ValueType, typename... Tags>
struct make_traits struct make_traits

View File

@ -121,7 +121,7 @@ namespace sqlpp
// assert_where_arg_is_not_cpp_bool_t>, // assert_where_arg_is_not_cpp_bool_t>,
// static_check_t<logic::all_t<is_expression_t<Expressions>::value...>::value, // static_check_t<logic::all_t<is_expression_t<Expressions>::value...>::value,
// assert_where_boolean_expressions_t>, // assert_where_boolean_expressions_t>,
// static_check_t<logic::all_t<is_boolean_t<Expressions>::value...>::value, // static_check_t<logic::all_t<is_boolean<Expressions>::value...>::value,
// assert_where_arg_is_boolean_expression_t>, // assert_where_arg_is_boolean_expression_t>,
// static_check_t<logic::all_t<(not contains_aggregate_function_t<Expressions>::value)...>::value, // static_check_t<logic::all_t<(not contains_aggregate_function_t<Expressions>::value)...>::value,
// assert_where_arg_contains_no_aggregate_functions_t>>; // assert_where_arg_contains_no_aggregate_functions_t>>;
@ -131,7 +131,7 @@ namespace sqlpp
using type = static_combined_check_t< using type = static_combined_check_t<
static_check_t<is_not_cpp_bool_t<Expression>::value, assert_where_arg_is_not_cpp_bool_t>, static_check_t<is_not_cpp_bool_t<Expression>::value, assert_where_arg_is_not_cpp_bool_t>,
static_check_t<is_expression_t<Expression>::value, assert_where_arg_is_boolean_expression_t>, static_check_t<is_expression_t<Expression>::value, assert_where_arg_is_boolean_expression_t>,
static_check_t<is_boolean_t<Expression>::value, assert_where_arg_is_boolean_expression_t>, static_check_t<is_boolean<Expression>::value, assert_where_arg_is_boolean_expression_t>,
static_check_t<not contains_aggregate_function_t<Expression>::value, static_check_t<not contains_aggregate_function_t<Expression>::value,
assert_where_arg_contains_no_aggregate_functions_t>>; assert_where_arg_contains_no_aggregate_functions_t>>;
}; };

View File

@ -176,6 +176,8 @@ int main()
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(assign(bar.intN, sqlpp::default_value))>, sqlpp::no_value_t>::value, ""); static_assert(std::is_same<sqlpp::value_type_of_t<decltype(assign(bar.intN, sqlpp::default_value))>, sqlpp::no_value_t>::value, "");
// as expressions retain the value type of the real thing // as expressions retain the value type of the real thing
static_assert(sqlpp::has_name<decltype(bar.intN)>::value, "");
sqlpp::as(bar.intN, bar.textN);
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(bar.intN.as(bar.textN))>, sqlpp::value_type_of_t<decltype(bar.intN)>>::value, ""); static_assert(std::is_same<sqlpp::value_type_of_t<decltype(bar.intN.as(bar.textN))>, sqlpp::value_type_of_t<decltype(bar.intN)>>::value, "");
// max can yield NULL if there are no results. // max can yield NULL if there are no results.
@ -183,7 +185,6 @@ int main()
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(max(foo.textNnD))>, sqlpp::compat::optional<sqlpp::text>>::value, ""); static_assert(std::is_same<sqlpp::value_type_of_t<decltype(max(foo.textNnD))>, sqlpp::compat::optional<sqlpp::text>>::value, "");
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(sqlpp::max(7))>, sqlpp::compat::optional<sqlpp::integral>>::value, ""); static_assert(std::is_same<sqlpp::value_type_of_t<decltype(sqlpp::max(7))>, sqlpp::compat::optional<sqlpp::integral>>::value, "");
#if 0 #if 0