diff --git a/include/sqlpp11/core/clause/update_list.h b/include/sqlpp11/core/clause/update_list.h index 39b3a5cc..88cef6e6 100644 --- a/include/sqlpp11/core/clause/update_list.h +++ b/include/sqlpp11/core/clause/update_list.h @@ -102,7 +102,7 @@ namespace sqlpp template struct check_update_static_set { - using type = static_combined_check_t, + using type = static_combined_check_t...>, static_check_t>; }; @@ -167,6 +167,13 @@ namespace sqlpp template auto to_sql_string(Context& context, const update_list_data_t& 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 + auto update_set(T&&... t) -> decltype(statement_t().set(std::forward(t)...)) + { + return statement_t().set(std::forward(t)...); + } + } // namespace sqlpp diff --git a/include/sqlpp11/core/operator/assign_expression.h b/include/sqlpp11/core/operator/assign_expression.h index 6309f8b6..39c05baa 100644 --- a/include/sqlpp11/core/operator/assign_expression.h +++ b/include/sqlpp11/core/operator/assign_expression.h @@ -28,6 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include #include #include diff --git a/include/sqlpp11/core/tuple_to_sql_string.h b/include/sqlpp11/core/tuple_to_sql_string.h index e1729c12..cde6ff39 100644 --- a/include/sqlpp11/core/tuple_to_sql_string.h +++ b/include/sqlpp11/core/tuple_to_sql_string.h @@ -45,7 +45,7 @@ namespace sqlpp 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 { template @@ -70,7 +70,8 @@ namespace sqlpp 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 ". struct tuple_operand_select_column { template diff --git a/include/sqlpp11/core/type_traits.h b/include/sqlpp11/core/type_traits.h index c136342c..4dce2a45 100644 --- a/include/sqlpp11/core/type_traits.h +++ b/include/sqlpp11/core/type_traits.h @@ -82,6 +82,9 @@ namespace sqlpp template struct can_be_null : public is_optional> {}; + template <> + struct can_be_null : public std::true_type {}; + template struct dynamic_t; diff --git a/tests/core/serialize/clause/CMakeLists.txt b/tests/core/serialize/clause/CMakeLists.txt index 270ac544..fab2aa63 100644 --- a/tests/core/serialize/clause/CMakeLists.txt +++ b/tests/core/serialize/clause/CMakeLists.txt @@ -32,4 +32,5 @@ endfunction() create_test(group_by) create_test(select_columns) create_test(select_flags) +create_test(update_set) diff --git a/tests/core/serialize/clause/update_set.cpp b/tests/core/serialize/clause/update_set.cpp new file mode 100644 index 00000000..ddb4e6cf --- /dev/null +++ b/tests/core/serialize/clause/update_set.cpp @@ -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 + +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; +}