]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/sql-wrap.c
should address ITS#3861
[openldap] / servers / slapd / back-sql / sql-wrap.c
index 7709f817de9cc4da892c1d4339ae8bf60a54c530..61beb4f14a176808d4aafb595f1d440817c9e632 100644 (file)
@@ -1,8 +1,10 @@
 /* $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.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 /* 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"
 
-#ifdef SLAPD_SQL
-
 #include <stdio.h>
 #include "ac/string.h"
 #include <sys/types.h>
 
 #include "slap.h"
-#include "ldap_pvt.h"
 #include "proto-sql.h"
 
 #define MAX_ATTR_LEN 16384
 
-typedef struct backsql_conn {
-       int             ldap_cid;
+typedef struct backsql_db_conn {
+       unsigned long   ldap_cid;
        SQLHDBC         dbh;
 } backsql_db_conn;
 
@@ -61,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 ) {
@@ -73,34 +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 );
+               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(): "
@@ -109,6 +116,8 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
                rc = SQLSetStmtOption( *sth, SQL_QUERY_TIMEOUT, timeout );
                if ( rc != SQL_SUCCESS ) {
                        backsql_PrintErrors( SQL_NULL_HENV, dbh, *sth, rc );
+                       SQLFreeStmt( *sth, SQL_DROP );
+                       return rc;
                }
        }
 
@@ -117,7 +126,7 @@ 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
@@ -165,7 +174,7 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                                        (SQLUINTEGER)( sizeof( colname ) - 1 ),
                                        &name_len, &col_type,
                                        &col_prec, &col_scale, &col_null );
-                       ber_str2bv( colname, 0, 1, &row->col_names[ i - 1 ] );
+                       ber_str2bv( (char *)colname, 0, 1, &row->col_names[ i - 1 ] );
 #ifdef BACKSQL_TRACE
                        Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
                                "col_name=%s, col_prec[%d]=%d\n",
@@ -268,13 +277,13 @@ backsql_close_db_conn( backsql_db_conn *conn )
 }
 
 int
-backsql_init_db_env( backsql_info *si )
+backsql_init_db_env( backsql_info *bi )
 {
        RETCODE         rc;
        int             ret = SQL_SUCCESS;
        
        Debug( LDAP_DEBUG_TRACE, "==>backsql_init_db_env()\n", 0, 0, 0 );
-       rc = SQLAllocEnv( &si->db_env );
+       rc = SQLAllocEnv( &bi->sql_db_env );
        if ( rc != SQL_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "init_db_env: SQLAllocEnv failed:\n",
                                0, 0, 0 );
@@ -287,7 +296,7 @@ backsql_init_db_env( backsql_info *si )
 }
 
 int
-backsql_free_db_env( backsql_info *si )
+backsql_free_db_env( backsql_info *bi )
 {
        Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_env()\n", 0, 0, 0 );
 
@@ -306,7 +315,7 @@ backsql_free_db_env( backsql_info *si )
 }
 
 static int
-backsql_open_db_conn( backsql_info *si, unsigned long ldap_cid, backsql_db_conn **pdbc )
+backsql_open_db_conn( backsql_info *bi, unsigned long ldap_cid, backsql_db_conn **pdbc )
 {
        /* TimesTen */
        char                    DBMSName[ 32 ];
