0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 12:51:13 +08:00

Add serialize tests for update_set

This commit is contained in:
Roland Bock 2024-09-14 18:38:02 +02:00
parent 8764f3862e
commit 57dd15143d
6 changed files with 67 additions and 4 deletions

View File

@ -102,7 +102,7 @@ namespace sqlpp
template <typename... Assignments> template <typename... Assignments>
struct check_update_static_set struct check_update_static_set
{ {
using type = static_combined_check_t<check_update_set_t<Assignments...>, using type = static_combined_check_t<check_update_set_t<remove_dynamic_t<Assignments>...>,
static_check_t<sizeof...(Assignments) != 0, assert_update_set_count_args_t>>; static_check_t<sizeof...(Assignments) != 0, assert_update_set_count_args_t>>;
}; };
@ -167,6 +167,13 @@ namespace sqlpp
template <typename Context, typename... Assignments> template <typename Context, typename... Assignments>
auto to_sql_string(Context& context, const update_list_data_t<Assignments...>& t) -> std::string auto to_sql_string(Context& context, const update_list_data_t<Assignments...>& t) -> std::string
{ {
return " SET " + tuple_to_sql_string(context, t._assignments, tuple_operand{", "}); return " SET " + tuple_to_sql_string(context, t._assignments, tuple_operand_no_dynamic{", "});
} }
template <typename... T>
auto update_set(T&&... t) -> decltype(statement_t<no_update_list_t>().set(std::forward<T>(t)...))
{
return statement_t<no_update_list_t>().set(std::forward<T>(t)...);
}
} // namespace sqlpp } // namespace sqlpp

View File

@ -28,6 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <utility> #include <utility>
#include <sqlpp11/core/to_sql_string.h>
#include <sqlpp11/core/basic/column_fwd.h> #include <sqlpp11/core/basic/column_fwd.h>
#include <sqlpp11/core/type_traits.h> #include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/default_value.h> #include <sqlpp11/core/default_value.h>

View File

@ -45,7 +45,7 @@ namespace sqlpp
sqlpp::string_view separator; sqlpp::string_view separator;
}; };
#warning: to be used by group_by and update // Used to serialize tuple tuples that should ignore dynamic elements.
struct tuple_operand_no_dynamic struct tuple_operand_no_dynamic
{ {
template <typename Context, typename T> template <typename Context, typename T>
@ -70,7 +70,8 @@ namespace sqlpp
mutable bool need_prefix = false; mutable bool need_prefix = false;
}; };
#warning: need documentation and a better name! // Used to serialize select columns.
// In particular, it serializes unselected dynamic columns as "NULL AS <name>".
struct tuple_operand_select_column struct tuple_operand_select_column
{ {
template <typename Context, typename T> template <typename Context, typename T>

View File

@ -82,6 +82,9 @@ namespace sqlpp
template <typename T> template <typename T>
struct can_be_null : public is_optional<value_type_of_t<T>> {}; struct can_be_null : public is_optional<value_type_of_t<T>> {};
template <>
struct can_be_null<sqlpp::nullopt_t> : public std::true_type {};
template <typename T> template <typename T>
struct dynamic_t; struct dynamic_t;

View File

@ -32,4 +32,5 @@ endfunction()
create_test(group_by) create_test(group_by)
create_test(select_columns) create_test(select_columns)
create_test(select_flags) create_test(select_flags)
create_test(update_set)

View File

@ -0,0 +1,50 @@
/*
* 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 assignments.
SQLPP_COMPARE(update_set(foo.id = 7), " SET id = 7");
SQLPP_COMPARE(update_set(foo.id = 7, foo.textNnD = "cheesecake"), " SET id = 7, text_nn_d = 'cheesecake'");
// Dynamic assignments.
SQLPP_COMPARE(update_set(sqlpp::dynamic(true, foo.id = 7), sqlpp::dynamic(false, foo.textNnD = "cheesecake")),
" SET id = 7");
SQLPP_COMPARE(update_set(sqlpp::dynamic(false, foo.id = 7), sqlpp::dynamic(true, foo.textNnD = "cheesecake")),
" SET text_nn_d = 'cheesecake'");
return 0;
}