From 451e3379799d2c0656f3ae6bca8595846f6b6298 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Tue, 31 Oct 2023 06:44:17 +0100 Subject: [PATCH] Introduce postgresql::remove_from with optional returning clause (#538) --- include/sqlpp11/postgresql/postgresql.h | 1 + include/sqlpp11/postgresql/remove.h | 65 +++++++++++++++++++++++++ tests/postgresql/usage/Returning.cpp | 5 ++ 3 files changed, 71 insertions(+) create mode 100644 include/sqlpp11/postgresql/remove.h diff --git a/include/sqlpp11/postgresql/postgresql.h b/include/sqlpp11/postgresql/postgresql.h index 374fe70d..0dcd1d0d 100644 --- a/include/sqlpp11/postgresql/postgresql.h +++ b/include/sqlpp11/postgresql/postgresql.h @@ -32,4 +32,5 @@ #include #include #include +#include #include diff --git a/include/sqlpp11/postgresql/remove.h b/include/sqlpp11/postgresql/remove.h new file mode 100644 index 00000000..d7ffb6a6 --- /dev/null +++ b/include/sqlpp11/postgresql/remove.h @@ -0,0 +1,65 @@ +#pragma once + +/** + * Copyright (c) 2023-2023, 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 +#include + +namespace sqlpp +{ + namespace postgresql + { + template + using blank_remove_t = statement_t, no_returning_t>; + + inline auto remove() -> blank_remove_t + { + return {}; + } + + template + auto remove_from(Table table) -> decltype(blank_remove_t().from(table)) + { + return {blank_remove_t().from(table)}; + } + + template + auto dynamic_remove(const Database& /*unused*/) -> decltype(blank_remove_t()) + { + static_assert(std::is_base_of::value, "Invalid database parameter"); + return {blank_remove_t()}; + } + + template + auto dynamic_remove_from(const Database& /*unused*/, Table table) + -> decltype(blank_remove_t().from(table)) + { + static_assert(std::is_base_of::value, "Invalid database parameter"); + return {blank_remove_t().from(table)}; + } + } // namespace postgresql +} // namespace sqlpp diff --git a/tests/postgresql/usage/Returning.cpp b/tests/postgresql/usage/Returning.cpp index a3cf5b05..5f3a74bc 100644 --- a/tests/postgresql/usage/Returning.cpp +++ b/tests/postgresql/usage/Returning.cpp @@ -45,6 +45,11 @@ int Returning(int, char*[]) for (const auto& row : updated) std::cout << "Gamma: " << row.gamma << " Beta: " << row.beta << std::endl; + auto removed = + db(sqlpp::postgresql::remove_from(foo).where(foo.beta == 0).returning(foo.gamma, foo.beta)); + for (const auto& row : removed) + std::cout << "Gamma: " << row.gamma << " Beta: " << row.beta << std::endl; + auto multi_insert = sqlpp::postgresql::insert_into(foo).columns(foo.beta).returning(foo.alpha, foo.beta); multi_insert.values.add(foo.beta = 1); multi_insert.values.add(foo.beta = 2);