jeudi 26 novembre 2015

SQLGetStmtAttr output value for SQL_ATTR_ROW_NUMBER

TL;TR: Is it correct to return success and report not an actual row number but other indicators like SQL_ROW_NUMBER_UNKNOWN?

AFAIK, not all ODBC drivers support querying of SQL_ATTR_ROW_NUMBER attribute, so this attribute is optional.

Am I correct, that if a driver does not support it, then SQLGetStmtAttr should return this code?

HYC00 - Optional feature not implemented

Is it correct to return success and report not an actual row number but other indicators like SQL_ROW_NUMBER_UNKNOWN?

There are ODBC drivers that do that, for example SQLite 3 for ODBC does this:

static SQLRETURN
drvgetstmtattr(SQLHSTMT stmt, SQLINTEGER attr, SQLPOINTER val,
           SQLINTEGER bufmax, SQLINTEGER *buflen)
{
...
    case SQL_ATTR_ROW_NUMBER:
    if (s->s3stmt) {
        *uval = (s->s3stmt_rownum < 0) ?
            SQL_ROW_NUMBER_UNKNOWN : (s->s3stmt_rownum + 1);
    } else {
        *uval = (s->rowp < 0) ? SQL_ROW_NUMBER_UNKNOWN : (s->rowp + 1);
    }
    *buflen = sizeof (SQLULEN);
    return SQL_SUCCESS;
...
}

Is this correct and kosher implementation?

PostgreSQL ODBC driver, for example, returns SQL_ERROR if row position can not be determined.

MySQL Connector/ODBC driver seems to always return SQL_SUCCESS and some value calculated according to this scheme:

case SQL_ATTR_ROW_NUMBER:
    *(SQLUINTEGER *)ValuePtr= stmt->current_row+1;

where default value is

stmt->current_row= -1;  /* Before first row */

I always thought that is reserved for SQLGetDiagField with SQL_DIAG_ROW_NUMBER identifier, as per the comment in ODBC's sqlext.h header:

/* define for SQL_DIAG_ROW_NUMBER and SQL_DIAG_COLUMN_NUMBER */
#if (ODBCVER >= 0x0300)
#define SQL_NO_ROW_NUMBER                       (-1)
#define SQL_NO_COLUMN_NUMBER                    (-1)
#define SQL_ROW_NUMBER_UNKNOWN                  (-2)
#define SQL_COLUMN_NUMBER_UNKNOWN               (-2)
#endif

Or, perhaps, the safest bet is to check value reported by SQLGetStmtAttr(SQL_ATTR_ROW_NUMBER) against SQL_ROW_NUMBER_UNKNOWN too?

(The same question has been asked on MSDN Forums, here.)

Aucun commentaire:

Enregistrer un commentaire