]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/sql-wrap.c
now I remember why I introduced the 'has_ldapinfo_dn_ru' flag
[openldap] / servers / slapd / back-sql / sql-wrap.c
index c549c99b7b47c8034fb9128ce3d6f0b2edd4c1f6..414aeafc54e47ea95c1abfc6593afd196c8ed46a 100644 (file)
@@ -1,10 +1,21 @@
-/*
- *      Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1999-2004 The OpenLDAP Foundation.
+ * Portions Copyright 1999 Dmitry Kovalev.
+ * All rights reserved.
  *
- *      Redistribution and use in source and binary forms are permitted only
- *      as authorized by the OpenLDAP Public License.  A copy of this
- *      license is available at http://www.OpenLDAP.org/license.html or
- *      in file LICENSE in the top-level directory of the distribution.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by Dmitry Kovalev for inclusion
+ * by OpenLDAP Software.
  */
 
 #include "portable.h"
@@ -14,8 +25,8 @@
 #include <stdio.h>
 #include "ac/string.h"
 #include <sys/types.h>
-#include "ldap_pvt.h"
 #include "slap.h"
+#include "ldap_pvt.h"
 #include "back-sql.h"
 #include "sql-types.h"
 #include "sql-wrap.h"
@@ -28,8 +39,6 @@ typedef struct backsql_conn {
        SQLHDBC         dbh;
 } backsql_db_conn;
 
-int backsql_dummy( void *, void * );
-
 void
 backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc )
 {
@@ -66,18 +75,19 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
                return rc;
        }
 
-#if 0
-       Debug( LDAP_DEBUG_TRACE, "==>_SQLPrepare()\n", 0, 0, 0 );
-#endif
+#ifdef BACKSQL_TRACE
+       Debug( LDAP_DEBUG_TRACE, "==>backsql_Prepare()\n", 0, 0, 0 );
+#endif /* BACKSQL_TRACE */
 
-       SQLGetInfo( dbh, SQL_DRIVER_NAME, drv_name, 30, &len );
+       SQLGetInfo( dbh, SQL_DRIVER_NAME, drv_name, sizeof( drv_name ), &len );
 
-#if 0
-       Debug( LDAP_DEBUG_TRACE, "_SQLPrepare(): driver name='%s'\n",
+#ifdef BACKSQL_TRACE
+       Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): driver name='%s'\n",
                        drv_name, 0, 0 );
-#endif
-       
-       if ( !strncmp( ldap_pvt_str2upper( drv_name ), "SQLSRV32.DLL", 30 ) ) {
+#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
@@ -90,8 +100,9 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
                rc = SQLSetStmtOption( *sth, SQL_CONCURRENCY, 
                                SQL_CONCUR_ROWVER );
                if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) {
-                       Debug( LDAP_DEBUG_TRACE, "_SQLPrepare(): "
-                               "SQLSetStmtOption(SQL_CONCURRENCY,SQL_CONCUR_ROWVER) failed:\n", 
+                       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 );
                }
@@ -107,15 +118,18 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
                }
        }
 
