0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Refactored of result and result row (clearer structure depending on connectors)

This commit is contained in:
Roland Bock 2013-12-17 22:12:13 +01:00
parent 2b43d3bc15
commit a915bd9e50
5 changed files with 54 additions and 25 deletions

View File

@ -40,21 +40,19 @@ namespace sqlpp
using result_row_t = typename db_result_t::result_row_t;
db_result_t _db_result;
result_row_t _result_row;
result_row_t _end;
db_result_t _end;
public:
result_t():
_db_result(),
_result_row(),
_end()
{}
result_t(db_result_t&& result):
_db_result(std::move(result)),
_result_row(_db_result.next()),
_end({})
{}
_end()
{
}
result_t(const result_t&) = delete;
result_t(result_t&&) = default;
@ -65,11 +63,9 @@ namespace sqlpp
class iterator
{
public:
iterator(result_t& result, result_row_t& result_row):
_result(result),
_result_row(result_row)
iterator(db_result_t& result):
_result(result)
{
//std::cerr << "result::iterator::constructor" << std::endl;
}
const result_row_t& operator*() const
@ -84,7 +80,7 @@ namespace sqlpp
bool operator==(const iterator& rhs) const
{
return _result_row == rhs._result_row;
return _result.front() == rhs._result.front();
}
bool operator!=(const iterator& rhs) const
@ -97,33 +93,32 @@ namespace sqlpp
_result.pop_front();
}
result_t& _result;
result_row_t& _result_row;
db_result_t& _result;
};
iterator begin()
{
return iterator(*this, _result_row);
return iterator(_db_result);
}
iterator end()
{
return iterator(*this, _end);
return iterator(_end);
}
const result_row_t& front() const
{
return _result_row;
return _db_result.front();
}
bool empty() const
{
return _result_row == _end;
return _db_result.front() == _end.front();
}
void pop_front()
{
_result_row = _db_result.next();
_db_result.pop_front();
}
};

View File

@ -106,20 +106,25 @@ namespace sqlpp
raw_result_row_t _raw_result_row;
result_row_t():
_raw_result_row(),
_impl(_raw_result_row),
_impl({}),
_raw_result_row({}),
_is_row(false)
{
}
template<typename T>
result_row_t(const raw_result_row_t& raw_result_row, const T&):
_impl(raw_result_row),
_raw_result_row(raw_result_row),
_impl(_raw_result_row),
_is_row(_raw_result_row.data != nullptr)
{
}
result_row_t(const result_row_t&) = delete;
result_row_t(result_row_t&&) = default;
result_row_t& operator=(const result_row_t&) = delete;
result_row_t& operator=(result_row_t&&) = default;
result_row_t& operator=(const raw_result_row_t& raw_result_row)
{
_impl::operator=(raw_result_row);
@ -128,7 +133,7 @@ namespace sqlpp
return *this;
}
bool operator==(const result_row_t& rhs)
bool operator==(const result_row_t& rhs) const
{
return _raw_result_row == rhs._raw_result_row;
}
@ -146,12 +151,21 @@ namespace sqlpp
using _field_type = detail::text::_result_entry_t<0>;
static constexpr size_t _last_static_index = _impl::_last_index;
raw_result_row_t _raw_result_row;
bool _is_row;
std::vector<std::string> _dynamic_columns;
std::map<std::string, _field_type> _dynamic_fields;
dynamic_result_row_t():
_impl({}),
_raw_result_row({}),
_is_row(false)
{
}
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),
_impl(raw_result_row),
_raw_result_row(raw_result_row),
_is_row(raw_result_row.data != nullptr)
{
raw_result_row_t dynamic_row = raw_result_row;
@ -176,9 +190,15 @@ namespace sqlpp
}
dynamic_result_row_t(const dynamic_result_row_t&) = delete;
dynamic_result_row_t(dynamic_result_row_t&&) = default;
dynamic_result_row_t& operator=(const dynamic_result_row_t&) = delete;
dynamic_result_row_t& operator=(dynamic_result_row_t&&) = default;
dynamic_result_row_t& operator=(const raw_result_row_t& raw_result_row)
{
detail::result_row_impl<0, 0, NamedExpr...>::operator=(raw_result_row);
_raw_result_row = raw_result_row;
_is_row = raw_result_row.data != nullptr;
raw_result_row_t dynamic_row = raw_result_row;
@ -204,6 +224,11 @@ namespace sqlpp
return *this;
}
bool operator==(const dynamic_result_row_t& rhs) const
{
return _raw_result_row == rhs._raw_result_row;
}
const _field_type& at(const std::string& field_name) const
{
return _dynamic_fields.at(field_name);

View File

@ -568,6 +568,11 @@ namespace sqlpp
return *this;
}
const typename ExpressionList::_dynamic_names_t& get_dynamic_names() const
{
return _expression_list._dynamic_expressions._dynamic_expression_names;
}
// Execute
template<typename Db>
auto run(Db& db) const

View File

@ -51,6 +51,12 @@ namespace sqlpp
template<size_t index>
struct _result_entry_t
{
_result_entry_t():
_is_valid(false),
_is_null(true),
_value("")
{}
_result_entry_t(const raw_result_row_t& row):
_is_valid(row.data != nullptr),
_is_null(row.data == nullptr or row.data[index] == nullptr),

View File

@ -298,8 +298,6 @@ int main()
decltype(t.alpha)::_value_type::_base_value_type,
decltype(f.epsilon)::_value_type::_base_value_type>::value, "Two bigint columns must have identical base_value_type");
static_assert(std::is_same<A, B>::value, "select with identical columns(name/value_type) need to have identical result_types");
static_assert(sqlpp::is_regular<A>::value, "type requirement");
static_assert(sqlpp::is_regular<B>::value, "type requirement");
}
{