]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/sql-wrap.c
plug more leaks; few remain (but seem to be in ODBC)
[openldap] / servers / slapd / back-sql / sql-wrap.c
index f9fdd07c9e9759841a4c7fd249aa2fd86812ff88..8173d504683ad701bed576747a48bdf3984e7dc4 100644 (file)
@@ -1,8 +1,9 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2004 The OpenLDAP Foundation.
+ * Copyright 1999-2005 The OpenLDAP Foundation.
  * Portions Copyright 1999 Dmitry Kovalev.
+ * Portions Copyright 2002 Pierangelo Masarati.
  * Portions Copyright 2004 Mark Adamson.
  * All rights reserved.
  *
@@ -16,7 +17,8 @@
  */
 /* ACKNOWLEDGEMENTS:
  * This work was initially developed by Dmitry Kovalev for inclusion
- * by OpenLDAP Software.
+ * by OpenLDAP Software.  Additional significant contributors include
+ * Pierangelo Masarati and Mark Adamson.
  */
 
 #include "portable.h"
@@ -59,8 +61,6 @@ RETCODE
 backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
 {
        RETCODE         rc;
-       char            drv_name[ 30 ];
-       SWORD           len;
 
        rc = SQLAllocStmt( dbh, sth );
        if ( rc != SQL_SUCCESS ) {
@@ -71,36 +71,43 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
        Debug( LDAP_DEBUG_TRACE, "==>backsql_Prepare()\n", 0, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
-       SQLGetInfo( dbh, SQL_DRIVER_NAME, drv_name, sizeof( drv_name ), &len );
+#ifdef BACKSQL_MSSQL_WORKAROUND
+       {
+               char            drv_name[ 30 ];
+               SWORD           len;
+
+               SQLGetInfo( dbh, SQL_DRIVER_NAME, drv_name, sizeof( drv_name ), &len );
 
 #ifdef BACKSQL_TRACE
-       Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): driver name=\"%s\"\n",
-                       drv_name, 0, 0 );
+               Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): driver name=\"%s\"\n",
+                               drv_name, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
-       ldap_pvt_str2upper( drv_name );
-       if ( !strncmp( drv_name, "SQLSRV32.DLL", sizeof( drv_name ) ) ) {
-               /*
-                * stupid default result set in MS SQL Server
-                * does not support multiple active statements
-                * on the same connection -- so we are trying 
-                * to make it not to use default result set...
-                */
-               Debug( LDAP_DEBUG_TRACE, "_SQLprepare(): "
-                       "enabling MS SQL Server default result "
-                       "set workaround\n", 0, 0, 0 );
-               rc = SQLSetStmtOption( *sth, SQL_CONCURRENCY, 
-                               SQL_CONCUR_ROWVER );
-               if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) {
-                       Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): "
-                               "SQLSetStmtOption(SQL_CONCURRENCY,"
-                               "SQL_CONCUR_ROWVER) failed:\n", 
-                               0, 0, 0 );
-                       backsql_PrintErrors( SQL_NULL_HENV, dbh, *sth, rc );
-                       SQLFreeStmt( *sth, SQL_DROP );
-                       return rc;
+               ldap_pvt_str2upper( drv_name );
+               if ( !strncmp( drv_name, "SQLSRV32.DLL", STRLENOF( "SQLSRV32.DLL" ) ) ) {
+                       /*
+                        * stupid default result set in MS SQL Server
+                        * does not support multiple active statements
+                        * on the same connection -- so we are trying 
+                        * to make it not to use default result set...
+                        */
+                       Debug( LDAP_DEBUG_TRACE, "_SQLprepare(): "
+                               "enabling MS SQL Server default result "
+                               "set workaround\n", 0, 0, 0 );
+                       rc = SQLSetStmtOption( *sth, SQL_CONCURRENCY, 
+                                       SQL_CONCUR_ROWVER );
+                       if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) {
+                               Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): "
+                                       "SQLSetStmtOption(SQL_CONCURRENCY,"
+                                       "SQL_CONCUR_ROWVER) failed:\n", 
+                                       0, 0, 0 );
+                               backsql_PrintErrors( SQL_NULL_HENV, dbh, *sth, rc );
+                               SQLFreeStmt( *sth, SQL_DROP );
+                               return rc;
+                       }
                }
        }
