Index: nsodbc.c =================================================================== RCS file: /cvsroot/aolserver/nsodbc/nsodbc.c,v retrieving revision 1.4 diff -u -r1.4 nsodbc.c --- nsodbc.c 23 Jul 2003 18:52:38 -0000 1.4 +++ nsodbc.c 26 Apr 2004 11:10:26 -0000 @@ -47,6 +47,7 @@ static char *ODBCName(void); static int ODBCServerInit(char *server, char *hModule, char *driver); +static int ODBCShutdown(HENV henv); static int ODBCOpenDb(Ns_DbHandle *handle); static int ODBCCloseDb(Ns_DbHandle *handle); static int ODBCGetRow(Ns_DbHandle *handle, Ns_Set *row); @@ -94,17 +95,15 @@ NS_EXPORT int Ns_DbDriverInit(char *driver, char *configPath) { - RETCODE rc; - - rc = SQLAllocEnv(&odbcenv); - if (rc != SQL_SUCCESS) { - Ns_Log(Error, "%s: failed to allocate odbc", driver); + if (SQLAllocEnv(&odbcenv) != SQL_SUCCESS) { + Ns_Log(Error, "%s: failed to allocate odbc", driver); return NS_ERROR; } if (Ns_DbRegisterDriver(driver, odbcProcs) != NS_OK) { Ns_Log(Error, "%s: failed to register driver", driver); return NS_ERROR; } + Ns_RegisterShutdown((Ns_Callback *) ODBCShutdown, odbcenv); return NS_OK; } @@ -142,6 +141,33 @@ /* *---------------------------------------------------------------------- * + * ODBCShutdown - + * + * Callback to clean up driver on server shutdown. + * + * Results: + * Resources are freed. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +static int +ODBCShutdown(HENV henv) +{ + RETCODE rc; + + rc = SQLFreeEnv(henv); + return RC_OK(rc) ? NS_OK : NS_ERROR; +} + + + +/* + *---------------------------------------------------------------------- + * * ODBCName - * * Return the ODBC driver name. @@ -181,35 +207,32 @@ static int ODBCOpenDb(Ns_DbHandle *handle) { - HDBC hdbc; + SQLHDBC hdbc; RETCODE rc; - int userlen, passwordlen; + + assert(handle != NULL); + assert(handle->datasource != NULL); handle->connected = NS_FALSE; handle->connection = NULL; handle->statement = NULL; + rc = SQLAllocConnect(odbcenv, &hdbc); + handle->connection = (void *) hdbc; ODBCLog(rc, handle); if (!RC_OK(rc)) { + handle->connection = NULL; + SQLFreeConnect(hdbc); return NS_ERROR; } - if (handle->user != NULL) { - userlen = strlen(handle->user); - } else { - userlen = 0; - } - if (handle->password != NULL) { - passwordlen = strlen(handle->password); - } else { - passwordlen = 0; - } Ns_Log(Notice, "%s[%s]: attemping to open '%s'", handle->driver, handle->poolname, handle->datasource); - rc = SQLConnect(hdbc, handle->datasource, - (SWORD) strlen(handle->datasource), handle->user, - (SWORD) userlen, handle->password, (SWORD) passwordlen); + rc = SQLConnect(hdbc, handle->datasource, SQL_NTS, + handle->user, SQL_NTS, handle->password, SQL_NTS); ODBCLog(rc, handle); - if (!RC_OK(rc)) { + if (!SQL_SUCCEEDED(rc)) { + handle->connection = NULL; + SQLFreeConnect(hdbc); return NS_ERROR; } handle->connection = (void *) hdbc; @@ -238,17 +261,20 @@ ODBCCloseDb(Ns_DbHandle *handle) { RETCODE rc; + SQLHDBC hdbc; - rc = SQLDisconnect((HDBC) handle->connection); + hdbc = (SQLHDBC) handle->connection; + handle->connection = NULL; + handle->connected = NS_FALSE; + + rc = SQLDisconnect(hdbc); if (!RC_OK(rc)) { return NS_ERROR; } - rc = SQLFreeConnect((HDBC) handle->connection); + rc = SQLFreeConnect(hdbc); if (!RC_OK(rc)) { return NS_ERROR; } - handle->connection = NULL; - handle->connected = NS_FALSE; return NS_OK; } @@ -256,7 +282,7 @@ /* *---------------------------------------------------------------------- * - * ODBExec - + * ODBCExec - * * Send an SQL statement. * @@ -275,13 +301,13 @@ HSTMT hstmt; RETCODE rc; int status; - short numcols; + short numcols; /* * Allocate a new statement. */ - rc = SQLAllocStmt((HDBC) handle->connection, &hstmt); + rc = SQLAllocStmt((SQLHDBC) handle->connection, &hstmt); ODBCLog(rc, handle); if (!RC_OK(rc)) { return NS_ERROR; @@ -296,15 +322,15 @@ ODBCLog(rc, handle); if (RC_OK(rc)) { rc = SQLNumResultCols(hstmt, &numcols); - ODBCLog(rc, handle); - if (RC_OK(rc)) { - if (numcols != 0) { - handle->fetchingRows = 1; - status = NS_ROWS; - } else { - status = NS_DML; - } - } + ODBCLog(rc, handle); + if (RC_OK(rc)) { + if (numcols != 0) { + handle->fetchingRows = 1; + status = NS_ROWS; + } else { + status = NS_DML; + } + } } /* @@ -312,10 +338,10 @@ */ if (!RC_OK(rc)) { - status = NS_ERROR; + status = NS_ERROR; } if (status != NS_ROWS && ODBCFreeStmt(handle) != NS_OK) { - status = NS_ERROR; + status = NS_ERROR; } return status; } @@ -519,14 +545,14 @@ } if (STREQ(argv[1], "dbmsname")) { - rc = SQLGetInfo((HDBC) handle->connection, SQL_DBMS_NAME, buf, sizeof(buf), &cbInfoValue); + rc = SQLGetInfo((SQLHDBC) handle->connection, SQL_DBMS_NAME, buf, sizeof(buf), &cbInfoValue); ODBCLog(rc, handle); if (!RC_OK(rc)) { Tcl_SetResult(interp, "could not determine dbmsname", TCL_STATIC); return TCL_ERROR; } } else if (STREQ(argv[1], "dbmsver")) { - rc = SQLGetInfo((HDBC) handle->connection, SQL_DBMS_VER, buf, sizeof(buf), &cbInfoValue); + rc = SQLGetInfo((SQLHDBC) handle->connection, SQL_DBMS_VER, buf, sizeof(buf), &cbInfoValue); ODBCLog(rc, handle); if (!RC_OK(rc)) { Tcl_SetResult(interp, "could not determine dbmsver", TCL_STATIC); @@ -562,8 +588,8 @@ static void ODBCLog(RETCODE rc, Ns_DbHandle *handle) { - HDBC hdbc; - HSTMT hstmt; + SQLHDBC hdbc; + SQLHSTMT hstmt; Ns_LogSeverity severity; char szSQLSTATE[6]; SDWORD nErr; @@ -575,10 +601,10 @@ } else if (rc == SQL_ERROR) { severity = Error; } else { - return; + return; } - hdbc = (HDBC) handle->connection; - hstmt = (HSTMT) handle->statement; + hdbc = (SQLHDBC) handle->connection; + hstmt = (SQLHSTMT) handle->statement; while (SQLError(odbcenv, hdbc, hstmt, szSQLSTATE, &nErr, msg, sizeof(msg), &cbmsg) == SQL_SUCCESS) { Ns_Log(severity, "%s[%s]: odbc message: " @@ -611,7 +637,7 @@ { RETCODE rc; - rc = SQLFreeStmt((HSTMT) handle->statement, SQL_DROP); + rc = SQLFreeStmt((SQLHSTMT) handle->statement, SQL_DROP); handle->statement = NULL; handle->fetchingRows = 0; if (!RC_OK(rc)) {