0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

No more type traits in generated code

This commit is contained in:
Roland Bock 2024-07-09 08:08:53 +02:00
parent 4baede7434
commit 08f643460c
14 changed files with 45 additions and 77 deletions

View File

@ -41,7 +41,8 @@ namespace sqlpp
using _rhs_t = Rhs; using _rhs_t = Rhs;
using _nodes = detail::type_vector<_lhs_t, _rhs_t>; 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, : not std::is_same<_rhs_t, null_t>::value,
"column must not be null"); "column must not be null");

View File

@ -47,13 +47,11 @@ namespace sqlpp
struct _traits struct _traits
{ {
using _value_type = typename ColumnSpec::value_type; using _value_type = typename ColumnSpec::value_type;
using _tags = detail::make_joined_set_t<detail::type_set<tag::is_column, tag::is_expression, tag::is_selectable>, using _tags = detail::make_joined_set_t<detail::type_set<tag::is_column, tag::is_expression, tag::is_selectable>>;
typename ColumnSpec::_traits::_tags>;
}; };
using _nodes = detail::type_vector<>; using _nodes = detail::type_vector<>;
using _required_tables = detail::type_set<Table>; using _required_tables = detail::type_set<Table>;
using _can_be_null = column_spec_can_be_null_t<ColumnSpec>;
using _spec_t = ColumnSpec; using _spec_t = ColumnSpec;
using _table = Table; using _table = Table;
@ -98,7 +96,7 @@ namespace sqlpp
auto operator=(null_t /*unused*/) const -> assignment_t<column_t, null_t> auto operator=(null_t /*unused*/) const -> assignment_t<column_t, null_t>
{ {
static_assert(can_be_null_t<column_t>::value, "column cannot be null"); static_assert(can_be_null<column_t>::value, "column cannot be null");
return {*this, null_t{}}; return {*this, null_t{}};
} }

View File

@ -94,8 +94,7 @@ namespace sqlpp
{ {
using _alias_t = typename FieldSpec::_alias_t; using _alias_t = typename FieldSpec::_alias_t;
using _traits = make_traits<value_type_of_t<FieldSpec>, using _traits = make_traits<value_type_of_t<FieldSpec>>;
tag_if<tag::can_be_null, column_spec_can_be_null_t<FieldSpec>::value>>;
}; };
template <typename AliasProvider, typename Statement, typename ResultRow> template <typename AliasProvider, typename Statement, typename ResultRow>

View File

@ -60,7 +60,6 @@ namespace sqlpp
using _traits = make_traits<text, tag::is_expression, tag::is_selectable>; using _traits = make_traits<text, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr>; using _nodes = detail::type_vector<Expr>;
using _can_be_null = can_be_null_t<Expr>;
using _auto_alias_t = lower_alias_t; using _auto_alias_t = lower_alias_t;

View File

@ -45,7 +45,7 @@ namespace sqlpp
#warning handle default_t, but only if the column does have a default! #warning handle default_t, but only if the column does have a default!
template <typename L, typename R> template <typename L, typename R>
using check_assign_args = std::enable_if_t<is_column_t<L>::value and values_are_comparable<L, R>::value and using check_assign_args = std::enable_if_t<is_column_t<L>::value and values_are_comparable<L, R>::value and
(can_be_null_t<L>::value or not is_optional<R>::value) and (can_be_null<L>::value or not is_optional<R>::value) and
(has_default_t<L>::value or not std::is_same<R, default_value_t>::value)>; (has_default_t<L>::value or not std::is_same<R, default_value_t>::value)>;
template <typename L, typename R, typename = check_assign_args<L, R>> template <typename L, typename R, typename = check_assign_args<L, R>>

View File