-#if 0
-       Debug( LDAP_DEBUG_TRACE, "<==_SQLPrepare() calling SQLPrepare()\n",
+#ifdef BACKSQL_TRACE
+       Debug( LDAP_DEBUG_TRACE, "<==backsql_Prepare() calling SQLPrepare()\n",
                        0, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
 
        return SQLPrepare( *sth, query, SQL_NTS );
 }
 
 #if 0
+/*
+ * Turned into macros --- see sql-wrap.h
+ */
 RETCODE
 backsql_BindParamStr( SQLHSTMT sth, int par_ind, char *str, int maxlen )
 {
@@ -150,42 +164,43 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                return SQL_ERROR;
        }
 
-#if 0
+#ifdef BACKSQL_TRACE
        Debug( LDAP_DEBUG_TRACE, "==> backsql_BindRowAsStrings()\n", 0, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
        
        rc = SQLNumResultCols( sth, &row->ncols );
        if ( rc != SQL_SUCCESS ) {
-#if 0
-               Debug( LDAP_DEBUG_TRACE, "_SQLBindRowAsStrings(): "
+#ifdef BACKSQL_TRACE
+               Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings(): "
                        "SQLNumResultCols() failed:\n", 0, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
                
                backsql_PrintErrors( SQL_NULL_HENV, SQL_NULL_HDBC, sth, rc );
        } else {
-#if 0
+#ifdef BACKSQL_TRACE
                Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
                        "ncols=%d\n", (int)row->ncols, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
 
-               row->col_names = (char **)ch_calloc( row->ncols, 
+               row->col_names = (BerVarray)ch_calloc( row->ncols + 1, 
+                               sizeof( struct berval ) );
+               row->cols = (char **)ch_calloc( row->ncols + 1, 
                                sizeof( char * ) );
-               row->cols = (char **)ch_calloc( row->ncols, sizeof( char * ) );
                row->col_prec = (UDWORD *)ch_calloc( row->ncols,
                                sizeof( UDWORD ) );
-               row->is_null = (SQLINTEGER *)ch_calloc( row->ncols,
+               row->value_len = (SQLINTEGER *)ch_calloc( row->ncols,
                                sizeof( SQLINTEGER ) );
                for ( i = 1; i <= row->ncols; i++ ) {
                        rc = SQLDescribeCol( sth, (SQLSMALLINT)i, &colname[ 0 ],
-                                       (SQLUINTEGER)sizeof( colname ) - 1,
+                                       (SQLUINTEGER)( sizeof( colname ) - 1 ),
                                        &name_len, &col_type,
                                        &col_prec, &col_scale, &col_null );
-                       row->col_names[ i - 1 ] = ch_strdup( colname );
-#if 0
+                       ber_str2bv( 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",
                                colname, (int)i, (int)col_prec );
-#endif
+#endif /* BACKSQL_TRACE */
                        if ( col_type == SQL_LONGVARCHAR 
                                        || col_type == SQL_LONGVARBINARY) {
 #if 0
@@ -209,7 +224,7 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                                                SQL_C_CHAR,
                                                (SQLPOINTER)row->cols[ i - 1 ],
                                                col_prec + 1,
-                                               &row->is_null[ i - 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;
@@ -217,13 +232,18 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                                                SQL_C_CHAR,
                                                (SQLPOINTER)row->cols[ i - 1 ],
                                                col_prec + 1,
-                                               &row->is_null[ i - 1 ] );
+                                               &row->value_len[ i - 1 ] );
                        }
                }
+
+               row->col_names[ i - 1 ].bv_val = NULL;
+               row->col_names[ i - 1 ].bv_len = 0;
+               row->cols[ i - 1 ] = NULL;
        }
-#if 0
+
+#ifdef BACKSQL_TRACE
        Debug( LDAP_DEBUG_TRACE, "<== backsql_BindRowAsStrings()\n", 0, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
 
        return rc;
 }
@@ -231,30 +251,22 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
 RETCODE
 backsql_FreeRow( BACKSQL_ROW_NTS *row )
 {
-       int     i;
-
        if ( row->cols == NULL ) {
                return SQL_ERROR;
        }
 
-       for ( i = 0; i < row->ncols; i++ ) {
-               /*
-                * FIXME: we need to free the col_names as well, don't we?
-                */
-               free( row->cols[ i ] );
-       }
-
-       free( row->col_names );
+       ber_bvarray_free( row->col_names );
+       ldap_charray_free( row->cols );
        free( row->col_prec );
-       free( row->cols );
-       free( row->is_null );
+       free( row->value_len );
 
        return SQL_SUCCESS;
 }
 
-int
-backsql_cmp_connid( backsql_db_conn *c1, backsql_db_conn *c2 )
+static int
+backsql_cmp_connid( const void *v_c1, const void *v_c2 )
 {
+       const backsql_db_conn *c1 = v_c1, *c2 = v_c2;
        if ( c1->ldap_cid > c2->ldap_cid ) {
                return 1;
        }
@@ -266,13 +278,19 @@ backsql_cmp_connid( backsql_db_conn *c1, backsql_db_conn *c2 )
        return 0;
 }
 
-int
+static int
 backsql_close_db_conn( backsql_db_conn *conn )
 {
        Debug( LDAP_DEBUG_TRACE, "==>backsql_close_db_conn()\n", 0, 0, 0 );
 
+       /*
+        * Default transact is SQL_ROLLBACK; commit is required only
+        * by write operations, and it is explicitly performed after
+        * each atomic operation succeeds.
+        */
+
        /* TimesTen */
-       SQLTransact( SQL_NULL_HENV, conn->dbh, SQL_COMMIT );
+       SQLTransact( SQL_NULL_HENV, conn->dbh, SQL_ROLLBACK );
        SQLDisconnect( conn->dbh );
        SQLFreeConnect( conn->dbh );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_close_db_conn()\n", 0, 0, 0 );
@@ -300,10 +318,11 @@ int
 backsql_free_db_env( backsql_info *si )
 {
        Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_env()\n", 0, 0, 0 );
-#if 0
+
+#ifdef BACKSQL_TRACE
        Debug( LDAP_DEBUG_TRACE, "free_db_env(): delete AVL tree here!!!\n",
                        0, 0, 0 );
-#endif
+#endif /* BACKSQL_TRACE */
 
        /*
         * stop, if frontend waits for all threads to shutdown 
@@ -314,24 +333,27 @@ backsql_free_db_env( backsql_info *si )
        return SQL_SUCCESS;
 }
 
-backsql_db_conn *
-backsql_open_db_conn( backsql_info *si, int ldap_cid )
+static int
+backsql_open_db_conn( backsql_info *si, int ldap_cid, backsql_db_conn **pdbc )
 {
        /* TimesTen */
        char                    DBMSName[ 32 ];
        backsql_db_conn         *dbc;
        int                     rc;
+
+       assert( pdbc );
+       *pdbc = NULL;
  
        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 );
-       if (!BACKSQL_SUCCESS( rc ) ) {
+       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,
                                SQL_NULL_HENV, rc );
-               return NULL;
+               return LDAP_UNAVAILABLE;
        }
 
        rc = SQLConnect( dbc->dbh, si->dbname, SQL_NTS, si->dbuser, 
@@ -344,7 +366,7 @@ backsql_open_db_conn( backsql_info *si, int ldap_cid )
                        "succeeded with info" : "failed" );
                backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc );
                if ( rc != SQL_SUCCESS_WITH_INFO ) {
-                       return NULL;
+                       return LDAP_UNAVAILABLE;
                }
        }
 
@@ -358,7 +380,8 @@ backsql_open_db_conn( backsql_info *si, int ldap_cid )
         * See if this connection is to TimesTen.  If it is,
         * remember that fact for later use.
         */
-       si->isTimesTen = 0;     /* Assume until proven otherwise */
+       /* Assume until proven otherwise */
+       si->bsql_flags &= ~BSQLF_USE_REVERSE_DN;
        DBMSName[ 0 ] = '\0';
        rc = SQLGetInfo( dbc->dbh, SQL_DBMS_NAME, (PTR)&DBMSName,
                        sizeof( DBMSName ), NULL );
@@ -367,37 +390,42 @@ backsql_open_db_conn( backsql_info *si, int ldap_cid )
                                strcmp( DBMSName, "Front-Tier" ) == 0 ) {
                        Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
                                "TimesTen database!\n", 0, 0, 0 );
-                       si->isTimesTen = 1;
+                       si->bsql_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 );
+               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 );
-       avl_insert( &si->db_conns, dbc, (AVL_CMP)backsql_cmp_connid,
-                       backsql_dummy );
+       if ( avl_insert( &si->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 );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_conn()\n", 0, 0, 0 );
 
-       return dbc;
+       *pdbc = dbc;
+
+       return LDAP_SUCCESS;
 }
 
 int
