2014-10-29 14:34:42 +08:00
|
|
|
/*
|
2015-02-16 02:00:21 +08:00
|
|
|
* Copyright (c) 2013-2015, Roland Bock
|
2014-10-29 14:34:42 +08:00
|
|
|
* All rights reserved.
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2014-10-29 14:34:42 +08:00
|
|
|
* are permitted provided that the following conditions are met:
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
2014-10-29 14:34:42 +08:00
|
|
|
* this list of conditions and the following disclaimer.
|
2015-08-05 20:43:21 +08:00
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
2014-10-29 14:34:42 +08:00
|
|
|
* and/or other materials provided with the distribution.
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
|
|
|
* 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
|
2014-10-29 14:34:42 +08:00
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "Sample.h"
|
|
|
|
#include "MockDb.h"
|
|
|
|
#include <sqlpp11/sqlpp11.h>
|
2024-07-29 13:00:12 +08:00
|
|
|
#include <sqlpp11/query/query/custom_query.h>
|
2024-04-13 18:04:41 +08:00
|
|
|
#include "../../include/test_helpers.h"
|
2014-10-29 14:34:42 +08:00
|
|
|
|
2018-12-25 00:32:16 +08:00
|
|
|
namespace
|
|
|
|
{
|
2024-06-09 00:33:40 +08:00
|
|
|
#warning restore this file!
|
|
|
|
#if 0
|
2018-12-25 00:32:16 +08:00
|
|
|
struct on_duplicate_key_update
|
|
|
|
{
|
|
|
|
std::string _serialized;
|
|
|
|
|
|
|
|
template <typename Db, typename Assignment>
|
|
|
|
on_duplicate_key_update(Db&, Assignment assignment)
|
|
|
|
{
|
|
|
|
typename Db::_serializer_context_t context;
|
|
|
|
_serialized = " ON DUPLICATE KEY UPDATE " + serialize(assignment, context).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Db, typename Assignment>
|
|
|
|
auto operator()(Db&, Assignment assignment) -> on_duplicate_key_update&
|
|
|
|
{
|
|
|
|
typename Db::_serializer_context_t context;
|
|
|
|
_serialized += ", " + serialize(assignment, context).str();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get() const -> sqlpp::verbatim_t<::sqlpp::no_value_t>
|
|
|
|
{
|
|
|
|
return ::sqlpp::verbatim(_serialized);
|
|
|
|
}
|
|
|
|
};
|
2024-06-09 00:33:40 +08:00
|
|
|
#endif
|
2018-12-25 00:32:16 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int CustomQuery(int, char*[])
|
2014-10-29 14:34:42 +08:00
|
|
|
{
|
2015-09-14 03:33:19 +08:00
|
|
|
MockDb db = {};
|
2015-12-28 05:25:05 +08:00
|
|
|
MockDb::_serializer_context_t printer = {};
|
2015-03-21 18:41:07 +08:00
|
|
|
|
2015-12-28 05:25:05 +08:00
|
|
|
const auto f = test::TabFoo{};
|
|
|
|
const auto t = test::TabBar{};
|
2014-10-29 14:34:42 +08:00
|
|
|
|
2024-07-29 13:00:12 +08:00
|
|
|
// A void custom query/query
|
2015-09-14 03:33:19 +08:00
|
|
|
printer.reset();
|
2024-06-09 00:33:40 +08:00
|
|
|
auto x =
|
2024-06-15 02:04:08 +08:00
|
|
|
//select(t.id).from(t).where(t.id > 7).group_by(t.id).having(max(t.id) > 13).order_by(t.textN.desc());
|
|
|
|
update(t).set(t.textN = "eight", t.boolNn = true);
|
2024-07-26 18:40:40 +08:00
|
|
|
std::cerr << serialize(printer, x).str() << std::endl;
|
|
|
|
#warning restore this file!
|
2024-06-09 00:33:40 +08:00
|
|
|
#if 0
|
2015-09-14 03:33:19 +08:00
|
|
|
db(x);
|
2014-11-06 00:50:46 +08:00
|
|
|
|
2024-07-29 13:00:12 +08:00
|
|
|
// Syntactically, it is possible to use this void query/query as a prepared statement, too, not sure, whether this makes
|
2015-09-14 03:33:19 +08:00
|
|
|
// sense very often...
|
|
|
|
db(db.prepare(x));
|
2014-11-09 23:26:36 +08:00
|
|
|
|
2015-09-14 03:33:19 +08:00
|
|
|
// A prepared custom select
|
2024-07-29 13:00:12 +08:00
|
|
|
// The return type of the custom query/query is determined from the first argument which does have a return type, in this
|
2015-09-14 03:33:19 +08:00
|
|
|
// case the select
|
2024-06-15 02:04:08 +08:00
|
|
|
auto p = db.prepare(custom_query(select(all_of(t)).from(t), where(t.id > sqlpp::parameter(t.id))));
|
|
|
|
p.params.id = 8;
|
2015-09-14 03:33:19 +08:00
|
|
|
for (const auto& row : db(p))
|
|
|
|
{
|
2024-06-15 02:04:08 +08:00
|
|
|
std::cerr << row.id << std::endl;
|
2015-09-14 03:33:19 +08:00
|
|
|
}
|
2014-11-09 23:26:36 +08:00
|
|
|
|
2016-03-31 13:11:41 +08:00
|
|
|
// Create a custom "insert or ignore"
|
|
|
|
db(custom_query(sqlpp::insert(), sqlpp::verbatim(" OR IGNORE"), into(t),
|
2024-06-15 02:04:08 +08:00
|
|
|
insert_set(t.textN = "sample", t.boolNn = true)));
|
2016-03-31 13:11:41 +08:00
|
|
|
|
2022-06-06 16:15:28 +08:00
|
|
|
// Create a custom mulit-row "insert or ignore"
|
2024-06-15 02:04:08 +08:00
|
|
|
auto batch = insert_columns(t.textN, t.boolNn);
|
|
|
|
batch.add_values(t.textN = "sample", t.boolNn = true);
|
|
|
|
batch.add_values(t.textN = "ample", t.boolNn = false);
|
2022-06-06 16:15:28 +08:00
|
|
|
db(custom_query(sqlpp::insert(), sqlpp::verbatim(" OR IGNORE"), into(t), batch));
|
|
|
|
|
2018-12-25 00:32:16 +08:00
|
|
|
// Create a MYSQL style custom "insert on duplicate update"
|
2024-06-15 02:04:08 +08:00
|
|
|
db(custom_query(sqlpp::insert_into(t).set(t.textN = "sample", t.boolNn = true),
|
|
|
|
on_duplicate_key_update(db, t.textN = "sample")(db, t.boolNn = false).get()));
|
2018-12-25 00:32:16 +08:00
|
|
|
|
2015-09-14 03:33:19 +08:00
|
|
|
// A custom (select ... into) with adjusted return type
|
2024-07-29 13:00:12 +08:00
|
|
|
// The first argument with a return type is the select, but the custom query/query is really an insert. Thus, we tell it so.
|
2015-09-14 03:33:19 +08:00
|
|
|
printer.reset();
|
|
|
|
auto c = custom_query(select(all_of(t)).from(t), into(f)).with_result_type_of(insert_into(f));
|
|
|
|
std::cerr << serialize(c, printer).str() << std::endl;
|
|
|
|
auto i = db(c);
|
|
|
|
static_assert(std::is_integral<decltype(i)>::value, "insert yields an integral value");
|
2014-11-05 14:27:19 +08:00
|
|
|
|
2019-01-23 22:34:47 +08:00
|
|
|
auto d = custom_query(sqlpp::verbatim("INSERT INTO tab_sample VALUES()")).with_result_type_of(sqlpp::insert());
|
|
|
|
auto j = db(d);
|
|
|
|
static_assert(std::is_integral<decltype(j)>::value, "insert yields an integral value");
|
|
|
|
|
2017-10-22 01:03:01 +08:00
|
|
|
for (const auto& row :
|
|
|
|
db(custom_query(sqlpp::verbatim("PRAGMA user_version")).with_result_type_of(select(all_of(t)))))
|
|
|
|
{
|
2024-06-15 02:04:08 +08:00
|
|
|
(void)row.id;
|
2017-10-22 01:03:01 +08:00
|
|
|
}
|
2024-06-09 00:33:40 +08:00
|
|
|
#endif
|
2017-10-22 01:03:01 +08:00
|
|
|
|
2015-09-14 03:33:19 +08:00
|
|
|
return 0;
|
2014-10-29 14:34:42 +08:00
|
|
|
}
|