@ -39,14 +39,14 @@ namespace sqlpp
{ {
using _alias_t = typename NamedExpr::_alias_t; using _alias_t = typename NamedExpr::_alias_t;
static constexpr bool _can_be_null = can_be_null_t<NamedExpr>::value;
static constexpr bool _depends_on_outer_table = static constexpr bool _depends_on_outer_table =
detail::make_intersect_set_t<required_tables_of<NamedExpr>, typename Select::_used_outer_tables>::size::value > detail::make_intersect_set_t<required_tables_of<NamedExpr>, typename Select::_used_outer_tables>::size::value >
0; 0;
#warning: somehow prevent insert... #warning: somehow prevent insert...
using _traits = make_traits<value_type_of_t<NamedExpr>, #warning: may need to make value type optional for outer tables
tag_if<tag::can_be_null, _can_be_null or _depends_on_outer_table>>; using _traits = make_traits<value_type_of_t<NamedExpr>/*,
tag_if<tag::can_be_null, _depends_on_outer_table>*/>;
}; };
template<typename... NamedExpr> template<typename... NamedExpr>
@ -60,7 +60,6 @@ namespace sqlpp
struct select_expression_type<NamedExpr> { struct select_expression_type<NamedExpr> {
using value_t = value_type_of_t<NamedExpr>; using value_t = value_type_of_t<NamedExpr>;
static constexpr bool _is_expression = true; static constexpr bool _is_expression = true;
static constexpr bool _can_be_null = can_be_null_t<NamedExpr>::value;
}; };
template <typename Select, typename... NamedExpr> template <typename Select, typename... NamedExpr>
@ -72,7 +71,6 @@ namespace sqlpp
// Usage as named expression // Usage as named expression
typename _expr_t::value_t, typename _expr_t::value_t,
tag_if<tag::is_expression, _expr_t::_is_expression>, tag_if<tag::is_expression, _expr_t::_is_expression>,
tag_if<tag::can_be_null, _expr_t::_can_be_null>,
// Usage as table // Usage as table
tag::is_table, tag::is_table,
tag::is_pseudo_table, tag::is_pseudo_table,

View File

@ -138,9 +138,12 @@ namespace sqlpp
make_traits<_value_type, tag_if<tag::is_expression, not std::is_same<_value_type, no_value_t>::value>>; make_traits<_value_type, tag_if<tag::is_expression, not std::is_same<_value_type, no_value_t>::value>>;
using _nodes = detail::type_vector<>; using _nodes = detail::type_vector<>;
#warning: maybe need to make value type optional
/*
using _can_be_null = logic::any_t<can_be_null_t<_result_type_provider>::value, using _can_be_null = logic::any_t<can_be_null_t<_result_type_provider>::value,
detail::make_intersect_set_t<required_tables_of<_result_type_provider>, detail::make_intersect_set_t<required_tables_of<_result_type_provider>,
_all_provided_outer_tables>::size::value != 0>; _all_provided_outer_tables>::size::value != 0>;
*/
using _parameters = detail::type_vector_cat_t<parameters_of<Policies>...>; using _parameters = detail::type_vector_cat_t<parameters_of<Policies>...>;
// required_tables and _required_ctes are defined above // required_tables and _required_ctes are defined above

View File

@ -44,7 +44,6 @@ namespace sqlpp
using _traits = make_traits<value_type_of_t<Table>, using _traits = make_traits<value_type_of_t<Table>,
tag::is_table, tag::is_table,
tag::is_alias, tag::is_alias,
tag_if<tag::can_be_null, can_be_null_t<Table>::value>,
tag_if<tag::is_selectable, is_expression_t<Table>::value>>; tag_if<tag::is_selectable, is_expression_t<Table>::value>>;
using _nodes = detail::type_vector<>; using _nodes = detail::type_vector<>;

View File

@ -61,7 +61,6 @@ namespace sqlpp
using _traits = make_traits<text, tag::is_expression, tag::is_selectable>; using _traits = make_traits<text, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr>; using _nodes = detail::type_vector<Expr>;
using _can_be_null = can_be_null_t<Expr>;
using _auto_alias_t = trim_alias_t; using _auto_alias_t = trim_alias_t;

