lurcher / unixODBC

The unixODBC Project goals are to develop and promote unixODBC to be the definitive standard for ODBC on non MS Windows platforms.
GNU Lesser General Public License v2.1
94 stars 51 forks source link

No tracefile created #121

Closed jubalh closed 1 year ago

jubalh commented 1 year ago

I have a case where queries are failing but no error is returned. To get more information we wanted to to check the trace file. However we didn't get to work properly. There was no tracefile written.

Here is an example to illustrate that:

/etc/unixODBC/odbc.ini:

[test]
Driver          = MySQL_ODBC
Description     = Connector/ODBC 3.0.2 Driver DSN
SERVER          = localhost

Code:

// SQLConnect_ref.cpp  
// compile with: odbc32.lib  
#include <stdlib.h>
#include <sqlext.h>
#include <stdio.h>

int main() {  
   SQLHENV henv;  
   SQLHDBC hdbc;  
   SQLHSTMT hstmt;  
   SQLRETURN retcode;  

   SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255);  
   SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255);  

   // Allocate environment handle  
   retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);  

   // Set the ODBC version environment attribute  
   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {  
      retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);   

      // Allocate connection handle  
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {  
         retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);   

         // Set login timeout to 5 seconds  
         if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {  
            SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);  

            // Connect to data source  
//            retcode = SQLConnect(hdbc, (SQLCHAR*) "test", SQL_NTS, (SQLCHAR*) NULL, 0, NULL, 0);  
            retcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*) "DSN=test;Trace=1;TraceFile=/tmp/trace.file", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);

            // Allocate statement handle  
            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {  
               retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);   

               // Process data  
               if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {  
                  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);  
          printf("connected\n");
               }  

               SQLDisconnect(hdbc);  
            }  
           else
               printf("ERRRRO %d\n", retcode);

            SQLFreeHandle(SQL_HANDLE_DBC, hdbc);  
         }  
      }  
      SQLFreeHandle(SQL_HANDLE_ENV, henv);  
   }  
}

It seems neither when using SQLConnect() nor SQLDriverConnect() a trace file is created.

@lurcher do you have any idea what could be the cause of this?

lurcher commented 1 year ago

On 26/09/2022 13:10, Michael Vetter wrote:

I have a case where queries are failing but no error is returned. To get more information we wanted to to check the trace file. However we didn't get to work properly. There was no tracefile written.

It depends on what you want to trace. If you want the driver manager to generate a log then its set in odbcinst.ini

[ODBC] Trace=Yes Tracefile=/tmp/sql.log

The way you are setting it will be passed to the driver and may or may not enable driver tracing, thats down to the driver.

If you want to enable driver manager tracing programmatically then

SQLSetConnectAttr( SQL_ATTR_TRACE )

and

SQLSetConnectAttr( SQL_ATTR_TRACEFILE )

Is the way

jubalh commented 1 year ago

If you want the driver manager to generate a log then its set in odbcinst.ini

That worked! Thanks for the explanation.