0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Merge branch 'feature/prevent_self_comparison' into develop

This commit is contained in:
rbock 2016-04-08 16:17:42 +02:00
commit 9d6ad6511f
14 changed files with 296 additions and 87 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -41,42 +41,23 @@
namespace sqlpp namespace sqlpp
{ {
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison"); SQLPP_PORTABLE_STATIC_ASSERT(assert_comparison_valid_rhs_operand_t, "invalid rhs operand in comparison");
SQLPP_PORTABLE_STATIC_ASSERT(assert_comparison_lhs_rhs_differ_t, "identical lhs and rhs operand types in comparison");
template <typename LhsValueType, typename RhsType> template <typename LhsType, typename RhsType>
using check_rhs_comparison_operand_t = using check_rhs_comparison_operand_t = static_combined_check_t<
static_check_t<(is_expression_t<sqlpp::wrap_operand_t<RhsType>>::value // expressions are OK static_check_t<(is_expression_t<sqlpp::wrap_operand_t<RhsType>>::value // expressions are OK
or or
is_multi_expression_t<sqlpp::wrap_operand_t<RhsType>>::value) // multi-expressions like ANY are is_multi_expression_t<sqlpp::wrap_operand_t<RhsType>>::value) // multi-expressions like ANY are
// OK for comparisons, too // OK for comparisons, too
and and
LhsValueType::template _is_valid_operand< value_type_of<LhsType>::template _is_valid_operand<
sqlpp::wrap_operand_t<RhsType>>::value, // the correct value type is required, of course sqlpp::wrap_operand_t<RhsType>>::value, // the correct value type is required, of course
assert_valid_rhs_comparison_operand_t>; assert_comparison_valid_rhs_operand_t>,
static_check_t<not std::is_same<LhsType, RhsType>::value, assert_comparison_lhs_rhs_differ_t>>;
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid"); template <typename LhsType, typename... InTypes>
using check_rhs_in_arguments_t = static_combined_check_t<check_rhs_comparison_operand_t<LhsType, InTypes>...>;
template <typename LhsValueType, typename... InTypes>
using check_rhs_in_arguments_t =
static_check_t<logic::all_t<check_rhs_comparison_operand_t<LhsValueType, InTypes>::value...>::value,
assert_valid_in_arguments_t>;
namespace detail
{
template <bool Enable, template <typename Lhs> class Expr, typename Lhs>
struct new_unary_expression_impl
{
using type = bad_statement;
};
template <template <typename Lhs> class Expr, typename Lhs>
struct new_unary_expression_impl<true, Expr, Lhs>
{
using type = Expr<Lhs>;
};
}
template <typename Check, template <typename Lhs> class Expr, typename Lhs>
using new_unary_expression_t = typename detail::new_unary_expression_impl<Check::value, Expr, Lhs>::type;
namespace detail namespace detail
{ {
@ -117,16 +98,20 @@ namespace sqlpp
struct basic_expression_operators struct basic_expression_operators
{ {
template <template <typename Lhs, typename Rhs> class NewExpr, typename T> template <template <typename Lhs, typename Rhs> class NewExpr, typename T>
using _new_binary_expression_t = struct _new_binary_expression
new_binary_expression_t<check_rhs_comparison_operand_t<ValueType, wrap_operand_t<T>>, {
using type = new_binary_expression_t<check_rhs_comparison_operand_t<Expr, wrap_operand_t<T>>,
NewExpr, NewExpr,
Expr, Expr,
wrap_operand_t<T>>; wrap_operand_t<T>>;
};
template <template <typename Lhs, typename Rhs> class NewExpr, typename T>
using _new_binary_expression_t = typename _new_binary_expression<NewExpr, T>::type;
// workaround for msvs bug // workaround for msvs bug
// template <template <typename Lhs, typename... Rhs> class NewExpr, typename... T> // template <template <typename Lhs, typename... Rhs> class NewExpr, typename... T>
// using _new_nary_expression_t = // using _new_nary_expression_t =
// new_nary_expression_t<logic::all_t<check_rhs_comparison_operand_t<ValueType, // new_nary_expression_t<logic::all_t<check_rhs_comparison_operand_t<
// wrap_operand_t<T>>::value...>, // wrap_operand_t<T>>::value...>,
// NewExpr, // NewExpr,
// Expr, // Expr,
@ -135,7 +120,7 @@ namespace sqlpp
struct _new_nary_expression struct _new_nary_expression
{ {
using type = using type =
new_nary_expression_t<logic::all_t<check_rhs_comparison_operand_t<ValueType, wrap_operand_t<T>>::value...>, new_nary_expression_t<logic::all_t<check_rhs_comparison_operand_t<Expr, wrap_operand_t<T>>::value...>,
NewExpr, NewExpr,
Expr, Expr,
wrap_operand_t<T>...>; wrap_operand_t<T>...>;
@ -145,25 +130,25 @@ namespace sqlpp
_new_binary_expression_t<equal_to_t, T> operator==(T t) const _new_binary_expression_t<equal_to_t, T> operator==(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), {rhs{t}}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
template <typename T> template <typename T>
_new_binary_expression_t<not_equal_to_t, T> operator!=(T t) const _new_binary_expression_t<not_equal_to_t, T> operator!=(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), {rhs{t}}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
template <typename T> template <typename T>
_new_binary_expression_t<less_than_t, T> operator<(T t) const _new_binary_expression_t<less_than_t, T> operator<(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
@ -172,7 +157,7 @@ namespace sqlpp
_new_binary_expression_t<less_equal_t, T> operator<=(T t) const _new_binary_expression_t<less_equal_t, T> operator<=(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
@ -181,7 +166,7 @@ namespace sqlpp
_new_binary_expression_t<greater_than_t, T> operator>(T t) const _new_binary_expression_t<greater_than_t, T> operator>(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
@ -190,7 +175,7 @@ namespace sqlpp
_new_binary_expression_t<greater_equal_t, T> operator>=(T t) const _new_binary_expression_t<greater_equal_t, T> operator>=(T t) const
{ {
using rhs = wrap_operand_t<T>; using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_(); check_rhs_comparison_operand_t<Expr, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}}; return {*static_cast<const Expr*>(this), rhs{t}};
} }
@ -220,13 +205,13 @@ namespace sqlpp
// template <typename... T> // template <typename... T>
// _new_nary_expression_t<in_t, T...> in(T... t) const // _new_nary_expression_t<in_t, T...> in(T... t) const
// { // {
// check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_(); // check_rhs_in_arguments_t<Expr, wrap_operand_t<T>...>::_();
// return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...}; // return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
// } // }
template <typename... T> template <typename... T>
typename _new_nary_expression<in_t, T...>::type in(T... t) const typename _new_nary_expression<in_t, T...>::type in(T... t) const
{ {
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_(); check_rhs_in_arguments_t<Expr, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...}; return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...};
} }
@ -234,13 +219,13 @@ namespace sqlpp
// template <typename... T> // template <typename... T>
// _new_nary_expression_t<not_in_t, T...> not_in(T... t) const // _new_nary_expression_t<not_in_t, T...> not_in(T... t) const
// { // {
// check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_(); // check_rhs_in_arguments_t<Expr, wrap_operand_t<T>...>::_();
// return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...}; // return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
// } // }
template <typename... T> template <typename... T>
typename _new_nary_expression<not_in_t, T...>::type not_in(T... t) const typename _new_nary_expression<not_in_t, T...>::type not_in(T... t) const
{ {
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_(); check_rhs_in_arguments_t<Expr, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...}; return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...};
} }

View File

@ -27,7 +27,6 @@
#ifndef SQLPP_FLOATING_POINT_RESULT_FIELD_H #ifndef SQLPP_FLOATING_POINT_RESULT_FIELD_H
#define SQLPP_FLOATING_POINT_RESULT_FIELD_H #define SQLPP_FLOATING_POINT_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h> #include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h> #include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h> #include <sqlpp11/result_field_base.h>

View File

@ -48,7 +48,7 @@ namespace sqlpp
using _rhs_t = rhs_wrap_t<allow_tvin_t<Rhs>, trivial_value_is_null_t<_lhs_t>::value>; using _rhs_t = rhs_wrap_t<allow_tvin_t<Rhs>, trivial_value_is_null_t<_lhs_t>::value>;
using _nodes = detail::type_vector<_lhs_t, _rhs_t>; using _nodes = detail::type_vector<_lhs_t, _rhs_t>;
binary_expression_t(_lhs_t lhs, _rhs_t rhs) : _lhs(lhs), _rhs(rhs) binary_expression_t(Lhs lhs, Rhs rhs) : _lhs(lhs), _rhs(rhs)
{ {
} }
@ -96,7 +96,7 @@ namespace sqlpp
using _rhs_t = rhs_wrap_t<allow_tvin_t<Rhs>, trivial_value_is_null_t<_lhs_t>::value>; using _rhs_t = rhs_wrap_t<allow_tvin_t<Rhs>, trivial_value_is_null_t<_lhs_t>::value>;
using _nodes = detail::type_vector<_lhs_t, _rhs_t>; using _nodes = detail::type_vector<_lhs_t, _rhs_t>;
binary_expression_t(Lhs lhs, _rhs_t rhs) : _lhs(lhs), _rhs(rhs) binary_expression_t(Lhs lhs, Rhs rhs) : _lhs(lhs), _rhs(rhs)
{ {
} }

View File

@ -80,7 +80,7 @@ namespace sqlpp
struct return_type_plus struct return_type_plus
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<L>>;
}; };
template <typename L, typename R> template <typename L, typename R>
using return_type_plus_t = typename return_type_plus<L, R>::type; using return_type_plus_t = typename return_type_plus<L, R>::type;
@ -89,7 +89,7 @@ namespace sqlpp
struct return_type_minus struct return_type_minus
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<L>>;
}; };
template <typename L, typename R> template <typename L, typename R>
using return_type_minus_t = typename return_type_minus<L, R>::type; using return_type_minus_t = typename return_type_minus<L, R>::type;
@ -98,7 +98,7 @@ namespace sqlpp
struct return_type_multiplies struct return_type_multiplies
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<L>>;
}; };
template <typename L, typename R> template <typename L, typename R>
using return_type_multiplies_t = typename return_type_multiplies<L, R>::type; using return_type_multiplies_t = typename return_type_multiplies<L, R>::type;
@ -107,7 +107,7 @@ namespace sqlpp
struct return_type_divides struct return_type_divides
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<L>>;
}; };
template <typename L, typename R> template <typename L, typename R>
using return_type_divides_t = typename return_type_divides<L, R>::type; using return_type_divides_t = typename return_type_divides<L, R>::type;
@ -116,7 +116,7 @@ namespace sqlpp
struct return_type_modulus struct return_type_modulus
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<L>>;
}; };
template <typename L, typename R> template <typename L, typename R>
using return_type_modulus_t = typename return_type_modulus<L, R>::type; using return_type_modulus_t = typename return_type_modulus<L, R>::type;
@ -125,7 +125,7 @@ namespace sqlpp
struct return_type_unary_plus struct return_type_unary_plus
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<T>>;
}; };
template <typename T, typename Defer> template <typename T, typename Defer>
using return_type_unary_plus_t = typename return_type_unary_plus<T, Defer>::type; using return_type_unary_plus_t = typename return_type_unary_plus<T, Defer>::type;
@ -134,7 +134,7 @@ namespace sqlpp
struct return_type_unary_minus struct return_type_unary_minus
{ {
using check = assert_valid_operands; using check = assert_valid_operands;
using type = bad_expression<boolean>; using type = bad_expression<value_type_of<T>>;
}; };
template <typename T, typename Defer> template <typename T, typename Defer>
using return_type_unary_minus_t = typename return_type_unary_minus<T, Defer>::type; using return_type_unary_minus_t = typename return_type_unary_minus<T, Defer>::type;