View File

@ -412,30 +412,8 @@ namespace sqlpp
template <typename T> 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>; using is_day_or_time_point_t = logic::any_t<is_day_point_t<T>::value, is_time_point_t<T>::value>;
namespace tag
{
struct can_be_null
{
};
} // namespace tag
namespace detail
{
template <typename T, typename Enable = void>
struct column_spec_can_be_null_impl
{
using type = std::false_type;
};
template <typename T>
struct column_spec_can_be_null_impl<
T,
typename std::enable_if<detail::is_element_of<tag::can_be_null, typename T::_traits::_tags>::value>::type>
{
using type = std::true_type;
};
} // namespace detail
template <typename T> template <typename T>
using column_spec_can_be_null_t = typename detail::column_spec_can_be_null_impl<T>::type; 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 \
@ -597,7 +575,6 @@ namespace sqlpp
template <typename T> \ template <typename T> \
using trait##_t = typename detail::trait##_impl<T>::type; using trait##_t = typename detail::trait##_impl<T>::type;
SQLPP_RECURSIVE_TRAIT_GENERATOR(can_be_null)
SQLPP_RECURSIVE_TRAIT_GENERATOR(contains_aggregate_function) SQLPP_RECURSIVE_TRAIT_GENERATOR(contains_aggregate_function)
template <typename ValueType, typename T> template <typename ValueType, typename T>

View File

@ -60,7 +60,6 @@ namespace sqlpp
using _traits = make_traits<text, tag::is_expression, tag::is_selectable>; using _traits = make_traits<text, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr>; using _nodes = detail::type_vector<Expr>;
using _can_be_null = can_be_null_t<Expr>;
using _auto_alias_t = upper_alias_t; using _auto_alias_t = upper_alias_t;

View File

