From a17a8ecfa6cd730a71a274d8b5e632341292751c Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Thu, 31 Oct 2013 09:43:10 +0100 Subject: [PATCH] Reduced resource requirements for non-dynamic selects --- include/sqlpp11/result.h | 6 +-- include/sqlpp11/result_row.h | 3 +- include/sqlpp11/select.h | 6 +-- include/sqlpp11/select_expression_list.h | 49 +++++++++++++++++------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/include/sqlpp11/result.h b/include/sqlpp11/result.h index bddba2c7..3acf6de4 100644 --- a/include/sqlpp11/result.h +++ b/include/sqlpp11/result.h @@ -33,7 +33,7 @@ namespace sqlpp { - template + template class result_t { using db_result_t = typename Db::_result_t; @@ -41,7 +41,7 @@ namespace sqlpp db_result_t _result; raw_result_row_t _raw_result_row; raw_result_row_t _end; - std::vector _dynamic_columns; // only needed in case of dynamic columns in the select + DynamicNames _dynamic_columns; // only needed in case of dynamic columns in the select ResultRow _result_row; public: @@ -52,7 +52,7 @@ namespace sqlpp _result_row(_raw_result_row, _dynamic_columns) {} - result_t(db_result_t&& result, std::vector dynamic_columns): + result_t(db_result_t&& result, DynamicNames dynamic_columns): _result(std::move(result)), _raw_result_row(_result.next()), _end({}), diff --git a/include/sqlpp11/result_row.h b/include/sqlpp11/result_row.h index ff418472..827dad68 100644 --- a/include/sqlpp11/result_row.h +++ b/include/sqlpp11/result_row.h @@ -104,7 +104,8 @@ namespace sqlpp using _impl = detail::result_row_impl<0, 0, NamedExpr...>; bool _is_row; - result_row_t(const raw_result_row_t& raw_result_row, const std::vector&): // FIXME: it hurts a bit to always transport the dynamic part as well + template + result_row_t(const raw_result_row_t& raw_result_row, const T&): _impl(raw_result_row), _is_row(raw_result_row.data != nullptr) { diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 57928ecb..c615b826 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -108,6 +108,7 @@ namespace sqlpp using set_offset_t = select_t; using _result_row_t = typename ExpressionList::_result_row_t; + using _dynamic_names_t = typename ExpressionList::_dynamic_names_t; // Indicators using _value_type = typename std::conditional< @@ -560,10 +561,9 @@ namespace sqlpp return *this; } - // Execute template - result_t run(Db& db) const + result_t run(Db& db) const { static_assert(not is_noop::value, "cannot run select without having selected anything"); static_assert(is_from_t::value, "cannot run select without a from()"); @@ -572,7 +572,7 @@ namespace sqlpp std::ostringstream oss; serialize(oss, db); - return {db.select(oss.str()), _expression_list._dynamic_expression_names}; + return {db.select(oss.str()), _expression_list._dynamic_expressions._dynamic_expression_names}; } Flags _flags; diff --git a/include/sqlpp11/select_expression_list.h b/include/sqlpp11/select_expression_list.h index 5d11f0ed..27cb1ea7 100644 --- a/include/sqlpp11/select_expression_list.h +++ b/include/sqlpp11/select_expression_list.h @@ -61,13 +61,43 @@ namespace sqlpp template struct dynamic_select_expression_list { - using type = std::vector>; + using _names_t = std::vector; + std::vector> _dynamic_expressions; + _names_t _dynamic_expression_names; + + template + void push_back(Expr&& expr) + { + _dynamic_expression_names.push_back(std::decay::type::_name_t::_get_name()); + _dynamic_expressions.push_back(std::forward(expr)); + } + void serialize(std::ostream& os, Db& db, bool first) const + { + for (const auto column : _dynamic_expressions) + { + if (not first) + os << ','; + column.serialize(os, db); + first = false; + } + } + }; template<> struct dynamic_select_expression_list { - using type = std::vector; + struct _names_t {}; + _names_t _dynamic_expression_names; + + template + void push_back(const T&) {} + + template + void serialize(std::ostream&, Db&, bool) const + { + } + }; } @@ -104,6 +134,8 @@ namespace sqlpp dynamic_result_row_t...>, result_row_t...>>::type; + using _dynamic_names_t = typename detail::dynamic_select_expression_list::_names_t; + template using _pseudo_table_t = select_pseudo_table_t; @@ -115,7 +147,6 @@ namespace sqlpp { static_assert(is_named_expression_t::type>::value, "select() arguments require to be named expressions"); _dynamic_expressions.push_back(std::forward(namedExpr)); - _dynamic_expression_names.push_back(std::decay::type::_name_t::_get_name()); } template @@ -125,19 +156,11 @@ namespace sqlpp static_assert(_is_dynamic::value or sizeof...(NamedExpr), "at least one select expression required"); detail::serialize_tuple(os, db, _expressions, ','); - bool first = sizeof...(NamedExpr) == 0; - for (const auto column : _dynamic_expressions) - { - if (not first) - os << ','; - column.serialize(os, db); - first = false; - } + _dynamic_expressions.serialize(os, db, sizeof...(NamedExpr) == 0); } std::tuple _expressions; - typename detail::dynamic_select_expression_list::type _dynamic_expressions; - std::vector _dynamic_expression_names; + detail::dynamic_select_expression_list _dynamic_expressions; }; }