changeset 86177:630450913d85

Implement SQLGetDiagField() for DiagIdentifier = SQL_DIAG_MESSAGE_TEXT. Also improved code and synced it with SQLGetDiagRec() logic.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 27 Jul 2022 22:10:50 +0200
parents 849c75ef4e32
children 9a31815ba521
files clients/odbc/driver/SQLGetDiagField.c clients/odbc/driver/SQLGetDiagRec.c
diffstat 2 files changed, 47 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- 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;
--- 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