Changeset: 630450913d85 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/630450913d85
Modified Files:
        clients/odbc/driver/SQLGetDiagField.c
        clients/odbc/driver/SQLGetDiagRec.c
Branch: default
Log Message:

Implement SQLGetDiagField() for DiagIdentifier = SQL_DIAG_MESSAGE_TEXT. Also 
improved code and synced it with SQLGetDiagRec() logic.


diffs (157 lines):

diff --git a/clients/odbc/driver/SQLGetDiagField.c 
b/clients/odbc/driver/SQLGetDiagField.c
--- a/clients/odbc/driver/SQLGetDiagField.c
+++ b/clients/odbc/driver/SQLGetDiagField.c
@@ -60,27 +60,27 @@ MNDBGetDiagField(SQLSMALLINT HandleType,
                /* Check if this struct is still valid/alive */
                if (!isValidEnv((ODBCEnv *) Handle))
                        return SQL_INVALID_HANDLE;
-               err = ((ODBCEnv *) Handle)->Error;
+               err = getEnvError((ODBCEnv *) Handle);
                break;
        case SQL_HANDLE_DBC:
                /* Check if this struct is still valid/alive */
                dbc = (ODBCDbc *) Handle;
                if (!isValidDbc(dbc))
                        return SQL_INVALID_HANDLE;
-               err = dbc->Error;
+               err = getDbcError(dbc);
                break;
        case SQL_HANDLE_STMT:
                /* Check if this struct is still valid/alive */
                if (!isValidStmt((ODBCStmt *) Handle))
                        return SQL_INVALID_HANDLE;
-               err = ((ODBCStmt *) Handle)->Error;
+               err = getStmtError((ODBCStmt *) Handle);
                dbc = ((ODBCStmt *) Handle)->Dbc;
                break;
        case SQL_HANDLE_DESC:
                /* Check if this struct is still valid/alive */
                if (!isValidDesc((ODBCDesc *) Handle))
                        return SQL_INVALID_HANDLE;
-               err = ((ODBCDesc *) Handle)->Error;
+               err = getDescError((ODBCDesc *) Handle);
                dbc = ((ODBCDesc *) Handle)->Dbc;
                break;
        default:
@@ -143,15 +143,26 @@ MNDBGetDiagField(SQLSMALLINT HandleType,
                copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
                return SQL_SUCCESS;
        }
-#if 0
-/* not clear yet what to return here */
-       case SQL_DIAG_MESSAGE_TEXT: {           /* SQLCHAR* */
-               char msg[1024];
-               snprintf(msg, sizeof(msg), "");
-               copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
+       case SQL_DIAG_MESSAGE_TEXT:{            /* SQLCHAR* */
+               char *msg = getMessage(err);
+
+               /* first write the error message prefix text:
+                * [MonetDB][ODBC driver VERSION][DSN]
+                * this is required by the ODBC spec:
+                * 
https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/diagnostic-messages
+                * and used to determine where the error originated
+                */
+               SQLSMALLINT msgLen;
+               if (dbc && dbc->dsn)
+                       msgLen = (SQLSMALLINT) strconcat_len((char *) 
DiagInfoPtr, BufferLength, ODBCErrorMsgPrefix, "[", dbc->dsn, "]", msg, NULL);
+               else
+                       msgLen = (SQLSMALLINT) strconcat_len((char *) 
DiagInfoPtr, BufferLength, ODBCErrorMsgPrefix, msg, NULL);
+               if (StringLengthPtr)
+                       *StringLengthPtr = msgLen;
+               if (DiagInfoPtr == NULL || msgLen >= BufferLength)
+                       return SQL_SUCCESS_WITH_INFO;
                return SQL_SUCCESS;
        }
-#endif
        case SQL_DIAG_NATIVE:                   /* SQLINTEGER */
                *(SQLINTEGER *) DiagInfoPtr = getNativeErrorCode(err);
                return SQL_SUCCESS;
diff --git a/clients/odbc/driver/SQLGetDiagRec.c 
b/clients/odbc/driver/SQLGetDiagRec.c
--- a/clients/odbc/driver/SQLGetDiagRec.c
+++ b/clients/odbc/driver/SQLGetDiagRec.c
@@ -43,7 +43,7 @@ MNDBGetDiagRec(SQLSMALLINT HandleType,
               SQLSMALLINT *TextLengthPtr)
 {
        ODBCError *err;
-       SQLRETURN retCode;
+       ODBCDbc *dbc = NULL;
        char *msg;
        SQLSMALLINT msgLen;
 
@@ -56,19 +56,25 @@ MNDBGetDiagRec(SQLSMALLINT HandleType,
                break;
        case SQL_HANDLE_DBC:
                /* Check if this struct is still valid/alive */
-               if (!isValidDbc((ODBCDbc *) Handle))
+               dbc = (ODBCDbc *) Handle;
+               if (!isValidDbc(dbc))
                        return SQL_INVALID_HANDLE;
-               err = getDbcError((ODBCDbc *) Handle);
+               err = getDbcError(dbc);
                break;
        case SQL_HANDLE_STMT:
                /* Check if this struct is still valid/alive */
                if (!isValidStmt((ODBCStmt *) Handle))
                        return SQL_INVALID_HANDLE;
                err = getStmtError((ODBCStmt *) Handle);
+               dbc = ((ODBCStmt *) Handle)->Dbc;
                break;
        case SQL_HANDLE_DESC:
-               /* not yet supported */
-               return Handle ? SQL_NO_DATA : SQL_INVALID_HANDLE;
+               /* Check if this struct is still valid/alive */
+               if (!isValidDesc((ODBCDesc *) Handle))
+                       return SQL_INVALID_HANDLE;
+               err = getDescError((ODBCDesc *) Handle);
+               dbc = ((ODBCDesc *) Handle)->Dbc;
+               break;
        default:
                return SQL_INVALID_HANDLE;
        }
@@ -81,7 +87,6 @@ MNDBGetDiagRec(SQLSMALLINT HandleType,
                return SQL_ERROR;
 
        err = getErrorRec(err, RecNumber);
-
        /* Check the error object from the handle, it may be NULL when
         * no (more) errors are available
         */
@@ -103,23 +108,26 @@ MNDBGetDiagRec(SQLSMALLINT HandleType,
                *NativeErrorPtr = getNativeErrorCode(err);
 
        msg = getMessage(err);
-       retCode = SQL_SUCCESS;
 
        /* first write the error message prefix text:
-        * [MonetDB][ODBC driver VERSION]; this is
-        * required by the ODBC spec and used to
-        * determine where the error originated
+        * [MonetDB][ODBC driver VERSION][DSN]
+        * this is required by the ODBC spec:
+        * 
https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/diagnostic-messages
+        * and used to determine where the error originated
         */
-       msgLen = (SQLSMALLINT) strconcat_len((char *) MessageText, 
BufferLength, ODBCErrorMsgPrefix, msg, NULL);
+       if (dbc && dbc->dsn)
+               msgLen = (SQLSMALLINT) strconcat_len((char *) MessageText, 
BufferLength, ODBCErrorMsgPrefix, "[", dbc->dsn, "]", msg, NULL);
+       else
+               msgLen = (SQLSMALLINT) strconcat_len((char *) MessageText, 
BufferLength, ODBCErrorMsgPrefix, msg, NULL);
+
+       if (TextLengthPtr)
+               *TextLengthPtr = msgLen;
+
        if (MessageText == NULL || msgLen >= BufferLength) {
                /* it didn't fit */
-               retCode = SQL_SUCCESS_WITH_INFO;
+               return SQL_SUCCESS_WITH_INFO;
        }
-
-       if (TextLengthPtr)
-               *TextLengthPtr = (SQLSMALLINT) (msgLen + 
ODBCErrorMsgPrefixLength);
-
-       return retCode;
+       return SQL_SUCCESS;
 }
 
 SQLRETURN SQL_API
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to