mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Simplified where() to have one argument only
This commit is contained in:
parent
1a93218dcc
commit
a836e8c8d8
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -42,19 +42,12 @@
|
||||
#include <sqlpp11/value_type.h>
|
||||
#include <sqlpp11/verbatim.h> // Csaba Csoma suggests: unsafe_sql instead of verbatim
|
||||
#include <sqlpp11/verbatim_table.h>
|
||||
#include <sqlpp11/value.h>
|
||||
#include <sqlpp11/value_or_null.h>
|
||||
#include <sqlpp11/eval.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template <typename T>
|
||||
auto value(T t) -> wrap_operand_t<T>
|
||||
{
|
||||
static_assert(is_wrapped_value_t<wrap_operand_t<T>>::value,
|
||||
"value() is to be called with non-sql-type like int, or string");
|
||||
return {t};
|
||||
}
|
||||
|
||||
template <typename Expression, typename Db>
|
||||
auto flatten(const Expression& exp, Db& db) -> verbatim_t<value_type_of<Expression>>
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -28,6 +28,7 @@
|
||||
#define SQLPP_HAVING_H
|
||||
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/value.h>
|
||||
#include <sqlpp11/expression.h>
|
||||
#include <sqlpp11/interpret_tuple.h>
|
||||
#include <sqlpp11/interpretable_list.h>
|
||||
@ -234,6 +235,11 @@ namespace sqlpp
|
||||
return _having_impl<_database_t>(_check<Expression>{}, expression);
|
||||
}
|
||||
|
||||
auto dynamic_having() const -> _new_statement_t<_check<boolean_operand>, having_t<_database_t, boolean_operand>>
|
||||
{
|
||||
return dynamic_having(::sqlpp::value(true));
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Database, typename Expression>
|
||||
auto _having_impl(const std::false_type&, Expression expression) const -> bad_statement;
|
||||
|
43
include/sqlpp11/value.h
Normal file
43
include/sqlpp11/value.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef SQLPP_VALUE_H
|
||||
#define SQLPP_VALUE_H
|
||||
|
||||
#include <sqlpp11/type_traits.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template <typename T>
|
||||
auto value(T t) -> wrap_operand_t<T>
|
||||
{
|
||||
static_assert(is_wrapped_value_t<wrap_operand_t<T>>::value,
|
||||
"value() is to be called with non-sql-type like int, or string");
|
||||
return {t};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -27,6 +27,7 @@
|
||||
#ifndef SQLPP_VERBATIM_TABLE_H
|
||||
#define SQLPP_VERBATIM_TABLE_H
|
||||
|
||||
#include <sqlpp11/table.h>
|
||||
#include <sqlpp11/data_types/no_value.h>
|
||||
|
||||
namespace sqlpp
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -29,6 +29,7 @@
|
||||
|
||||
#include <sqlpp11/statement_fwd.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/value.h>
|
||||
#include <sqlpp11/parameter_list.h>
|
||||
#include <sqlpp11/expression.h>
|
||||
#include <sqlpp11/interpret_tuple.h>
|
||||
@ -39,10 +40,10 @@
|
||||
namespace sqlpp
|
||||
{
|
||||
// WHERE DATA
|
||||
template <typename Database, typename... Expressions>
|
||||
template <typename Database, typename Expression>
|
||||
struct where_data_t
|
||||
{
|
||||
where_data_t(Expressions... expressions) : _expressions(expressions...)
|
||||
where_data_t(Expression expression) : _expression(expression)
|
||||
{
|
||||
}
|
||||
|
||||
@ -52,7 +53,7 @@ namespace sqlpp
|
||||
where_data_t& operator=(where_data_t&&) = default;
|
||||
~where_data_t() = default;
|
||||
|
||||
std::tuple<Expressions...> _expressions;
|
||||
Expression _expression;
|
||||
interpretable_list_t<Database> _dynamic_expressions;
|
||||
};
|
||||
|
||||
@ -61,16 +62,16 @@ namespace sqlpp
|
||||
"at least one expression in where() requires a table which is otherwise not known in the statement");
|
||||
|
||||
// WHERE(EXPR)
|
||||
template <typename Database, typename... Expressions>
|
||||
template <typename Database, typename Expression>
|
||||
struct where_t
|
||||
{
|
||||
using _traits = make_traits<no_value_t, tag::is_where>;
|
||||
using _nodes = detail::type_vector<Expressions...>;
|
||||
using _nodes = detail::type_vector<Expression>;
|
||||
|
||||
using _is_dynamic = is_database<Database>;
|
||||
|
||||
// Data
|
||||
using _data_t = where_data_t<Database, Expressions...>;
|
||||
using _data_t = where_data_t<Database, Expression>;
|
||||
|
||||
// Member implementation with data and methods
|
||||
template <typename Policies>
|
||||
@ -82,39 +83,39 @@ namespace sqlpp
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Expression>
|
||||
void add_ntc(Expression expression)
|
||||
template <typename Expr>
|
||||
void add_ntc(Expr expression)
|
||||
{
|
||||
add<Expression, std::false_type>(expression);
|
||||
add<Expr, std::false_type>(expression);
|
||||
}
|
||||
|
||||
template <typename Expression, typename TableCheckRequired = std::true_type>
|
||||
void add(Expression expression)
|
||||
template <typename Expr, typename TableCheckRequired = std::true_type>
|
||||
void add(Expr expression)
|
||||
{
|
||||
static_assert(_is_dynamic::value, "where::add() can only be called for dynamic_where");
|
||||
static_assert(is_expression_t<Expression>::value, "invalid expression argument in where::add()");
|
||||
static_assert(is_boolean_t<Expression>::value, "invalid expression argument in where::add()");
|
||||
static_assert(not TableCheckRequired::value or Policies::template _no_unknown_tables<Expression>::value,
|
||||
static_assert(is_expression_t<Expr>::value, "invalid expression argument in where::add()");
|
||||
static_assert(is_boolean_t<Expr>::value, "invalid expression argument in where::add()");
|
||||
static_assert(not TableCheckRequired::value or Policies::template _no_unknown_tables<Expr>::value,
|
||||
"expression uses tables unknown to this statement in where::add()");
|
||||
static_assert(not contains_aggregate_function_t<Expression>::value,
|
||||
static_assert(not contains_aggregate_function_t<Expr>::value,
|
||||
"where expression must not contain aggregate functions");
|
||||
using _serialize_check = sqlpp::serialize_check_t<typename Database::_serializer_context_t, Expression>;
|
||||
using _serialize_check = sqlpp::serialize_check_t<typename Database::_serializer_context_t, Expr>;
|
||||
_serialize_check::_();
|
||||
|
||||
using ok = logic::all_t<_is_dynamic::value, is_expression_t<Expression>::value, _serialize_check::type::value>;
|
||||
using ok = logic::all_t<_is_dynamic::value, is_expression_t<Expr>::value, _serialize_check::type::value>;
|
||||
|
||||
_add_impl(expression, ok()); // dispatch to prevent compile messages after the static_assert
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Expression>
|
||||
void _add_impl(Expression expression, const std::true_type&)
|
||||
template <typename Expr>
|
||||
void _add_impl(Expr expression, const std::true_type&)
|
||||
{
|
||||
return _data._dynamic_expressions.emplace_back(expression);
|
||||
}
|
||||
|
||||
template <typename Expression>
|
||||
void _add_impl(Expression expression, const std::false_type&);
|
||||
template <typename Expr>
|
||||
void _add_impl(Expr expression, const std::false_type&);
|
||||
|
||||
public:
|
||||
_data_t _data;
|
||||
@ -124,7 +125,7 @@ namespace sqlpp
|
||||
template <typename Policies>
|
||||
struct _base_t
|
||||
{
|
||||
using _data_t = where_data_t<Database, Expressions...>;
|
||||
using _data_t = where_data_t<Database, Expression>;
|
||||
|
||||
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
|
||||
template <typename... Args>
|
||||
@ -225,7 +226,6 @@ namespace sqlpp
|
||||
"where() argument has to be an sqlpp boolean expression.");
|
||||
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_no_aggregate_functions_t,
|
||||
"at least one aggregate function used in where()");
|
||||
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_static_count_args_t, "missing argument in where()");
|
||||
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_dynamic_statement_dynamic_t,
|
||||
"dynamic_where() must not be called in a static statement");
|
||||
|
||||
@ -240,30 +240,26 @@ namespace sqlpp
|
||||
// static_check_t<logic::all_t<is_boolean_t<Expressions>::value...>::value, assert_where_boolean_expression_t>,
|
||||
// static_check_t<logic::all_t<(not contains_aggregate_function_t<Expressions>::value)...>::value,
|
||||
// assert_where_no_aggregate_functions_t>>;
|
||||
template <typename... Expressions>
|
||||
template <typename Expression>
|
||||
struct check_where
|
||||
{
|
||||
using type = static_combined_check_t<
|
||||
static_check_t<logic::all_t<is_not_cpp_bool_t<Expressions>::value...>::value, assert_where_not_cpp_bool_t>,
|
||||
static_check_t<logic::all_t<detail::is_expression_impl<Expressions>::type::value...>::value,
|
||||
assert_where_boolean_expression_t>,
|
||||
static_check_t<logic::all_t<is_boolean_t<Expressions>::value...>::value, assert_where_boolean_expression_t>,
|
||||
static_check_t<logic::all_t<(not detail::contains_aggregate_function_impl<Expressions>::type::value)...>::value,
|
||||
assert_where_no_aggregate_functions_t>>;
|
||||
static_check_t<is_not_cpp_bool_t<Expression>::value, assert_where_not_cpp_bool_t>,
|
||||
static_check_t<is_expression_t<Expression>::value, assert_where_boolean_expression_t>,
|
||||
static_check_t<is_boolean_t<Expression>::value, assert_where_boolean_expression_t>,
|
||||
static_check_t<not contains_aggregate_function_t<Expression>::value, assert_where_no_aggregate_functions_t>>;
|
||||
};
|
||||
|
||||
template <typename... Expressions>
|
||||
using check_where_t = typename check_where<Expressions...>::type;
|
||||
template <typename Expression>
|
||||
using check_where_t = typename check_where<Expression>::type;
|
||||
|
||||
template <typename... Expressions>
|
||||
using check_where_static_t =
|
||||
static_combined_check_t<check_where_t<Expressions...>,
|
||||
static_check_t<sizeof...(Expressions) != 0, assert_where_static_count_args_t>>;
|
||||
template <typename Expression>
|
||||
using check_where_static_t = check_where_t<Expression>;
|
||||
|
||||
template <typename Database, typename... Expressions>
|
||||
template <typename Database, typename Expression>
|
||||
using check_where_dynamic_t = static_combined_check_t<
|
||||
static_check_t<not std::is_same<Database, void>::value, assert_where_dynamic_statement_dynamic_t>,
|
||||
check_where_t<Expressions...>>;
|
||||
check_where_t<Expression>>;
|
||||
|
||||
// NO WHERE YET
|
||||
template <bool WhereRequired>
|
||||
@ -332,54 +328,58 @@ namespace sqlpp
|
||||
return {static_cast<const derived_statement_t<Policies>&>(*this), where_data_t<void, unconditional_t>{}};
|
||||
}
|
||||
|
||||
template <typename... Expressions>
|
||||
auto where(Expressions... expressions) const
|
||||
-> _new_statement_t<check_where_static_t<Expressions...>, where_t<void, Expressions...>>
|
||||
template <typename Expression>
|
||||
auto where(Expression expression) const
|
||||
-> _new_statement_t<check_where_static_t<Expression>, where_t<void, Expression>>
|
||||
{
|
||||
using Check = check_where_static_t<Expressions...>;
|
||||
using Check = check_where_static_t<Expression>;
|
||||
Check{}._();
|
||||
|
||||
return _where_impl<void>(Check{}, expressions...);
|
||||
return _where_impl<void>(Check{}, expression);
|
||||
}
|
||||
|
||||
template <typename... Expressions>
|
||||
auto dynamic_where(Expressions... expressions) const
|
||||
-> _new_statement_t<check_where_dynamic_t<_database_t, Expressions...>, where_t<_database_t, Expressions...>>
|
||||
template <typename Expression>
|
||||
auto dynamic_where(Expression expression) const
|
||||
-> _new_statement_t<check_where_dynamic_t<_database_t, Expression>, where_t<_database_t, Expression>>
|
||||
{
|
||||
using Check = check_where_dynamic_t<_database_t, Expressions...>;
|
||||
using Check = check_where_dynamic_t<_database_t, Expression>;
|
||||
Check{}._();
|
||||
|
||||
return _where_impl<_database_t>(Check{}, expressions...);
|
||||
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>>
|
||||
{
|
||||
return dynamic_where(::sqlpp::value(true));
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Database, typename... Expressions>
|
||||
auto _where_impl(const std::false_type&, Expressions... expressions) const -> bad_statement;
|
||||
template <typename Database, typename Expression>
|
||||
auto _where_impl(const std::false_type&, Expression expression) const -> bad_statement;
|
||||
|
||||
template <typename Database, typename... Expressions>
|
||||
auto _where_impl(const std::true_type&, Expressions... expressions) const
|
||||
-> _new_statement_t<std::true_type, where_t<Database, Expressions...>>
|
||||
template <typename Database, typename Expression>
|
||||
auto _where_impl(const std::true_type&, Expression expression) const
|
||||
-> _new_statement_t<std::true_type, where_t<Database, Expression>>
|
||||
{
|
||||
return {static_cast<const derived_statement_t<Policies>&>(*this),
|
||||
where_data_t<Database, Expressions...>{expressions...}};
|
||||
where_data_t<Database, Expression>{expression}};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Interpreters
|
||||
template <typename Context, typename Database, typename... Expressions>
|
||||
struct serializer_t<Context, where_data_t<Database, Expressions...>>
|
||||
template <typename Context, typename Database, typename Expression>
|
||||
struct serializer_t<Context, where_data_t<Database, Expression>>
|
||||
{
|
||||
using _serialize_check = serialize_check_of<Context, Expressions...>;
|
||||
using T = where_data_t<Database, Expressions...>;
|
||||
using _serialize_check = serialize_check_of<Context, Expression>;
|
||||
using T = where_data_t<Database, Expression>;
|
||||
|
||||
static Context& _(const T& t, Context& context)
|
||||
{
|
||||
if (sizeof...(Expressions) == 0 and t._dynamic_expressions.empty())
|
||||
return context;
|
||||
context << " WHERE ";
|
||||
interpret_tuple(t._expressions, " AND ", context);
|
||||
if (sizeof...(Expressions) and not t._dynamic_expressions.empty())
|
||||
serialize(t._expression, context);
|
||||
if (not t._dynamic_expressions.empty())
|
||||
context << " AND ";
|
||||
interpret_list(t._dynamic_expressions, " AND ", context);
|
||||
return context;
|
||||
|
@ -43,31 +43,31 @@ namespace
|
||||
T::_print_me_;
|
||||
}
|
||||
|
||||
template <typename Assert, typename... Expressions>
|
||||
void where_static_check(const Expressions&... expressions)
|
||||
template <typename Assert, typename Expression>
|
||||
void where_static_check(const Expression& expression)
|
||||
{
|
||||
using CheckResult = sqlpp::check_where_static_t<Expressions...>;
|
||||
using CheckResult = sqlpp::check_where_static_t<Expression>;
|
||||
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
|
||||
print_type_on_error<CheckResult>(ExpectedCheckResult{});
|
||||
static_assert(ExpectedCheckResult::value, "Unexpected check result");
|
||||
|
||||
using ReturnType = decltype(remove_from(t).where(expressions...));
|
||||
using ReturnType = decltype(remove_from(t).where(expression));
|
||||
using ExpectedReturnType =
|
||||
sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>;
|
||||
print_type_on_error<ReturnType>(ExpectedReturnType{});
|
||||
static_assert(ExpectedReturnType::value, "Unexpected return type");
|
||||
}
|
||||
|
||||
template <typename Assert, typename... Expressions>
|
||||
void where_dynamic_check(const Expressions&... expressions)
|
||||
template <typename Assert, typename Expression>
|
||||
void where_dynamic_check(const Expression& expression)
|
||||
{
|
||||
static auto db = MockDb{};
|
||||
using CheckResult = sqlpp::check_where_dynamic_t<decltype(db), Expressions...>;
|
||||
using CheckResult = sqlpp::check_where_dynamic_t<decltype(db), Expression>;
|
||||
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
|
||||
print_type_on_error<CheckResult>(ExpectedCheckResult{});
|
||||
static_assert(ExpectedCheckResult::value, "Unexpected check result");
|
||||
|
||||
using ReturnType = decltype(dynamic_remove_from(db, t).dynamic_where(expressions...));
|
||||
using ReturnType = decltype(dynamic_remove_from(db, t).dynamic_where(expression));
|
||||
using ExpectedReturnType =
|
||||
sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>;
|
||||
print_type_on_error<ReturnType>(ExpectedReturnType{});
|
||||
@ -80,9 +80,6 @@ namespace
|
||||
where_static_check<sqlpp::consistent_t>(t.gamma);
|
||||
where_static_check<sqlpp::consistent_t>(t.gamma == true);
|
||||
|
||||
// Try no expression
|
||||
where_static_check<sqlpp::assert_where_static_count_args_t>();
|
||||
|
||||
// Try assignment as condition
|
||||
where_static_check<sqlpp::assert_where_boolean_expression_t>(t.gamma = true);
|
||||
|
||||
@ -110,7 +107,6 @@ namespace
|
||||
void dynamic_where()
|
||||
{
|
||||
// OK
|
||||
where_dynamic_check<sqlpp::consistent_t>();
|
||||
where_dynamic_check<sqlpp::consistent_t>(t.gamma);
|
||||
where_dynamic_check<sqlpp::consistent_t>(t.gamma == true);
|
||||
|
||||
@ -138,7 +134,7 @@ namespace
|
||||
case_when(count(t.alpha) > 0).then(t.gamma).else_(not t.gamma));
|
||||
|
||||
// Try dynamic_where on a non-dynamic remove
|
||||
using CheckResult = sqlpp::check_where_dynamic_t<void>;
|
||||
using CheckResult = sqlpp::check_where_dynamic_t<void, sqlpp::boolean_operand>;
|
||||
using ExpectedCheckResult = std::is_same<CheckResult, sqlpp::assert_where_dynamic_statement_dynamic_t>;
|
||||
print_type_on_error<CheckResult>(ExpectedCheckResult{});
|
||||
static_assert(ExpectedCheckResult::value, "Unexpected check result");
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
Loading…
Reference in New Issue
Block a user