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

Add tests and fix serialization of select columns

This commit is contained in:
Roland Bock 2024-08-21 07:27:57 +02:00
parent 92037acd7c
commit 135dceeba3
8 changed files with 145 additions and 25 deletions

View File

@ -330,7 +330,11 @@ namespace sqlpp
template <typename Context, typename... Columns>
auto to_sql_string(Context& context, const std::tuple<Columns...>& t) -> std::string
{
return tuple_to_sql_string(context, t, tuple_operand{", "});
//dynamic(false, foo.id) -> NULL as id
//dynamic(false, foo.id).as(cheesecake) -> NULL AS cheesecake
//max(something) -> max(something) as _max
//max(something.as(cheesecake) -> max(something) AS cheesecake
return tuple_to_sql_string(context, t, tuple_operand_select_column{", "});
}
template <typename... T>

View File

@ -58,6 +58,11 @@ namespace sqlpp
{
};
template <typename Expr>
struct requires_parentheses<group_by_column<Expr>> : public requires_parentheses<Expr>
{
};
template <typename Expr>
struct value_type_of<group_by_column<Expr>> : public value_type_of<Expr>
{

View File

@ -80,28 +80,6 @@ namespace sqlpp
return operand_to_sql_string(context, t._expression) + " AS " + name_to_sql_string(context, name_tag_of_t<AliasProvider>::name);
}
template <typename Expr>
struct dynamic_t;
// In case of dynamic expression, we want
// - dynamic(true, value(10)).as(cheese) -> 10 AS cheese
// - dynamic(NULL, value(10)).as(cheese) -> 10 AS cheese
// - dynamic(true, tab.id).as(cheese) -> tab.id AS cheese
// - dynamic(NULL, tab.id).as(cheese) -> NULL AS cheese
// - dynamic(true, tab.id) -> tab.id
// - dynamic(NULL, tab.id) -> NULL AS id
template <typename Context, typename Expression, typename AliasProvider>
auto to_sql_string(Context& context, const as_expression<dynamic_t<Expression>, AliasProvider>& t) -> std::string
{
if (t._expression._condition)
{
return operand_to_sql_string(context, t._expression._expr) + " AS " +
name_to_sql_string(context, name_tag_of_t<AliasProvider>::name);
}
return to_sql_string(context, ::sqlpp::nullopt) + " AS " +
name_to_sql_string(context, name_tag_of_t<AliasProvider>::name);
}
template <typename Expr, typename AliasProvider>
using check_as_args = ::sqlpp::enable_if_t<
has_value_type<Expr>::value and not is_alias_t<Expr>::value and has_name<AliasProvider>::value
@ -113,10 +91,19 @@ namespace sqlpp
return {std::move(expr)};
}
template <typename Expr>
struct dynamic_t;
template <typename Expr, typename AliasProvider, typename = check_as_args<Expr, AliasProvider>>
constexpr auto as(dynamic_t<Expr> expr, const AliasProvider&) -> as_expression<dynamic_t<Expr>, AliasProvider>
{
return {std::move(expr)};
}
template <typename AliasProvider>
constexpr auto as(sqlpp::nullopt_t expr, const AliasProvider&) -> as_expression<nullopt_t, AliasProvider>
{
return {std::move(expr)};
}
} // namespace sqlpp

View File

@ -266,7 +266,8 @@ namespace sqlpp
template <typename Context>
auto name_to_sql_string(Context& , const char* t) -> std::string
{
#warning: We used to have a version of SQLPP_ALIAS_PROVIDER that escaped the name
#warning: We used to have a version of SQLPP_ALIAS_PROVIDER that marked names as keywords
#warning: IIUC, the standard SQL way of handling keywords as names is to put them in square brackets, MySQL uses backticks, though
return std::string(t);
}

View File

@ -70,6 +70,42 @@ namespace sqlpp
mutable bool need_prefix = false;
};
#warning: need documentation and a better name!
struct tuple_operand_select_column
{
template <typename Context, typename T>
auto operator()(Context& context, const T& t, size_t index) const -> std::string
{
const auto prefix = index ? std::string{separator} : std::string{};
return prefix + operand_to_sql_string(context, t);
}
template <typename Context, typename T, typename NameProvider>
auto operator()(Context& context,
const as_expression<sqlpp::dynamic_t<T>, NameProvider>& t,
size_t index) const -> std::string
{
if (t._expression._condition)
{
return operator()(context, as(t._expression._expr, NameProvider{}), index);
}
return operator()(context, as(sqlpp::nullopt, NameProvider{}), index);
}
template <typename Context, typename T>
auto operator()(Context& context, const sqlpp::dynamic_t<T>& t, size_t index) const -> std::string
{
if (t._condition)
{
return operator()(context, t._expr, index);
}
static_assert(has_name<T>::value, "select columns have to have a name");
return operator()(context, as(sqlpp::nullopt, t._expr), index);
}
sqlpp::string_view separator;
};
struct tuple_clause
{
template <typename Context, typename T>

View File

@ -30,4 +30,5 @@ function(create_test name)
endfunction()
create_test(group_by)
create_test(select_columns)

View File

@ -38,7 +38,8 @@ int main(int, char* [])
// Plain columns.
SQLPP_COMPARE(group_by(foo.id), " GROUP BY tab_foo.id");
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.textNnD), " GROUP BY tab_foo.text_nn_d");
SQLPP_COMPARE(group_by(foo.boolN), " GROUP BY tab_foo.bool_n");
// 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");

