From 94a0eee117cd69d00b32a741749333e03f85cb27 Mon Sep 17 00:00:00 2001 From: rbock Date: Tue, 6 Sep 2016 22:34:59 +0200 Subject: [PATCH] Enforce unsigned integral values for limit and offset --- include/sqlpp11/limit.h | 9 +++-- include/sqlpp11/offset.h | 11 ++--- test_serializer/CustomQuery.cpp | 2 +- tests/Interpret.cpp | 19 +++++---- tests/Select.cpp | 12 +++--- tests/SelectType.cpp | 71 +++++++++++++++++++-------------- 6 files changed, 71 insertions(+), 53 deletions(-) diff --git a/include/sqlpp11/limit.h b/include/sqlpp11/limit.h index 294c2ed0..0b35d50f 100644 --- a/include/sqlpp11/limit.h +++ b/include/sqlpp11/limit.h @@ -153,7 +153,8 @@ namespace sqlpp { // FIXME: Make sure that Limit does not require external tables? Need to read up on SQL using arg_t = wrap_operand_t; - static_assert(is_integral_t::value, "limit requires an integral value or integral parameter"); + static_assert(is_unsigned_integral_t::value, + "limit requires an unsigned integral value or unsigned integral parameter"); _data._value = arg_t{value}; _data._initialized = true; } @@ -194,11 +195,13 @@ namespace sqlpp }; }; - SQLPP_PORTABLE_STATIC_ASSERT(assert_limit_is_integral, "argument for limit() must be an integral expressions"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_limit_is_unsigned_integral, + "argument for limit() must be an unsigned integral expressions"); template struct check_limit { - using type = static_combined_check_t::value, assert_limit_is_integral>>; + using type = + static_combined_check_t::value, assert_limit_is_unsigned_integral>>; }; template using check_limit_t = typename check_limit>::type; diff --git a/include/sqlpp11/offset.h b/include/sqlpp11/offset.h index 458b974a..fa2f9e1c 100644 --- a/include/sqlpp11/offset.h +++ b/include/sqlpp11/offset.h @@ -57,8 +57,6 @@ namespace sqlpp using _traits = make_traits; using _nodes = detail::type_vector; - static_assert(is_integral_t::value, "offset requires an integral value or integral parameter"); - // Data using _data_t = offset_data_t; @@ -155,7 +153,8 @@ namespace sqlpp { // FIXME: Make sure that Offset does not require external tables? Need to read up on SQL using arg_t = wrap_operand_t; - static_assert(is_integral_t::value, "offset requires an integral value or integral parameter"); + static_assert(is_unsigned_integral_t::value, + "offset requires an unsigned integral value or unsigned integral parameter"); _data._value = arg_t{value}; _data._initialized = true; } @@ -208,11 +207,13 @@ namespace sqlpp interpretable_t _value; }; - SQLPP_PORTABLE_STATIC_ASSERT(assert_offset_is_integral, "argument for offset() must be an integral expressions"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_offset_is_unsigned_integral, + "argument for offset() must be an integral expressions"); template struct check_offset { - using type = static_combined_check_t::value, assert_offset_is_integral>>; + using type = + static_combined_check_t::value, assert_offset_is_unsigned_integral>>; }; template using check_offset_t = typename check_offset>::type; diff --git a/test_serializer/CustomQuery.cpp b/test_serializer/CustomQuery.cpp index 67294930..e4afaeb2 100644 --- a/test_serializer/CustomQuery.cpp +++ b/test_serializer/CustomQuery.cpp @@ -46,7 +46,7 @@ int CustomQuery(int, char* []) compare(__LINE__, custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo.join(bar).on(foo.omega == bar.alpha)), where(bar.alpha > 17), group_by(foo.omega), - having(avg(bar.alpha) > 19), order_by(foo.omega.asc()), sqlpp::limit(10), sqlpp::offset(100)), + having(avg(bar.alpha) > 19), order_by(foo.omega.asc()), sqlpp::limit(10u), sqlpp::offset(100u)), "SELECT DISTINCT tab_foo.omega FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha) WHERE " "(tab_bar.alpha>17) GROUP BY tab_foo.omega HAVING (AVG(tab_bar.alpha)>19) ORDER BY tab_foo.omega ASC " "LIMIT 10 OFFSET 100"); diff --git a/tests/Interpret.cpp b/tests/Interpret.cpp index 8e671948..29e473b4 100644 --- a/tests/Interpret.cpp +++ b/tests/Interpret.cpp @@ -23,8 +23,8 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "Sample.h" #include "MockDb.h" +#include "Sample.h" #include #include @@ -74,23 +74,26 @@ int Interpret(int, char* []) serialize(select(t.alpha, t.beta).from(t).where(t.alpha == 3), printer).str(); serialize(select(t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma), printer).str(); serialize(select(t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")), - printer).str(); + printer) + .str(); serialize(select(t.alpha, t.beta) .from(t) .where(t.alpha == 3) .group_by(t.gamma) .having(t.beta.like("%kuchen")) .order_by(t.beta.asc()), - printer).str(); + printer) + .str(); serialize(select(t.alpha, t.beta) .from(t) .where(t.alpha == 3) .group_by(t.gamma) .having(t.beta.like("%kuchen")) .order_by(t.beta.asc()) - .limit(17) - .offset(3), - printer).str(); + .limit(17u) + .offset(3u), + printer) + .str(); serialize(parameter(sqlpp::bigint(), t.alpha), printer).str(); serialize(parameter(t.alpha), printer).str(); @@ -199,7 +202,9 @@ int Interpret(int, char* []) printer.reset(); std::cerr << serialize(select(all_of(t)).from(t).where(t.alpha.in(select(f.epsilon).from(f).unconditionally())), - printer).str() << std::endl; + printer) + .str() + << std::endl; printer.reset(); std::cerr << serialize(select(all_of(t)).from(t).where(t.alpha.in()), printer).str() << std::endl; diff --git a/tests/Select.cpp b/tests/Select.cpp index c60fb4e4..4b99b3a7 100644 --- a/tests/Select.cpp +++ b/tests/Select.cpp @@ -122,8 +122,8 @@ int Select(int, char* []) .group_by(t.alpha) .order_by(t.gamma.asc()) .having(t.gamma) - .offset(19) - .limit(7); + .offset(19u) + .limit(7u); printer.reset(); std::cerr << serialize(stat, printer).str() << std::endl; @@ -135,8 +135,8 @@ int Select(int, char* []) .group_by(t.alpha) .order_by(t.gamma.asc()) .having(t.gamma) - .limit(7) - .offset(19); + .limit(7u) + .offset(19u); printer.reset(); std::cerr << serialize(s0, printer).str() << std::endl; @@ -156,8 +156,8 @@ int Select(int, char* []) s.from.add(dynamic_cross_join(f)); s.where.add(t.alpha > 7); s.having.add(t.alpha > 7); - s.limit.set(3); - s.offset.set(3); + s.limit.set(3u); + s.offset.set(3u); s.group_by.add(t.beta); s.order_by.add(t.beta.asc()); for (const auto& row : db(s)) diff --git a/tests/SelectType.cpp b/tests/SelectType.cpp index 26b7857b..9dc7d845 100644 --- a/tests/SelectType.cpp +++ b/tests/SelectType.cpp @@ -23,14 +23,14 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include "Sample.h" #include "MockDb.h" +#include "Sample.h" #include "is_regular.h" +#include #include -#include -#include #include +#include +#include #include namespace alias @@ -137,29 +137,38 @@ int SelectType(int, char* []) static_assert(not sqlpp::is_alias_t::value, "type requirement"); static_assert(not sqlpp::is_table_t::value, "type requirement"); static_assert(sqlpp::is_regular::value, "type requirement"); - //subtraction on unsigned makes it signed - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - //any operation on float makes it float - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - static_assert(sqlpp::is_floating_point_t>::value, "type requirement"); - //signed operation on unsigned makes it signed - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); - static_assert(sqlpp::is_integral_t>::value, "type requirement"); + // subtraction on unsigned makes it signed + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + // any operation on float makes it float + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + static_assert(sqlpp::is_floating_point_t>::value, + "type requirement"); + // signed operation on unsigned makes it signed + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); + static_assert(sqlpp::is_integral_t>::value, "type requirement"); } // Test a floating point table column @@ -370,8 +379,8 @@ int SelectType(int, char* []) auto s = dynamic_select(db, all_of(t)).dynamic_from(t).dynamic_where().dynamic_limit().dynamic_offset(); s.from.add(dynamic_join(f).on(f.omega > t.alpha)); s.where.add(without_table_check(f.omega > 7 and t.alpha == any(select(t.alpha).from(t).where(t.alpha < 3)))); - s.limit.set(30); - s.limit.set(3); + s.limit.set(30u); + s.limit.set(3u); std::cerr << "------------------------\n"; serialize(s, printer).str(); std::cerr << "------------------------\n"; @@ -424,8 +433,8 @@ int SelectType(int, char* []) .group_by(l.gamma, r.a) .having(r.a != true) .order_by(l.beta.asc()) - .limit(17) - .offset(3) + .limit(17u) + .offset(3u) .as(alias::a); return 0;