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

Added support for dynamic columns

This commit is contained in:
Roland Bock 2013-10-29 19:32:52 +01:00
parent 902583a33b
commit 7050491bf0
5 changed files with 102 additions and 20 deletions

View File

@ -27,7 +27,7 @@
#ifndef SQLPP_DETAIL_BASIC_OPERATORS_H #ifndef SQLPP_DETAIL_BASIC_OPERATORS_H
#define SQLPP_DETAIL_BASIC_OPERATORS_H #define SQLPP_DETAIL_BASIC_OPERATORS_H
#include <sqlpp11/expression_fwd.h> #include <sqlpp11/expression.h>
#include <sqlpp11/alias.h> #include <sqlpp11/alias.h>
#include <sqlpp11/sort_order.h> #include <sqlpp11/sort_order.h>
#include <sqlpp11/in.h> #include <sqlpp11/in.h>

View File

@ -41,20 +41,23 @@ namespace sqlpp
db_result_t _result; db_result_t _result;
raw_result_row_t _raw_result_row; raw_result_row_t _raw_result_row;
raw_result_row_t _end; raw_result_row_t _end;
std::vector<std::string> _dynamic_columns; // only needed in case of dynamic columns in the select
ResultRow _result_row; ResultRow _result_row;
public: public:
result_t(): result_t():
_raw_result_row({}), _raw_result_row({}),
_end({}), _end({}),
_result_row(_raw_result_row) _dynamic_columns(),
_result_row(_raw_result_row, _dynamic_columns)
{} {}
result_t(db_result_t&& result): result_t(db_result_t&& result, std::vector<std::string> dynamic_columns):
_result(std::move(result)), _result(std::move(result)),
_raw_result_row(_result.next()), _raw_result_row(_result.next()),
_end({}), _end({}),
_result_row(_raw_result_row) _dynamic_columns(dynamic_columns),
_result_row(_raw_result_row, _dynamic_columns)
{} {}
result_t(const result_t&) = delete; result_t(const result_t&) = delete;

View File

