2014-01-11 07:11:47 +08:00
|
|
|
/*
|
2016-03-18 00:28:59 +08:00
|
|
|
* Copyright (c) 2013-2016, Roland Bock
|
2014-01-11 07:11:47 +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-01-11 07:11:47 +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-01-11 07:11:47 +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-01-11 07:11:47 +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-01-11 07:11:47 +08:00
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MockDb.h"
|
2016-09-07 04:34:59 +08:00
|
|
|
#include "Sample.h"
|
2014-08-13 00:48:50 +08:00
|
|
|
#include <sqlpp11/sqlpp11.h>
|
2014-01-11 07:11:47 +08:00
|
|
|
|
2024-04-13 18:04:41 +08:00
|
|
|
#include "../../include/test_helpers.h"
|
2014-01-11 07:11:47 +08:00
|
|
|
|
2015-12-29 18:03:17 +08:00
|
|
|
int Interpret(int, char* [])
|
2014-01-11 07:11:47 +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-09-14 03:33:19 +08:00
|
|
|
|
2015-12-28 05:25:05 +08:00
|
|
|
const auto f = test::TabFoo{};
|
|
|
|
const auto t = test::TabBar{};
|
2024-06-15 02:04:08 +08:00
|
|
|
select(t.id.as(t.textN));
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(insert_into(t).columns(t.textN, t.boolNn), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
{
|
2024-06-15 02:04:08 +08:00
|
|
|
auto i = insert_into(t).columns(t.boolNn, t.textN);
|
|
|
|
i.add_values(t.boolNn = true, t.textN = "cheesecake");
|
2015-09-14 03:33:19 +08:00
|
|
|
serialize(i, printer).str();
|
2024-06-15 02:04:08 +08:00
|
|
|
i.add_values(t.boolNn = sqlpp::default_value, t.textN = sqlpp::null);
|
2015-09-14 03:33:19 +08:00
|
|
|
serialize(i, printer).str();
|
|
|
|
}
|
|
|
|
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(t.id, printer).str();
|
|
|
|
serialize(-t.id, printer).str();
|
|
|
|
serialize(+t.id, printer).str();
|
|
|
|
serialize(-(t.id + 7), printer).str();
|
|
|
|
serialize(t.id = 0, printer).str();
|
|
|
|
serialize(t.id == 0, printer).str();
|
|
|
|
serialize(t.id != 0, printer).str();
|
|
|
|
serialize(t.id == 7, printer).str();
|
|
|
|
serialize(t.textN + "kaesekuchen", printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
serialize(sqlpp::select(), printer).str();
|
|
|
|
serialize(sqlpp::select().flags(sqlpp::distinct), printer).str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(select(t.id, t.textN).flags(sqlpp::distinct), printer).str();
|
|
|
|
serialize(select(t.id, t.textN), printer).str();
|
|
|
|
serialize(select(t.id, t.textN).from(t), printer).str();
|
|
|
|
serialize(select(t.id, t.textN).from(t).where(t.id == 3), printer).str();
|
|
|
|
serialize(select(t.id, t.textN).from(t).where(t.id == 3).group_by(t.boolNn), printer).str();
|
|
|
|
serialize(select(t.id, t.textN).from(t).where(t.id == 3).group_by(t.boolNn).having(t.textN.like("%kuchen")),
|
2016-09-07 04:34:59 +08:00
|
|
|
printer)
|
|
|
|
.str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(select(t.id, t.textN)
|
2015-09-14 03:33:19 +08:00
|
|
|
.from(t)
|
2024-06-15 02:04:08 +08:00
|
|
|
.where(t.id == 3)
|
|
|
|
.group_by(t.boolNn)
|
|
|
|
.having(t.textN.like("%kuchen"))
|
|
|
|
.order_by(t.textN.asc()),
|
2016-09-07 04:34:59 +08:00
|
|
|
printer)
|
|
|
|
.str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(select(t.id, t.textN)
|
2015-09-14 03:33:19 +08:00
|
|
|
.from(t)
|
2024-06-15 02:04:08 +08:00
|
|
|
.where(t.id == 3)
|
|
|
|
.group_by(t.boolNn)
|
|
|
|
.having(t.textN.like("%kuchen"))
|
|
|
|
.order_by(t.textN.asc())
|
2016-09-07 04:34:59 +08:00
|
|
|
.limit(17u)
|
|
|
|
.offset(3u),
|
|
|
|
printer)
|
|
|
|
.str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(parameter(sqlpp::bigint(), t.id), printer).str();
|
|
|
|
serialize(parameter(t.id), printer).str();
|
|
|
|
serialize(t.id == parameter(t.id), printer).str();
|
|
|
|
serialize(t.id == parameter(t.id) and (t.textN + "gimmick").like(parameter(t.textN)), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
serialize(insert_into(t), printer).str();
|
|
|
|
serialize(insert_into(f).default_values(), printer).str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(insert_into(t).set(t.boolNn = true), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
serialize(update(t), printer).str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(update(t).set(t.boolNn = true), printer).str();
|
|
|
|
serialize(update(t).set(t.boolNn = true).where(t.textN.in("kaesekuchen", "cheesecake")), printer).str();
|
|
|
|
serialize(update(t).set(t.boolNn = true).where(t.textN.in()), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
serialize(remove_from(t), printer).str();
|
|
|
|
serialize(remove_from(t).using_(t), printer).str();
|
|
|
|
|
|
|
|
// functions
|
|
|
|
serialize(sqlpp::value(7), printer).str();
|
|
|
|
serialize(sqlpp::verbatim<sqlpp::integral>("irgendwas integrales"), printer).str();
|
|
|
|
serialize(sqlpp::value_list(std::vector<int>({1, 2, 3, 4, 5, 6, 8})), printer).str();
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(exists(select(t.id).from(t)), printer).str();
|
|
|
|
serialize(any(select(t.id).from(t)), printer).str();
|
|
|
|
serialize(some(select(t.id).from(t)), printer).str();
|
|
|
|
serialize(count(t.id), printer).str();
|
|
|
|
serialize(min(t.id), printer).str();
|
|
|
|
serialize(max(t.id), printer).str();
|
|
|
|
serialize(avg(t.id), printer).str();
|
|
|
|
serialize(sum(t.id), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
serialize(sqlpp::verbatim_table("whatever"), printer).str();
|
|
|
|
|
|
|
|
// alias
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(t.as(t.id), printer).str();
|
|
|
|
serialize(t.as(t.id).textN, printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
// select alias
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(select(t.id).from(t).where(t.textN > "kaesekuchen").as(t.boolNn), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2024-04-13 14:56:57 +08:00
|
|
|
// Comparison to null
|
2024-06-15 02:04:08 +08:00
|
|
|
static_assert(not sqlpp::can_be_null_t<decltype(t.id)>::value, "expected id cannot be null");
|
|
|
|
static_assert(not sqlpp::can_be_null_t<decltype(f.textNnD)>::value, "expected intN cannot be null");
|
|
|
|
serialize(t.id.is_null(), printer).str();
|
|
|
|
serialize(f.textNnD.is_null(), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
// join
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(t.inner_join(t.as(t.id)).on(t.textN == t.as(t.id).textN), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
{
|
2024-06-15 02:04:08 +08:00
|
|
|
auto inner = t.inner_join(t.as(t.id)).on(t.textN == t.as(t.id).textN);
|
|
|
|
serialize(select(t.id).from(inner), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// distinct aggregate
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(count(sqlpp::distinct, t.id % 7), printer).str();
|
|
|
|
serialize(avg(sqlpp::distinct, t.id - 7), printer).str();
|
|
|
|
serialize(sum(sqlpp::distinct, t.id + 7), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2016-02-25 14:57:28 +08:00
|
|
|
serialize(select(all_of(t)).from(t).unconditionally(), printer).str();
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2016-02-25 14:57:28 +08:00
|
|
|
for (const auto& row : db(select(all_of(t)).from(t).unconditionally()))
|
2015-09-14 03:33:19 +08:00
|
|
|
{
|
2024-06-15 02:04:08 +08:00
|
|
|
serialize(t.id == row.id, printer);
|
|
|
|
serialize(t.textN == row.textN.value(), printer);
|
|
|
|
serialize(t.boolNn == row.boolNn, printer);
|
2015-09-14 03:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get_sql_name(t);
|
2024-06-15 02:04:08 +08:00
|
|
|
get_sql_name(t.id);
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2024-06-15 02:04:08 +08:00
|
|
|
flatten(t.id == 7, db);
|
2015-09-14 03:33:19 +08:00
|
|
|
|
|
|
|
printer.reset();
|
2024-06-15 02:04:08 +08:00
|
|
|
std::cerr << serialize(select(all_of(t)).from(t).where(t.id.in(select(f.intN).from(f).unconditionally())),
|
2016-09-07 04:34:59 +08:00
|
|
|
printer)
|
|
|
|
.str()
|
|
|
|
<< std::endl;
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2015-12-19 22:15:04 +08:00
|
|
|
printer.reset();
|
2024-06-15 02:04:08 +08:00
|
|
|
std::cerr << serialize(select(all_of(t)).from(t).where(t.id.in()), printer).str() << std::endl;
|
2015-12-19 22:15:04 +08:00
|
|
|
|
|
|
|
printer.reset();
|
2024-06-15 02:04:08 +08:00
|
|
|
std::cerr << serialize(select(all_of(t)).from(t).where(t.id.not_in()), printer).str() << std::endl;
|
2015-12-19 22:15:04 +08:00
|
|
|
|
2015-09-14 03:33:19 +08:00
|
|
|
auto schema = db.attach("lorem");
|
|
|
|
auto s = schema_qualified_table(schema, t).as(sqlpp::alias::x);
|
|
|
|
|
|
|
|
printer.reset();
|
2016-02-25 14:57:28 +08:00
|
|
|
std::cerr << serialize(select(all_of(s)).from(s).unconditionally(), printer).str() << std::endl;
|
2015-09-14 03:33:19 +08:00
|
|
|
|
2015-12-23 18:50:40 +08:00
|
|
|
printer.reset();
|
2024-06-15 02:04:08 +08:00
|
|
|
std::cerr << serialize(sqlpp::case_when(true).then(t.id).else_(t.id + 1).as(t.textN), printer).str()
|
2015-12-23 18:50:40 +08:00
|
|
|
<< std::endl;
|
|
|
|
|
2015-09-14 03:33:19 +08:00
|
|
|
return 0;
|
2014-01-11 07:11:47 +08:00
|
|
|
}
|