Skip to content

Commit 54e9d47

Browse files
committed
SQLRowCount: always return row set size (#182)
With this commit SQLRowCount() will always return the size of current row set, also in the case a cursor is present. The previous implementation returned 0 when a cursor was present, since the function can't evaluate the total result set size (without actually fetching it all) and applications shouldn't rely on this result. The driver - that internally also uses the function for SYS queries - assumed that ES/SQL will never return a cursor for SYS commands. This seems however not to be a valid assumption. (cherry picked from commit 1058242)
1 parent 0c030e3 commit 54e9d47

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

driver/queries.c

+22-20
Original file line numberDiff line numberDiff line change
@@ -4040,28 +4040,30 @@ SQLRETURN EsSQLRowCount(_In_ SQLHSTMT StatementHandle, _Out_ SQLLEN *RowCount)
40404040
RET_HDIAGS(stmt, SQL_STATE_HY010);
40414041
}
40424042

4043-
if (STMT_HAS_CURSOR(stmt)) {
4044-
/* fetch_size or scroller size chunks the result */
4045-
ERRH(stmt, "can't evaluate the total size of paged result set.");
4046-
/* returning a _WITH_INFO here will fail the query for MSQRY32. */
4047-
# if 0
4048-
RET_HDIAG(stmt, SQL_STATE_01000, "row count is for partial result "
4049-
"only", 0);
4050-
# endif /* 0 */
4051-
*RowCount = 0;
4043+
if (stmt->rset.pack_json) {
4044+
*RowCount = UJLengthArray(stmt->rset.pack.json.rows_obj);
40524045
} else {
4053-
if (stmt->rset.pack_json) {
4054-
*RowCount = UJLengthArray(stmt->rset.pack.json.rows_obj);
4055-
} else {
4056-
res = cbor_get_array_count(stmt->rset.pack.cbor.rows_obj, &nrows);
4057-
if (res != CborNoError) {
4058-
ERRH(stmt, "failed to read row array count: %s.",
4059-
cbor_error_string(res));
4060-
RET_HDIAGS(stmt, SQL_STATE_HY000);
4061-
}
4062-
*RowCount = (SQLLEN)nrows;
4046+
res = cbor_get_array_count(stmt->rset.pack.cbor.rows_obj, &nrows);
4047+
if (res != CborNoError) {
4048+
ERRH(stmt, "failed to read row array count: %s.",
4049+
cbor_error_string(res));
4050+
RET_HDIAGS(stmt, SQL_STATE_HY000);
40634051
}
4064-
DBGH(stmt, "result set rows count: %zd.", *RowCount);
4052+
*RowCount = (SQLLEN)nrows;
4053+
}
4054+
DBGH(stmt, "result set rows count: %zd.", *RowCount);
4055+
4056+
/* Log a warning if a cursor is present.
4057+
* Note: ES/SQL can apparently attach a cursor even for SYS queries and no
4058+
* restrictive max fetch row count. Since this function is now also called
4059+
* while attaching types during bootstrapping, it shouldn't fail if a
4060+
* cursor is attached. This assumes however that ES will never return just
4061+
* a cursor (and no rows) on a first page. */
4062+
if (STMT_HAS_CURSOR(stmt)) {
4063+
/* fetch_size or scroller size will chunk the result */
4064+
WARNH(stmt, "can't evaluate the total size of paged result set.");
4065+
RET_HDIAG(stmt, SQL_STATE_01000, "row count is for current row set "
4066+
"only", 0);
40654067
}
40664068

40674069
return SQL_SUCCESS;

0 commit comments

Comments
 (0)