View File

@ -66,7 +66,7 @@ namespace sqlpp
template <typename L, template <typename> class LPred, typename R, template <typename> class RPred> template <typename L, template <typename> class LPred, typename R, template <typename> class RPred>
using binary_operand_check_t = using binary_operand_check_t =
typename binary_operand_check<wrap_operand_t<L>, LPred, wrap_operand_t<L>, RPred>::type; typename binary_operand_check<wrap_operand_t<L>, LPred, wrap_operand_t<R>, RPred>::type;
template <typename L, template <typename> class LPred, typename R, template <typename> class RPred> template <typename L, template <typename> class LPred, typename R, template <typename> class RPred>
using unwrapped_binary_operand_check_t = typename binary_operand_check<L, LPred, R, RPred>::type; using unwrapped_binary_operand_check_t = typename binary_operand_check<L, LPred, R, RPred>::type;

View File

@ -44,10 +44,9 @@ int In(int, char* [])
const auto bar = test::TabBar{}; const auto bar = test::TabBar{};
// Individual values // Individual values
compare(__LINE__, foo.omega.in(foo.omega), "tab_foo.omega IN(tab_foo.omega)"); compare(__LINE__, foo.omega.in(17), "tab_foo.omega IN(17)");
compare(__LINE__, foo.omega.in(foo.omega, bar.alpha), "tab_foo.omega IN(tab_foo.omega,tab_bar.alpha)"); compare(__LINE__, foo.omega.in(17, bar.alpha), "tab_foo.omega IN(17,tab_bar.alpha)");
compare(__LINE__, foo.omega.in(foo.omega, bar.alpha, sqlpp::value(17)), compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "tab_foo.omega IN(17,tab_bar.alpha,19)");
"tab_foo.omega IN(tab_foo.omega,tab_bar.alpha,17)");
// Lists // Lists
compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.7f, 2.5f, 17.f, 0.f})), compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.7f, 2.5f, 17.f, 0.f})),

View File

@ -142,6 +142,28 @@ namespace test
}; };
using _traits = sqlpp::make_traits<sqlpp::time_point, sqlpp::tag::can_be_null>; using _traits = sqlpp::make_traits<sqlpp::time_point, sqlpp::tag::can_be_null>;
}; };
struct OtherString
{
struct _alias_t
{
static constexpr const char _literal[] = "other_string";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherString;
T& operator()()
{
return otherString;
}
const T& operator()() const
{
return otherString;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::can_be_null>;
};
struct OtherInt struct OtherInt
{ {
struct _alias_t struct _alias_t
@ -261,6 +283,7 @@ namespace test
TabAllTypes_::SomeBool, TabAllTypes_::SomeBool,
TabAllTypes_::SomeDayPoint, TabAllTypes_::SomeDayPoint,
TabAllTypes_::SomeTimePoint, TabAllTypes_::SomeTimePoint,
TabAllTypes_::OtherString,
TabAllTypes_::OtherInt, TabAllTypes_::OtherInt,
TabAllTypes_::OtherFloat, TabAllTypes_::OtherFloat,
TabAllTypes_::OtherBool, TabAllTypes_::OtherBool,

View File

@ -23,7 +23,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function(test_compile name) function(test_compile name)
set(target sqlpp11_${name}) set(target sqlpp11_assert_${name})
add_executable(${target} ${name}.cpp) add_executable(${target} ${name}.cpp)
target_link_libraries(${target} PRIVATE sqlpp11 sqlpp11_testing) target_link_libraries(${target} PRIVATE sqlpp11 sqlpp11_testing)
endfunction() endfunction()
@ -36,5 +36,7 @@ test_compile(where)
test_compile(insert) test_compile(insert)
test_compile(date) test_compile(date)
test_compile(date_time) test_compile(date_time)
test_compile(text)
test_compile(no_self_compare)
test_compile(unwrapped_bool) test_compile(unwrapped_bool)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015-2015, Roland Bock * Copyright (c) 2015-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -66,20 +66,21 @@ namespace
void allowed_comparands() void allowed_comparands()
{ {
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now()); static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDayPoint); static_check_comparison<sqlpp::consistent_t>(t.otherDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.someTimePoint); static_check_comparison<sqlpp::consistent_t>(t.someTimePoint);
static_check_comparison<sqlpp::consistent_t>(t.otherTimePoint);
} }
void disallowed_comparands() void disallowed_comparands()
{ {
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a'); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a")); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015-2015, Roland Bock * Copyright (c) 2015-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -67,19 +67,20 @@ namespace
{ {
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now()); static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDayPoint); static_check_comparison<sqlpp::consistent_t>(t.someDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.someTimePoint); static_check_comparison<sqlpp::consistent_t>(t.otherDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.otherTimePoint);
} }
void disallowed_comparands() void disallowed_comparands()
{ {
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a'); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a")); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString); static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
} }
} }

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2015-2016, 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.
*/
#include <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_self_compare(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<Operand, Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<decltype(operand < operand), decltype(operand <= operand),
decltype(operand == operand), decltype(operand != operand),
decltype(operand >= operand), decltype(operand > operand)>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void disallowed_self_comparison()
{
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someString);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someInt);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someFloat);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someBool);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someDayPoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someTimePoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherString);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherInt);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherFloat);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherBool);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherDayPoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherTimePoint);
}
}
int main(int, char* [])
{
// t.someString == t.someString;
disallowed_self_comparison();
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 2015-2016, 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.
*/
#include <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<decltype(t.someString), Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<
decltype(t.someString < operand), decltype(t.someString <= operand), decltype(t.someString == operand),
decltype(t.someString != operand), decltype(t.someString >= operand), decltype(t.someString > operand),
decltype(t.someString.in(operand)), decltype(t.someString.in(operand, operand)),
decltype(t.someString.not_in(operand)), decltype(t.someString.not_in(operand, operand))>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>("");
// static_check_comparison<sqlpp::consistent_t>('d'); // not today
static_check_comparison<sqlpp::consistent_t>(std::string(""));
static_check_comparison<sqlpp::consistent_t>(t.otherString);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17.4);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someDayPoint);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someTimePoint);
}
template <typename Expected, typename Expression>
void static_check_type()
{
static_assert(std::is_same<Expected, Expression>::value, "Unexpected check result");
}
auto check_expressions() -> void
{
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString + 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString - 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString - "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString * 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString * "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString / 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString / "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString % 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString % "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(-t.someString)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(+t.someString)>();
}
}
int main(int, char* [])
{
allowed_comparands();
disallowed_comparands();
check_expressions();
}

