From 040047e5ff3654b6e6fc93ea1f02f8e1e70d9c82 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 29 Sep 2024 11:28:32 +0200 Subject: [PATCH] Add serialize tests for limit and offset --- include/sqlpp11/core/clause/limit.h | 19 +++++++-- include/sqlpp11/core/clause/offset.h | 16 ++++++-- tests/core/serialize/clause/CMakeLists.txt | 2 + tests/core/serialize/clause/limit.cpp | 45 ++++++++++++++++++++++ tests/core/serialize/clause/offset.cpp | 45 ++++++++++++++++++++++ 5 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 tests/core/serialize/clause/limit.cpp create mode 100644 tests/core/serialize/clause/offset.cpp diff --git a/include/sqlpp11/core/clause/limit.h b/include/sqlpp11/core/clause/limit.h index 68675899..f94678cb 100644 --- a/include/sqlpp11/core/clause/limit.h +++ b/include/sqlpp11/core/clause/limit.h @@ -73,16 +73,17 @@ namespace sqlpp }; }; - SQLPP_PORTABLE_STATIC_ASSERT(assert_limit_is_unsigned_integral, - "argument for limit() must be an unsigned integral expressions"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_limit_is_integral, + "argument for limit() must be an integral expressions"); template struct check_limit { +#warning: document that limits can be integral (not just unsigned integral) using type = - static_combined_check_t::value, assert_limit_is_unsigned_integral>>; + static_combined_check_t::value, assert_limit_is_integral>>; }; template - using check_limit_t = typename check_limit::type; + using check_limit_t = typename check_limit>::type; struct no_limit_t { @@ -133,6 +134,16 @@ namespace sqlpp return " LIMIT " + operand_to_sql_string(context, t._value); } + template + auto to_sql_string(Context& context, const limit_data_t>& t) -> std::string + { + if (not t._value._condition) + { + return ""; + } + return " LIMIT " + operand_to_sql_string(context, t._value._expr); + } + template auto limit(T&& t) -> decltype(statement_t().limit(std::forward(t))) { diff --git a/include/sqlpp11/core/clause/offset.h b/include/sqlpp11/core/clause/offset.h index acd008fb..76683499 100644 --- a/include/sqlpp11/core/clause/offset.h +++ b/include/sqlpp11/core/clause/offset.h @@ -72,16 +72,16 @@ namespace sqlpp }; }; - SQLPP_PORTABLE_STATIC_ASSERT(assert_offset_is_unsigned_integral, + SQLPP_PORTABLE_STATIC_ASSERT(assert_offset_is_integral, "argument for offset() must be an integral expressions"); template struct check_offset { using type = - static_combined_check_t::value, assert_offset_is_unsigned_integral>>; + static_combined_check_t::value, assert_offset_is_integral>>; }; template - using check_offset_t = typename check_offset::type; + using check_offset_t = typename check_offset>::type; struct no_offset_t { @@ -144,6 +144,16 @@ namespace sqlpp return " OFFSET " + operand_to_sql_string(context, t._value); } + template + auto to_sql_string(Context& context, const offset_data_t>& t) -> std::string + { + if (not t._value._condition) + { + return ""; + } + return " OFFSET " + operand_to_sql_string(context, t._value._expr); + } + template auto offset(T&& t) -> decltype(statement_t().offset(std::forward(t))) { diff --git a/tests/core/serialize/clause/CMakeLists.txt b/tests/core/serialize/clause/CMakeLists.txt index b498fbb6..0580ecc0 100644 --- a/tests/core/serialize/clause/CMakeLists.txt +++ b/tests/core/serialize/clause/CMakeLists.txt @@ -36,6 +36,8 @@ create_test(insert) create_test(insert_columns) create_test(insert_default_values) create_test(insert_set) +create_test(limit) +create_test(offset) create_test(order_by) create_test(select_columns) create_test(select_flags) diff --git a/tests/core/serialize/clause/limit.cpp b/tests/core/serialize/clause/limit.cpp new file mode 100644 index 00000000..79fe3f91 --- /dev/null +++ b/tests/core/serialize/clause/limit.cpp @@ -0,0 +1,45 @@ +/* + * 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 + +int main(int, char* []) +{ + const auto val = sqlpp::value(17); + const auto expr = sqlpp::value(17) + 4; + + // Without static limit. + SQLPP_COMPARE(sqlpp::limit(17), " LIMIT 17"); + SQLPP_COMPARE(limit(val), " LIMIT 17"); + SQLPP_COMPARE(limit(expr), " LIMIT (17 + 4)"); + + // With dynamic limit. + SQLPP_COMPARE(limit(dynamic(true, val)), " LIMIT 17"); + SQLPP_COMPARE(limit(dynamic(false, val)), ""); + + return 0; +} diff --git a/tests/core/serialize/clause/offset.cpp b/tests/core/serialize/clause/offset.cpp new file mode 100644 index 00000000..8bfdb008 --- /dev/null +++ b/tests/core/serialize/clause/offset.cpp @@ -0,0 +1,45 @@ +/* + * 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 + +int main(int, char* []) +{ + const auto val = sqlpp::value(17); + const auto expr = sqlpp::value(17) + 4; + + // Without static offset. + SQLPP_COMPARE(sqlpp::offset(17), " OFFSET 17"); + SQLPP_COMPARE(offset(val), " OFFSET 17"); + SQLPP_COMPARE(offset(expr), " OFFSET (17 + 4)"); + + // With dynamic offset. + SQLPP_COMPARE(offset(dynamic(true, val)), " OFFSET 17"); + SQLPP_COMPARE(offset(dynamic(false, val)), ""); + + return 0; +}