-backsql_free_db_conn( Backend *be, Connection *ldapc )
+backsql_free_db_conn( Operation *op )
 {
-       backsql_info            *si = (backsql_info *)be->be_private;
+       backsql_info            *si = (backsql_info *)op->o_bd->be_private;
        backsql_db_conn         tmp, *conn;
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_conn()\n", 0, 0, 0 );
-       tmp.ldap_cid = ldapc->c_connid;
+       tmp.ldap_cid = op->o_connid;
        ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
-       conn = (backsql_db_conn *)avl_delete( &si->db_conns, &tmp,
-                       (AVL_CMP)backsql_cmp_connid );
+       conn = avl_delete( &si->db_conns, &tmp, backsql_cmp_connid );
        ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
 
        /*
@@ -413,43 +441,55 @@ backsql_free_db_conn( Backend *be, Connection *ldapc )
        return SQL_SUCCESS;
 }
 
-SQLHDBC
-backsql_get_db_conn( Backend *be, Connection *ldapc )
+int
+backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
 {
-       backsql_info            *si = (backsql_info *)be->be_private;
+       backsql_info            *si = (backsql_info *)op->o_bd->be_private;
        backsql_db_conn         *dbc;
        backsql_db_conn         tmp;
+       int                     rc = LDAP_SUCCESS;
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_get_db_conn()\n", 0, 0, 0 );
 
-       tmp.ldap_cid = ldapc->c_connid;
+       assert( dbh );
+       *dbh = SQL_NULL_HDBC;
+
+       tmp.ldap_cid = op->o_connid;
 
        /*
         * we have one thread per connection, as I understand -- 
         * so we do not need locking here
         */
-       dbc = (backsql_db_conn *)avl_find( si->db_conns, &tmp,
-                       (AVL_CMP)backsql_cmp_connid );
+       dbc = avl_find( si->db_conns, &tmp, backsql_cmp_connid );
        if ( !dbc ) {
-               dbc = backsql_open_db_conn( si, ldapc->c_connid );
-       }
-       if ( !dbc ) {
-               Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
-                       "could not get connection handle -- returning NULL\n",
-                       0, 0, 0 );
-               return SQL_NULL_HDBC;
+               rc = backsql_open_db_conn( si, op->o_connid, &dbc );
+               if ( rc != LDAP_SUCCESS) {
+                       Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
+                               "could not get connection handle "
+                               "-- returning NULL\n", 0, 0, 0 );
+                       return rc;
+               }
        }
+
        ldap_pvt_thread_mutex_lock( &si->schema_mutex );
-       if ( !si->schema_loaded ) {
+       if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
                        "first call -- reading schema map\n", 0, 0, 0 );
-               backsql_load_schema_map( si, dbc->dbh );
+               rc = backsql_load_schema_map( si, dbc->dbh );
+               if ( rc != LDAP_SUCCESS ) {
+                       ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
+                       backsql_free_db_conn( op );
+                       return rc;
+               }
        }
        ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
 
+       *dbh = dbc->dbh;
+
        Debug( LDAP_DEBUG_TRACE, "<==backsql_get_db_conn()\n", 0, 0, 0 );
-       return dbc->dbh;
+
+       return LDAP_SUCCESS;
 }
 
 #endif /* SLAPD_SQL */
+