|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Roland Bock |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 19 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 22 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef SQLPP11_IS_EQUAL_TO_OR_NULL_H |
| 28 | +#define SQLPP11_IS_EQUAL_TO_OR_NULL_H |
| 29 | + |
| 30 | +#include <sqlpp11/value_or_null.h> |
| 31 | + |
| 32 | +namespace sqlpp |
| 33 | +{ |
| 34 | + template <typename Expr, typename ValueType> |
| 35 | + struct is_equal_to_or_null_t: public expression_operators<is_equal_to_or_null_t<Expr, ValueType>, boolean>, |
| 36 | + public alias_operators<is_equal_to_or_null_t<Expr, ValueType>> |
| 37 | + { |
| 38 | + using _traits = make_traits<boolean, tag::is_expression>; |
| 39 | + using _nodes = detail::type_vector<Expr, value_or_null_t<ValueType>>; |
| 40 | + |
| 41 | + is_equal_to_or_null_t(Expr expr, value_or_null_t<ValueType> value) : _expr(expr), _value(value) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + is_equal_to_or_null_t(const is_equal_to_or_null_t&) = default; |
| 46 | + is_equal_to_or_null_t(is_equal_to_or_null_t&&) = default; |
| 47 | + is_equal_to_or_null_t& operator=(const is_equal_to_or_null_t&) = default; |
| 48 | + is_equal_to_or_null_t& operator=(is_equal_to_or_null_t&&) = default; |
| 49 | + ~is_equal_to_or_null_t() = default; |
| 50 | + |
| 51 | + |
| 52 | + Expr _expr; |
| 53 | + value_or_null_t<ValueType> _value; |
| 54 | + }; |
| 55 | + |
| 56 | + template <typename Context, typename Expr, typename ValueType> |
| 57 | + struct serializer_t<Context, is_equal_to_or_null_t<Expr, ValueType>> |
| 58 | + { |
| 59 | + using _serialize_check = consistent_t; |
| 60 | + using Operand = is_equal_to_or_null_t<Expr, ValueType>; |
| 61 | + |
| 62 | + static Context& _(const Operand& t, Context& context) |
| 63 | + { |
| 64 | + if (t._value._is_null) |
| 65 | + serialize(t._expr.is_null(), context); |
| 66 | + else |
| 67 | + serialize(t._expr == t._value, context); |
| 68 | + |
| 69 | + return context; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + template <typename Expr, typename ValueType> |
| 74 | + auto is_equal_to_or_null(Expr expr, value_or_null_t<ValueType> value) -> is_equal_to_or_null_t<Expr, ValueType> |
| 75 | + { |
| 76 | + static_assert(is_expression_t<Expr>::value, |
| 77 | + "is_equal_to_or_null() is to be called an expression (e.g. a column) and a value_or_null expression"); |
| 78 | + static_assert(std::is_same<value_type_of<Expr>, ValueType>::value, |
| 79 | + "is_equal_to_or_null() arguments need to have the same value type"); |
| 80 | + return {expr, value}; |
| 81 | + } |
| 82 | + |
| 83 | +} // namespace sqlpp |
| 84 | + |
| 85 | +#endif |
0 commit comments