summaryrefslogtreecommitdiff
path: root/libodbcpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2020-12-10 19:09:42 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2020-12-10 19:09:42 +0000
commit781a30ba9d4114636fa6ed0985ca83903531a199 (patch)
treeaf241150c945f84fb02744ea338901dc717a81ac /libodbcpp
parentTidy container move check (diff)
downloadlibdbpp-odbc-781a30ba9d4114636fa6ed0985ca83903531a199.tar.bz2
libdbpp-odbc-781a30ba9d4114636fa6ed0985ca83903531a199.tar.xz
libdbpp-odbc-781a30ba9d4114636fa6ed0985ca83903531a199.zip
Replace recursive fetch with a looping one
Diffstat (limited to 'libodbcpp')
-rw-r--r--libodbcpp/odbc-selectcommand.cpp39
1 files changed, 15 insertions, 24 deletions
diff --git a/libodbcpp/odbc-selectcommand.cpp b/libodbcpp/odbc-selectcommand.cpp
index d40ab2f..9d8a344 100644
--- a/libodbcpp/odbc-selectcommand.cpp
+++ b/libodbcpp/odbc-selectcommand.cpp
@@ -4,6 +4,7 @@
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <cstring>
+#include <numeric>
#include <sqlext.h>
ODBC::SelectCommand::SelectCommand(const Connection & c, const std::string & s) :
@@ -18,23 +19,19 @@ ODBC::SelectCommand::~SelectCommand()
}
}
-bool
-ODBC::SelectCommand::fetch()
-{
- return fetch(SQL_FETCH_NEXT, 0);
-}
-
constexpr std::array<SQLCHAR, 6> truncated = {'0', '1', '0', '0', '4', '\0'};
bool
-ODBC::SelectCommand::fetch(SQLSMALLINT orientation, SQLLEN offset)
+ODBC::SelectCommand::fetch()
{
if (!columnCount()) {
execute();
}
- RETCODE rc = SQLFetchScroll(hStmt, orientation, offset);
- switch (rc) {
- case SQL_SUCCESS_WITH_INFO:
- default: {
+ for (RETCODE rc = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); rc != SQL_NO_DATA;
+ rc = SQLFetchScroll(hStmt, SQL_FETCH_RELATIVE, 0)) {
+ if (!SQL_SUCCEEDED(rc)) {
+ throw Error(rc, SQL_HANDLE_STMT, hStmt);
+ }
+ if (rc != SQL_SUCCESS) {
std::array<SQLCHAR, 6> sqlstatus {};
RETCODE diagrc = SQLGetDiagRec(SQL_HANDLE_STMT, hStmt, 1, sqlstatus.data(), nullptr, nullptr, 0, nullptr);
if (SQL_SUCCEEDED(diagrc)) {
@@ -42,24 +39,18 @@ ODBC::SelectCommand::fetch(SQLSMALLINT orientation, SQLLEN offset)
for (const auto & c : largeColumns) {
c->resize();
}
- return fetch(SQL_FETCH_RELATIVE, 0);
+ continue;
}
}
}
- [[fallthrough]];
- case SQL_SUCCESS: {
- bool resized = false;
- for (const auto & c : largeColumns) {
- resized |= c->resize();
- }
- if (resized) {
- return fetch(SQL_FETCH_RELATIVE, 0);
- }
- return true;
+ if (std::accumulate(largeColumns.begin(), largeColumns.end(), false, [](auto && resized, auto && c) {
+ return resized |= c->resize();
+ })) {
+ continue;
}
- case SQL_NO_DATA:
- return false;
+ return true;
}
+ return false;
}
void