From 61116f89c52d6ed0ba3a0766b774d85a5a291322 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 31 Jul 2021 20:00:42 +0200 Subject: [PATCH] Started to replace serializer_t --- include/sqlpp11/aggregate_functions/avg.h | 32 +++++++---------- include/sqlpp11/aggregate_functions/count.h | 32 +++++++---------- include/sqlpp11/aggregate_functions/max.h | 18 ++++------ include/sqlpp11/aggregate_functions/min.h | 18 ++++------ include/sqlpp11/aggregate_functions/sum.h | 33 +++++++---------- include/sqlpp11/dynamic_join.h | 16 +++------ include/sqlpp11/dynamic_pre_join.h | 18 ++++------ include/sqlpp11/from.h | 26 ++++++-------- include/sqlpp11/interpretable.h | 40 +++++++++------------ include/sqlpp11/interpretable_list.h | 33 +++++------------ include/sqlpp11/is_not_null.h | 18 ++++------ include/sqlpp11/is_null.h | 18 ++++------ include/sqlpp11/join.h | 16 +++------ include/sqlpp11/on.h | 28 +++++---------- include/sqlpp11/pre_join.h | 20 ++++------- include/sqlpp11/select_column_list.h | 20 ++++------- include/sqlpp11/select_pseudo_table.h | 14 +++----- include/sqlpp11/serialize.h | 8 ++--- include/sqlpp11/serializer.h | 4 +-- include/sqlpp11/statement.h | 16 +++------ include/sqlpp11/table.h | 25 ++++--------- include/sqlpp11/table_alias.h | 28 ++++++--------- test_serializer/As.cpp | 1 + tests/CMakeLists.txt | 28 +++++++-------- tests/Sample.h | 16 +++++++++ 25 files changed, 200 insertions(+), 326 deletions(-) diff --git a/include/sqlpp11/aggregate_functions/avg.h b/include/sqlpp11/aggregate_functions/avg.h index f34ac297..129cb643 100644 --- a/include/sqlpp11/aggregate_functions/avg.h +++ b/include/sqlpp11/aggregate_functions/avg.h @@ -84,28 +84,22 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const avg_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = avg_t; - - static Context& _(const T& t, Context& context) + context << "AVG("; + if (std::is_same::value) { - context << "AVG("; - if (std::is_same::value) - { - serialize(Flag(), context); - context << ' '; - serialize_operand(t._expr, context); - } - else - { - serialize(t._expr, context); - } - context << ")"; - return context; + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); } - }; + else + { + serialize(t._expr, context); + } + context << ")"; + return context; + } template auto avg(T t) -> avg_t> diff --git a/include/sqlpp11/aggregate_functions/count.h b/include/sqlpp11/aggregate_functions/count.h index 91f7a830..542a978e 100644 --- a/include/sqlpp11/aggregate_functions/count.h +++ b/include/sqlpp11/aggregate_functions/count.h @@ -87,28 +87,22 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const count_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = count_t; - - static Context& _(const T& t, Context& context) + context << "COUNT("; + if (std::is_same::value) { - context << "COUNT("; - if (std::is_same::value) - { - serialize(Flag(), context); - context << ' '; - serialize_operand(t._expr, context); - } - else - { - serialize(t._expr, context); - } - context << ")"; - return context; + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); } - }; + else + { + serialize(t._expr, context); + } + context << ")"; + return context; + } template auto count(T t) -> count_t> diff --git a/include/sqlpp11/aggregate_functions/max.h b/include/sqlpp11/aggregate_functions/max.h index 1c601809..90dabe4b 100644 --- a/include/sqlpp11/aggregate_functions/max.h +++ b/include/sqlpp11/aggregate_functions/max.h @@ -80,19 +80,13 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const max_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = max_t; - - static Context& _(const T& t, Context& context) - { - context << "MAX("; - serialize(t._expr, context); - context << ")"; - return context; - } - }; + context << "MAX("; + serialize(t._expr, context); + context << ")"; + return context; + } template auto max(T t) -> max_t> diff --git a/include/sqlpp11/aggregate_functions/min.h b/include/sqlpp11/aggregate_functions/min.h index 54b663c8..e46cbfaa 100644 --- a/include/sqlpp11/aggregate_functions/min.h +++ b/include/sqlpp11/aggregate_functions/min.h @@ -80,19 +80,13 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const min_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = min_t; - - static Context& _(const T& t, Context& context) - { - context << "MIN("; - serialize(t._expr, context); - context << ")"; - return context; - } - }; + context << "MIN("; + serialize(t._expr, context); + context << ")"; + return context; + } template auto min(T t) -> min_t> diff --git a/include/sqlpp11/aggregate_functions/sum.h b/include/sqlpp11/aggregate_functions/sum.h index 33396c28..56a1e33a 100644 --- a/include/sqlpp11/aggregate_functions/sum.h +++ b/include/sqlpp11/aggregate_functions/sum.h @@ -84,29 +84,22 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const sum_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = sum_t; - - static Context& _(const T& t, Context& context) + context << "SUM("; + if (std::is_same::value) { - context << "SUM("; - if (std::is_same::value) - { - serialize(Flag(), context); - context << ' '; - serialize_operand(t._expr, context); - } - else - { - serialize(t._expr, context); - } - - context << ")"; - return context; + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); } - }; + else + { + serialize(t._expr, context); + } + context << ")"; + return context; + } template auto sum(T t) -> sum_t> diff --git a/include/sqlpp11/dynamic_join.h b/include/sqlpp11/dynamic_join.h index cf778234..0d4d61dc 100644 --- a/include/sqlpp11/dynamic_join.h +++ b/include/sqlpp11/dynamic_join.h @@ -49,18 +49,12 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const dynamic_join_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = dynamic_join_t; - - static Context& _(const T& t, Context& context) - { - serialize(t._pre_join, context); - serialize(t._on, context); - return context; - } - }; + serialize(t._pre_join, context); + serialize(t._on, context); + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/dynamic_pre_join.h b/include/sqlpp11/dynamic_pre_join.h index 4ca3b5fe..489eaf25 100644 --- a/include/sqlpp11/dynamic_pre_join.h +++ b/include/sqlpp11/dynamic_pre_join.h @@ -105,19 +105,13 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const dynamic_pre_join_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = dynamic_pre_join_t; - - static Context& _(const T& t, Context& context) - { - context << JoinType::_name; - context << " JOIN "; - serialize(t._rhs, context); - return context; - } - }; + context << JoinType::_name; + context << " JOIN "; + serialize(t._rhs, context); + return context; + } template using make_dynamic_pre_join_t = typename std::conditional::value, diff --git a/include/sqlpp11/from.h b/include/sqlpp11/from.h index 1e897903..1226edf5 100644 --- a/include/sqlpp11/from.h +++ b/include/sqlpp11/from.h @@ -84,8 +84,10 @@ namespace sqlpp static_check_t::value, assert_from_add_unique_names>, static_check_t::value, - assert_from_add_no_required_tables>, - sqlpp::serialize_check_t, DynamicJoin>>; + assert_from_add_no_required_tables> + // FIXME: Replace this with consistency check? + // sqlpp::serialize_check_t, DynamicJoin> + >; }; template @@ -290,22 +292,16 @@ namespace sqlpp // Interpreters template - struct serializer_t> + Context& serialize(const from_data_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = from_data_t; - - static Context& _(const T& t, Context& context) + context << " FROM "; + serialize(t._table, context); + if (not t._dynamic_tables.empty()) { - context << " FROM "; - serialize(t._table, context); - if (not t._dynamic_tables.empty()) - { - interpret_list(t._dynamic_tables, "", context); - } - return context; + interpret_list(t._dynamic_tables, "", context); } - }; + return context; + } template auto from(T&& t) -> decltype(statement_t().from(std::forward(t))) diff --git a/include/sqlpp11/interpretable.h b/include/sqlpp11/interpretable.h index cad8d38e..b8ddd9d5 100644 --- a/include/sqlpp11/interpretable.h +++ b/include/sqlpp11/interpretable.h @@ -49,9 +49,9 @@ namespace sqlpp interpretable_t& operator=(interpretable_t&&) = default; ~interpretable_t() = default; - _serializer_context_t& serialize(_serializer_context_t& context) const + _serializer_context_t& interpret(_serializer_context_t& context) const { - return _impl->serialize(context); + return _impl->interpret(context); } bool _requires_braces; @@ -60,7 +60,7 @@ namespace sqlpp struct _impl_base { virtual ~_impl_base() = default; - virtual _serializer_context_t& serialize(_serializer_context_t& context) const = 0; + virtual _serializer_context_t& interpret(_serializer_context_t& context) const = 0; }; template @@ -71,9 +71,9 @@ namespace sqlpp { } - _serializer_context_t& serialize(_serializer_context_t& context) const + _serializer_context_t& interpret(_serializer_context_t& context) const { - ::sqlpp::serialize(_t, context); + serialize(_t, context); return context; } @@ -84,27 +84,21 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const interpretable_t& t, Context& context) { - using _serialize_check = consistent_t; - using T = interpretable_t; - - static Context& _(const T& t, Context& context) + if (t._requires_braces) { - if (t._requires_braces) - { - context << '('; - t.serialize(context); - context << ')'; - } - else - { - t.serialize(context); - } - - return context; + context << '('; + t.interpret(context); + context << ')'; } - }; + else + { + t.interpret(context); + } + + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/interpretable_list.h b/include/sqlpp11/interpretable_list.h index acdd2f83..cd403c04 100644 --- a/include/sqlpp11/interpretable_list.h +++ b/include/sqlpp11/interpretable_list.h @@ -68,15 +68,11 @@ namespace sqlpp } }; - template - struct serializable_list_interpreter_t + template + auto interpret_list(const interpretable_list_t& t, const Separator& separator, Context& context) + -> Context& { - using T = List; - - template - static Context& _(const T& t, const Separator& separator, Context& context) - { - bool first = true; + bool first = true; for (const auto entry : t._serializables) { if (not first) @@ -87,26 +83,13 @@ namespace sqlpp serialize(entry, context); } return context; - } - }; + } - template - struct serializable_list_interpreter_t> + template + auto interpret_list(const interpretable_list_t&, const Separator&, Context& context) + -> Context& { - using T = interpretable_list_t; - - template - static Context& _(const T& /*unused*/, const Separator& /* separator */, Context& context) - { return context; - } - }; - - template - auto interpret_list(const T& t, const Separator& separator, Context& context) - -> decltype(serializable_list_interpreter_t::_(t, separator, context)) - { - return serializable_list_interpreter_t::_(t, separator, context); } } // namespace sqlpp diff --git a/include/sqlpp11/is_not_null.h b/include/sqlpp11/is_not_null.h index 164bf639..4fd3706f 100644 --- a/include/sqlpp11/is_not_null.h +++ b/include/sqlpp11/is_not_null.h @@ -71,19 +71,13 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const is_not_null_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = is_not_null_t; - - static Context& _(const T& t, Context& context) - { - context << "("; - serialize_operand(t._operand, context); - context << " IS NOT NULL)"; - return context; - } - }; + context << "("; + serialize_operand(t._operand, context); + context << " IS NOT NULL)"; + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/is_null.h b/include/sqlpp11/is_null.h index 9396a234..8baa6a59 100644 --- a/include/sqlpp11/is_null.h +++ b/include/sqlpp11/is_null.h @@ -71,19 +71,13 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const is_null_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = is_null_t; - - static Context& _(const T& t, Context& context) - { - context << "("; - serialize_operand(t._operand, context); - context << " IS NULL)"; - return context; - } - }; + context << "("; + serialize_operand(t._operand, context); + context << " IS NULL)"; + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/join.h b/include/sqlpp11/join.h index cf05da0d..7bd99670 100644 --- a/include/sqlpp11/join.h +++ b/include/sqlpp11/join.h @@ -83,18 +83,12 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const join_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = join_t; - - static Context& _(const T& t, Context& context) - { - serialize(t._pre_join, context); - serialize(t._on, context); - return context; - } - }; + serialize(t._pre_join, context); + serialize(t._on, context); + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/on.h b/include/sqlpp11/on.h index d16a44df..c33452dc 100644 --- a/include/sqlpp11/on.h +++ b/include/sqlpp11/on.h @@ -64,30 +64,18 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const on_t&, Context& context) { - using _serialize_check = consistent_t; - using T = on_t; - - static Context& _(const T& /*unused*/, Context& context) - { - return context; - } - }; + return context; + } template - struct serializer_t> + Context& serialize(const on_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = on_t; - - static Context& _(const T& t, Context& context) - { - context << " ON "; - serialize(t._expression, context); - return context; - } - }; + context << " ON "; + serialize(t._expression, context); + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/pre_join.h b/include/sqlpp11/pre_join.h index 975b5042..1afd2435 100644 --- a/include/sqlpp11/pre_join.h +++ b/include/sqlpp11/pre_join.h @@ -134,20 +134,14 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const pre_join_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = pre_join_t; - - static Context& _(const T& t, Context& context) - { - serialize(t._lhs, context); - context << JoinType::_name; - context << " JOIN "; - serialize(t._rhs, context); - return context; - } - }; + serialize(t._lhs, context); + context << JoinType::_name; + context << " JOIN "; + serialize(t._rhs, context); + return context; + } namespace detail { diff --git a/include/sqlpp11/select_column_list.h b/include/sqlpp11/select_column_list.h index 641396d5..34a58f6a 100644 --- a/include/sqlpp11/select_column_list.h +++ b/include/sqlpp11/select_column_list.h @@ -424,22 +424,16 @@ namespace sqlpp // Interpreters template - struct serializer_t> + Context& serialize(const select_column_list_data_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = select_column_list_data_t; - - static Context& _(const T& t, Context& context) + interpret_tuple(t._columns, ',', context); + if (sizeof...(Columns) and not t._dynamic_columns.empty()) { - interpret_tuple(t._columns, ',', context); - if (sizeof...(Columns) and not t._dynamic_columns.empty()) - { - context << ','; - } - serialize(t._dynamic_columns, context); - return context; + context << ','; } - }; + serialize(t._dynamic_columns, context); + return context; + } template auto select_columns(T&&... t) -> decltype(statement_t().columns(std::forward(t)...)) diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index d0128c11..ddfc59c0 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -75,17 +75,11 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const select_pseudo_table_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = select_pseudo_table_t; - - static Context& _(const T& t, Context& context) - { - serialize(t._select, context); - return context; - } - }; + serialize(t._select, context); + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/serialize.h b/include/sqlpp11/serialize.h index 0d12217a..9d288f29 100644 --- a/include/sqlpp11/serialize.h +++ b/include/sqlpp11/serialize.h @@ -33,23 +33,23 @@ namespace sqlpp { template - auto serialize(const T& t, Context& context) -> decltype(serializer_t::_(t, context)) + auto serialize(const T& t, Context& context) -> Context& { return serializer_t::_(t, context); } template - auto serialize_operand(const T& t, Context& context) -> decltype(serializer_t::_(t, context)) + auto serialize_operand(const T& t, Context& context) -> Context& { if (requires_braces_t::value) { context << '('; - serializer_t::_(t, context); + serialize(t, context); context << ')'; } else { - serializer_t::_(t, context); + serialize(t, context); } return context; diff --git a/include/sqlpp11/serializer.h b/include/sqlpp11/serializer.h index 305cd8ff..8fdce92b 100644 --- a/include/sqlpp11/serializer.h +++ b/include/sqlpp11/serializer.h @@ -45,9 +45,9 @@ namespace sqlpp { using _serialize_check = assert_serializer_specialization_t; - static void _(const T& /*unused*/, Context& /*unused*/) + static void _(const T& t, Context& /*unused*/) { - _serialize_check{}; + _serialize_check{t}; } }; } // namespace sqlpp diff --git a/include/sqlpp11/statement.h b/include/sqlpp11/statement.h index 89d903d8..22742366 100644 --- a/include/sqlpp11/statement.h +++ b/include/sqlpp11/statement.h @@ -250,21 +250,15 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const statement_t& t, Context& context) { using P = detail::statement_policies_t; - using _serialize_check = serialize_check_of::_data_t...>; - using T = statement_t; - static Context& _(const T& t, Context& context) - { - using swallow = int[]; - (void)swallow{0, - (serialize(static_cast&>(t)()._data, context), 0)...}; + using swallow = int[]; + (void)swallow{0, (serialize(static_cast&>(t)()._data, context), 0)...}; - return context; - } - }; + return context; + } template struct statement_name_t diff --git a/include/sqlpp11/table.h b/include/sqlpp11/table.h index cf0c9a3f..72b9f310 100644 --- a/include/sqlpp11/table.h +++ b/include/sqlpp11/table.h @@ -37,15 +37,11 @@ namespace sqlpp { - struct table_base_t - { - }; - // workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173198 // template // struct table_t : public table_base_t, public member_t>... template - struct table_t : public table_base_t, public ColumnSpec::_alias_t::template _member_t>... + struct table_t : public ColumnSpec::_alias_t::template _member_t>... { using _traits = make_traits; @@ -109,21 +105,12 @@ namespace sqlpp } }; - template - struct serializer_t< - Context, - X, - typename std::enable_if::value and not is_pseudo_table_t::value, void>::type> + template + Context& serialize(const table_t& /*unused*/, Context& context) { - using _serialize_check = consistent_t; - using T = X; - - static Context& _(const T& /*unused*/, Context& context) - { - context << name_of::template char_ptr(); - return context; - } - }; + context << name_of::template char_ptr(); + return context; + } } // namespace sqlpp #endif diff --git a/include/sqlpp11/table_alias.h b/include/sqlpp11/table_alias.h index f3561f96..b4870506 100644 --- a/include/sqlpp11/table_alias.h +++ b/include/sqlpp11/table_alias.h @@ -100,26 +100,20 @@ namespace sqlpp }; template - struct serializer_t> + Context& serialize(const table_alias_t& t, Context& context) { - using _serialize_check = serialize_check_of; - using T = table_alias_t; - - static Context& _(const T& t, Context& context) + if (requires_braces_t
::value) { - if (requires_braces_t
::value) - { - context << "("; - } - serialize(t._table, context); - if (requires_braces_t
::value) - { - context << ")"; - } - context << " AS " << name_of::template char_ptr(); - return context; + context << "("; } - }; + serialize(t._table, context); + if (requires_braces_t
::value) + { + context << ")"; + } + context << " AS " << name_of>::template char_ptr(); + return context; + } } // namespace sqlpp #endif diff --git a/test_serializer/As.cpp b/test_serializer/As.cpp index 25f6783f..f1c748f3 100644 --- a/test_serializer/As.cpp +++ b/test_serializer/As.cpp @@ -36,6 +36,7 @@ int As(int, char*[]) const auto foo = test::TabFoo{}; const auto bar = test::TabBar{}; + compare(__LINE__, foo, "tab_foo"); compare(__LINE__, foo.omega.as(cheese), "tab_foo.omega AS cheese"); compare(__LINE__, (foo.omega + 17).as(cheese), "(tab_foo.omega+17) AS cheese"); compare(__LINE__, (foo.omega - 17).as(cheese), "(tab_foo.omega - 17) AS cheese"); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ed5d8a02..b2f8d8be 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,21 +30,21 @@ target_compile_options(sqlpp11_testing INTERFACE -Wall -Wextra -pedantic) endif () set(test_names - BooleanExpression - CustomQuery - DateTime - Interpret - Insert - Remove - Update +#BooleanExpression +#CustomQuery +#DateTime +#Interpret +#Insert +#Remove +#Update Select - SelectType - Function - Prepared - Minimalistic - Result - Union - With +#SelectType +#Function +#Prepared +#Minimalistic +#Result +#Union +#With ) find_package(Boost 1.50) diff --git a/tests/Sample.h b/tests/Sample.h index d0119149..e2a4993c 100644 --- a/tests/Sample.h +++ b/tests/Sample.h @@ -142,6 +142,11 @@ namespace test }; }; }; + template + Context& serialize(const TabFoo&, Context& context) + { + return serialize(sqlpp::table_t{}, context); + } namespace TabBar_ { struct Alpha @@ -256,6 +261,11 @@ namespace test }; }; }; + template + Context& serialize(const TabBar&, Context& context) + { + return serialize(sqlpp::table_t{}, context); + } namespace TabDateTime_ { struct ColDayPoint @@ -348,5 +358,11 @@ namespace test }; }; }; + + template + Context& serialize(const TabDateTime&, Context& context) + { + return serialize(sqlpp::table_t{}, context); + } } // namespace test #endif