0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Simplified where() to have one argument only

This commit is contained in:
rbock 2016-03-16 07:58:12 +01:00
parent 1a93218dcc
commit a836e8c8d8
8 changed files with 128 additions and 89 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -42,19 +42,12 @@
#include <sqlpp11/value_type.h> #include <sqlpp11/value_type.h>
#include <sqlpp11/verbatim.h> // Csaba Csoma suggests: unsafe_sql instead of verbatim #include <sqlpp11/verbatim.h> // Csaba Csoma suggests: unsafe_sql instead of verbatim
#include <sqlpp11/verbatim_table.h> #include <sqlpp11/verbatim_table.h>
#include <sqlpp11/value.h>
#include <sqlpp11/value_or_null.h> #include <sqlpp11/value_or_null.h>
#include <sqlpp11/eval.h> #include <sqlpp11/eval.h>
namespace sqlpp 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> template <typename Expression, typename Db>
auto flatten(const Expression& exp, Db& db) -> verbatim_t<value_type_of<Expression>> auto flatten(const Expression& exp, Db& db) -> verbatim_t<value_type_of<Expression>>
{ {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -28,6 +28,7 @@
#define SQLPP_HAVING_H #define SQLPP_HAVING_H
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/value.h>
#include <sqlpp11/expression.h> #include <sqlpp11/expression.h>
#include <sqlpp11/interpret_tuple.h> #include <sqlpp11/interpret_tuple.h>
#include <sqlpp11/interpretable_list.h> #include <sqlpp11/interpretable_list.h>
@ -234,6 +235,11 @@ namespace sqlpp
return _having_impl<_database_t>(_check<Expression>{}, expression); 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: private:
template <typename Database, typename Expression> template <typename Database, typename Expression>
auto _having_impl(const std::false_type&, Expression expression) const -> bad_statement; auto _having_impl(const std::false_type&, Expression expression) const -> bad_statement;

43
include/sqlpp11/value.h Normal file
View 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

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -27,6 +27,7 @@
#ifndef SQLPP_VERBATIM_TABLE_H #ifndef SQLPP_VERBATIM_TABLE_H
#define SQLPP_VERBATIM_TABLE_H #define SQLPP_VERBATIM_TABLE_H
#include <sqlpp11/table.h>
#include <sqlpp11/data_types/no_value.h> #include <sqlpp11/data_types/no_value.h>
namespace sqlpp namespace sqlpp

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -29,6 +29,7 @@
#include <sqlpp11/statement_fwd.h> #include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/value.h>
#include <sqlpp11/parameter_list.h> #include <sqlpp11/parameter_list.h>
#include <sqlpp11/expression.h> #include <sqlpp11/expression.h>
#include <sqlpp11/interpret_tuple.h> #include <sqlpp11/interpret_tuple.h>
@ -39,10 +40,10 @@
namespace sqlpp namespace sqlpp
{ {
// WHERE DATA // WHERE DATA
template <typename Database, typename... Expressions> template <typename Database, typename Expression>
struct where_data_t 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& operator=(where_data_t&&) = default;
~where_data_t() = default; ~where_data_t() = default;
std::tuple<Expressions...> _expressions; Expression _expression;
interpretable_list_t<Database> _dynamic_expressions; 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"); "at least one expression in where() requires a table which is otherwise not known in the statement");
// WHERE(EXPR) // WHERE(EXPR)
template <typename Database, typename... Expressions> template <typename Database, typename Expression>
struct where_t struct where_t
{ {
using _traits = make_traits<no_value_t, tag::is_where>; 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>; using _is_dynamic = is_database<Database>;
// Data // Data
using _data_t = where_data_t<Database, Expressions...>; using _data_t = where_data_t<Database, Expression>;
// Member implementation with data and methods // Member implementation with data and methods
template <typename Policies> template <typename Policies>
@ -82,39 +83,39 @@ namespace sqlpp
{ {
} }
template <typename Expression> template <typename Expr>
void add_ntc(Expression expression) 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> template <typename Expr, typename TableCheckRequired = std::true_type>
void add(Expression expression) void add(Expr expression)
{ {
static_assert(_is_dynamic::value, "where::add() can only be called for dynamic_where"); 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_expression_t<Expr>::value, "invalid expression argument in where::add()");
static_assert(is_boolean_t<Expression>::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<Expression>::value, static_assert(not TableCheckRequired::value or Policies::template _no_unknown_tables<Expr>::value,
"expression uses tables unknown to this statement in where::add()"); "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"); "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::_(); _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 _add_impl(expression, ok()); // dispatch to prevent compile messages after the static_assert
} }
private: private:
template <typename Expression> template <typename Expr>
void _add_impl(Expression expression, const std::true_type&) void _add_impl(Expr expression, const std::true_type&)
{ {
return _data._dynamic_expressions.emplace_back(expression); return _data._dynamic_expressions.emplace_back(expression);
} }
template <typename Expression> template <typename Expr>
void _add_impl(Expression expression, const std::false_type&); void _add_impl(Expr expression, const std::false_type&);
public: public:
_data_t _data; _data_t _data;
@ -124,7 +125,7 @@ namespace sqlpp
template <typename Policies> template <typename Policies>
struct _base_t 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 // workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
template <typename... Args> template <typename... Args>
@ -225,7 +226,6 @@ namespace sqlpp
"where() argument has to be an sqlpp boolean expression."); "where() argument has to be an sqlpp boolean expression.");
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_no_aggregate_functions_t, SQLPP_PORTABLE_STATIC_ASSERT(assert_where_no_aggregate_functions_t,
"at least one aggregate function used in where()"); "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, SQLPP_PORTABLE_STATIC_ASSERT(assert_where_dynamic_statement_dynamic_t,
"dynamic_where() must not be called in a static statement"); "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<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, // static_check_t<logic::all_t<(not contains_aggregate_function_t<Expressions>::value)...>::value,
// assert_where_no_aggregate_functions_t>>; // assert_where_no_aggregate_functions_t>>;
template <typename... Expressions> template <typename Expression>
struct check_where struct check_where
{ {
using type = static_combined_check_t< 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<is_not_cpp_bool_t<Expression>::value, assert_where_not_cpp_bool_t>,
static_check_t<logic::all_t<detail::is_expression_impl<Expressions>::type::value...>::value, static_check_t<is_expression_t<Expression>::value, assert_where_boolean_expression_t>,
assert_where_boolean_expression_t>, static_check_t<is_boolean_t<Expression>::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<not contains_aggregate_function_t<Expression>::value, assert_where_no_aggregate_functions_t>>;
static_check_t<logic::all_t<(not detail::contains_aggregate_function_impl<Expressions>::type::value)...>::value,
assert_where_no_aggregate_functions_t>>;
}; };
template <typename... Expressions> template <typename Expression>
using check_where_t = typename check_where<Expressions...>::type; using check_where_t = typename check_where<Expression>::type;
template <typename... Expressions> template <typename Expression>
using check_where_static_t = using check_where_static_t = check_where_t<Expression>;
static_combined_check_t<check_where_t<Expressions...>,
static_check_t<sizeof...(Expressions) != 0, assert_where_static_count_args_t>>;
template <typename Database, typename... Expressions> template <typename Database, typename Expression>
using check_where_dynamic_t = static_combined_check_t< 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>, 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 // NO WHERE YET
template <bool WhereRequired> template <bool WhereRequired>
@ -332,54 +328,58 @@ namespace sqlpp
return {static_cast<const derived_statement_t<Policies>&>(*this), where_data_t<void, unconditional_t>{}}; return {static_cast<const derived_statement_t<Policies>&>(*this), where_data_t<void, unconditional_t>{}};
} }
template <typename... Expressions> template <typename Expression>
auto where(Expressions... expressions) const auto where(Expression expression) const
-> _new_statement_t<check_where_static_t<Expressions...>, where_t<void, Expressions...>> -> _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{}._(); Check{}._();
return _where_impl<void>(Check{}, expressions...); return _where_impl<void>(Check{}, expression);
} }
template <typename... Expressions> template <typename Expression>
auto dynamic_where(Expressions... expressions) const auto dynamic_where(Expression expression) const
-> _new_statement_t<check_where_dynamic_t<_database_t, Expressions...>, where_t<_database_t, Expressions...>> -> _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{}._(); 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: private:
template <typename Database, typename... Expressions> template <typename Database, typename Expression>
auto _where_impl(const std::false_type&, Expressions... expressions) const -> bad_statement; auto _where_impl(const std::false_type&, Expression expression) const -> bad_statement;
template <typename Database, typename... Expressions> template <typename Database, typename Expression>
auto _where_impl(const std::true_type&, Expressions... expressions) const auto _where_impl(const std::true_type&, Expression expression) const
-> _new_statement_t<std::true_type, where_t<Database, Expressions...>> -> _new_statement_t<std::true_type, where_t<Database, Expression>>
{ {
return {static_cast<const derived_statement_t<Policies>&>(*this), return {static_cast<const derived_statement_t<Policies>&>(*this),
where_data_t<Database, Expressions...>{expressions...}}; where_data_t<Database, Expression>{expression}};
} }
}; };
}; };
// Interpreters // Interpreters
template <typename Context, typename Database, typename... Expressions> template <typename Context, typename Database, typename Expression>
struct serializer_t<Context, where_data_t<Database, Expressions...>> struct serializer_t<Context, where_data_t<Database, Expression>>
{ {
using _serialize_check = serialize_check_of<Context, Expressions...>; using _serialize_check = serialize_check_of<Context, Expression>;
using T = where_data_t<Database, Expressions...>; using T = where_data_t<Database, Expression>;
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
if (sizeof...(Expressions) == 0 and t._dynamic_expressions.empty())
return context;
context << " WHERE "; context << " WHERE ";
interpret_tuple(t._expressions, " AND ", context); serialize(t._expression, context);
if (sizeof...(Expressions) and not t._dynamic_expressions.empty()) if (not t._dynamic_expressions.empty())
context << " AND "; context << " AND ";
interpret_list(t._dynamic_expressions, " AND ", context); interpret_list(t._dynamic_expressions, " AND ", context);
return context; return context;

View File

@ -43,31 +43,31 @@ namespace
T::_print_me_; T::_print_me_;
} }
template <typename Assert, typename... Expressions> template <typename Assert, typename Expression>
void where_static_check(const Expressions&... expressions) 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>; using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
print_type_on_error<CheckResult>(ExpectedCheckResult{}); print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result"); 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 = using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>; sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>;
print_type_on_error<ReturnType>(ExpectedReturnType{}); print_type_on_error<ReturnType>(ExpectedReturnType{});
static_assert(ExpectedReturnType::value, "Unexpected return type"); static_assert(ExpectedReturnType::value, "Unexpected return type");
} }
template <typename Assert, typename... Expressions> template <typename Assert, typename Expression>
void where_dynamic_check(const Expressions&... expressions) void where_dynamic_check(const Expression& expression)
{ {
static auto db = MockDb{}; 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>; using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
print_type_on_error<CheckResult>(ExpectedCheckResult{}); print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result"); 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 = using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>; sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>;
print_type_on_error<ReturnType>(ExpectedReturnType{}); 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);
where_static_check<sqlpp::consistent_t>(t.gamma == true); 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 // Try assignment as condition
where_static_check<sqlpp::assert_where_boolean_expression_t>(t.gamma = true); where_static_check<sqlpp::assert_where_boolean_expression_t>(t.gamma = true);
@ -110,7 +107,6 @@ namespace
void dynamic_where() void dynamic_where()
{ {
// OK // OK
where_dynamic_check<sqlpp::consistent_t>();
where_dynamic_check<sqlpp::consistent_t>(t.gamma); where_dynamic_check<sqlpp::consistent_t>(t.gamma);
where_dynamic_check<sqlpp::consistent_t>(t.gamma == true); 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)); case_when(count(t.alpha) > 0).then(t.gamma).else_(not t.gamma));
// Try dynamic_where on a non-dynamic remove // 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>; using ExpectedCheckResult = std::is_same<CheckResult, sqlpp::assert_where_dynamic_statement_dynamic_t>;
print_type_on_error<CheckResult>(ExpectedCheckResult{}); print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result"); static_assert(ExpectedCheckResult::value, "Unexpected check result");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,