From 781a30ba9d4114636fa6ed0985ca83903531a199 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Dec 2020 19:09:42 +0000 Subject: Replace recursive fetch with a looping one --- libodbcpp/odbc-selectcommand.cpp | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'libodbcpp') 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 #include #include +#include #include 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 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 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 -- cgit v1.2.3