View File

@ -358,7 +358,8 @@ int SelectType(int, char* [])
static_assert(sqlpp::is_numeric_t<T>::value, "T has to be numeric"); static_assert(sqlpp::is_numeric_t<T>::value, "T has to be numeric");
static_assert(sqlpp::is_numeric_t<decltype(t.alpha)>::value, "TabBar.alpha has to be a numeric"); static_assert(sqlpp::is_numeric_t<decltype(t.alpha)>::value, "TabBar.alpha has to be a numeric");
((t.alpha + 7) + 4).asc(); ((t.alpha + 7) + 4).asc();
static_assert(sqlpp::is_boolean_t<decltype(t.gamma == t.gamma)>::value, "Comparison expression have to be boolean"); static_assert(sqlpp::is_boolean_t<decltype(t.gamma != not(t.gamma))>::value,
"Comparison expression have to be boolean");
!t.gamma; !t.gamma;
serialize(t.beta < "kaesekuchen", printer).str(); serialize(t.beta < "kaesekuchen", printer).str();
serialize(t.beta + "hallenhalma", printer).str(); serialize(t.beta + "hallenhalma", printer).str();

View File

@ -54,7 +54,7 @@ int Update(int, char* [])
serialize(update(t), printer).str(); serialize(update(t), printer).str();
serialize(update(t).set(t.gamma = false), printer).str(); serialize(update(t).set(t.gamma = false), printer).str();
serialize(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).str(); serialize(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).str();
serialize(update(t).set(t.beta = "opaque").where(t.beta != t.beta), printer).str(); serialize(update(t).set(t.beta = "opaque").where(t.beta != t.beta + "this is nonsense"), printer).str();
auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where(); auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where();
u.assignments.add(t.beta = "cannot update gamma a second time"); u.assignments.add(t.beta = "cannot update gamma a second time");
u.where.add(t.gamma != false); u.where.add(t.gamma != false);