@@ -319,24 +328,26 @@ backsql_open_db_conn( backsql_info *si, unsigned long ldap_cid, backsql_db_conn
        Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_conn()\n", 0, 0, 0 );
        dbc = (backsql_db_conn *)ch_calloc( 1, sizeof( backsql_db_conn ) );
        dbc->ldap_cid = ldap_cid;
-       rc = SQLAllocConnect( si->db_env, &dbc->dbh );
+       rc = SQLAllocConnect( bi->sql_db_env, &dbc->dbh );
        if ( !BACKSQL_SUCCESS( rc ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                        "SQLAllocConnect() failed:\n", 0, 0, 0 );
-               backsql_PrintErrors( si->db_env, SQL_NULL_HDBC,
+               backsql_PrintErrors( bi->sql_db_env, SQL_NULL_HDBC,
                                SQL_NULL_HENV, rc );
                return LDAP_UNAVAILABLE;
        }
 
-       rc = SQLConnect( dbc->dbh, si->dbname, SQL_NTS, si->dbuser, 
-                       SQL_NTS, si->dbpasswd, SQL_NTS );
+       rc = SQLConnect( dbc->dbh,
+                       (SQLCHAR*)bi->sql_dbname, SQL_NTS,
+                       (SQLCHAR*)bi->sql_dbuser, SQL_NTS,
+                       (SQLCHAR*)bi->sql_dbpasswd, SQL_NTS );
        if ( rc != SQL_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                        "SQLConnect() to database \"%s\" as user \"%s\" "
-                       "%s:\n", si->dbname, si->dbuser,
+                       "%s:\n", bi->sql_dbname, bi->sql_dbuser,
                        rc == SQL_SUCCESS_WITH_INFO ?
                        "succeeded with info" : "failed" );
-               backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc );
+               backsql_PrintErrors( bi->sql_db_env, dbc->dbh, SQL_NULL_HENV, rc );
                if ( rc != SQL_SUCCESS_WITH_INFO ) {
                        return LDAP_UNAVAILABLE;
                }
@@ -353,7 +364,7 @@ backsql_open_db_conn( backsql_info *si, unsigned long ldap_cid, backsql_db_conn
         * remember that fact for later use.
         */
        /* Assume until proven otherwise */
-       si->bsql_flags &= ~BSQLF_USE_REVERSE_DN;
+       bi->sql_flags &= ~BSQLF_USE_REVERSE_DN;
        DBMSName[ 0 ] = '\0';
        rc = SQLGetInfo( dbc->dbh, SQL_DBMS_NAME, (PTR)&DBMSName,
                        sizeof( DBMSName ), NULL );
@@ -362,25 +373,25 @@ backsql_open_db_conn( backsql_info *si, unsigned long ldap_cid, backsql_db_conn
                                strcmp( DBMSName, "Front-Tier" ) == 0 ) {
                        Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                                "TimesTen database!\n", 0, 0, 0 );
-                       si->bsql_flags |= BSQLF_USE_REVERSE_DN;
+                       bi->sql_flags |= BSQLF_USE_REVERSE_DN;
                }
        } else {
                Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                        "SQLGetInfo() failed:\n", 0, 0, 0 );
-               backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc );
+               backsql_PrintErrors( bi->sql_db_env, dbc->dbh, SQL_NULL_HENV, rc );
                return rc;
        }
        /* end TimesTen */
 
        Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(): "
                "connected, adding to tree\n", 0, 0, 0 );
-       ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
-       if ( avl_insert( &si->db_conns, dbc, backsql_cmp_connid, avl_dup_error ) ) {
+       ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
+       if ( avl_insert( &bi->sql_db_conns, dbc, backsql_cmp_connid, avl_dup_error ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                        "duplicate connection ID\n", 0, 0, 0 );
                return LDAP_OTHER;
        }
-       ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
+       ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_conn()\n", 0, 0, 0 );
 
        *pdbc = dbc;
@@ -391,15 +402,15 @@ backsql_open_db_conn( backsql_info *si, unsigned long ldap_cid, backsql_db_conn
 int
 backsql_free_db_conn( Operation *op )
 {
-       backsql_info            *si = (backsql_info *)op->o_bd->be_private;
+       backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
        backsql_db_conn         tmp = { 0 },
                                *conn;
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_conn()\n", 0, 0, 0 );
        tmp.ldap_cid = op->o_connid;
-       ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
-       conn = avl_delete( &si->db_conns, &tmp, backsql_cmp_connid );
-       ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
+       ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
+       conn = avl_delete( &bi->sql_db_conns, &tmp, backsql_cmp_connid );
+       ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
 
        /*
         * we have one thread per connection, as I understand -- so we can
@@ -417,7 +428,7 @@ backsql_free_db_conn( Operation *op )
 int
 backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
 {
-       backsql_info            *si = (backsql_info *)op->o_bd->be_private;
+       backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
        backsql_db_conn         *dbc,
                                tmp = { 0 };
        int                     rc = LDAP_SUCCESS;
@@ -433,9 +444,9 @@ backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
         * we have one thread per connection, as I understand -- 
         * so we do not need locking here
         */
-       dbc = avl_find( si->db_conns, &tmp, backsql_cmp_connid );
+       dbc = avl_find( bi->sql_db_conns, &tmp, backsql_cmp_connid );
        if ( !dbc ) {
-               rc = backsql_open_db_conn( si, op->o_connid, &dbc );
+               rc = backsql_open_db_conn( bi, op->o_connid, &dbc );
                if ( rc != LDAP_SUCCESS) {
                        Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
                                "could not get connection handle "
@@ -444,18 +455,18 @@ backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
                }
        }
 
-       ldap_pvt_thread_mutex_lock( &si->schema_mutex );
-       if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
+       ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
+       if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
                        "first call -- reading schema map\n", 0, 0, 0 );
-               rc = backsql_load_schema_map( si, dbc->dbh );
+               rc = backsql_load_schema_map( bi, dbc->dbh );
                if ( rc != LDAP_SUCCESS ) {
-                       ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
+                       ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
                        backsql_free_db_conn( op );
                        return rc;
                }
        }
-       ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
+       ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
 
        *dbh = dbc->dbh;
 
@@ -464,5 +475,3 @@ backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
        return LDAP_SUCCESS;
 }
 
-#endif /* SLAPD_SQL */
-