@ -703,8 +703,6 @@ def createHeader():
) )
print("You can disable this warning using -no-timestamp-warning") print("You can disable this warning using -no-timestamp-warning")
noTimestampWarning = True noTimestampWarning = True
traitslist = ["::sqlpp::" + columnType]
columnCanBeNull = not column.notNull and not column.isPrimaryKey
print(" struct " + columnClass, file=header) print(" struct " + columnClass, file=header)
print(" {", file=header) print(" {", file=header)
print(" struct _alias_t", file=header) print(" struct _alias_t", file=header)
@ -735,21 +733,19 @@ def createHeader():
) )
print(" };", file=header) print(" };", file=header)
print(" };", file=header) print(" };", file=header)
hasAutoValue = column.hasAutoValue or (autoId and sqlColumnName == "id") columnCanBeNull = not column.notNull and not column.isPrimaryKey
if columnCanBeNull:
traitslist.append("::sqlpp::tag::can_be_null")
if column.hasDefaultValue or hasAutoValue or columnCanBeNull:
traitslist.append("::sqlpp::tag::has_default")
if columnCanBeNull: if columnCanBeNull:
print(" using value_type = ::sqlpp::compat::optional<::sqlpp::" + columnType + ">;", file=header) print(" using value_type = ::sqlpp::compat::optional<::sqlpp::" + columnType + ">;", file=header)
else: else:
print(" using value_type = ::sqlpp::" + columnType + ";", file=header) print(" using value_type = ::sqlpp::" + columnType + ";", file=header)
print( columnHasDefault = column.hasDefaultValue or \
" using _traits = ::sqlpp::make_traits<" columnCanBeNull or \
+ ", ".join(traitslist) column.hasAutoValue or \
+ ">;", (autoId and sqlColumnName == "id")
file=header, 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(" };", file=header)
print(" } // namespace " + tableNamespace, file=header) print(" } // namespace " + tableNamespace, file=header)
print("", file=header) print("", file=header)

View File

@ -44,12 +44,12 @@ namespace
int main() int main()
{ {
static_assert(not sqlpp::can_be_null_t<decltype(bar.id)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(bar.id)>::value, "");
static_assert(sqlpp::can_be_null_t<decltype(foo.doubleN)>::value, ""); static_assert(sqlpp::can_be_null<decltype(foo.doubleN)>::value, "");
static_assert(not sqlpp::can_be_null_t<decltype(foo.textNnD)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(foo.textNnD)>::value, "");
static_assert(not sqlpp::can_be_null_t<decltype(bar.boolNn)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(bar.boolNn)>::value, "");
const auto seven = sqlpp::value(7).as(sqlpp::alias::s); const auto seven = sqlpp::value(7).as(sqlpp::alias::s);
static_assert(not sqlpp::can_be_null_t<decltype(seven)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(seven)>::value, "");
// Select non-optional column or alias of it. // 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)) 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 // aggregates of nullable values
#warning use a nullable value #warning use a nullable value
const auto a = bar.id; const auto a = bar.id;
static_assert(not sqlpp::can_be_null_t<decltype(bar.id)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(bar.id)>::value, "");
static_assert(not sqlpp::can_be_null_t<decltype(a)>::value, ""); static_assert(not sqlpp::can_be_null<decltype(a)>::value, "");
const auto rows = db(select(count(a), avg(a), max(a), min(a), sum(a)).from(bar).unconditionally()); const auto rows = db(select(count(a), avg(a), max(a), min(a), sum(a)).from(bar).unconditionally());
auto& x = rows.front(); auto& x = rows.front();
static_assert(not is_optional<decltype(x.count)>::value, ""); static_assert(not is_optional<decltype(x.count)>::value, "");
@ -332,8 +332,8 @@ int main()
{ {
// aggregates of nullable values // aggregates of nullable values
const auto o = foo.doubleN; const auto o = foo.doubleN;
static_assert(sqlpp::can_be_null_t<decltype(foo.doubleN)>::value, ""); static_assert(sqlpp::can_be_null<decltype(foo.doubleN)>::value, "");
static_assert(sqlpp::can_be_null_t<decltype(o)>::value, ""); static_assert(sqlpp::can_be_null<decltype(o)>::value, "");
const auto rows = db(select(count(o), avg(o), max(o), min(o), sum(o)).from(foo).unconditionally()); const auto rows = db(select(count(o), avg(o), max(o), min(o), sum(o)).from(foo).unconditionally());
auto& x = rows.front(); auto& x = rows.front();
static_assert(not is_optional<decltype(x.count)>::value, ""); static_assert(not is_optional<decltype(x.count)>::value, "");

View File

@ -25,7 +25,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::integer; 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 struct TextNnD
{ {
@ -42,7 +42,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::text; 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 struct IntN
{ {
@ -59,7 +59,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::integer>; 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 struct DoubleN
{ {
@ -76,7 +76,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::floating_point>; 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 struct UIntN
{ {
@ -93,7 +93,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::integer_unsigned>; 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 struct BlobN
{ {
@ -110,7 +110,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::blob>; 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_ } // namespace TabFoo_
@ -152,7 +152,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::integer; 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 struct TextN
{ {
@ -169,7 +169,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::text>; 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 struct BoolNn
{ {
@ -186,7 +186,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::boolean; using value_type = ::sqlpp::boolean;
using _traits = ::sqlpp::make_traits<::sqlpp::boolean>; using has_default_value = std::false_type;
}; };
struct IntN struct IntN
{ {
@ -203,7 +203,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::integer>; 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_ } // namespace TabBar_
@ -243,7 +243,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::integer; 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 struct DayPointN
{ {
@ -260,7 +260,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::day_point>; 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 struct TimePointN
{ {
@ -277,7 +277,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::time_point>; 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 struct TimeOfDayN
{ {
@ -294,7 +294,7 @@ namespace test
}; };
}; };
using value_type = ::sqlpp::compat::optional<::sqlpp::time_of_day>; 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_ } // namespace TabDateTime_