void pdo_odbc_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, PDO_ODBC_HSTMT statement, char *what, const char *file, int line TSRMLS_DC) /* {{{ */
{
- RETCODE rc;
- SWORD errmsgsize = 0;
+ SQLRETURN rc;
+ SQLSMALLINT errmsgsize = 0;
+ SQLHANDLE eh;
+ SQLSMALLINT htype, recno = 1;
pdo_odbc_db_handle *H = (pdo_odbc_db_handle*)dbh->driver_data;
pdo_odbc_errinfo *einfo = &H->einfo;
pdo_odbc_stmt *S = NULL;
if (statement == SQL_NULL_HSTMT && S) {
statement = S->stmt;
}
-
- rc = SQLError(H->env, H->dbc, statement, einfo->last_state, &einfo->last_error,
+
+ if (statement) {
+ htype = SQL_HANDLE_STMT;
+ eh = statement;
+ } else if (H->dbc) {
+ htype = SQL_HANDLE_DBC;
+ eh = H->dbc;
+ } else {
+ htype = SQL_HANDLE_ENV;
+ eh = H->env;
+ }
+
+ rc = SQLGetDiagRec(htype, eh, recno++, einfo->last_state, &einfo->last_error,
einfo->last_err_msg, sizeof(einfo->last_err_msg)-1, &errmsgsize);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
*pdo_err, what, einfo->last_error, einfo->last_err_msg);
}
+
+ /* just like a cursor, once you start pulling, you need to keep
+ * going until the end; SQL Server (at least) will mess with the
+ * actual cursor state if you don't finish retrieving all the
+ * diagnostic records (which can be generated by PRINT statements
+ * in the query, for instance). */
+ while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
+ char discard_state[5];
+ char discard_buf[1024];
+ SQLINTEGER code;
+ rc = SQLGetDiagRec(htype, eh, recno++, discard_state, &code,
+ discard_buf, sizeof(discard_buf)-1, &errmsgsize);
+ }
+
}
/* }}} */
pdo_odbc_stmt_error("SQLPrepare");
}
+ stmt->driver_data = S;
+ stmt->methods = &odbc_stmt_methods;
+
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- SQLFreeHandle(SQL_HANDLE_STMT, S->stmt);
return 0;
}
-
- stmt->driver_data = S;
- stmt->methods = &odbc_stmt_methods;
-
return 1;
}