Open gbromov opened 3 years ago
From the logs it seems that instead of reading the results you issue a cancel
That's weird, I don't even know how to do that. These are the lines my code executes:
SQLHSTMT hStatement = nullptr;
SQLRETURN status_1 = SQLAllocHandle( SQL_HANDLE_STMT , hConnection , &hStatement );
std::u16string u16_sql = u"declare ..";
SQLRETURN status_2 = SQLExecDirect( hStatement , (SQLWCHAR*)&u16_sql[ 0 ] , (SQLINTEGER)u16_sql.size( ) );
SQLSMALLINT num_columns = -1;
SQLRETURN status_3 = SQLNumResultCols( hStatement , &num_columns );
status_1, status_2 and status_3 are all SQL_SUCCESS, and num_columns is set to 0.
Actually maybe the problem is that since SQLNumResultCols returns zero columns, I am just calling SQLFreeStmt( ), SQLFreeHandle( ) instead of SQLFetch( ) first, that's why I am issuing the cancel? With driver reporting zero columns, I don't see a point in trying to SQLFetch( ), I am assuming that zero columns means no result set.
Even if I try calling SQLFetch( ), what can I do with it if there are no columns to read from.
You are forgetting SQLMoreResults
. But I won't expect an empty recordset looking at your query. It's pretty odd. The set
statement should not produce that. Less the declare
statement.
OT: personally I would prefer u16_sql.c_str()
or u16_sql.data()
instead of &u16_sql[0]
Alright, weirdly enough calling SQLMoreResults worked. Apparently the second result set it what the select clause produces, what is the first one is unclear. I still believe this is a bug though, it feels like freetds somehow decided there should be two result sets from the statement, which shouldn't happen I think. Of course, it is possible that the response from the sql server is incorrect, but I don't have any knowledge of the protocol to verify it. Microsoft's ODBC driver works as expected here though - only one result set with one column and one row.
On the OT: How do you use those, c_str( ) returns const char16_t*
while the function expects non-const pointer, and data( ) is a non-const function and the strings almost always (at least on my code) are passed by const reference. Do you const_cast those?
A query like this:
works fine when using Microsoft ODBC driver, but for some reason FreeTDS can't read the results. I can't really understand what the logs say, but there is a weird entry:
util.c:83:logic error: cannot change query state from IDLE to PENDING
Here's the relevant part of the log, maybe it'll be helpful: