From 08f643460ce47fbde7bc6022f2f81fbda9d47be3 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Tue, 9 Jul 2024 08:08:53 +0200 Subject: [PATCH] No more type traits in generated code --- include/sqlpp11/assignment.h | 3 ++- include/sqlpp11/column.h | 6 ++---- include/sqlpp11/cte.h | 3 +-- include/sqlpp11/lower.h | 1 - include/sqlpp11/operator/assign.h | 2 +- include/sqlpp11/select_pseudo_table.h | 8 +++----- include/sqlpp11/statement.h | 3 +++ include/sqlpp11/table_alias.h | 1 - include/sqlpp11/trim.h | 1 - include/sqlpp11/type_traits.h | 25 +----------------------- include/sqlpp11/upper.h | 1 - scripts/ddl2cpp | 22 +++++++++------------ tests/core/types/result_row.cpp | 18 ++++++++--------- tests/core/usage/Sample.h | 28 +++++++++++++-------------- 14 files changed, 45 insertions(+), 77 deletions(-) diff --git a/include/sqlpp11/assignment.h b/include/sqlpp11/assignment.h index 5fc5872d..929523e1 100644 --- a/include/sqlpp11/assignment.h +++ b/include/sqlpp11/assignment.h @@ -41,7 +41,8 @@ namespace sqlpp using _rhs_t = Rhs; using _nodes = detail::type_vector<_lhs_t, _rhs_t>; - static_assert(can_be_null_t<_lhs_t>::value ? true +#warning: We check this in the factory function + static_assert(can_be_null<_lhs_t>::value ? true : not std::is_same<_rhs_t, null_t>::value, "column must not be null"); diff --git a/include/sqlpp11/column.h b/include/sqlpp11/column.h index 547c342c..e9158300 100644 --- a/include/sqlpp11/column.h +++ b/include/sqlpp11/column.h @@ -47,13 +47,11 @@ namespace sqlpp struct _traits { using _value_type = typename ColumnSpec::value_type; - using _tags = detail::make_joined_set_t, - typename ColumnSpec::_traits::_tags>; + using _tags = detail::make_joined_set_t>; }; using _nodes = detail::type_vector<>; using _required_tables = detail::type_set; - using _can_be_null = column_spec_can_be_null_t; using _spec_t = ColumnSpec; using _table = Table; @@ -98,7 +96,7 @@ namespace sqlpp auto operator=(null_t /*unused*/) const -> assignment_t { - static_assert(can_be_null_t::value, "column cannot be null"); + static_assert(can_be_null::value, "column cannot be null"); return {*this, null_t{}}; } diff --git a/include/sqlpp11/cte.h b/include/sqlpp11/cte.h index c6e047a7..9cdf2ef3 100644 --- a/include/sqlpp11/cte.h +++ b/include/sqlpp11/cte.h @@ -94,8 +94,7 @@ namespace sqlpp { using _alias_t = typename FieldSpec::_alias_t; - using _traits = make_traits, - tag_if::value>>; + using _traits = make_traits>; }; template diff --git a/include/sqlpp11/lower.h b/include/sqlpp11/lower.h index f9ad1a4b..b0cd16ea 100644 --- a/include/sqlpp11/lower.h +++ b/include/sqlpp11/lower.h @@ -60,7 +60,6 @@ namespace sqlpp using _traits = make_traits; using _nodes = detail::type_vector; - using _can_be_null = can_be_null_t; using _auto_alias_t = lower_alias_t; diff --git a/include/sqlpp11/operator/assign.h b/include/sqlpp11/operator/assign.h index 444ff5ae..067b5d31 100644 --- a/include/sqlpp11/operator/assign.h +++ b/include/sqlpp11/operator/assign.h @@ -45,7 +45,7 @@ namespace sqlpp #warning handle default_t, but only if the column does have a default! template using check_assign_args = std::enable_if_t::value and values_are_comparable::value and - (can_be_null_t::value or not is_optional::value) and + (can_be_null::value or not is_optional::value) and (has_default_t::value or not std::is_same::value)>; template > diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index a2e55658..58ed1f4d 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -39,14 +39,14 @@ namespace sqlpp { using _alias_t = typename NamedExpr::_alias_t; - static constexpr bool _can_be_null = can_be_null_t::value; static constexpr bool _depends_on_outer_table = detail::make_intersect_set_t, typename Select::_used_outer_tables>::size::value > 0; #warning: somehow prevent insert... - using _traits = make_traits, - tag_if>; +#warning: may need to make value type optional for outer tables + using _traits = make_traits/*, + tag_if*/>; }; template @@ -60,7 +60,6 @@ namespace sqlpp struct select_expression_type { using value_t = value_type_of_t; static constexpr bool _is_expression = true; - static constexpr bool _can_be_null = can_be_null_t::value; }; template @@ -72,7 +71,6 @@ namespace sqlpp // Usage as named expression typename _expr_t::value_t, tag_if, - tag_if, // Usage as table tag::is_table, tag::is_pseudo_table, diff --git a/include/sqlpp11/statement.h b/include/sqlpp11/statement.h index d9036a91..452eb5e2 100644 --- a/include/sqlpp11/statement.h +++ b/include/sqlpp11/statement.h @@ -138,9 +138,12 @@ namespace sqlpp make_traits<_value_type, tag_if::value>>; using _nodes = detail::type_vector<>; +#warning: maybe need to make value type optional + /* using _can_be_null = logic::any_t::value, detail::make_intersect_set_t, _all_provided_outer_tables>::size::value != 0>; + */ using _parameters = detail::type_vector_cat_t...>; // required_tables and _required_ctes are defined above diff --git a/include/sqlpp11/table_alias.h b/include/sqlpp11/table_alias.h index 4d3bfda6..572a1cb3 100644 --- a/include/sqlpp11/table_alias.h +++ b/include/sqlpp11/table_alias.h @@ -44,7 +44,6 @@ namespace sqlpp using _traits = make_traits, tag::is_table, tag::is_alias, - tag_if::value>, tag_if::value>>; using _nodes = detail::type_vector<>; diff --git a/include/sqlpp11/trim.h b/include/sqlpp11/trim.h index 3eec5492..664392f0 100644 --- a/include/sqlpp11/trim.h +++ b/include/sqlpp11/trim.h @@ -61,7 +61,6 @@ namespace sqlpp using _traits = make_traits; using _nodes = detail::type_vector; - using _can_be_null = can_be_null_t; using _auto_alias_t = trim_alias_t; diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index c8bc0281..c0ea2abf 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -412,30 +412,8 @@ namespace sqlpp template using is_day_or_time_point_t = logic::any_t::value, is_time_point_t::value>; - namespace tag - { - struct can_be_null - { - }; - } // namespace tag - - namespace detail - { - template - struct column_spec_can_be_null_impl - { - using type = std::false_type; - }; - template - struct column_spec_can_be_null_impl< - T, - typename std::enable_if::value>::type> - { - using type = std::true_type; - }; - } // namespace detail template - using column_spec_can_be_null_t = typename detail::column_spec_can_be_null_impl::type; + struct can_be_null : public is_optional> {}; #define SQLPP_VALUE_TRAIT_GENERATOR(name) \ namespace tag \ @@ -597,7 +575,6 @@ namespace sqlpp template \ using trait##_t = typename detail::trait##_impl::type; - SQLPP_RECURSIVE_TRAIT_GENERATOR(can_be_null) SQLPP_RECURSIVE_TRAIT_GENERATOR(contains_aggregate_function) template diff --git a/include/sqlpp11/upper.h b/include/sqlpp11/upper.h index 0b26221e..6059071a 100644 --- a/include/sqlpp11/upper.h +++ b/include/sqlpp11/upper.h @@ -60,7 +60,6 @@ namespace sqlpp using _traits = make_traits; using _nodes = detail::type_vector; - using _can_be_null = can_be_null_t; using _auto_alias_t = upper_alias_t; diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 7e154366..517b1753 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -703,8 +703,6 @@ def createHeader(): ) print("You can disable this warning using -no-timestamp-warning") noTimestampWarning = True - traitslist = ["::sqlpp::" + columnType] - columnCanBeNull = not column.notNull and not column.isPrimaryKey print(" struct " + columnClass, file=header) print(" {", file=header) print(" struct _alias_t", file=header) @@ -735,21 +733,19 @@ def createHeader(): ) print(" };", file=header) print(" };", file=header) - hasAutoValue = column.hasAutoValue or (autoId and sqlColumnName == "id") - if columnCanBeNull: - traitslist.append("::sqlpp::tag::can_be_null") - if column.hasDefaultValue or hasAutoValue or columnCanBeNull: - traitslist.append("::sqlpp::tag::has_default") + columnCanBeNull = not column.notNull and not column.isPrimaryKey if columnCanBeNull: print(" using value_type = ::sqlpp::compat::optional<::sqlpp::" + columnType + ">;", file=header) else: print(" using value_type = ::sqlpp::" + columnType + ";", file=header) - print( - " using _traits = ::sqlpp::make_traits<" - + ", ".join(traitslist) - + ">;", - file=header, - ) + columnHasDefault = column.hasDefaultValue or \ + columnCanBeNull or \ + column.hasAutoValue or \ + (autoId and sqlColumnName == "id") + if columnHasDefault: + print(" using has_default_value = std::true_type;", file=header) + else: + print(" using has_default_value = std::false_type;", file=header) print(" };", file=header) print(" } // namespace " + tableNamespace, file=header) print("", file=header) diff --git a/tests/core/types/result_row.cpp b/tests/core/types/result_row.cpp index e0da2af1..c82253bb 100644 --- a/tests/core/types/result_row.cpp +++ b/tests/core/types/result_row.cpp @@ -44,12 +44,12 @@ namespace int main() { - static_assert(not sqlpp::can_be_null_t::value, ""); - static_assert(sqlpp::can_be_null_t::value, ""); - static_assert(not sqlpp::can_be_null_t::value, ""); - static_assert(not sqlpp::can_be_null_t::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); + static_assert(sqlpp::can_be_null::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); const auto seven = sqlpp::value(7).as(sqlpp::alias::s); - static_assert(not sqlpp::can_be_null_t::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); // Select non-optional column or alias of it. for (const auto& row : db(select(foo.id, foo.id.as(sqlpp::alias::a), foo.textNnD, foo.textNnD.as(sqlpp::alias::b)) @@ -319,8 +319,8 @@ int main() // aggregates of nullable values #warning use a nullable value const auto a = bar.id; - static_assert(not sqlpp::can_be_null_t::value, ""); - static_assert(not sqlpp::can_be_null_t::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); + static_assert(not sqlpp::can_be_null::value, ""); const auto rows = db(select(count(a), avg(a), max(a), min(a), sum(a)).from(bar).unconditionally()); auto& x = rows.front(); static_assert(not is_optional::value, ""); @@ -332,8 +332,8 @@ int main() { // aggregates of nullable values const auto o = foo.doubleN; - static_assert(sqlpp::can_be_null_t::value, ""); - static_assert(sqlpp::can_be_null_t::value, ""); + static_assert(sqlpp::can_be_null::value, ""); + static_assert(sqlpp::can_be_null::value, ""); const auto rows = db(select(count(o), avg(o), max(o), min(o), sum(o)).from(foo).unconditionally()); auto& x = rows.front(); static_assert(not is_optional::value, ""); diff --git a/tests/core/usage/Sample.h b/tests/core/usage/Sample.h index 890724e5..220e9a28 100644 --- a/tests/core/usage/Sample.h +++ b/tests/core/usage/Sample.h @@ -25,7 +25,7 @@ namespace test }; }; using value_type = ::sqlpp::integer; - using _traits = ::sqlpp::make_traits<::sqlpp::integer, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct TextNnD { @@ -42,7 +42,7 @@ namespace test }; }; using value_type = ::sqlpp::text; - using _traits = ::sqlpp::make_traits<::sqlpp::text, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct IntN { @@ -59,7 +59,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::integer>; - using _traits = ::sqlpp::make_traits<::sqlpp::integer, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct DoubleN { @@ -76,7 +76,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::floating_point>; - using _traits = ::sqlpp::make_traits<::sqlpp::floating_point, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct UIntN { @@ -93,7 +93,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::integer_unsigned>; - using _traits = ::sqlpp::make_traits<::sqlpp::integer_unsigned, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct BlobN { @@ -110,7 +110,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::blob>; - using _traits = ::sqlpp::make_traits<::sqlpp::blob, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; } // namespace TabFoo_ @@ -152,7 +152,7 @@ namespace test }; }; using value_type = ::sqlpp::integer; - using _traits = ::sqlpp::make_traits<::sqlpp::integer, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct TextN { @@ -169,7 +169,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::text>; - using _traits = ::sqlpp::make_traits<::sqlpp::text, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct BoolNn { @@ -186,7 +186,7 @@ namespace test }; }; using value_type = ::sqlpp::boolean; - using _traits = ::sqlpp::make_traits<::sqlpp::boolean>; + using has_default_value = std::false_type; }; struct IntN { @@ -203,7 +203,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::integer>; - using _traits = ::sqlpp::make_traits<::sqlpp::integer, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; } // namespace TabBar_ @@ -243,7 +243,7 @@ namespace test }; }; using value_type = ::sqlpp::integer; - using _traits = ::sqlpp::make_traits<::sqlpp::integer, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct DayPointN { @@ -260,7 +260,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::day_point>; - using _traits = ::sqlpp::make_traits<::sqlpp::day_point, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct TimePointN { @@ -277,7 +277,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::time_point>; - using _traits = ::sqlpp::make_traits<::sqlpp::time_point, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; struct TimeOfDayN { @@ -294,7 +294,7 @@ namespace test }; }; using value_type = ::sqlpp::compat::optional<::sqlpp::time_of_day>; - using _traits = ::sqlpp::make_traits<::sqlpp::time_of_day, ::sqlpp::tag::can_be_null, ::sqlpp::tag::has_default>; + using has_default_value = std::true_type; }; } // namespace TabDateTime_