@ -29,7 +29,10 @@
#include <sqlpp11/raw_result_row.h> #include <sqlpp11/raw_result_row.h>
#include <sqlpp11/field.h> #include <sqlpp11/field.h>
#include <sqlpp11/text.h>
#include <iostream> #include <iostream>
#include <map>
namespace sqlpp namespace sqlpp
{ {
namespace detail namespace detail
@ -44,6 +47,7 @@ namespace sqlpp
{ {
using _field = typename NamedExpr::_name_t::template _member_t<typename NamedExpr::_value_type::template _result_entry_t<index>>; using _field = typename NamedExpr::_name_t::template _member_t<typename NamedExpr::_value_type::template _result_entry_t<index>>;
using _rest = result_row_impl<level, index + 1, Rest...>; using _rest = result_row_impl<level, index + 1, Rest...>;
static constexpr size_t _last_index = _rest::_last_index;
result_row_impl(const raw_result_row_t& raw_result_row): result_row_impl(const raw_result_row_t& raw_result_row):
_field({raw_result_row}), _field({raw_result_row}),
@ -65,6 +69,7 @@ namespace sqlpp
{ {
using _multi_field = typename AliasProvider::_name_t::template _member_t<result_row_impl<level + 1, index, Col...>>; using _multi_field = typename AliasProvider::_name_t::template _member_t<result_row_impl<level + 1, index, Col...>>;
using _rest = result_row_impl<level, index + sizeof...(Col), Rest...>; using _rest = result_row_impl<level, index + sizeof...(Col), Rest...>;
static constexpr size_t _last_index = _rest::_last_index;
result_row_impl(const raw_result_row_t& raw_result_row): result_row_impl(const raw_result_row_t& raw_result_row):
_multi_field({raw_result_row}), _multi_field({raw_result_row}),
@ -82,6 +87,7 @@ namespace sqlpp
template<size_t level, size_t index> template<size_t level, size_t index>
struct result_row_impl<level, index> struct result_row_impl<level, index>
{ {
static constexpr size_t _last_index = index;
result_row_impl(const raw_result_row_t& raw_result_row) result_row_impl(const raw_result_row_t& raw_result_row)
{} {}
@ -95,16 +101,18 @@ namespace sqlpp
template<typename... NamedExpr> template<typename... NamedExpr>
struct result_row_t: public detail::result_row_impl<0, 0, NamedExpr...> struct result_row_t: public detail::result_row_impl<0, 0, NamedExpr...>
{ {
using _impl = detail::result_row_impl<0, 0, NamedExpr...>;
bool _is_row; bool _is_row;
result_row_t(const raw_result_row_t& raw_result_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
detail::result_row_impl<0, 0, NamedExpr...>(raw_result_row), _impl(raw_result_row),
_is_row(raw_result_row.data != nullptr) _is_row(raw_result_row.data != nullptr)
{} {
}
result_row_t& operator=(const raw_result_row_t& raw_result_row) result_row_t& operator=(const raw_result_row_t& raw_result_row)
{ {
detail::result_row_impl<0, 0, NamedExpr...>::operator=(raw_result_row); _impl::operator=(raw_result_row);
_is_row = raw_result_row.data != nullptr; _is_row = raw_result_row.data != nullptr;
return *this; return *this;
} }
@ -114,6 +122,83 @@ namespace sqlpp
return _is_row; return _is_row;
} }
}; };
template<typename... NamedExpr>
struct dynamic_result_row_t: public detail::result_row_impl<0, 0, NamedExpr...>
{
using _impl = detail::result_row_impl<0, 0, NamedExpr...>;
using _field_type = detail::text::_result_entry_t<0>;
static constexpr size_t _last_static_index = _impl::_last_index;
bool _is_row;
std::vector<std::string> _dynamic_columns;
std::map<std::string, _field_type> _dynamic_fields;
dynamic_result_row_t(const raw_result_row_t& raw_result_row, std::vector<std::string> dynamic_columns):
detail::result_row_impl<0, 0, NamedExpr...>(raw_result_row),
_is_row(raw_result_row.data != nullptr)
{
raw_result_row_t dynamic_row = raw_result_row;
if (_is_row)
{
dynamic_row.data += _last_static_index;
dynamic_row.len += _last_static_index;
for (const auto& column : _dynamic_columns)
{
_dynamic_fields.insert(std::make_pair(column, _field_type(dynamic_row)));
++dynamic_row.data;
++dynamic_row.len;
}
}
else
{
for (const auto& column : _dynamic_columns)
{
_dynamic_fields.insert(std::make_pair(column, _field_type(dynamic_row)));
}
}
}
dynamic_result_row_t& operator=(const raw_result_row_t& raw_result_row)
{
detail::result_row_impl<0, 0, NamedExpr...>::operator=(raw_result_row);
_is_row = raw_result_row.data != nullptr;
raw_result_row_t dynamic_row = raw_result_row;
if (_is_row)
{
dynamic_row.data += _last_static_index;
dynamic_row.len += _last_static_index;
for (const auto& column : _dynamic_columns)
{
_dynamic_fields.at(column) = dynamic_row;
++dynamic_row.data;
++dynamic_row.len;
}
}
else
{
for (const auto& column : _dynamic_columns)
{
_dynamic_fields.at(column) = dynamic_row;
}
}
return *this;
}
const _field_type& at(const std::string& field_name) const
{
return _dynamic_fields.at(field_name);
}
explicit operator bool() const
{
return _is_row;
}
};
} }
#endif #endif

View File

@ -572,7 +572,7 @@ namespace sqlpp
std::ostringstream oss; std::ostringstream oss;
serialize(oss, db); serialize(oss, db);
return {db.select(oss.str())}; return {db.select(oss.str()), _expression_list._dynamic_expression_names};
} }
Flags _flags; Flags _flags;
@ -621,15 +621,5 @@ namespace sqlpp
}; };
} }
#warning: need to add dynamic fields
/* Idea: Use a vector of serializable or similar for select.
* Translate the vector into a map<string, text-similar with an index>, first = name, second = something similar to text which knows its index
*
* What about default constructing the result? Not a problem. The map is empty then.
*
* But how to transport the vector of serializables from the select into the result?
*
* Maybe store the names of the map content in a field in the query?
*/
} }
#endif #endif

View File

@ -100,7 +100,9 @@ namespace sqlpp
}; };
using _name_t = typename detail::get_first_argument_if_unique<NamedExpr...>::_name_t; using _name_t = typename detail::get_first_argument_if_unique<NamedExpr...>::_name_t;
using _result_row_t = result_row_t<make_field_t<NamedExpr>...>; using _result_row_t = typename std::conditional<_is_dynamic::value,
dynamic_result_row_t<make_field_t<NamedExpr>...>,
result_row_t<make_field_t<NamedExpr>...>>::type;
template <typename Select> template <typename Select>
using _pseudo_table_t = select_pseudo_table_t<Select, NamedExpr...>; using _pseudo_table_t = select_pseudo_table_t<Select, NamedExpr...>;
@ -113,6 +115,7 @@ namespace sqlpp
{ {
static_assert(is_named_expression_t<typename std::decay<Expr>::type>::value, "select() arguments require to be named expressions"); 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_expressions.push_back(std::forward<Expr>(namedExpr));
_dynamic_expression_names.push_back(std::decay<Expr>::type::_name_t::_get_name());
} }
template<typename Db> template<typename Db>
@ -134,6 +137,7 @@ namespace sqlpp
std::tuple<NamedExpr...> _expressions; std::tuple<NamedExpr...> _expressions;
typename detail::dynamic_select_expression_list<Database>::type _dynamic_expressions; typename detail::dynamic_select_expression_list<Database>::type _dynamic_expressions;
std::vector<std::string> _dynamic_expression_names;
}; };
} }