/* * Copyright (c) 2013-2016, 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 "MockDb.h" #include "Sample.h" #include "is_regular.h" #include #include #include #include #include #include #include #include "../../include/test_helpers.h" struct to_cerr { template auto operator()(const Field& field) const -> void { std::cerr << field << std::endl; } }; template void print_row(Row const& row) { const sqlpp::compat::optional a = row.id; const sqlpp::compat::optional b = row.textN; std::cout << a << ", " << b << std::endl; } SQLPP_ALIAS_PROVIDER(cheese) int Select(int, char*[]) { MockDb db = {}; MockDb::_serializer_context_t printer = {}; const auto f = test::TabFoo{}; const auto t = test::TabBar{}; const auto tab_a = f.as(sqlpp::alias::a); select(count(t.id)); select(sqlpp::count(1)); select(count(sqlpp::value(1))); std::cerr << serialize(select(sqlpp::value(false).as(sqlpp::alias::a)), printer).str() << std::endl; for (const auto& row : db(select(sqlpp::value(false).as(sqlpp::alias::a)))) { std::cout << row.a << std::endl; } { // using stl algorithms auto rows = db(select(all_of(t)).from(t).unconditionally()); // nicer in C++14 std::for_each(rows.begin(), rows.end(), &print_row); } for (const auto& row : db(select(all_of(t)).from(t).unconditionally())) { const sqlpp::compat::optional a = row.id; const sqlpp::compat::optional b = row.textN; std::cout << a << ", " << b << std::endl; } for (const auto& row : db(select(all_of(t), t.boolNn.as(t)).from(t).where(t.id > 7 and trim(t.textN) == "test").for_update())) { const sqlpp::compat::optional a = row.id; const sqlpp::compat::optional b = row.textN; const bool g = row.tabBar; std::cout << a << ", " << b << ", " << g << std::endl; } for (const auto& row : db(select(all_of(t), f.textNnD).from(t.join(f).on(t.id > f.doubleN and not t.boolNn)).unconditionally())) { std::cout << row.id << std::endl; } for (const auto& row : db(select(all_of(t), f.textNnD) .from(t.join(f).on(t.id > f.doubleN).join(tab_a).on(t.id == tab_a.doubleN)) .unconditionally())) { std::cout << row.id << std::endl; } for (const auto& row : db(select(sqlpp::count(1), avg(t.id)).from(t).unconditionally())) { std::cout << row.count << std::endl; } for (const auto& row : db(select(count(t.id), avg(t.id)).from(t).where(t.id == 0))) { std::cout << row.count << std::endl; } auto stat = sqlpp::select() .columns(all_of(t)) .flags(sqlpp::all) .from(t) .where(t.id > 0) .group_by(t.id) .order_by(t.boolNn.asc()) .having(t.boolNn) .offset(19u) .limit(7u); printer.reset(); std::cerr << serialize(stat, printer).str() << std::endl; auto s = sqlpp::select() .columns(t.id) .flags(sqlpp::distinct) .from(t) .where(t.id > 3) .group_by(t.id) .order_by(t.textN.asc()) .having(sum(t.id) > parameter(t.intN)) .limit(32u) .offset(7u); #warning add tests for optional everything for (const auto& row : db(db.prepare(s))) { const sqlpp::compat::optional a = row.id; std::cout << a << std::endl; } printer.reset(); std::cerr << serialize(s, printer).str() << std::endl; select(sqlpp::value(7).as(t.id)); for (const auto& row : db(select(sqlpp::case_when(true).then(t.textN).else_(sqlpp::null).as(t.textN)).from(t).unconditionally())) { std::cerr << row.textN << std::endl; } for (const auto& row : db(select(all_of(t)).from(t).unconditionally())) { for_each_field(row, to_cerr{}); } { auto transaction = start_transaction(db, sqlpp::isolation_level::read_committed); if (db._mock_data._last_isolation_level != sqlpp::isolation_level::read_committed) { std::cout << "Error: transaction isolation level does not match expected level" << std::endl; } } db.set_default_isolation_level(sqlpp::isolation_level::read_uncommitted); { auto transaction = start_transaction(db); if (db._mock_data._last_isolation_level != sqlpp::isolation_level::read_uncommitted) { std::cout << "Error: transaction isolation level does not match default level" << std::endl; } } for (const auto& row : db(select(f.doubleN, select(count(t.id)).from(t).unconditionally().as(cheese)).from(f).unconditionally())) { std::cout << row.doubleN << " " << row.cheese << std::endl; } // checking #584 auto abs = db.prepare(select(t.alpha).from(t).where(sqlpp::parameterized_verbatim( "ABS(field1 -", sqlpp::parameter(t.alpha), ")") <= sqlpp::parameter(sqlpp::unsigned_integral(), param2))); abs.params.alpha = 7; abs.params.param2 = 7; return 0; }