From d3963e04b6b75b8ba5af3415d739caaf11472900 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Fri, 1 Nov 2013 15:25:52 +0100 Subject: [PATCH] Added more checks for database traits --- include/sqlpp11/avg.h | 3 ++- include/sqlpp11/count.h | 1 + include/sqlpp11/exists.h | 1 + include/sqlpp11/group_by.h | 2 ++ include/sqlpp11/having.h | 2 ++ include/sqlpp11/in.h | 12 +++++++----- include/sqlpp11/like.h | 1 + include/sqlpp11/limit.h | 2 ++ include/sqlpp11/max.h | 1 + include/sqlpp11/min.h | 1 + include/sqlpp11/order_by.h | 2 ++ include/sqlpp11/select_pseudo_table.h | 1 + include/sqlpp11/sum.h | 7 ++++--- tests/SelectTest.cpp | 28 +++++++++++++++++++++++++-- 14 files changed, 53 insertions(+), 11 deletions(-) diff --git a/include/sqlpp11/avg.h b/include/sqlpp11/avg.h index 419ebd29..95506838 100644 --- a/include/sqlpp11/avg.h +++ b/include/sqlpp11/avg.h @@ -37,7 +37,7 @@ namespace sqlpp template struct avg_t: public boolean::template operators> { - static_assert(is_value_t::value, "avg() requires a value expression as argument"); + static_assert(is_numeric_t::value, "avg() requires a value expression as argument"); struct _value_type: public Expr::_value_type::_base_value_type { @@ -71,6 +71,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_avg, "avg() not supported by current database"); os << "AVG("; _expr.serialize(os, db); os << ")"; diff --git a/include/sqlpp11/count.h b/include/sqlpp11/count.h index aaff9064..5b88d864 100644 --- a/include/sqlpp11/count.h +++ b/include/sqlpp11/count.h @@ -71,6 +71,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_count, "count() not supported by current database"); os << "COUNT("; _expr.serialize(os, db); os << ")"; diff --git a/include/sqlpp11/exists.h b/include/sqlpp11/exists.h index 0b52beda..83f96b44 100644 --- a/include/sqlpp11/exists.h +++ b/include/sqlpp11/exists.h @@ -71,6 +71,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_exists, "exists() not supported by current database"); os << "EXISTS("; _select.serialize(os, db); os << ")"; diff --git a/include/sqlpp11/group_by.h b/include/sqlpp11/group_by.h index 951e3bbc..945cb6e7 100644 --- a/include/sqlpp11/group_by.h +++ b/include/sqlpp11/group_by.h @@ -57,6 +57,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_group_by, "group_by() not supported by current database"); os << " GROUP BY "; detail::serialize_tuple(os, db, _expressions, ','); } @@ -80,6 +81,7 @@ namespace sqlpp void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_group_by, "group_by() not supported by current database"); if (_expressions.empty()) return; os << " GROUP BY "; diff --git a/include/sqlpp11/having.h b/include/sqlpp11/having.h index 2c93561c..1ae9cc21 100644 --- a/include/sqlpp11/having.h +++ b/include/sqlpp11/having.h @@ -46,6 +46,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_having, "having() not supported by current database"); os << " HAVING "; _expr.serialize(os, db); } @@ -69,6 +70,7 @@ namespace sqlpp void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_having, "having() not supported by current database"); if (_conditions.empty()) return; diff --git a/include/sqlpp11/in.h b/include/sqlpp11/in.h index 50e6b4a2..f7345ecf 100644 --- a/include/sqlpp11/in.h +++ b/include/sqlpp11/in.h @@ -35,10 +35,10 @@ namespace sqlpp { namespace detail { - template - struct in_t: public Operand::_value_type::template operators> + template + struct in_t: public Operand::_value_type::template operators> { - static constexpr bool inverted = not not_inverted; + static constexpr bool _inverted = not NotInverted; static_assert(sizeof...(Args) > 0, "in() requires at least one argument"); struct _value_type: public Operand::_value_type::_base_value_type @@ -48,7 +48,7 @@ namespace sqlpp struct _name_t { - static constexpr const char* _get_name() { return inverted ? "NOT IN" : "IN"; } + static constexpr const char* _get_name() { return _inverted ? "NOT IN" : "IN"; } template struct _member_t { @@ -75,8 +75,10 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(NotInverted and Db::_supports_in + or _inverted and Db::_supports_not_in, "in() not supported by current database"); _operand.serialize(os, db); - os << (inverted ? " NOT IN(" : " IN("); + os << (_inverted ? " NOT IN(" : " IN("); detail::serialize_tuple(os, db, _args, ','); os << ")"; } diff --git a/include/sqlpp11/like.h b/include/sqlpp11/like.h index 1c8049c8..4213841e 100644 --- a/include/sqlpp11/like.h +++ b/include/sqlpp11/like.h @@ -75,6 +75,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_like, "like() not supported by current database"); _operand.serialize(os, db); os << " LIKE("; _pattern.serialize(os, db); diff --git a/include/sqlpp11/limit.h b/include/sqlpp11/limit.h index 35dc03c7..7e19696b 100644 --- a/include/sqlpp11/limit.h +++ b/include/sqlpp11/limit.h @@ -40,6 +40,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_limit, "limit not supported by current database"); os << " LIMIT " << _limit; } @@ -69,6 +70,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_limit, "limit not supported by current database"); if (_limit > 0) os << " LIMIT " << _limit; } diff --git a/include/sqlpp11/max.h b/include/sqlpp11/max.h index 1431b576..d3e32690 100644 --- a/include/sqlpp11/max.h +++ b/include/sqlpp11/max.h @@ -71,6 +71,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_max, "max not supported by current database"); os << "MAX("; _expr.serialize(os, db); os << ")"; diff --git a/include/sqlpp11/min.h b/include/sqlpp11/min.h index c611f374..3d1c76b4 100644 --- a/include/sqlpp11/min.h +++ b/include/sqlpp11/min.h @@ -71,6 +71,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_min, "min not supported by current database"); os << "MIN("; _expr.serialize(os, db); os << ")"; diff --git a/include/sqlpp11/order_by.h b/include/sqlpp11/order_by.h index 596000b0..3c5cd10a 100644 --- a/include/sqlpp11/order_by.h +++ b/include/sqlpp11/order_by.h @@ -54,6 +54,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_order_by, "order by not supported by current database"); os << " ORDER BY "; detail::serialize_tuple(os, db, _orderExpressions, ','); } @@ -76,6 +77,7 @@ namespace sqlpp void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_order_by, "order by not supported by current database"); if (_expressions.empty()) return; os << " ORDER BY "; diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index b90759bd..78932454 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -69,6 +69,7 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { + static_assert(Db::_supports_select_as_table, "select as table not supported by current database"); _select.serialize(os, db); } diff --git a/include/sqlpp11/sum.h b/include/sqlpp11/sum.h index 1431b576..6b8d7c2b 100644 --- a/include/sqlpp11/sum.h +++ b/include/sqlpp11/sum.h @@ -37,7 +37,7 @@ namespace sqlpp template struct max_t: public boolean::template operators> { - static_assert(is_value_t::value, "max() requires a value expression as argument"); + static_assert(is_numeric_t::value, "sum() requires a numeric expression as argument"); struct _value_type: public Expr::_value_type::_base_value_type { @@ -46,7 +46,7 @@ namespace sqlpp struct _name_t { - static constexpr const char* _get_name() { return "MAX"; } + static constexpr const char* _get_name() { return "SUM"; } template struct _member_t { @@ -71,7 +71,8 @@ namespace sqlpp template void serialize(std::ostream& os, Db& db) const { - os << "MAX("; + static_assert(Db::_supports_sum, "sum not supported by current database"); + os << "SUM("; _expr.serialize(os, db); os << ")"; } diff --git a/tests/SelectTest.cpp b/tests/SelectTest.cpp index 8af6d64f..9348f3d2 100644 --- a/tests/SelectTest.cpp +++ b/tests/SelectTest.cpp @@ -32,10 +32,34 @@ class DbMock: public sqlpp::connection { public: - static constexpr bool _supports_any = true; + // join types + static constexpr bool _supports_inner_join = true; + static constexpr bool _supports_outer_join = true; + static constexpr bool _supports_left_outer_join = true; + static constexpr bool _supports_right_outer_join = true; + // functions + static constexpr bool _supports_avg = true; + static constexpr bool _supports_count = true; + static constexpr bool _supports_exists = true; + static constexpr bool _supports_like = true; + static constexpr bool _supports_in = true; + static constexpr bool _supports_max = true; + static constexpr bool _supports_min = true; + static constexpr bool _supports_not_in = true; + static constexpr bool _supports_sum = true; + + // select + static constexpr bool _supports_group_by = true; + static constexpr bool _supports_having = true; + static constexpr bool _supports_limit = true; + static constexpr bool _supports_order_by = true; + static constexpr bool _supports_select_as_table = true; + + static constexpr bool _supports_some = true; + static constexpr bool _supports_any = true; + static constexpr bool _use_concat_operator = true; static constexpr bool _use_concat_function = true; - static constexpr bool _use_concat_operator = false; const std::string& escape(const std::string& text) { return text; } };