mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Reduced resource requirements for non-dynamic selects
This commit is contained in:
parent
7050491bf0
commit
a17a8ecfa6
@ -33,7 +33,7 @@
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename Db, typename ResultRow>
|
||||
template<typename Db, typename ResultRow, typename DynamicNames>
|
||||
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<std::string> _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<std::string> dynamic_columns):
|
||||
result_t(db_result_t&& result, DynamicNames dynamic_columns):
|
||||
_result(std::move(result)),
|
||||
_raw_result_row(_result.next()),
|
||||
_end({}),
|
||||
|
@ -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<std::string>&): // FIXME: it hurts a bit to always transport the dynamic part as well
|
||||
template<typename T>
|
||||
result_row_t(const raw_result_row_t& raw_result_row, const T&):
|
||||
_impl(raw_result_row),
|
||||
_is_row(raw_result_row.data != nullptr)
|
||||
{
|
||||
|
@ -108,6 +108,7 @@ namespace sqlpp
|
||||
using set_offset_t = select_t<Database, Flags, ExpressionList, From, Where, GroupBy, Having, OrderBy, Limit, OffsetT>;
|
||||
|
||||
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<typename Db>
|
||||
result_t<Db, _result_row_t> run(Db& db) const
|
||||
result_t<Db, _result_row_t, _dynamic_names_t> run(Db& db) const
|
||||
{
|
||||
static_assert(not is_noop<ExpressionList>::value, "cannot run select without having selected anything");
|
||||
static_assert(is_from_t<From>::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;
|
||||
|
@ -61,13 +61,43 @@ namespace sqlpp
|
||||
template<typename Db>
|
||||
struct dynamic_select_expression_list
|
||||
{
|
||||
using type = std::vector<detail::named_serializable_t<Db>>;
|
||||
using _names_t = std::vector<std::string>;
|
||||
std::vector<detail::named_serializable_t<Db>> _dynamic_expressions;
|
||||
_names_t _dynamic_expression_names;
|
||||
|
||||
template<typename Expr>
|
||||
void push_back(Expr&& expr)
|
||||
{
|
||||
_dynamic_expression_names.push_back(std::decay<Expr>::type::_name_t::_get_name());
|
||||
_dynamic_expressions.push_back(std::forward<Expr>(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<void>
|
||||
{
|
||||
using type = std::vector<noop>;
|
||||
struct _names_t {};
|
||||
_names_t _dynamic_expression_names;
|
||||
|
||||
template<typename T>
|
||||
void push_back(const T&) {}
|
||||
|
||||
template<typename Db>
|
||||
void serialize(std::ostream&, Db&, bool) const
|
||||
{
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@ -104,6 +134,8 @@ namespace sqlpp
|
||||
dynamic_result_row_t<make_field_t<NamedExpr>...>,
|
||||
result_row_t<make_field_t<NamedExpr>...>>::type;
|
||||
|
||||
using _dynamic_names_t = typename detail::dynamic_select_expression_list<Database>::_names_t;
|
||||
|
||||
template <typename Select>
|
||||
using _pseudo_table_t = select_pseudo_table_t<Select, NamedExpr...>;
|
||||
|
||||
@ -115,7 +147,6 @@ namespace sqlpp
|
||||
{
|
||||
static_assert(is_named_expression_t<typename std::decay<Expr>::type>::value, "select() arguments require to be named expressions");
|
||||
_dynamic_expressions.push_back(std::forward<Expr>(namedExpr));
|
||||
_dynamic_expression_names.push_back(std::decay<Expr>::type::_name_t::_get_name());
|
||||
}
|
||||
|
||||
template<typename Db>
|
||||
@ -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<NamedExpr...> _expressions;
|
||||
typename detail::dynamic_select_expression_list<Database>::type _dynamic_expressions;
|
||||
std::vector<std::string> _dynamic_expression_names;
|
||||
detail::dynamic_select_expression_list<Database> _dynamic_expressions;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user