From e3eb52a08039c895b4717b4d32b0f8ed38da1c05 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 29 Sep 2024 11:06:34 +0200 Subject: [PATCH] Add tests for order_by Also improve handling of group_by in case all columns are false dynamic. --- include/sqlpp11/core/clause/group_by.h | 8 ++- include/sqlpp11/core/clause/order_by.h | 10 ++- tests/core/serialize/clause/CMakeLists.txt | 1 + tests/core/serialize/clause/group_by.cpp | 10 +-- tests/core/serialize/clause/order_by.cpp | 72 ++++++++++++++++++++++ 5 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 tests/core/serialize/clause/order_by.cpp diff --git a/include/sqlpp11/core/clause/group_by.h b/include/sqlpp11/core/clause/group_by.h index 69a82591..f9b225aa 100644 --- a/include/sqlpp11/core/clause/group_by.h +++ b/include/sqlpp11/core/clause/group_by.h @@ -153,7 +153,13 @@ namespace sqlpp template auto to_sql_string(Context& context, const group_by_data_t& t) -> std::string { - return " GROUP BY " + tuple_to_sql_string(context, t._columns, tuple_operand_no_dynamic{", "}); + const auto columns = tuple_to_sql_string(context, t._columns, tuple_operand_no_dynamic{", "}); + + if (columns.empty()) { + return ""; + } + + return " GROUP BY " + columns; } template diff --git a/include/sqlpp11/core/clause/order_by.h b/include/sqlpp11/core/clause/order_by.h index 33190dce..41f5ff83 100644 --- a/include/sqlpp11/core/clause/order_by.h +++ b/include/sqlpp11/core/clause/order_by.h @@ -90,7 +90,7 @@ namespace sqlpp assert_order_by_args_are_sort_order_expressions_t>>; }; template - using check_order_by_t = typename check_order_by::type; + using check_order_by_t = typename check_order_by...>::type; // NO ORDER BY YET struct no_order_by_t @@ -148,7 +148,13 @@ namespace sqlpp template auto to_sql_string(Context& context, const order_by_data_t& t) -> std::string { - return " ORDER BY " + tuple_to_sql_string(context, t._expressions, tuple_operand{", "}); + const auto columns = tuple_to_sql_string(context, t._expressions, tuple_operand_no_dynamic{", "}); + + if (columns.empty()) { + return ""; + } + + return " ORDER BY " + columns; } template diff --git a/tests/core/serialize/clause/CMakeLists.txt b/tests/core/serialize/clause/CMakeLists.txt index 10d9d3b5..b498fbb6 100644 --- a/tests/core/serialize/clause/CMakeLists.txt +++ b/tests/core/serialize/clause/CMakeLists.txt @@ -36,6 +36,7 @@ create_test(insert) create_test(insert_columns) create_test(insert_default_values) create_test(insert_set) +create_test(order_by) create_test(select_columns) create_test(select_flags) create_test(update_set) diff --git a/tests/core/serialize/clause/group_by.cpp b/tests/core/serialize/clause/group_by.cpp index b0c2ca62..f95fd688 100644 --- a/tests/core/serialize/clause/group_by.cpp +++ b/tests/core/serialize/clause/group_by.cpp @@ -44,12 +44,12 @@ int main(int, char* []) // Multiple plain columns. SQLPP_COMPARE(group_by(foo.id, foo.textNnD, foo.boolN), " GROUP BY tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n"); -#warning: Should we prevent the first column to be dynamic? Might be easier to just document it. - // Single dynamic column (this is odd) + // Single dynamic column SQLPP_COMPARE(group_by(dynamic(true, foo.id)), " GROUP BY tab_foo.id"); - SQLPP_COMPARE(group_by(dynamic(false, foo.id)), " GROUP BY "); // not good +#warning: document that GROUP BY gets omitted if all columns are dynamic false + SQLPP_COMPARE(group_by(dynamic(false, foo.id)), ""); - // Multiple dynamic columns (this is odd if all are dynamic) + // Multiple dynamic columns (including all dynamic) SQLPP_COMPARE(group_by(dynamic(true, foo.id), foo.textNnD, foo.boolN), " GROUP BY tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n"); SQLPP_COMPARE(group_by(foo.id, dynamic(true, foo.textNnD), foo.boolN), " GROUP BY tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n"); SQLPP_COMPARE(group_by(foo.id, foo.textNnD, dynamic(true, foo.boolN)), " GROUP BY tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n"); @@ -62,6 +62,8 @@ int main(int, char* []) SQLPP_COMPARE(group_by(dynamic(false, foo.id), foo.textNnD, dynamic(false, foo.boolN)), " GROUP BY tab_foo.text_nn_d"); SQLPP_COMPARE(group_by(dynamic(false, foo.id), dynamic(false, foo.textNnD), foo.boolN), " GROUP BY tab_foo.bool_n"); + SQLPP_COMPARE(group_by(dynamic(false, foo.id), dynamic(false, foo.textNnD), dynamic(false, foo.boolN)), ""); + // Single declared column SQLPP_COMPARE(group_by(declare_group_by_column(val)), " GROUP BY 17"); #warning: Do we really want these extra parentheses? diff --git a/tests/core/serialize/clause/order_by.cpp b/tests/core/serialize/clause/order_by.cpp new file mode 100644 index 00000000..265f31bd --- /dev/null +++ b/tests/core/serialize/clause/order_by.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Sample.h" +#include "../compare.h" +#include + +SQLPP_CREATE_NAME_TAG(v); + +int main(int, char* []) +{ + const auto val = sqlpp::value(17); + const auto expr = sqlpp::value(17) + 4; + + const auto foo = test::TabFoo{}; + + // Plain columns. + SQLPP_COMPARE(order_by(foo.id.asc()), " ORDER BY tab_foo.id ASC"); + SQLPP_COMPARE(order_by(foo.textNnD.asc()), " ORDER BY tab_foo.text_nn_d ASC"); + SQLPP_COMPARE(order_by(foo.boolN.asc()), " ORDER BY tab_foo.bool_n ASC"); + + SQLPP_COMPARE(order_by(foo.id.desc()), " ORDER BY tab_foo.id DESC"); + SQLPP_COMPARE(order_by(foo.textNnD.desc()), " ORDER BY tab_foo.text_nn_d DESC"); + SQLPP_COMPARE(order_by(foo.boolN.desc()), " ORDER BY tab_foo.bool_n DESC"); + + // Multiple plain columns. + SQLPP_COMPARE(order_by(foo.id.asc(), foo.textNnD.desc(), foo.boolN.desc()), " ORDER BY tab_foo.id ASC, tab_foo.text_nn_d DESC, tab_foo.bool_n DESC"); + + // Single dynamic column (this is odd) + SQLPP_COMPARE(order_by(dynamic(true, foo.id.asc())), " ORDER BY tab_foo.id ASC"); +#warning: document that ORDER BY gets omitted if all columns are dynamic false + SQLPP_COMPARE(order_by(dynamic(false, foo.id.asc())), ""); + + // Multiple dynamic columns (this is odd if all are dynamic) + SQLPP_COMPARE(order_by(dynamic(true, foo.id.asc()), foo.textNnD.asc(), foo.boolN.asc()), " ORDER BY tab_foo.id ASC, tab_foo.text_nn_d ASC, tab_foo.bool_n ASC"); + SQLPP_COMPARE(order_by(foo.id.asc(), dynamic(true, foo.textNnD.asc()), foo.boolN.asc()), " ORDER BY tab_foo.id ASC, tab_foo.text_nn_d ASC, tab_foo.bool_n ASC"); + SQLPP_COMPARE(order_by(foo.id.asc(), foo.textNnD.asc(), dynamic(true, foo.boolN.asc())), " ORDER BY tab_foo.id ASC, tab_foo.text_nn_d ASC, tab_foo.bool_n ASC"); + + SQLPP_COMPARE(order_by(dynamic(false, foo.id.asc()), foo.textNnD.asc(), foo.boolN.asc()), " ORDER BY tab_foo.text_nn_d ASC, tab_foo.bool_n ASC"); + SQLPP_COMPARE(order_by(foo.id.asc(), dynamic(false, foo.textNnD.asc()), foo.boolN.asc()), " ORDER BY tab_foo.id ASC, tab_foo.bool_n ASC"); + SQLPP_COMPARE(order_by(foo.id.asc(), foo.textNnD.asc(), dynamic(false, foo.boolN.asc())), " ORDER BY tab_foo.id ASC, tab_foo.text_nn_d ASC"); + + SQLPP_COMPARE(order_by(foo.id.asc(), dynamic(false, foo.textNnD.asc()), dynamic(false, foo.boolN.asc())), " ORDER BY tab_foo.id ASC"); + SQLPP_COMPARE(order_by(dynamic(false, foo.id.asc()), foo.textNnD.asc(), dynamic(false, foo.boolN.asc())), " ORDER BY tab_foo.text_nn_d ASC"); + SQLPP_COMPARE(order_by(dynamic(false, foo.id.asc()), dynamic(false, foo.textNnD.asc()), foo.boolN.asc()), " ORDER BY tab_foo.bool_n ASC"); + + SQLPP_COMPARE(order_by(dynamic(false, foo.id.asc()), dynamic(false, foo.textNnD.asc()), dynamic(false, foo.boolN.asc())), ""); + + return 0; +}