From 1d57d28994478c9e27b93c98e38a07241337ca6c Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 14 Aug 2021 10:49:18 +0200 Subject: [PATCH] Add order_by and limit for mysql remove and update Thanks to ZerQAQ for suggesting this on github. --- include/sqlpp11/mysql/connection.h | 2 + include/sqlpp11/mysql/remove.h | 69 ++++++++++++++++++++++++++ include/sqlpp11/mysql/update.h | 57 +++++++++++++++++++++ tests/mysql/usage/CMakeLists.txt | 2 + tests/mysql/usage/RemoveTest.cpp | 79 ++++++++++++++++++++++++++++++ tests/mysql/usage/UpdateTest.cpp | 79 ++++++++++++++++++++++++++++++ 6 files changed, 288 insertions(+) create mode 100644 include/sqlpp11/mysql/remove.h create mode 100644 include/sqlpp11/mysql/update.h create mode 100644 tests/mysql/usage/RemoveTest.cpp create mode 100644 tests/mysql/usage/UpdateTest.cpp diff --git a/include/sqlpp11/mysql/connection.h b/include/sqlpp11/mysql/connection.h index 0edc8efb..c6674a36 100644 --- a/include/sqlpp11/mysql/connection.h +++ b/include/sqlpp11/mysql/connection.h @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/include/sqlpp11/mysql/remove.h b/include/sqlpp11/mysql/remove.h new file mode 100644 index 00000000..a8935177 --- /dev/null +++ b/include/sqlpp11/mysql/remove.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021-2021, Roland Bock, ZerQAQ + * 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. + */ + +#ifndef SQLPP11_MYSQL_REMOVE_H +#define SQLPP11_MYSQL_REMOVE_H + +#include +#include +#include + +namespace sqlpp +{ + namespace mysql{ + template + using blank_remove_t = statement_t, no_order_by_t, no_limit_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 mysql + +} // namespace sqlpp + +#endif + diff --git a/include/sqlpp11/mysql/update.h b/include/sqlpp11/mysql/update.h new file mode 100644 index 00000000..c48d085f --- /dev/null +++ b/include/sqlpp11/mysql/update.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021-2021, Roland Bock, ZerQAQ + * 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. + */ + +#ifndef SQLPP11_MYSQL_UPDATE_H +#define SQLPP11_MYSQL_UPDATE_H + +#include +#include +#include + +namespace sqlpp +{ + namespace mysql{ + template + using blank_update_t = statement_t, no_order_by_t, no_limit_t>; + + template + constexpr auto update(Table table) -> decltype(blank_update_t().single_table(table)) + { + return {blank_update_t().single_table(table)}; + } + + template + constexpr auto dynamic_update(const Database& /*unused*/, Table table) + -> decltype(blank_update_t().single_table(table)) + { + static_assert(std::is_base_of::value, "Invalid database parameter"); + return {blank_update_t().single_table(table)}; + } + } // namespace mysql +} // namespace sqlpp + +#endif + diff --git a/tests/mysql/usage/CMakeLists.txt b/tests/mysql/usage/CMakeLists.txt index f6df8364..410045d7 100644 --- a/tests/mysql/usage/CMakeLists.txt +++ b/tests/mysql/usage/CMakeLists.txt @@ -23,3 +23,5 @@ build_and_run(DynamicSelectTest) build_and_run(MoveConstructorTest) build_and_run(PreparedTest) build_and_run(TruncatedTest) +build_and_run(UpdateTest) +build_and_run(RemoveTest) diff --git a/tests/mysql/usage/RemoveTest.cpp b/tests/mysql/usage/RemoveTest.cpp new file mode 100644 index 00000000..a759b774 --- /dev/null +++ b/tests/mysql/usage/RemoveTest.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 - 2021, Roland Bock, ZerQAQ + * 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 "TabSample.h" +#include +#include + +#include +#include + +const auto tab = TabSample{}; + +namespace sql = sqlpp::mysql; + +int main() +{ + auto config = std::make_shared(); + config->user = "root"; + config->database = "sqlpp_mysql"; + config->debug = true; + try + { + sql::connection db(config); + } + catch (const sqlpp::exception& e) + { + std::cerr << "For testing, you'll need to create a database sqlpp_mysql for user root (no password)" << std::endl; + std::cerr << e.what() << std::endl; + return 1; + } + try + { + sql::connection db(config); + db.execute(R"(DROP TABLE IF EXISTS tab_sample)"); + db.execute(R"(CREATE TABLE tab_sample ( + alpha bigint(20) AUTO_INCREMENT, + beta varchar(255) DEFAULT NULL, + gamma bool DEFAULT NULL, + PRIMARY KEY (alpha) + ))"); + + db(insert_into(tab).set(tab.beta = "1", tab.gamma = false)); + db(insert_into(tab).set(tab.beta = "2", tab.gamma = false)); + db(insert_into(tab).set(tab.beta = "3", tab.gamma = false)); + + db(sql::remove_from(tab).unconditionally().order_by(tab.alpha.desc()).limit(1u)); + for(const auto &row : db(sqlpp::select(tab.beta).from(tab).unconditionally().order_by(tab.alpha.desc()).limit(1u))){ + assert(row.beta == "2"); + } + } + catch (const std::exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + return 1; + } +} + diff --git a/tests/mysql/usage/UpdateTest.cpp b/tests/mysql/usage/UpdateTest.cpp new file mode 100644 index 00000000..20780ea3 --- /dev/null +++ b/tests/mysql/usage/UpdateTest.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 - 2021, Roland Bock, ZerQAQ + * 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 "TabSample.h" +#include +#include + +#include +#include + +const auto tab = TabSample{}; + +namespace sql = sqlpp::mysql; + +int main() +{ + auto config = std::make_shared(); + config->user = "root"; + config->database = "sqlpp_mysql"; + config->debug = true; + try + { + sql::connection db(config); + } + catch (const sqlpp::exception& e) + { + std::cerr << "For testing, you'll need to create a database sqlpp_mysql for user root (no password)" << std::endl; + std::cerr << e.what() << std::endl; + return 1; + } + try + { + sql::connection db(config); + db.execute(R"(DROP TABLE IF EXISTS tab_sample)"); + db.execute(R"(CREATE TABLE tab_sample ( + alpha bigint(20) AUTO_INCREMENT, + beta varchar(255) DEFAULT NULL, + gamma bool DEFAULT NULL, + PRIMARY KEY (alpha) + ))"); + + db(insert_into(tab).set(tab.beta = "1", tab.gamma = false)); + db(insert_into(tab).set(tab.beta = "2", tab.gamma = false)); + db(insert_into(tab).set(tab.beta = "3", tab.gamma = false)); + + db(sql::update(tab).set(tab.gamma = true).unconditionally().order_by(tab.alpha.desc()).limit(1u)); + for(const auto &row : db(sqlpp::select(tab.gamma).from(tab).where(tab.beta == "3"))){ + assert(row.gamma); + } + } + catch (const std::exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + return 1; + } +} +