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

Turned a few operators into free functions.

This might be problematic since they are not selective enough. In
particular, they even take the result values...
This commit is contained in:
rbock 2015-11-08 15:12:40 +01:00
parent 12f7457775
commit 5556880bb9
8 changed files with 277 additions and 28 deletions

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2013-2015, 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_BAD_EXPRESSION_H
#define SQLPP_BAD_EXPRESSION_H
#include <sqlpp11/portable_static_assert.h>
#include <sqlpp11/type_traits.h>
namespace sqlpp
{
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_operands, "Invalid operand(s)");
template <typename ValueType>
struct bad_expression
{
template <typename... T>
bad_expression(T&&...)
{
}
using _traits = make_traits<ValueType, tag::is_expression>;
using _nodes = detail::type_vector<>;
};
template <typename Context, typename ValueType>
struct serializer_t<Context, bad_expression<ValueType>>
{
using _serialize_check = assert_valid_operands;
using T = bad_expression<ValueType>;
static Context& _(const T&, Context&);
};
}
#endif

View File

@ -33,7 +33,7 @@ namespace sqlpp
{ {
struct boolean struct boolean
{ {
using _traits = make_traits<void, tag::is_value_type>; // using _traits = make_traits<void, tag::is_value_type>;
using _cpp_value_type = bool; using _cpp_value_type = bool;
template <typename T> template <typename T>

View File

@ -27,39 +27,38 @@
#ifndef SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H #ifndef SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H
#define SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H #define SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H
#include <sqlpp11/operators.h>
#include <sqlpp11/expression_return_types.h>
#include <sqlpp11/operand_check.h>
#include <sqlpp11/expression_operators.h> #include <sqlpp11/expression_operators.h>
#include <sqlpp11/basic_expression_operators.h> #include <sqlpp11/basic_expression_operators.h>
namespace sqlpp namespace sqlpp
{ {
template <typename Base> template <typename Expression>
struct expression_operators<Base, boolean> : public basic_expression_operators<Base, boolean> struct expression_operators<Expression, boolean> : public basic_expression_operators<Expression, boolean>
{ {
template <typename T> };
using _is_valid_operand = is_valid_operand<boolean, T>;
template <typename T> template <typename L, typename R>
logical_and_t<Base, wrap_operand_t<T>> operator and(T t) const struct return_type_and<L, R, binary_operand_check_t<L, is_boolean_t, R, is_boolean_t>>
{ {
using rhs = wrap_operand_t<T>; using check = consistent_t;
static_assert(_is_valid_operand<rhs>::value, "invalid rhs operand"); using type = logical_and_t<wrap_operand_t<L>, wrap_operand_t<R>>;
};
return {*static_cast<const Base*>(this), rhs{t}}; template <typename L, typename R>
} struct return_type_or<L, R, binary_operand_check_t<L, is_boolean_t, R, is_boolean_t>>
{
using check = consistent_t;
using type = logical_or_t<wrap_operand_t<L>, wrap_operand_t<R>>;
};
template <typename T> template <typename T>
logical_or_t<Base, wrap_operand_t<T>> operator or(T t) const struct return_type_not<T, unary_operand_check_t<T, is_boolean_t>>
{ {
using rhs = wrap_operand_t<T>; using check = consistent_t;
static_assert(_is_valid_operand<rhs>::value, "invalid rhs operand"); using type = logical_not_t<wrap_operand_t<T>>;
return {*static_cast<const Base*>(this), rhs{t}};
}
logical_not_t<Base> operator not() const
{
return {*static_cast<const Base*>(this)};
}
}; };
} }

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2013-2015, 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_EXPRESSION_RETURN_TYPES_H
#define SQLPP_EXPRESSION_RETURN_TYPES_H
#include <sqlpp11/bad_expression.h>
namespace sqlpp
{
template <typename L, typename R, typename Enable = void>
struct return_type_and
{
using check = assert_valid_operands;
using type = bad_expression<boolean>;
};
template <typename L, typename R>
using return_type_and_t = typename return_type_and<L, R>::type;
template <typename L, typename R, typename Enable = void>
struct return_type_or
{
using check = assert_valid_operands;
using type = bad_expression<boolean>;
};
template <typename L, typename R>
using return_type_or_t = typename return_type_or<L, R>::type;
template <typename T, typename Enable = void>
struct return_type_not
{
using check = assert_valid_operands;
using type = bad_expression<boolean>;
};
template <typename T>
using return_type_not_t = typename return_type_not<T>::type;
}
#endif

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2013-2015, 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_OPERAND_CHECK_H
#define SQLPP_OPERAND_CHECK_H
#include <sqlpp11/wrap_operand.h>
#include <sqlpp11/detail/enable_if.h>
namespace sqlpp
{
template <typename T, template <typename> class Pred, typename Enable = void>
struct unary_operand_check
{
};
template <typename T, template <typename> class Pred>
struct unary_operand_check<T, Pred, detail::enable_if_t<Pred<wrap_operand_t<T>>::value>>
{
using type = void;
};
template <typename T, template <typename> class Pred>
using unary_operand_check_t = typename unary_operand_check<T, Pred>::type;
template <typename L,
template <typename> class LPred,
typename R,
template <typename> class RPred,
typename Enable = void>
struct binary_operand_check
{
};
template <typename L, template <typename> class LPred, typename R, template <typename> class RPred>
struct binary_operand_check<L,
LPred,
R,
RPred,
detail::enable_if_t<LPred<wrap_operand_t<L>>::value and RPred<wrap_operand_t<R>>::value>>
{
using type = void;
};
template <typename L, template <typename> class LPred, typename R, template <typename> class RPred>
using binary_operand_check_t = typename binary_operand_check<L, LPred, R, RPred>::type;
}
#endif

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2013-2015, 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_OPERATORS_H
#define SQLPP_OPERATORS_H
#include <sqlpp11/wrap_operand.h>
#include <sqlpp11/expression_return_types.h>
namespace sqlpp
{
template <typename L, typename R>
auto operator and(const L& l, const R& r) -> return_type_and_t<L, R>
{
return_type_and<L, R>::check::_();
return {wrap_operand_t<L>{l}, wrap_operand_t<R>{r}};
}
template <typename L, typename R>
auto operator or(const L& l, const R& r) -> return_type_or_t<L, R>
{
return_type_or<L, R>::check::_();
return {wrap_operand_t<L>{l}, wrap_operand_t<R>{r}};
}
template <typename T>
auto operator not(const T& t) -> return_type_not_t<T>
{
return_type_not<T>::check::_();
return {wrap_operand_t<T>{t}};
}
}
#endif

View File

@ -43,7 +43,7 @@ namespace sqlpp
template <typename T, typename Enable = void> template <typename T, typename Enable = void>
struct value_type_of_impl struct value_type_of_impl
{ {
static_assert(wrong_t<value_type_of_impl>::value, "Attempting to optain value type from type without value_type"); static_assert(wrong_t<value_type_of_impl>::value, "Attempting to obtain value type from type without value_type");
}; };
template <typename T> template <typename T>

View File

@ -33,9 +33,9 @@ int BooleanExpression(int, char**)
MockDb db = {}; MockDb db = {};
test::TabBar t; test::TabBar t;
auto x = boolean_expression(db, t.alpha == 7); auto x = boolean_expression(db, not(t.alpha == 7));
x = sqlpp::boolean_expression<MockDb>(t.beta.like("%cheesecake")); x = sqlpp::boolean_expression<MockDb>(t.beta.like("%cheesecake"));
x = x and boolean_expression(db, t.gamma); x = false or (x and boolean_expression(db, t.gamma));
db(select(t.alpha).from(t).where(x)); db(select(t.alpha).from(t).where(x));