From 6e5ee5657772c5788d04c64788bf70de410547eb Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Thu, 2 Jan 2014 20:20:58 +0100 Subject: [PATCH] prepared to detemplatify connector results --- include/sqlpp11/boolean.h | 1 - .../{raw_result_row.h => char_result_row.h} | 8 +-- include/sqlpp11/floating_point.h | 6 +- include/sqlpp11/integral.h | 1 - include/sqlpp11/prepared_select.h | 2 +- include/sqlpp11/result.h | 47 +++++++------- include/sqlpp11/result_row.h | 63 ++++++++----------- include/sqlpp11/select.h | 2 +- include/sqlpp11/text.h | 1 - 9 files changed, 60 insertions(+), 71 deletions(-) rename include/sqlpp11/{raw_result_row.h => char_result_row.h} (91%) diff --git a/include/sqlpp11/boolean.h b/include/sqlpp11/boolean.h index 798d2175..f345406a 100644 --- a/include/sqlpp11/boolean.h +++ b/include/sqlpp11/boolean.h @@ -30,7 +30,6 @@ #include #include #include -#include #include namespace sqlpp diff --git a/include/sqlpp11/raw_result_row.h b/include/sqlpp11/char_result_row.h similarity index 91% rename from include/sqlpp11/raw_result_row.h rename to include/sqlpp11/char_result_row.h index 32f15626..b4f884a9 100644 --- a/include/sqlpp11/raw_result_row.h +++ b/include/sqlpp11/char_result_row.h @@ -24,17 +24,17 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SQLPP_RAW_RESULT_ROW_H -#define SQLPP_RAW_RESULT_ROW_H +#ifndef SQLPP_CHAR_RESULT_ROW_H +#define SQLPP_CHAR_RESULT_ROW_H namespace sqlpp { - struct raw_result_row_t + struct char_result_row_t { char** data; size_t* len; - bool operator==(const raw_result_row_t& rhs) const + bool operator==(const char_result_row_t& rhs) const { return data == rhs.data and len == rhs.len; } diff --git a/include/sqlpp11/floating_point.h b/include/sqlpp11/floating_point.h index 24ae32e9..ad6c3f29 100644 --- a/include/sqlpp11/floating_point.h +++ b/include/sqlpp11/floating_point.h @@ -30,7 +30,7 @@ #include #include #include -#include +#include // FIXME: Need to update floating_point #include namespace sqlpp @@ -113,13 +113,13 @@ namespace sqlpp _value(0) {} - _result_entry_t(const raw_result_row_t& row): + _result_entry_t(const char_result_row_t& row): _is_valid(row.data != nullptr), _is_null(row.data == nullptr or row.data[index] == nullptr), _value(_is_null ? 0 : std::strtod(row.data[index], nullptr)) {} - _result_entry_t& operator=(const raw_result_row_t& row) + _result_entry_t& operator=(const char_result_row_t& row) { _is_valid = (row.data != nullptr); _is_null = row.data == nullptr or row.data[index] == nullptr; diff --git a/include/sqlpp11/integral.h b/include/sqlpp11/integral.h index a52d44c2..c1208874 100644 --- a/include/sqlpp11/integral.h +++ b/include/sqlpp11/integral.h @@ -30,7 +30,6 @@ #include #include #include -#include #include namespace sqlpp diff --git a/include/sqlpp11/prepared_select.h b/include/sqlpp11/prepared_select.h index 8f3304d4..27696bf3 100644 --- a/include/sqlpp11/prepared_select.h +++ b/include/sqlpp11/prepared_select.h @@ -41,7 +41,7 @@ namespace sqlpp using _prepared_query_t = typename Db::_prepared_query_t; auto run(Db& db) const - -> result_t + -> result_t { return {db.run_prepared_select(*this)}; } diff --git a/include/sqlpp11/result.h b/include/sqlpp11/result.h index f7ea4c8d..7f893ee6 100644 --- a/include/sqlpp11/result.h +++ b/include/sqlpp11/result.h @@ -27,31 +27,30 @@ #ifndef SQLPP_RESULT_H #define SQLPP_RESULT_H -#include - #include namespace sqlpp { - template + template class result_t { using db_result_t = DbResult; - using result_row_t = typename db_result_t::result_row_t; + using result_row_t = ResultRow; - db_result_t _db_result; + db_result_t _result; + result_row_t _result_row; db_result_t _end; + result_row_t _end_row; public: - result_t(): - _db_result(), - _end() - {} + result_t() = default; - result_t(db_result_t&& result): - _db_result(std::move(result)), - _end() + template + result_t(db_result_t&& result, const DynamicNames& dynamic_names): + _result(std::move(result)), + _result_row(dynamic_names) { + _result.next(_result_row); } result_t(const result_t&) = delete; @@ -63,24 +62,25 @@ namespace sqlpp class iterator { public: - iterator(db_result_t& result): - _result(result) + iterator(db_result_t& result, result_row_t& result_row): + _result(result), + _result_row(result_row) { } const result_row_t& operator*() const { - return _result.front(); + return _result_row; } const result_row_t* operator->() const { - return &_result.front(); + return &_result_row; } bool operator==(const iterator& rhs) const { - return _result.front() == rhs._result.front(); + return _result_row == rhs._result_row; } bool operator!=(const iterator& rhs) const @@ -90,35 +90,36 @@ namespace sqlpp void operator++() { - _result.pop_front(); + _result.next(result_row); } db_result_t& _result; + result_row_t& _result_row; }; iterator begin() { - return iterator(_db_result); + return iterator(_result, _result_row); } iterator end() { - return iterator(_end); + return iterator(_end, _end_row); } const result_row_t& front() const { - return _db_result.front(); + return _result_row; } bool empty() const { - return _db_result.front() == _end.front(); + return _result_row == _end_row; } void pop_front() { - _db_result.pop_front(); + _result.next(_result_row); } }; diff --git a/include/sqlpp11/result_row.h b/include/sqlpp11/result_row.h index 9ed148b9..644c0869 100644 --- a/include/sqlpp11/result_row.h +++ b/include/sqlpp11/result_row.h @@ -27,7 +27,7 @@ #ifndef SQLPP_RESULT_ROW_H #define SQLPP_RESULT_ROW_H -#include +#include #include #include #include @@ -52,16 +52,16 @@ namespace sqlpp static constexpr size_t _last_index = _rest::_last_index; result_row_impl() = default; - result_row_impl(const raw_result_row_t& raw_result_row): - _field({{raw_result_row.data[index], raw_result_row.len[index]}}), - _rest(raw_result_row) + result_row_impl(const char_result_row_t& char_result_row_t): + _field({{char_result_row_t.data[index], char_result_row_t.len[index]}}), + _rest(char_result_row_t) { } - result_row_impl& operator=(const raw_result_row_t& raw_result_row) + result_row_impl& operator=(const char_result_row_t& char_result_row_t) { - _field::operator()().assign(raw_result_row.data[index], raw_result_row.len[index]); - _rest::operator=(raw_result_row); + _field::operator()().assign(char_result_row_t.data[index], char_result_row_t.len[index]); + _rest::operator=(char_result_row_t); return *this; } @@ -90,15 +90,15 @@ namespace sqlpp static constexpr size_t _last_index = _rest::_last_index; result_row_impl() = default; - result_row_impl(const raw_result_row_t& raw_result_row): - _multi_field({raw_result_row}), - _rest(raw_result_row) + result_row_impl(const char_result_row_t& char_result_row_t): + _multi_field({char_result_row_t}), + _rest(char_result_row_t) {} - result_row_impl& operator=(const raw_result_row_t& raw_result_row) + result_row_impl& operator=(const char_result_row_t& char_result_row_t) { - _multi_field::operator=({raw_result_row}); - _rest::operator=(raw_result_row); + _multi_field::operator=({char_result_row_t}); + _rest::operator=(char_result_row_t); return *this; } @@ -121,11 +121,11 @@ namespace sqlpp { static constexpr size_t _last_index = index; result_row_impl() = default; - result_row_impl(const raw_result_row_t& raw_result_row) + result_row_impl(const char_result_row_t& char_result_row_t) { } - result_row_impl& operator=(const raw_result_row_t& raw_result_row) + result_row_impl& operator=(const char_result_row_t& char_result_row_t) { return *this; } @@ -154,10 +154,10 @@ namespace sqlpp { } - template - result_row_t(const raw_result_row_t& raw_result_row, const T&): - _impl(raw_result_row), - _is_valid(true) + template + result_row_t(const DynamicNames&): + _impl(), + _is_valid(false) { } @@ -166,9 +166,9 @@ namespace sqlpp 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) + result_row_t& operator=(const char_result_row_t& char_result_row_t) { - _impl::operator=(raw_result_row); + _impl::operator=(char_result_row_t); _is_valid = true; return *this; } @@ -218,20 +218,11 @@ namespace sqlpp { } - dynamic_result_row_t(const raw_result_row_t& raw_result_row, std::vector dynamic_columns): - _impl(raw_result_row), - _is_valid(true), + dynamic_result_row_t(const std::vector& dynamic_columns): + _impl(), + _is_valid(false), _dynamic_columns(dynamic_columns) { - raw_result_row_t dynamic_row = raw_result_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.data[0], dynamic_row.len[0]))); - ++dynamic_row.data; - ++dynamic_row.len; - } } dynamic_result_row_t(const dynamic_result_row_t&) = delete; @@ -239,12 +230,12 @@ namespace sqlpp 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) + dynamic_result_row_t& operator=(const char_result_row_t& char_result_row) { - _impl::operator=(raw_result_row); + _impl::operator=(char_result_row); _is_valid = true; - raw_result_row_t dynamic_row = raw_result_row; + char_result_row_t dynamic_row = char_result_row; dynamic_row.data += _last_static_index; dynamic_row.len += _last_static_index; diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 9fa3592f..df5f9732 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -586,7 +586,7 @@ namespace sqlpp // Execute template auto run(Db& db) const - -> result_t + -> result_t { 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()"); diff --git a/include/sqlpp11/text.h b/include/sqlpp11/text.h index 8eee1774..d2fda13c 100644 --- a/include/sqlpp11/text.h +++ b/include/sqlpp11/text.h @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include