+#endif /* BACKSQL_MSSQL_WORKAROUND */
 
        if ( timeout > 0 ) {
                Debug( LDAP_DEBUG_TRACE, "_SQLprepare(): "
@@ -119,11 +126,11 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
                        0, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
-       return SQLPrepare( *sth, query, SQL_NTS );
+       return SQLPrepare( *sth, (SQLCHAR *)query, SQL_NTS );
 }
 
 RETCODE
-backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
+backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx )
 {
        RETCODE         rc;
        SQLCHAR         colname[ 64 ];
@@ -154,62 +161,45 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                        "ncols=%d\n", (int)row->ncols, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
-               row->col_names = (BerVarray)ch_calloc( row->ncols + 1, 
-                               sizeof( struct berval ) );
-               row->cols = (char **)ch_calloc( row->ncols + 1, 
-                               sizeof( char * ) );
-               row->col_prec = (UDWORD *)ch_calloc( row->ncols,
-                               sizeof( UDWORD ) );
-               row->value_len = (SQLINTEGER *)ch_calloc( row->ncols,
-                               sizeof( SQLINTEGER ) );
+               row->col_names = (BerVarray)ber_memcalloc_x( row->ncols + 1, 
+                               sizeof( struct berval ), ctx );
+               row->cols = (char **)ber_memcalloc_x( row->ncols + 1, 
+                               sizeof( char * ), ctx );
+               row->col_prec = (UDWORD *)ber_memcalloc_x( row->ncols,
+                               sizeof( UDWORD ), ctx );
+               row->value_len = (SQLINTEGER *)ber_memcalloc_x( row->ncols,
+                               sizeof( SQLINTEGER ), ctx );
                for ( i = 1; i <= row->ncols; i++ ) {
                        rc = SQLDescribeCol( sth, (SQLSMALLINT)i, &colname[ 0 ],
                                        (SQLUINTEGER)( sizeof( colname ) - 1 ),
                                        &name_len, &col_type,
                                        &col_prec, &col_scale, &col_null );
-                       ber_str2bv( colname, 0, 1, &row->col_names[ i - 1 ] );
+                       /* FIXME: test rc? */
+
+                       ber_str2bv_x( (char *)colname, 0, 1,
+                                       &row->col_names[ i - 1 ], ctx );
 #ifdef BACKSQL_TRACE
                        Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
                                "col_name=%s, col_prec[%d]=%d\n",
                                colname, (int)i, (int)col_prec );
 #endif /* BACKSQL_TRACE */
-                       if ( col_type == SQL_LONGVARCHAR 
-                                       || col_type == SQL_LONGVARBINARY) {
-#if 0
-                               row->cols[ i - 1 ] = NULL;
-                               row->col_prec[ i - 1 ] = -1;
-
-                               /*
-                                * such fields must be handled 
-                                * in some other way since they return 2G 
-                                * as their precision (at least it does so 
-                                * with MS SQL Server w/native driver)
-                                * for now, we just set fixed precision 
-                                * for such fields - dirty hack, but...
-                                * no time to deal with SQLGetData()
-                                */
-#endif
+                       if ( col_type != SQL_CHAR && col_type != SQL_VARCHAR )
+                       {
                                col_prec = MAX_ATTR_LEN;
-                               row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
-                               row->col_prec[ i - 1 ] = col_prec;
-                               rc = SQLBindCol( sth, (SQLUSMALLINT)i,
-                                               SQL_C_CHAR,
-                                               (SQLPOINTER)row->cols[ i - 1 ],
-                                               col_prec + 1,
-                                               &row->value_len[ i - 1 ] );
-                       } else {
-                               row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
-                               row->col_prec[ i - 1 ] = col_prec;
-                               rc = SQLBindCol( sth, (SQLUSMALLINT)i,
-                                               SQL_C_CHAR,
-                                               (SQLPOINTER)row->cols[ i - 1 ],
-                                               col_prec + 1,
-                                               &row->value_len[ i - 1 ] );
                        }
+
+                       row->cols[ i - 1 ] = (char *)ber_memcalloc_x( col_prec + 1,
+                                       sizeof( char ), ctx );
+                       row->col_prec[ i - 1 ] = col_prec;
+                       rc = SQLBindCol( sth, (SQLUSMALLINT)i,
+                                        SQL_C_CHAR,
+                                        (SQLPOINTER)row->cols[ i - 1 ],
+                                        col_prec + 1,
+                                        &row->value_len[ i - 1 ] );
+                       /* FIXME: test rc? */
                }
 
-               row->col_names[ i - 1 ].bv_val = NULL;
-               row->col_names[ i - 1 ].bv_len = 0;
+               BER_BVZERO( &row->col_names[ i - 1 ] );
                row->cols[ i - 1 ] = NULL;
        }
 
@@ -221,20 +211,33 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
 }
 
 RETCODE
-backsql_FreeRow( BACKSQL_ROW_NTS *row )
+backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
+{
+       return backsql_BindRowAsStrings_x( sth, row, NULL );
+}
+
+RETCODE
+backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx )
 {
        if ( row->cols == NULL ) {
                return SQL_ERROR;
        }
 
-       ber_bvarray_free( row->col_names );
-       ldap_charray_free( row->cols );
-       free( row->col_prec );
-       free( row->value_len );
+       ber_bvarray_free_x( row->col_names, ctx );
+       ber_memvfree_x( (void **)row->cols, ctx );
+       ber_memfree_x( row->col_prec, ctx );
+       ber_memfree_x( row->value_len, ctx );
 
        return SQL_SUCCESS;
 }
 
+
+RETCODE
+backsql_FreeRow( BACKSQL_ROW_NTS *row )
+{
+       return backsql_FreeRow_x( row, NULL );
+}
+
 static int
 backsql_cmp_connid( const void *v_c1, const void *v_c2 )
 {
@@ -265,7 +268,10 @@ backsql_close_db_conn( backsql_db_conn *conn )
        SQLTransact( SQL_NULL_HENV, conn->dbh, SQL_ROLLBACK );
        SQLDisconnect( conn->dbh );
        SQLFreeConnect( conn->dbh );
+       ch_free( conn );
+
        Debug( LDAP_DEBUG_TRACE, "<==backsql_close_db_conn()\n", 0, 0, 0 );
+
        return 1;
 }
 
@@ -315,7 +321,7 @@ backsql_open_db_conn( backsql_info *bi, unsigned long ldap_cid, backsql_db_conn
        backsql_db_conn         *dbc;
        int                     rc;
 
-       assert( pdbc );
+       assert( pdbc != NULL );
        *pdbc = NULL;
  
        Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_conn()\n", 0, 0, 0 );
@@ -428,7 +434,7 @@ backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_get_db_conn()\n", 0, 0, 0 );
 
-       assert( dbh );
+       assert( dbh != NULL );
        *dbh = SQL_NULL_HDBC;
 
        tmp.ldap_cid = op->o_connid;