From 1a8ea5a3b3641ead5ad3411c05ffb7bb47e512d6 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Wed, 2 Oct 2013 10:55:36 +0200 Subject: [PATCH] Re-wrote in()/not_in() to use a specific template and removed the generic nary member function template The specific version is easier to understand and could be adjusted more easily if some database should not support it... --- include/sqlpp11/detail/basic_operators.h | 13 +--- include/sqlpp11/expression.h | 39 ---------- include/sqlpp11/in.h | 91 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 49 deletions(-) create mode 100644 include/sqlpp11/in.h diff --git a/include/sqlpp11/detail/basic_operators.h b/include/sqlpp11/detail/basic_operators.h index 919a6034..6442144f 100644 --- a/include/sqlpp11/detail/basic_operators.h +++ b/include/sqlpp11/detail/basic_operators.h @@ -30,6 +30,7 @@ #include #include #include +#include namespace sqlpp { @@ -75,12 +76,6 @@ namespace sqlpp static constexpr const char* _name = "IS NOT NULL"; }; - struct in_ - { - using _value_type = boolean; - static constexpr const char* _name = "IN"; - }; - struct not_in_ { using _value_type = boolean; @@ -149,16 +144,14 @@ namespace sqlpp // Hint: use value_list wrapper for containers... template - nary_member_function_t::type...> in(T&&... t) const + in_t::type...> in(T&&... t) const { - static_assert(sizeof...(T) > 0, "in() requires at least one argument"); return { *static_cast(this), std::forward(t)... }; } template - nary_member_function_t::type...> not_in(T&&... t) const + in_t::type...> not_in(T&&... t) const { - static_assert(sizeof...(T) > 0, "not_in() requires at least one argument"); return { *static_cast(this), std::forward(t)... }; } diff --git a/include/sqlpp11/expression.h b/include/sqlpp11/expression.h index 175f539a..b5a27f4b 100644 --- a/include/sqlpp11/expression.h +++ b/include/sqlpp11/expression.h @@ -244,45 +244,6 @@ namespace sqlpp Rhs _rhs; }; - template - struct nary_member_function_t: public O::_value_type::template operators> - { - using _value_type = typename O::_value_type; - - nary_member_function_t(Lhs&& l, Rhs&&... r): - _lhs(std::move(l)), - _rhs(std::move(r)...) - {} - - nary_member_function_t(const Lhs& l, const Rhs&... r): - _lhs(l), - _rhs(r...) - {} - - nary_member_function_t(const nary_member_function_t&) = default; - nary_member_function_t(nary_member_function_t&&) = default; - nary_member_function_t& operator=(const nary_member_function_t&) = default; - nary_member_function_t& operator=(nary_member_function_t&&) = default; - ~nary_member_function_t() = default; - - template - void serialize(std::ostream& os, Db& db) const - { - os << "("; - _lhs.serialize(os, db); - os << " "; - os << O::_name; - os << "("; - detail::serialize_tuple(os, db, _rhs, ','); - os << ")"; - os << ")"; - } - - private: - Lhs _lhs; - std::tuple _rhs; - }; - } #endif diff --git a/include/sqlpp11/in.h b/include/sqlpp11/in.h new file mode 100644 index 00000000..50e6b4a2 --- /dev/null +++ b/include/sqlpp11/in.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2013, 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_IN_H +#define SQLPP_IN_H + +#include +#include +#include + +namespace sqlpp +{ + namespace detail + { + template + struct in_t: public Operand::_value_type::template operators> + { + static constexpr bool inverted = not not_inverted; + static_assert(sizeof...(Args) > 0, "in() requires at least one argument"); + + struct _value_type: public Operand::_value_type::_base_value_type + { + using _is_named_expression = std::true_type; + }; + + struct _name_t + { + static constexpr const char* _get_name() { return inverted ? "NOT IN" : "IN"; } + template + struct _member_t + { + T in; + }; + }; + + in_t(const Operand& operand, const Args&... args): + _operand(operand), + _args(args...) + {} + + in_t(Operand&& operand, Args&&... args): + _operand(std::move(operand)), + _args(std::move(args...)) + {} + + in_t(const in_t&) = default; + in_t(in_t&&) = default; + in_t& operator=(const in_t&) = default; + in_t& operator=(in_t&&) = default; + ~in_t() = default; + + template + void serialize(std::ostream& os, Db& db) const + { + _operand.serialize(os, db); + os << (inverted ? " NOT IN(" : " IN("); + detail::serialize_tuple(os, db, _args, ','); + os << ")"; + } + + private: + Operand _operand; + std::tuple _args; + }; + } +} + +#endif