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

Add tests for order_by

Also improve handling of group_by in case all columns are false dynamic.
This commit is contained in:
Roland Bock 2024-09-29 11:06:34 +02:00
parent 223bf2795f
commit e3eb52a080
5 changed files with 94 additions and 7 deletions

View File

@ -153,7 +153,13 @@ namespace sqlpp
template <typename Context, typename... Columns> template <typename Context, typename... Columns>
auto to_sql_string(Context& context, const group_by_data_t<Columns...>& t) -> std::string auto to_sql_string(Context& context, const group_by_data_t<Columns...>& 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 <typename... T> template <typename... T>

View File

@ -90,7 +90,7 @@ namespace sqlpp
assert_order_by_args_are_sort_order_expressions_t>>; assert_order_by_args_are_sort_order_expressions_t>>;
}; };
template <typename... Exprs> template <typename... Exprs>
using check_order_by_t = typename check_order_by<Exprs...>::type; using check_order_by_t = typename check_order_by<remove_dynamic_t<Exprs>...>::type;
// NO ORDER BY YET // NO ORDER BY YET
struct no_order_by_t struct no_order_by_t
@ -148,7 +148,13 @@ namespace sqlpp
template <typename Context, typename... Expressions> template <typename Context, typename... Expressions>
auto to_sql_string(Context& context, const order_by_data_t<Expressions...>& t) -> std::string auto to_sql_string(Context& context, const order_by_data_t<Expressions...>& 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 <typename... T> template <typename... T>

View File

@ -36,6 +36,7 @@ create_test(insert)
create_test(insert_columns) create_test(insert_columns)
create_test(insert_default_values) create_test(insert_default_values)
create_test(insert_set) create_test(insert_set)
create_test(order_by)
create_test(select_columns) create_test(select_columns)
create_test(select_flags) create_test(select_flags)
create_test(update_set) create_test(update_set)

View File

@ -44,12 +44,12 @@ int main(int, char* [])
// Multiple plain columns. // 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"); 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
// Single dynamic column (this is odd)
SQLPP_COMPARE(group_by(dynamic(true, foo.id)), " GROUP BY tab_foo.id"); 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(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, 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"); 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), 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), 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 // Single declared column
SQLPP_COMPARE(group_by(declare_group_by_column(val)), " GROUP BY 17"); SQLPP_COMPARE(group_by(declare_group_by_column(val)), " GROUP BY 17");
#warning: Do we really want these extra parentheses? #warning: Do we really want these extra parentheses?

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}