View File

@ -0,0 +1,85 @@
/*
* 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_ALIAS_PROVIDER(cheese);
SQLPP_ALIAS_PROVIDER(cake);
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(select_columns(foo.id), "tab_foo.id");
SQLPP_COMPARE(select_columns(foo.textNnD), "tab_foo.text_nn_d");
SQLPP_COMPARE(select_columns(foo.boolN), "tab_foo.bool_n");
// Multiple plain columns.
SQLPP_COMPARE(select_columns(foo.id, foo.textNnD, foo.boolN), "tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n");
// Single expression
SQLPP_COMPARE(select_columns((foo.id + 17).as(cake)), "(tab_foo.id + 17) AS cake");
// Single dynamic column.
SQLPP_COMPARE(select_columns(dynamic(true, foo.id)), "tab_foo.id");
#warning: This needs a special special to-string strategy in select_columns
SQLPP_COMPARE(select_columns(dynamic(false, foo.id)), "NULL AS id");
// Multiple dynamic columns (this is odd if all are dynamic)
SQLPP_COMPARE(select_columns(dynamic(true, foo.id), foo.textNnD, foo.boolN), "tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n");
SQLPP_COMPARE(select_columns(foo.id, dynamic(true, foo.textNnD), foo.boolN), "tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n");
SQLPP_COMPARE(select_columns(foo.id, foo.textNnD, dynamic(true, foo.boolN)), "tab_foo.id, tab_foo.text_nn_d, tab_foo.bool_n");
SQLPP_COMPARE(select_columns(dynamic(false, foo.id), foo.textNnD, foo.boolN), "NULL AS id, tab_foo.text_nn_d, tab_foo.bool_n");
SQLPP_COMPARE(select_columns(foo.id, dynamic(false, foo.textNnD), foo.boolN), "tab_foo.id, NULL AS text_nn_d, tab_foo.bool_n");
SQLPP_COMPARE(select_columns(foo.id, foo.textNnD, dynamic(false, foo.boolN)), "tab_foo.id, tab_foo.text_nn_d, NULL AS bool_n");
SQLPP_COMPARE(select_columns(foo.id, dynamic(false, foo.textNnD), dynamic(false, foo.boolN)), "tab_foo.id, NULL AS text_nn_d, NULL AS bool_n");
SQLPP_COMPARE(select_columns(dynamic(false, foo.id), foo.textNnD, dynamic(false, foo.boolN)), "NULL AS id, tab_foo.text_nn_d, NULL AS bool_n");
SQLPP_COMPARE(select_columns(dynamic(false, foo.id), dynamic(false, foo.textNnD), foo.boolN), "NULL AS id, NULL AS text_nn_d, tab_foo.bool_n");
// Single declared column
SQLPP_COMPARE(select_columns(declare_group_by_column(val).as(cheese)), "17 AS cheese");
SQLPP_COMPARE(select_columns(declare_group_by_column(foo.id + 17).as(cake)), "(tab_foo.id + 17) AS cake");
// Mixed declared column
SQLPP_COMPARE(select_columns(foo.id, declare_group_by_column(val).as(cheese)), "tab_foo.id, 17 AS cheese");
SQLPP_COMPARE(select_columns(declare_group_by_column(val).as(cake), foo.id), "17 AS cake, tab_foo.id");
// Mixed dynamic declared column
SQLPP_COMPARE(select_columns(foo.id, dynamic(true, declare_group_by_column(val)).as(cheese)), "tab_foo.id, 17 AS cheese");
SQLPP_COMPARE(select_columns(dynamic(true, declare_group_by_column(val)).as(cake), foo.id), "17 AS cake, tab_foo.id");
SQLPP_COMPARE(select_columns(foo.id, dynamic(false, declare_group_by_column(val)).as(cheese)), "tab_foo.id, NULL AS cheese");
SQLPP_COMPARE(select_columns(dynamic(false, declare_group_by_column(val)).as(cake), foo.id), "NULL AS cake, tab_foo.id");
return 0;
}