From 5556880bb944198952a5205316092b264f658e9f Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 8 Nov 2015 15:12:40 +0100 Subject: [PATCH] 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... --- include/sqlpp11/bad_expression.h | 58 +++++++++++++++ .../sqlpp11/data_types/boolean/data_type.h | 2 +- .../data_types/boolean/expression_operators.h | 47 ++++++------ include/sqlpp11/expression_return_types.h | 65 +++++++++++++++++ include/sqlpp11/operand_check.h | 72 +++++++++++++++++++ include/sqlpp11/operators.h | 55 ++++++++++++++ include/sqlpp11/type_traits.h | 2 +- tests/BooleanExpression.cpp | 4 +- 8 files changed, 277 insertions(+), 28 deletions(-) create mode 100644 include/sqlpp11/bad_expression.h create mode 100644 include/sqlpp11/expression_return_types.h create mode 100644 include/sqlpp11/operand_check.h create mode 100644 include/sqlpp11/operators.h diff --git a/include/sqlpp11/bad_expression.h b/include/sqlpp11/bad_expression.h new file mode 100644 index 00000000..3aa43f0c --- /dev/null +++ b/include/sqlpp11/bad_expression.h @@ -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 +#include + +namespace sqlpp +{ + SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_operands, "Invalid operand(s)"); + + template + struct bad_expression + { + template + bad_expression(T&&...) + { + } + using _traits = make_traits; + using _nodes = detail::type_vector<>; + }; + + template + struct serializer_t> + { + using _serialize_check = assert_valid_operands; + using T = bad_expression; + + static Context& _(const T&, Context&); + }; +} + +#endif diff --git a/include/sqlpp11/data_types/boolean/data_type.h b/include/sqlpp11/data_types/boolean/data_type.h index 1e58810f..25f6d1f5 100644 --- a/include/sqlpp11/data_types/boolean/data_type.h +++ b/include/sqlpp11/data_types/boolean/data_type.h @@ -33,7 +33,7 @@ namespace sqlpp { struct boolean { - using _traits = make_traits; + // using _traits = make_traits; using _cpp_value_type = bool; template diff --git a/include/sqlpp11/data_types/boolean/expression_operators.h b/include/sqlpp11/data_types/boolean/expression_operators.h index d4ff9d27..7d934910 100644 --- a/include/sqlpp11/data_types/boolean/expression_operators.h +++ b/include/sqlpp11/data_types/boolean/expression_operators.h @@ -27,39 +27,38 @@ #ifndef SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H #define SQLPP_BOOLEAN_EXPRESSION_OPERATORS_H +#include +#include +#include #include #include namespace sqlpp { - template - struct expression_operators : public basic_expression_operators + template + struct expression_operators : public basic_expression_operators { - template - using _is_valid_operand = is_valid_operand; + }; - template - logical_and_t> operator and(T t) const - { - using rhs = wrap_operand_t; - static_assert(_is_valid_operand::value, "invalid rhs operand"); + template + struct return_type_and> + { + using check = consistent_t; + using type = logical_and_t, wrap_operand_t>; + }; - return {*static_cast(this), rhs{t}}; - } + template + struct return_type_or> + { + using check = consistent_t; + using type = logical_or_t, wrap_operand_t>; + }; - template - logical_or_t> operator or(T t) const - { - using rhs = wrap_operand_t; - static_assert(_is_valid_operand::value, "invalid rhs operand"); - - return {*static_cast(this), rhs{t}}; - } - - logical_not_t operator not() const - { - return {*static_cast(this)}; - } + template + struct return_type_not> + { + using check = consistent_t; + using type = logical_not_t>; }; } diff --git a/include/sqlpp11/expression_return_types.h b/include/sqlpp11/expression_return_types.h new file mode 100644 index 00000000..7b2a27af --- /dev/null +++ b/include/sqlpp11/expression_return_types.h @@ -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 + +namespace sqlpp +{ + template + struct return_type_and + { + using check = assert_valid_operands; + using type = bad_expression; + }; + + template + using return_type_and_t = typename return_type_and::type; + + template + struct return_type_or + { + using check = assert_valid_operands; + using type = bad_expression; + }; + + template + using return_type_or_t = typename return_type_or::type; + + template + struct return_type_not + { + using check = assert_valid_operands; + using type = bad_expression; + }; + + template + using return_type_not_t = typename return_type_not::type; +} + +#endif diff --git a/include/sqlpp11/operand_check.h b/include/sqlpp11/operand_check.h new file mode 100644 index 00000000..45df1cfb --- /dev/null +++ b/include/sqlpp11/operand_check.h @@ -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 +#include + +namespace sqlpp +{ + template class Pred, typename Enable = void> + struct unary_operand_check + { + }; + + template class Pred> + struct unary_operand_check>::value>> + { + using type = void; + }; + + template class Pred> + using unary_operand_check_t = typename unary_operand_check::type; + + template class LPred, + typename R, + template class RPred, + typename Enable = void> + struct binary_operand_check + { + }; + + template class LPred, typename R, template class RPred> + struct binary_operand_check>::value and RPred>::value>> + { + using type = void; + }; + + template class LPred, typename R, template class RPred> + using binary_operand_check_t = typename binary_operand_check::type; +} + +#endif diff --git a/include/sqlpp11/operators.h b/include/sqlpp11/operators.h new file mode 100644 index 00000000..9a32c6ca --- /dev/null +++ b/include/sqlpp11/operators.h @@ -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 +#include + +namespace sqlpp +{ + template + auto operator and(const L& l, const R& r) -> return_type_and_t + { + return_type_and::check::_(); + return {wrap_operand_t{l}, wrap_operand_t{r}}; + } + template + auto operator or(const L& l, const R& r) -> return_type_or_t + { + return_type_or::check::_(); + return {wrap_operand_t{l}, wrap_operand_t{r}}; + } + template + auto operator not(const T& t) -> return_type_not_t + { + return_type_not::check::_(); + return {wrap_operand_t{t}}; + } +} + +#endif diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index f0a7598f..83f49e16 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -43,7 +43,7 @@ namespace sqlpp template struct value_type_of_impl { - static_assert(wrong_t::value, "Attempting to optain value type from type without value_type"); + static_assert(wrong_t::value, "Attempting to obtain value type from type without value_type"); }; template diff --git a/tests/BooleanExpression.cpp b/tests/BooleanExpression.cpp index aef09aaa..c3b9daa1 100644 --- a/tests/BooleanExpression.cpp +++ b/tests/BooleanExpression.cpp @@ -33,9 +33,9 @@ int BooleanExpression(int, char**) MockDb db = {}; test::TabBar t; - auto x = boolean_expression(db, t.alpha == 7); + auto x = boolean_expression(db, not(t.alpha == 7)); x = sqlpp::boolean_expression(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));