mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Add order_by and limit for mysql remove and update
Thanks to ZerQAQ for suggesting this on github.
This commit is contained in:
parent
c1e754d83c
commit
1d57d28994
@ -32,6 +32,8 @@
|
|||||||
#include <sqlpp11/mysql/char_result.h>
|
#include <sqlpp11/mysql/char_result.h>
|
||||||
#include <sqlpp11/mysql/connection_config.h>
|
#include <sqlpp11/mysql/connection_config.h>
|
||||||
#include <sqlpp11/mysql/prepared_statement.h>
|
#include <sqlpp11/mysql/prepared_statement.h>
|
||||||
|
#include <sqlpp11/mysql/remove.h>
|
||||||
|
#include <sqlpp11/mysql/update.h>
|
||||||
#include <sqlpp11/serialize.h>
|
#include <sqlpp11/serialize.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
69
include/sqlpp11/mysql/remove.h
Normal file
69
include/sqlpp11/mysql/remove.h
Normal file
@ -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 <sqlpp11/remove.h>
|
||||||
|
#include <sqlpp11/order_by.h>
|
||||||
|
#include <sqlpp11/limit.h>
|
||||||
|
|
||||||
|
namespace sqlpp
|
||||||
|
{
|
||||||
|
namespace mysql{
|
||||||
|
template <typename Database>
|
||||||
|
using blank_remove_t = statement_t<Database, remove_t, no_from_t, no_using_t, no_where_t<true>, no_order_by_t, no_limit_t>;
|
||||||
|
|
||||||
|
inline auto remove() -> blank_remove_t<void>
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Table>
|
||||||
|
auto remove_from(Table table) -> decltype(blank_remove_t<void>().from(table))
|
||||||
|
{
|
||||||
|
return {blank_remove_t<void>().from(table)};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Database>
|
||||||
|
auto dynamic_remove(const Database & /*unused*/) -> decltype(blank_remove_t<Database>())
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of<connection, Database>::value, "Invalid database parameter");
|
||||||
|
return {blank_remove_t<Database>()};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Database, typename Table>
|
||||||
|
auto dynamic_remove_from(const Database& /*unused*/, Table table) -> decltype(blank_remove_t<Database>().from(table))
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of<connection, Database>::value, "Invalid database parameter");
|
||||||
|
return {blank_remove_t<Database>().from(table)};
|
||||||
|
}
|
||||||
|
} //namespace mysql
|
||||||
|
|
||||||
|
} // namespace sqlpp
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
57
include/sqlpp11/mysql/update.h
Normal file
57
include/sqlpp11/mysql/update.h
Normal file
@ -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 <sqlpp11/update.h>
|
||||||
|
#include <sqlpp11/limit.h>
|
||||||
|
#include <sqlpp11/order_by.h>
|
||||||
|
|
||||||
|
namespace sqlpp
|
||||||
|
{
|
||||||
|
namespace mysql{
|
||||||
|
template <typename Database>
|
||||||
|
using blank_update_t = statement_t<Database, update_t, no_single_table_t, no_update_list_t, no_where_t<true>, no_order_by_t, no_limit_t>;
|
||||||
|
|
||||||
|
template <typename Table>
|
||||||
|
constexpr auto update(Table table) -> decltype(blank_update_t<void>().single_table(table))
|
||||||
|
{
|
||||||
|
return {blank_update_t<void>().single_table(table)};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Database, typename Table>
|
||||||
|
constexpr auto dynamic_update(const Database& /*unused*/, Table table)
|
||||||
|
-> decltype(blank_update_t<Database>().single_table(table))
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of<connection, Database>::value, "Invalid database parameter");
|
||||||
|
return {blank_update_t<Database>().single_table(table)};
|
||||||
|
}
|
||||||
|
} // namespace mysql
|
||||||
|
} // namespace sqlpp
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -23,3 +23,5 @@ build_and_run(DynamicSelectTest)
|
|||||||
build_and_run(MoveConstructorTest)
|
build_and_run(MoveConstructorTest)
|
||||||
build_and_run(PreparedTest)
|
build_and_run(PreparedTest)
|
||||||
build_and_run(TruncatedTest)
|
build_and_run(TruncatedTest)
|
||||||
|
build_and_run(UpdateTest)
|
||||||
|
build_and_run(RemoveTest)
|
||||||
|
79
tests/mysql/usage/RemoveTest.cpp
Normal file
79
tests/mysql/usage/RemoveTest.cpp
Normal file
@ -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 <sqlpp11/mysql/mysql.h>
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
const auto tab = TabSample{};
|
||||||
|
|
||||||
|
namespace sql = sqlpp::mysql;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto config = std::make_shared<sql::connection_config>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
79
tests/mysql/usage/UpdateTest.cpp
Normal file
79
tests/mysql/usage/UpdateTest.cpp
Normal file
@ -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 <sqlpp11/mysql/mysql.h>
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
const auto tab = TabSample{};
|
||||||
|
|
||||||
|
namespace sql = sqlpp::mysql;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto config = std::make_shared<sql::connection_config>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user