From db0295a9d052ee8b42a21010088460e5362508f8 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 11 Jul 2021 12:47:13 +0200 Subject: [PATCH] Remove tvin This was a special feature for a project I was working on long ago. It provided implicit behavior for trivial value (0 or "") which were supposed to be interpreted as NULL. They led to `operator==` potentially being serialized as 'IS NULL'. It makes more sense to introduce explicit helpers, e.g. - equal_or_is_null(col, optional) - not_equal_or_is_not_null(col, optional) --- include/sqlpp11/assignment.h | 5 +- .../sqlpp11/data_types/blob/parameter_value.h | 1 - .../data_types/boolean/parameter_value.h | 1 - .../data_types/day_point/parameter_value.h | 1 - .../floating_point/parameter_value.h | 1 - .../data_types/integral/parameter_value.h | 1 - .../sqlpp11/data_types/parameter_value_base.h | 16 -- .../sqlpp11/data_types/text/parameter_value.h | 1 - .../data_types/time_of_day/parameter_value.h | 1 - .../data_types/time_point/parameter_value.h | 1 - .../unsigned_integral/parameter_value.h | 1 - include/sqlpp11/expression.h | 27 +-- include/sqlpp11/insert_value.h | 8 +- include/sqlpp11/rhs_wrap.h | 19 -- include/sqlpp11/tvin.h | 169 ------------------ test_serializer/Insert.cpp | 2 +- test_serializer/Where.cpp | 2 +- tests/Function.cpp | 25 --- tests/Insert.cpp | 4 +- tests/Interpret.cpp | 11 -- tests/Prepared.cpp | 1 - tests/Select.cpp | 2 +- 22 files changed, 14 insertions(+), 286 deletions(-) delete mode 100644 include/sqlpp11/tvin.h diff --git a/include/sqlpp11/assignment.h b/include/sqlpp11/assignment.h index 41b66d18..90b2ba09 100644 --- a/include/sqlpp11/assignment.h +++ b/include/sqlpp11/assignment.h @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -42,11 +41,11 @@ namespace sqlpp { using _traits = make_traits; using _lhs_t = Lhs; - using _rhs_t = rhs_wrap_t>; + using _rhs_t = rhs_wrap_t; using _nodes = detail::type_vector<_lhs_t, _rhs_t>; static_assert(can_be_null_t<_lhs_t>::value ? true - : not(std::is_same<_rhs_t, null_t>::value or is_tvin_t<_rhs_t>::value), + : not std::is_same<_rhs_t, null_t>::value, "column must not be null"); assignment_t(_lhs_t lhs, _rhs_t rhs) : _lhs(lhs), _rhs(rhs) diff --git a/include/sqlpp11/data_types/blob/parameter_value.h b/include/sqlpp11/data_types/blob/parameter_value.h index 39b57fd1..f36cd311 100644 --- a/include/sqlpp11/data_types/blob/parameter_value.h +++ b/include/sqlpp11/data_types/blob/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/boolean/parameter_value.h b/include/sqlpp11/data_types/boolean/parameter_value.h index ee6375d0..0e854abe 100644 --- a/include/sqlpp11/data_types/boolean/parameter_value.h +++ b/include/sqlpp11/data_types/boolean/parameter_value.h @@ -30,7 +30,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/day_point/parameter_value.h b/include/sqlpp11/data_types/day_point/parameter_value.h index bb8b5387..05077a11 100644 --- a/include/sqlpp11/data_types/day_point/parameter_value.h +++ b/include/sqlpp11/data_types/day_point/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/floating_point/parameter_value.h b/include/sqlpp11/data_types/floating_point/parameter_value.h index 11df153f..beec2d37 100644 --- a/include/sqlpp11/data_types/floating_point/parameter_value.h +++ b/include/sqlpp11/data_types/floating_point/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/integral/parameter_value.h b/include/sqlpp11/data_types/integral/parameter_value.h index af98bb62..244de247 100644 --- a/include/sqlpp11/data_types/integral/parameter_value.h +++ b/include/sqlpp11/data_types/integral/parameter_value.h @@ -30,7 +30,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/parameter_value_base.h b/include/sqlpp11/data_types/parameter_value_base.h index b75f78ca..c7e103d5 100644 --- a/include/sqlpp11/data_types/parameter_value_base.h +++ b/include/sqlpp11/data_types/parameter_value_base.h @@ -28,7 +28,6 @@ #define SQLPP11_DATA_TYPES_PARAMETER_VALUE_BASE_H #include -#include namespace sqlpp { @@ -54,21 +53,6 @@ namespace sqlpp return *this; } - parameter_value_base& operator=(const tvin_t>& t) - { - if (t._is_trivial()) - { - _value = {}; - _is_null = true; - } - else - { - _value = t._value._t; - _is_null = false; - } - return *this; - } - void set_null() { _value = {}; diff --git a/include/sqlpp11/data_types/text/parameter_value.h b/include/sqlpp11/data_types/text/parameter_value.h index cf7bbf07..e1ac1bd0 100644 --- a/include/sqlpp11/data_types/text/parameter_value.h +++ b/include/sqlpp11/data_types/text/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/time_of_day/parameter_value.h b/include/sqlpp11/data_types/time_of_day/parameter_value.h index e062c32f..b7a0e8c1 100644 --- a/include/sqlpp11/data_types/time_of_day/parameter_value.h +++ b/include/sqlpp11/data_types/time_of_day/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/time_point/parameter_value.h b/include/sqlpp11/data_types/time_point/parameter_value.h index a05b0cf6..e2ed62f6 100644 --- a/include/sqlpp11/data_types/time_point/parameter_value.h +++ b/include/sqlpp11/data_types/time_point/parameter_value.h @@ -32,7 +32,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/data_types/unsigned_integral/parameter_value.h b/include/sqlpp11/data_types/unsigned_integral/parameter_value.h index 64e0b81d..c847d728 100644 --- a/include/sqlpp11/data_types/unsigned_integral/parameter_value.h +++ b/include/sqlpp11/data_types/unsigned_integral/parameter_value.h @@ -30,7 +30,6 @@ #include #include #include -#include namespace sqlpp { diff --git a/include/sqlpp11/expression.h b/include/sqlpp11/expression.h index 9e53f36a..386c3acd 100644 --- a/include/sqlpp11/expression.h +++ b/include/sqlpp11/expression.h @@ -33,7 +33,6 @@ #include #include #include -#include #include namespace sqlpp @@ -45,7 +44,7 @@ namespace sqlpp { using _traits = make_traits; using _lhs_t = Lhs; - using _rhs_t = rhs_wrap_t>; + using _rhs_t = rhs_wrap_t; using _nodes = detail::type_vector<_lhs_t, _rhs_t>; binary_expression_t(Lhs lhs, Rhs rhs) : _lhs(lhs), _rhs(rhs) @@ -72,15 +71,8 @@ namespace sqlpp { context << "("; serialize_operand(t._lhs, context); - if (t._rhs._is_null()) - { - context << " IS NULL"; - } - else - { - context << "="; - serialize_operand(t._rhs, context); - } + context << "="; + serialize_operand(t._rhs, context); context << ")"; return context; } @@ -93,7 +85,7 @@ namespace sqlpp { using _traits = make_traits; using _lhs_t = Lhs; - using _rhs_t = rhs_wrap_t>; + using _rhs_t = rhs_wrap_t; using _nodes = detail::type_vector<_lhs_t, _rhs_t>; binary_expression_t(Lhs lhs, Rhs rhs) : _lhs(lhs), _rhs(rhs) @@ -120,15 +112,8 @@ namespace sqlpp { context << "("; serialize_operand(t._lhs, context); - if (t._rhs._is_null()) - { - context << " IS NOT NULL"; - } - else - { - context << "<>"; - serialize_operand(t._rhs, context); - } + context << "<>"; + serialize_operand(t._rhs, context); context << ")"; return context; } diff --git a/include/sqlpp11/insert_value.h b/include/sqlpp11/insert_value.h index c78e5b54..6218cd76 100644 --- a/include/sqlpp11/insert_value.h +++ b/include/sqlpp11/insert_value.h @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -63,7 +62,7 @@ namespace sqlpp using _column_t = Column; using _pure_value_t = typename value_type_of::_cpp_value_type; using _wrapped_value_t = wrap_operand_t<_pure_value_t>; - using _tvin_t = tvin_t<_wrapped_value_t>; + using _tvin_t = _wrapped_value_t; using _value_or_null_t = value_or_null_t; insert_value_t(rhs_wrap_t<_wrapped_value_t> rhs) @@ -71,11 +70,6 @@ namespace sqlpp { } - insert_value_t(rhs_wrap_t<_tvin_t> rhs) - : _is_null(rhs._is_null()), _is_default(rhs._is_default()), _value(rhs._expr._value) - { - } - insert_value_t(const rhs_wrap_t& /*unused*/) : _is_null(true), _is_default(false), _value{} { diff --git a/include/sqlpp11/rhs_wrap.h b/include/sqlpp11/rhs_wrap.h index 1c1deee1..0ce54d95 100644 --- a/include/sqlpp11/rhs_wrap.h +++ b/include/sqlpp11/rhs_wrap.h @@ -29,7 +29,6 @@ #include #include -#include namespace sqlpp { @@ -53,15 +52,6 @@ namespace sqlpp } }; - template - struct rhs_is_trivial_t::value, void>::type> - { - static bool _(const Expr& t) - { - return t._is_trivial(); - } - }; - template struct rhs_is_trivial_t::value, void>::type> { @@ -85,15 +75,6 @@ namespace sqlpp } }; - template - struct rhs_is_null_t::value, void>::type> - { - static bool _(const Expr& t) - { - return t._is_null(); - } - }; - template struct rhs_is_null_t::value, void>::type> { diff --git a/include/sqlpp11/tvin.h b/include/sqlpp11/tvin.h deleted file mode 100644 index 3e62ac95..00000000 --- a/include/sqlpp11/tvin.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * 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 SQLPP11_TVIN_H -#define SQLPP11_TVIN_H - -// TVIN: Trivial value is NULL - -#include -#include -#include -#include - -namespace sqlpp -{ - template - struct tvin_arg_t - { - using _traits = make_traits, tag::is_expression>; - using _nodes = detail::type_vector; - - using _operand_t = Operand; - - tvin_arg_t(_operand_t operand) : _value(operand) - { - } - tvin_arg_t(const tvin_arg_t&) = default; - tvin_arg_t(tvin_arg_t&&) = default; - tvin_arg_t& operator=(const tvin_arg_t&) = default; - tvin_arg_t& operator=(tvin_arg_t&&) = default; - ~tvin_arg_t() = default; - - _operand_t _value; - }; - - SQLPP_PORTABLE_STATIC_ASSERT(assert_tvin_with_correct_operator_t, - "tvin may only be used with operators =, == and !="); - - template - struct serializer_t> - { - using _serialize_check = assert_tvin_with_correct_operator_t; - using T = tvin_arg_t; - - static Context& _(const T& /*unused*/, Context& /*unused*/) - { - _serialize_check{}; - } - }; - - template - struct tvin_t; - - namespace detail - { - template - struct allow_tvin_impl - { - using type = T; - }; - template - struct allow_tvin_impl> - { - using type = tvin_t; - }; - } // namespace detail - template - using allow_tvin_t = typename detail::allow_tvin_impl::type; - - template - struct tvin_t - { - using _traits = make_traits, tag::is_expression>; - using _nodes = detail::type_vector; - - using _operand_t = Operand; - - tvin_t(tvin_arg_t arg) : _value(arg._value) - { - } - tvin_t(const tvin_t&) = default; - tvin_t(tvin_t&&) = default; - tvin_t& operator=(const tvin_t&) = default; - tvin_t& operator=(tvin_t&&) = default; - ~tvin_t() = default; - - bool _is_trivial() const - { - return _value._is_trivial(); - } - - bool _is_null() const - { - return _value._is_trivial(); - } - - _operand_t _value; - }; - - namespace detail - { - template - struct is_tvin_impl - { - using type = std::false_type; - }; - template - struct is_tvin_impl> - { - using type = std::true_type; - }; - } // namespace detail - template - using is_tvin_t = typename detail::is_tvin_impl::type; - - template - struct serializer_t> - { - using _serialize_check = serialize_check_of; - using T = tvin_t; - - static Context& _(const T& t, Context& context) - { - if (t._is_trivial()) - { - context << "NULL"; - } - else - { - serialize(t._value, context); - } - return context; - } - }; - - template - auto tvin(Operand operand) -> tvin_arg_t> - { - using _operand_t = wrap_operand_t; - static_assert(not std::is_same<_operand_t, Operand>::value or is_result_field_t::value, - "tvin() used with invalid type (only string and primitive types allowed)"); - return {{operand}}; - } -} // namespace sqlpp - -#endif diff --git a/test_serializer/Insert.cpp b/test_serializer/Insert.cpp index dd4c6806..d4b59e94 100644 --- a/test_serializer/Insert.cpp +++ b/test_serializer/Insert.cpp @@ -45,7 +45,7 @@ int Insert(int, char* []) compare(__LINE__, insert_into(bar).default_values(), "INSERT INTO tab_bar DEFAULT VALUES"); compare(__LINE__, insert_into(bar).set(bar.beta = "cheesecake", bar.gamma = true), "INSERT INTO tab_bar (beta,gamma) VALUES('cheesecake'," + getTrue() + ")"); - compare(__LINE__, insert_into(bar).set(bar.beta = ::sqlpp::tvin(""), bar.gamma = true), + compare(__LINE__, insert_into(bar).set(bar.beta = ::sqlpp::null, bar.gamma = true), "INSERT INTO tab_bar (beta,gamma) VALUES(NULL," + getTrue() + ")"); #if __cplusplus >= 201703L // string_view argument diff --git a/test_serializer/Where.cpp b/test_serializer/Where.cpp index 0527525e..d3f5761b 100644 --- a/test_serializer/Where.cpp +++ b/test_serializer/Where.cpp @@ -63,7 +63,7 @@ int Where(int, char*[]) // Sometimes compare(__LINE__, where(bar.gamma), " WHERE tab_bar.gamma"); compare(__LINE__, where(bar.gamma == false), " WHERE (tab_bar.gamma=" + getFalse() + ")"); - compare(__LINE__, where(bar.gamma == ::sqlpp::tvin(false)), " WHERE (tab_bar.gamma IS NULL)"); + compare(__LINE__, where(bar.beta.is_null()), " WHERE tab_bar.beta IS NULL"); compare(__LINE__, where(bar.beta == "SQL"), " WHERE (tab_bar.beta='SQL')"); #if __cplusplus >= 201703L // string_view argument diff --git a/tests/Function.cpp b/tests/Function.cpp index dba35ab3..fca97784 100644 --- a/tests/Function.cpp +++ b/tests/Function.cpp @@ -436,30 +436,5 @@ int Function(int, char* []) static_assert(sqlpp::is_alias_t::value, "type requirement"); } - // test tvin - { - static_assert(std::is_same>::value, - "integral values are accepted and wrapped"); - static_assert(std::is_same>::value, - "bool values are accepted and wrapped"); - static_assert(std::is_same>::value, - "float values are accepted and wrapped"); - static_assert(std::is_same>::value, - "text values are accepted and wrapped"); - - for (const auto& row : db(select(all_of(t)).from(t).unconditionally())) - { - static_assert(std::is_same::type>>::value, - "result fields are accepted and not wrapped"); - static_assert(std::is_same::type>>::value, - "result fields are accepted and not wrapped"); - static_assert(std::is_same::type>>::value, - "result fields are accepted and not wrapped"); - } - } - return 0; } diff --git a/tests/Insert.cpp b/tests/Insert.cpp index 85a0571d..166133bc 100644 --- a/tests/Insert.cpp +++ b/tests/Insert.cpp @@ -87,12 +87,12 @@ int Insert(int, char*[]) db(multi_insert); - auto values = [&t]() { return std::make_tuple(t.gamma = true, t.delta = sqlpp::tvin(0)); }; + auto values = [&t]() { return std::make_tuple(t.gamma = true, t.beta = sqlpp::null); }; db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::verbatim("17+4"))); db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::null)); db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::default_value)); - db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::tvin(0))); + db(insert_into(t).set(t.gamma = true, t.delta = 0)); db(insert_into(t).set(values())); return 0; diff --git a/tests/Interpret.cpp b/tests/Interpret.cpp index 29e473b4..148498e1 100644 --- a/tests/Interpret.cpp +++ b/tests/Interpret.cpp @@ -43,9 +43,6 @@ int Interpret(int, char* []) auto i = insert_into(t).columns(t.gamma, t.beta); i.values.add(t.gamma = true, t.beta = "cheesecake"); serialize(i, printer).str(); - i.values.add(t.gamma = false, t.beta = sqlpp::tvin("coffee")); - i.values.add(t.gamma = false, t.beta = sqlpp::tvin(std::string())); - serialize(i, printer).str(); i.values.add(t.gamma = sqlpp::default_value, t.beta = sqlpp::null); serialize(i, printer).str(); } @@ -57,13 +54,9 @@ int Interpret(int, char* []) serialize(+t.alpha, printer).str(); serialize(-(t.alpha + 7), printer).str(); serialize(t.alpha = 0, printer).str(); - serialize(t.alpha = sqlpp::tvin(0), printer).str(); serialize(t.alpha == 0, printer).str(); - serialize(t.alpha == sqlpp::tvin(0), printer).str(); serialize(t.alpha != 0, printer).str(); - serialize(t.gamma != sqlpp::tvin(false), printer).str(); serialize(t.alpha == 7, printer).str(); - serialize(t.delta = sqlpp::tvin(0), printer).str(); serialize(t.beta + "kaesekuchen", printer).str(); serialize(sqlpp::select(), printer).str(); @@ -103,8 +96,6 @@ int Interpret(int, char* []) serialize(insert_into(t), printer).str(); serialize(insert_into(f).default_values(), printer).str(); serialize(insert_into(t).set(t.gamma = true), printer).str(); - // serialize(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).str(); cannot test this since gamma cannot be - // null and a static assert is thrown serialize(update(t), printer).str(); serialize(update(t).set(t.gamma = true), printer).str(); @@ -113,8 +104,6 @@ int Interpret(int, char* []) serialize(remove_from(t), printer).str(); serialize(remove_from(t).using_(t), printer).str(); - serialize(remove_from(t).where(t.alpha == sqlpp::tvin(0)), printer).str(); - serialize(remove_from(t).using_(t).where(t.alpha == sqlpp::tvin(0)), printer).str(); // functions serialize(sqlpp::value(7), printer).str(); diff --git a/tests/Prepared.cpp b/tests/Prepared.cpp index 637c030a..af9cd96b 100644 --- a/tests/Prepared.cpp +++ b/tests/Prepared.cpp @@ -108,7 +108,6 @@ int Prepared(int, char* []) t.gamma != parameter(t.gamma)); auto p = db.prepare(s); p.params.alpha = 7; - p.params.alpha = sqlpp::tvin(0); using S = decltype(s); using P = sqlpp::make_parameter_list_t; P npl; diff --git a/tests/Select.cpp b/tests/Select.cpp index 0860479e..cd11eaa7 100644 --- a/tests/Select.cpp +++ b/tests/Select.cpp @@ -134,7 +134,7 @@ int Select(int, char*[]) std::cout << row.count << std::endl; } - for (const auto& row : db(select(count(t.alpha), avg(t.alpha)).from(t).where(t.alpha == sqlpp::tvin(0)))) + for (const auto& row : db(select(count(t.alpha), avg(t.alpha)).from(t).where(t.alpha == 0))) { std::cout << row.count << std::endl; }