0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Fix serialization of empty dynamic_where

It was serialized with a dummy expression, which turned out to be
problematic for some vendors, see #282
This commit is contained in:
rbock 2019-08-20 10:31:54 +02:00
parent a3970ceae9
commit 43ec2343c3
4 changed files with 95 additions and 8 deletions

View File

@ -256,6 +256,10 @@ namespace sqlpp
static_check_t<not std::is_same<Database, void>::value, assert_where_dynamic_used_with_dynamic_statement_t>,
check_where_t<Expression>>;
template <typename Database>
using check_where_empty_dynamic_t = static_combined_check_t<
static_check_t<not std::is_same<Database, void>::value, assert_where_dynamic_used_with_dynamic_statement_t>>;
// NO WHERE YET
template <bool WhereRequired>
struct no_where_t
@ -338,10 +342,11 @@ namespace sqlpp
return _where_impl<_database_t>(Check{}, expression);
}
auto dynamic_where() const -> _new_statement_t<check_where_dynamic_t<_database_t, boolean_operand>,
where_t<_database_t, boolean_operand>>
auto dynamic_where() const
-> _new_statement_t<check_where_empty_dynamic_t<_database_t>, where_t<_database_t, unconditional_t>>
{
return dynamic_where(::sqlpp::value(true));
return {static_cast<const derived_statement_t<Policies>&>(*this),
where_data_t<_database_t, unconditional_t>{unconditional_t{}}};
}
private:
@ -378,6 +383,24 @@ namespace sqlpp
}
};
template <typename Context, typename Database>
struct serializer_t<Context, where_data_t<Database, unconditional_t>>
{
using _serialize_check = consistent_t;
using T = where_data_t<Database, unconditional_t>;
static Context& _(const T& t, Context& context)
{
if (t._dynamic_expressions.empty())
{
return context;
}
context << " WHERE ";
interpret_list(t._dynamic_expressions, " AND ", context);
return context;
}
};
template <typename Context>
struct serializer_t<Context, where_data_t<void, unconditional_t>>
{
@ -403,6 +426,13 @@ namespace sqlpp
return statement_t<Database, no_where_t<false>>().dynamic_where(std::forward<T>(t));
}
template <typename Database>
auto dynamic_where(const Database & /*unused*/)
-> decltype(statement_t<Database, no_where_t<false>>().dynamic_where())
{
return statement_t<Database, no_where_t<false>>().dynamic_where();
}
inline auto unconditionally() -> decltype(statement_t<void, no_where_t<false>>().unconditionally())
{
return statement_t<void, no_where_t<false>>().unconditionally();

View File

@ -26,12 +26,13 @@ set(test_serializer_names
As
Blob
CustomQuery
ForUpdate
From
In
Insert
DynamicWhere
ForUpdate
From
In
Insert
TableAlias
Where
Where
)
create_test_sourcelist(test_serializer_sources test_serializer_main.cpp ${test_serializer_names})

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2016-2019, 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 "compare.h"
#include "Sample.h"
#include <sqlpp11/sqlpp11.h>
#include <iostream>
int DynamicWhere(int, char*[])
{
const auto bar = test::TabBar{};
auto db = MockDb{};
compare(__LINE__, dynamic_where(db), "");
compare(__LINE__, dynamic_where(db, bar.gamma), " WHERE tab_bar.gamma");
{
auto statement = sqlpp::dynamic_where(db);
statement.where.add(without_table_check(bar.gamma));
compare(__LINE__, statement, " WHERE tab_bar.gamma");
}
{
auto statement = dynamic_where(db, bar.gamma);
statement.where.add(without_table_check(bar.gamma));
compare(__LINE__, statement, " WHERE tab_bar.gamma AND tab_bar.gamma");
}
return 0;
}

View File

@ -66,6 +66,9 @@ int Update(int, char*[])
auto values = [&t]() { return std::make_tuple(t.delta += t.alpha, t.beta = "no cake this time"); };
db(update(t).set(t.delta = sqlpp::verbatim<sqlpp::integer>("17+4")).unconditionally());
db(update(t)
.set(t.delta = sqlpp::verbatim<sqlpp::integer>("17+4"))
.where(sqlpp::verbatim<sqlpp::text>("'hansi'") == "hansi"));
db(update(t).set(t.delta = sqlpp::null).unconditionally());
db(update(t).set(t.delta = sqlpp::default_value).unconditionally());