From 8764f3862ef86f1dd479683174862279eb41b34c Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 14 Sep 2024 12:23:22 +0200 Subject: [PATCH] Add dynamic select flags --- .../sqlpp11/core/clause/select_flag_list.h | 14 +++++--- include/sqlpp11/core/clause/select_flags.h | 27 +++++----------- include/sqlpp11/core/query/dynamic.h | 12 ++----- include/sqlpp11/core/type_traits.h | 4 ++- tests/core/serialize/CMakeLists.txt | 1 - tests/core/serialize/clause/CMakeLists.txt | 1 + .../select_flags.cpp} | 32 +++++++++++++++---- 7 files changed, 50 insertions(+), 41 deletions(-) rename tests/core/serialize/{SelectFlags.cpp => clause/select_flags.cpp} (58%) diff --git a/include/sqlpp11/core/clause/select_flag_list.h b/include/sqlpp11/core/clause/select_flag_list.h index 42e76052..5577f3a0 100644 --- a/include/sqlpp11/core/clause/select_flag_list.h +++ b/include/sqlpp11/core/clause/select_flag_list.h @@ -81,7 +81,7 @@ namespace sqlpp struct check_select_flags { using type = static_combined_check_t< - static_check_t::value...>::value, assert_select_flags_are_flags_t>>; + static_check_t>::value...>::value, assert_select_flags_are_flags_t>>; }; template using check_select_flags_t = typename check_select_flags::type; @@ -137,13 +137,17 @@ namespace sqlpp template auto to_sql_string(Context& context, const select_flag_list_data_t& t) -> std::string { - return tuple_to_sql_string(context, t._flags, tuple_operand{" "}); + auto flags = tuple_to_sql_string(context, t._flags, tuple_operand_no_dynamic{" "}); + if (flags.empty()) { + return flags; + } + return flags + " "; } - template - auto select_flags(T&& t) -> decltype(statement_t().flags(std::forward(t))) + template + auto select_flags(T... t) -> decltype(statement_t().flags(std::forward(t)...)) { - return statement_t().flags(std::forward(t)); + return statement_t().flags(std::forward(t)...); } } // namespace sqlpp diff --git a/include/sqlpp11/core/clause/select_flags.h b/include/sqlpp11/core/clause/select_flags.h index a446dc3a..ad35769e 100644 --- a/include/sqlpp11/core/clause/select_flags.h +++ b/include/sqlpp11/core/clause/select_flags.h @@ -36,41 +36,30 @@ namespace sqlpp // standard select flags struct all_t { - using _traits = make_traits; - using _nodes = detail::type_vector<>; }; static constexpr all_t all = {}; + template <> + struct is_select_flag : public std::true_type {}; + template auto to_sql_string(Context& , const all_t&) -> std::string { - return "ALL "; + return "ALL"; } struct distinct_t { - using _traits = make_traits; - using _nodes = detail::type_vector<>; }; static constexpr distinct_t distinct = {}; + template <> + struct is_select_flag : public std::true_type {}; + template auto to_sql_string(Context& , const distinct_t&) -> std::string { - return "DISTINCT "; + return "DISTINCT"; } - struct straight_join_t - { - using _traits = make_traits; - using _nodes = detail::type_vector<>; - }; - static constexpr straight_join_t straight_join = {}; - -#warning: Why is this in the select flags? - template - auto to_sql_string(Context& , const straight_join_t&) -> std::string - { - return "STRAIGHT_JOIN"; - } } // namespace sqlpp diff --git a/include/sqlpp11/core/query/dynamic.h b/include/sqlpp11/core/query/dynamic.h index d7e34872..b57dd7c7 100644 --- a/include/sqlpp11/core/query/dynamic.h +++ b/include/sqlpp11/core/query/dynamic.h @@ -97,7 +97,8 @@ namespace sqlpp } template - using check_dynamic_args = ::sqlpp::enable_if_t::value>; + using check_dynamic_args = ::sqlpp::enable_if_t::value or is_table::value or + is_select_flag::value or is_sort_order::value>; template > auto dynamic(bool condition, Expr t) -> dynamic_t @@ -111,18 +112,11 @@ namespace sqlpp return {condition, std::move(t)}; } +#warning: Check if we can dynamically join a table_as? template > auto dynamic(bool condition, sort_order_expression t) -> dynamic_t> { return {condition, std::move(t)}; } - template - struct table_t; - - template - auto dynamic(bool condition, table_t t) -> dynamic_t> - { - return {condition, std::move(t)}; - } } // namespace sqlpp diff --git a/include/sqlpp11/core/type_traits.h b/include/sqlpp11/core/type_traits.h index ee497d46..c136342c 100644 --- a/include/sqlpp11/core/type_traits.h +++ b/include/sqlpp11/core/type_traits.h @@ -216,7 +216,6 @@ namespace sqlpp SQLPP_VALUE_TRAIT_GENERATOR(is_expression) SQLPP_VALUE_TRAIT_GENERATOR(is_multi_expression) SQLPP_VALUE_TRAIT_GENERATOR(is_alias) - SQLPP_VALUE_TRAIT_GENERATOR(is_select_flag) SQLPP_VALUE_TRAIT_GENERATOR(is_union_flag) SQLPP_VALUE_TRAIT_GENERATOR(is_result_field) @@ -564,6 +563,9 @@ namespace sqlpp using type = typename Db::_serializer_context_t; }; + template + struct is_select_flag : public std::false_type {}; + template struct is_sort_order : public std::false_type {}; diff --git a/tests/core/serialize/CMakeLists.txt b/tests/core/serialize/CMakeLists.txt index a2543289..361b9472 100644 --- a/tests/core/serialize/CMakeLists.txt +++ b/tests/core/serialize/CMakeLists.txt @@ -35,7 +35,6 @@ set(test_files ParameterizedVerbatim.cpp SelectAs.cpp SelectColumns.cpp - SelectFlags.cpp TableAlias.cpp Trim.cpp Upper.cpp diff --git a/tests/core/serialize/clause/CMakeLists.txt b/tests/core/serialize/clause/CMakeLists.txt index 91eb57ad..270ac544 100644 --- a/tests/core/serialize/clause/CMakeLists.txt +++ b/tests/core/serialize/clause/CMakeLists.txt @@ -31,4 +31,5 @@ endfunction() create_test(group_by) create_test(select_columns) +create_test(select_flags) diff --git a/tests/core/serialize/SelectFlags.cpp b/tests/core/serialize/clause/select_flags.cpp similarity index 58% rename from tests/core/serialize/SelectFlags.cpp rename to tests/core/serialize/clause/select_flags.cpp index 65ebefff..6faf333a 100644 --- a/tests/core/serialize/SelectFlags.cpp +++ b/tests/core/serialize/clause/select_flags.cpp @@ -23,21 +23,19 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "compare.h" +#include "../compare.h" #include "Sample.h" #include -int SelectFlags(int, char*[]) +int main(int, char*[]) { const auto foo = test::TabFoo{}; - const auto bar = test::TabBar{}; // No flags SQLPP_COMPARE(select(foo.doubleN), "SELECT tab_foo.double_n"); // No flags -#warning: This should work - //SQLPP_COMPARE(sqlpp::select_flags(), ""); + SQLPP_COMPARE(sqlpp::select_flags(), ""); // One flag SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct), "SELECT DISTINCT tab_foo.double_n"); @@ -45,7 +43,29 @@ int SelectFlags(int, char*[]) // One flag SQLPP_COMPARE(select_flags(sqlpp::distinct), "DISTINCT "); -#warning: Add tests for dynamic select flags + // Two flags + SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::all), "DISTINCT ALL "); + + // Two flags + SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::all), "SELECT DISTINCT ALL tab_foo.double_n"); + + // Dynamic flags + + // One flag + SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::dynamic(true, sqlpp::distinct)), "SELECT DISTINCT tab_foo.double_n"); + SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::dynamic(false, sqlpp::distinct)), "SELECT tab_foo.double_n"); + + // One flag + SQLPP_COMPARE(select_flags(sqlpp::dynamic(true, sqlpp::distinct)), "DISTINCT "); + SQLPP_COMPARE(select_flags(sqlpp::dynamic(false, sqlpp::distinct)), ""); + + // Two flags + SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::dynamic(true, sqlpp::all)), "DISTINCT ALL "); + SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::dynamic(false, sqlpp::all)), "DISTINCT "); + + // Two flags + SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::dynamic(true, sqlpp::all)), "SELECT DISTINCT ALL tab_foo.double_n"); + SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::dynamic(false, sqlpp::all)), "SELECT DISTINCT tab_foo.double_n"); return 0; }