diff --git a/include/sqlpp11/assignment.h b/include/sqlpp11/assignment.h index db8dc9e3..a16c1abf 100644 --- a/include/sqlpp11/assignment.h +++ b/include/sqlpp11/assignment.h @@ -30,9 +30,7 @@ #include #include #include -#include -#include -#include +#include #include #include #include @@ -46,7 +44,7 @@ namespace sqlpp using _recursive_traits = make_recursive_traits; using _lhs_t = Lhs; - using _rhs_t = allow_tvin_t; + using _rhs_t = rhs_wrap_t, trivial_value_is_null_t<_lhs_t>::value>; 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), "column must not be null"); @@ -73,20 +71,8 @@ namespace sqlpp static Context& _(const T& t, Context& context) { serialize(simple_column(t._lhs), context); - if ((trivial_value_is_null_t::value and rhs_is_trivial(t)) - or rhs_is_null(t)) - { - context << "=NULL"; - } - else if (rhs_is_default(t)) - { - context << "=DEFAULT"; - } - else - { - context << "="; - serialize(t._rhs, context); - } + context << "="; + serialize(t._rhs, context); return context; } }; diff --git a/include/sqlpp11/basic_expression_operators.h b/include/sqlpp11/basic_expression_operators.h index deeb5342..710ea844 100644 --- a/include/sqlpp11/basic_expression_operators.h +++ b/include/sqlpp11/basic_expression_operators.h @@ -57,7 +57,7 @@ namespace sqlpp using rhs = wrap_operand_t; static_assert(_is_valid_comparison_operand::value, "invalid rhs operand in comparison"); - return { *static_cast(this), rhs{t} }; + return { *static_cast(this), {rhs{t}} }; } template @@ -66,7 +66,7 @@ namespace sqlpp using rhs = wrap_operand_t; static_assert(_is_valid_comparison_operand::value, "invalid rhs operand in comparison"); - return { *static_cast(this), rhs{t} }; + return { *static_cast(this), {rhs{t}} }; } template diff --git a/include/sqlpp11/column.h b/include/sqlpp11/column.h index 99a239eb..9f9a089a 100644 --- a/include/sqlpp11/column.h +++ b/include/sqlpp11/column.h @@ -92,20 +92,20 @@ namespace sqlpp using rhs = wrap_operand_t; static_assert(_is_valid_operand::value, "invalid rhs operand assignment operand"); - return { *this, rhs{t} }; + return { *this, {rhs{t}} }; } auto operator =(sqlpp::null_t) const ->assignment_t { static_assert(can_be_null_t::value, "column cannot be null"); - return { *this, {} }; + return { *this, sqlpp::null_t{} }; } auto operator =(sqlpp::default_value_t) const ->assignment_t { - return { *this, {} }; + return { *this, sqlpp::default_value_t{} }; } }; diff --git a/include/sqlpp11/expression.h b/include/sqlpp11/expression.h index 73b6c3a8..35982e16 100644 --- a/include/sqlpp11/expression.h +++ b/include/sqlpp11/expression.h @@ -30,8 +30,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -46,7 +45,7 @@ namespace sqlpp using _traits = make_traits; using _recursive_traits = make_recursive_traits; using _lhs_t = Lhs; - using _rhs_t = allow_tvin_t; + using _rhs_t = rhs_wrap_t, trivial_value_is_null_t<_lhs_t>::value>; binary_expression_t(_lhs_t lhs, _rhs_t rhs): _lhs(lhs), @@ -72,8 +71,7 @@ namespace sqlpp { context << "("; serialize(t._lhs, context); - if ((trivial_value_is_null_t::value and rhs_is_trivial(t)) - or rhs_is_null(t)) + if (t._rhs._is_null()) { context << " IS NULL"; } @@ -94,7 +92,7 @@ namespace sqlpp using _traits = make_traits; using _recursive_traits = make_recursive_traits; using _lhs_t = Lhs; - using _rhs_t = allow_tvin_t; + using _rhs_t = rhs_wrap_t, trivial_value_is_null_t<_lhs_t>::value>; binary_expression_t(Lhs lhs, _rhs_t rhs): _lhs(lhs), @@ -120,8 +118,7 @@ namespace sqlpp { context << "("; serialize(t._lhs, context); - if ((trivial_value_is_null_t::value and rhs_is_trivial(t)) - or rhs_is_null(t)) + if (t._rhs._is_null()) { context << " IS NOT NULL"; } diff --git a/include/sqlpp11/insert_value.h b/include/sqlpp11/insert_value.h index 73c30a2f..03c28895 100644 --- a/include/sqlpp11/insert_value.h +++ b/include/sqlpp11/insert_value.h @@ -60,41 +60,35 @@ namespace sqlpp { using _is_insert_value = std::true_type; using _column_t = Column; + static constexpr bool _trivial_value_is_null = trivial_value_is_null_t::value; using _pure_value_t = typename value_type_of::_cpp_value_type; using _wrapped_value_t = typename wrap_operand<_pure_value_t>::type; using _tvin_t = tvin_t<_wrapped_value_t>; - insert_value_t(_wrapped_value_t rhs): - _is_null(false), - _is_default(false), - _value(rhs) + insert_value_t(rhs_wrap_t<_wrapped_value_t, _trivial_value_is_null> rhs): + _is_null(rhs._is_null()), + _is_default(rhs._is_default()), + _value(rhs._expr._t) {} - insert_value_t(_tvin_t rhs): - _is_null(rhs._is_trivial()), - _is_default(false), - _value(rhs._value) + insert_value_t(rhs_wrap_t<_tvin_t, _trivial_value_is_null> rhs): + _is_null(rhs._is_null()), + _is_default(rhs._is_default()), + _value(rhs._expr._value) {} - insert_value_t(const null_t&): + insert_value_t(const rhs_wrap_t&): _is_null(true), _is_default(false), _value{} {} - insert_value_t(const default_value_t&): + insert_value_t(const rhs_wrap_t&): _is_null(false), _is_default(true), _value{} {} - template - insert_value_t(const result_field_t, Db, FieldSpec>& rhs): - _is_null(rhs.is_null()), - _is_default(false), - _value(rhs.is_null() ? _wrapped_value_t{} : rhs.value()) - {} - insert_value_t(const insert_value_t&) = default; insert_value_t(insert_value_t&&) = default; insert_value_t& operator=(const insert_value_t&) = default; diff --git a/include/sqlpp11/insert_value_list.h b/include/sqlpp11/insert_value_list.h index 3157b2bf..05bd92c8 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -87,7 +87,7 @@ namespace sqlpp insert_list_data_t(Assignments... assignments): _assignments(assignments...), _columns({assignments._lhs}...), - _values(insert_value_t{assignments._rhs}...) + _values({assignments._rhs}...) {} insert_list_data_t(const insert_list_data_t&) = default; @@ -98,7 +98,7 @@ namespace sqlpp std::tuple _assignments; // FIXME: Need to replace _columns and _values by _assignments (connector-container requires assignments) std::tuple...> _columns; - std::tuple...> _values; + std::tuple _values; interpretable_list_t _dynamic_columns; interpretable_list_t _dynamic_values; }; diff --git a/include/sqlpp11/rhs_is_default.h b/include/sqlpp11/rhs_is_default.h deleted file mode 100644 index 23dafe4c..00000000 --- a/include/sqlpp11/rhs_is_default.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2013-2014, 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_RHS_IS_DEFAULT_H -#define SQLPP_RHS_IS_DEFAULT_H - -#include - -namespace sqlpp -{ - template - struct rhs_is_default_t - { - static constexpr bool _(const T&) - { - return false; - } - }; - - template - struct rhs_is_default_t::value, void>::type> - { - static constexpr bool _(const T& t) - { - return true; - } - }; - - template - constexpr bool rhs_is_default(const Expression& e) - { - return rhs_is_default_t::type::_rhs_t>::_(e._rhs); - } - -} - -#endif diff --git a/include/sqlpp11/rhs_is_null.h b/include/sqlpp11/rhs_is_null.h deleted file mode 100644 index c77e7a30..00000000 --- a/include/sqlpp11/rhs_is_null.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2013-2014, 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_RHS_IS_NULL_H -#define SQLPP_RHS_IS_NULL_H - -#include -#include - -namespace sqlpp -{ - template - struct rhs_is_null_t - { - static constexpr bool _(const T&) - { - return false; - } - }; - - template - struct rhs_is_null_t::value, void>::type> - { - static constexpr bool _(const T& t) - { - return true; - } - }; - - template - struct rhs_is_null_t::value, void>::type> - { - static bool _(const T& t) - { - return t._is_null(); - } - }; - - template - struct rhs_is_null_t::value, void>::type> - { - static bool _(const T& t) - { - return t.is_null(); - } - }; - - template - constexpr bool rhs_is_null(const Expression& e) - { - return rhs_is_null_t::type::_rhs_t>::_(e._rhs); - } - -} - -#endif diff --git a/include/sqlpp11/rhs_is_trivial.h b/include/sqlpp11/rhs_is_trivial.h deleted file mode 100644 index 5a38db17..00000000 --- a/include/sqlpp11/rhs_is_trivial.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2013-2014, 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_RHS_IS_TRIVIAL_H -#define SQLPP_RHS_IS_TRIVIAL_H - -#include - -namespace sqlpp -{ - template - struct rhs_is_trivial_t - { - static constexpr bool _(const T&) - { - return false; - } - }; - - template - struct rhs_is_trivial_t::value, void>::type> - { - static bool _(const T& t) - { - return t._is_trivial(); - } - }; - - template - struct rhs_is_trivial_t::value, void>::type> - { - static bool _(const T& t) - { - return t._is_trivial(); - } - }; - - template - struct rhs_is_trivial_t::value, void>::type> - { - static bool _(const T& t) - { - if (null_is_trivial_value_t::value) - { - return t._is_trivial(); - } - else - { - if (t.is_null()) - { - return false; - } - else - { - return t._is_trivial(); - } - } - } - }; - - template - constexpr bool rhs_is_trivial(const Expression& e) - { - return rhs_is_trivial_t::type::_rhs_t>::_(e._rhs); - } -} - -#endif diff --git a/include/sqlpp11/rhs_wrap.h b/include/sqlpp11/rhs_wrap.h new file mode 100644 index 00000000..c664d580 --- /dev/null +++ b/include/sqlpp11/rhs_wrap.h @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2013-2014, 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_RHS_WRAP_H +#define SQLPP_RHS_WRAP_H + +#include +#include +#include + +namespace sqlpp +{ + namespace detail + { + template + struct rhs_is_trivial_t + { + static constexpr bool _(const Expr&) + { + return false; + } + }; + + 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> + { + static bool _(const Expr& t) + { + return t._is_trivial(); + } + }; + + template + struct rhs_is_trivial_t::value, void>::type> + { + static bool _(const Expr& t) + { + if (null_is_trivial_value_t::value) + { + return t.is_null(); + } + else + { + if (t.is_null()) + { + return false; + } + else + { + return t._is_trivial(); + } + } + } + }; + + template + struct rhs_is_null_t + { + static constexpr bool _(const Expr&) + { + return false; + } + }; + + 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> + { + static bool _(const Expr& t) + { + return t.is_null(); + } + }; + + } + + template + struct rhs_wrap_t + { + using _traits = typename Expr::_traits; + using _recursive_traits = typename Expr::_recursive_traits; + + rhs_wrap_t(Expr expr): + _expr(expr) + {} + + rhs_wrap_t(const rhs_wrap_t&) = default; + rhs_wrap_t(rhs_wrap_t&&) = default; + rhs_wrap_t& operator=(const rhs_wrap_t&) = default; + rhs_wrap_t& operator=(rhs_wrap_t&&) = default; + ~rhs_wrap_t() = default; + + bool _is_null() const + { + return (TrivialValueIsNull and detail::rhs_is_trivial_t::_(_expr)) + or detail::rhs_is_null_t::_(_expr); + } + + static constexpr bool _is_default() + { + return std::is_same::value; + } + + Expr _expr; + }; + + template + struct serializer_t> + { + using T = rhs_wrap_t; + + static Context& _(const T& t, Context& context) + { + if (t._is_null()) + { + context << "NULL"; + } + else if (t._is_default()) + { + context << "DEFAULT"; + } + else + { + serialize(t._expr, context); + } + return context; + } + }; + +} + +#endif diff --git a/tests/InsertTest.cpp b/tests/InsertTest.cpp index 68b06d82..aae6103f 100644 --- a/tests/InsertTest.cpp +++ b/tests/InsertTest.cpp @@ -27,6 +27,7 @@ #include "MockDb.h" #include "is_regular.h" #include +#include #include MockDb db; @@ -68,7 +69,12 @@ int main() printer.reset(); std::cerr << serialize(i, printer).str() << std::endl; + db(multi_insert); + db(insert_into(t).set(t.delta = sqlpp::verbatim("17+4"))); + db(insert_into(t).set(t.delta = sqlpp::null)); + db(insert_into(t).set(t.delta = sqlpp::default_value)); + return 0; } diff --git a/tests/ResultTest.cpp b/tests/ResultTest.cpp index cfdffe3e..edae0437 100644 --- a/tests/ResultTest.cpp +++ b/tests/ResultTest.cpp @@ -52,8 +52,8 @@ int main() static_assert(sqlpp::can_be_null_t::value, "row.alpha can be null"); static_assert(sqlpp::is_result_field_t::value, "result_fields are not wrapped"); - bool x = sqlpp::rhs_is_null(t.alpha == row.alpha); - bool y = sqlpp::rhs_is_trivial(t.alpha == row.alpha); + bool x = (t.alpha == row.alpha)._rhs._is_null(); + bool y = (t.alpha == row.alpha)._rhs._is_default(); std::cerr << x << std::endl; std::cerr << y << std::endl; diff --git a/tests/UpdateTest.cpp b/tests/UpdateTest.cpp index e3f8de62..04f73024 100644 --- a/tests/UpdateTest.cpp +++ b/tests/UpdateTest.cpp @@ -24,7 +24,7 @@ */ #include -#include +#include #include "Sample.h" #include "MockDb.h" #include "is_regular.h" @@ -64,5 +64,10 @@ int main() db(u); + db(update(t).set(t.delta = sqlpp::verbatim("17+4")).where(true)); + db(update(t).set(t.delta = sqlpp::null).where(true)); + db(update(t).set(t.delta = sqlpp::default_value).where(true)); + + return 0; }