Skip to content

Commit 98402aa

Browse files
rbockLeon0402
authored andcommitted
Add is_equal_or_null(col, some_value_or_null)
This replaces the former implicit tvin logic with an explicit function.
1 parent 2f412bc commit 98402aa

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

include/sqlpp11/functions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <sqlpp11/verbatim_table.h>
4848
#include <sqlpp11/value.h>
4949
#include <sqlpp11/value_or_null.h>
50+
#include <sqlpp11/is_equal_to_or_null.h>
5051
#include <sqlpp11/eval.h>
5152

5253
namespace sqlpp

include/sqlpp11/is_equal_to_or_null.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

include/sqlpp11/is_not_null.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ namespace sqlpp
7878

7979
static Context& _(const T& t, Context& context)
8080
{
81+
context << "(";
8182
serialize_operand(t._operand, context);
82-
context << " IS NOT NULL";
83+
context << " IS NOT NULL)";
8384
return context;
8485
}
8586
};

include/sqlpp11/is_null.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ namespace sqlpp
7878

7979
static Context& _(const T& t, Context& context)
8080
{
81+
context << "(";
8182
serialize_operand(t._operand, context);
82-
context << " IS NULL";
83+
context << " IS NULL)";
8384
return context;
8485
}
8586
};

test_serializer/Where.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ int Where(int, char*[])
6363
// Sometimes
6464
compare(__LINE__, where(bar.gamma), " WHERE tab_bar.gamma");
6565
compare(__LINE__, where(bar.gamma == false), " WHERE (tab_bar.gamma=" + getFalse() + ")");
66-
compare(__LINE__, where(bar.beta.is_null()), " WHERE tab_bar.beta IS NULL");
66+
compare(__LINE__, where(bar.beta.is_null()), " WHERE (tab_bar.beta IS NULL)");
6767
compare(__LINE__, where(bar.beta == "SQL"), " WHERE (tab_bar.beta='SQL')");
68+
compare(__LINE__, where(is_equal_to_or_null(bar.beta, ::sqlpp::value_or_null("SQL"))), " WHERE (tab_bar.beta='SQL')");
69+
compare(__LINE__, where(is_equal_to_or_null(bar.beta, ::sqlpp::value_or_null<sqlpp::text>(::sqlpp::null))),
70+
" WHERE (tab_bar.beta IS NULL)");
6871
#if __cplusplus >= 201703L
6972
// string_view argument
7073
std::string_view sqlString = "SQL";

0 commit comments

Comments
 (0)