]> 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 bbc2374d0954eab1fc6645aa93823faedcacc48f..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"
@@ -65,13 +76,13 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
        }
 
 #ifdef BACKSQL_TRACE
-       Debug( LDAP_DEBUG_TRACE, "==>_SQLPrepare()\n", 0, 0, 0 );
+       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_TRACE
-       Debug( LDAP_DEBUG_TRACE, "_SQLPrepare(): driver name='%s'\n",
+       Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): driver name='%s'\n",
                        drv_name, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
@@ -89,7 +100,7 @@ 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(): "
+                       Debug( LDAP_DEBUG_TRACE, "backsql_Prepare(): "
                                "SQLSetStmtOption(SQL_CONCURRENCY,"
                                "SQL_CONCUR_ROWVER) failed:\n", 
                                0, 0, 0 );
@@ -108,7 +119,7 @@ backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
        }
 
 #ifdef BACKSQL_TRACE
-       Debug( LDAP_DEBUG_TRACE, "<==_SQLPrepare() calling SQLPrepare()\n",
+       Debug( LDAP_DEBUG_TRACE, "<==backsql_Prepare() calling SQLPrepare()\n",
                        0, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
@@ -160,7 +171,7 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
        rc = SQLNumResultCols( sth, &row->ncols );
        if ( rc != SQL_SUCCESS ) {
 #ifdef BACKSQL_TRACE
-               Debug( LDAP_DEBUG_TRACE, "_SQLBindRowAsStrings(): "
+               Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings(): "
                        "SQLNumResultCols() failed:\n", 0, 0, 0 );
 #endif /* BACKSQL_TRACE */
                
@@ -177,7 +188,7 @@ backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
                                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 ],
@@ -213,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;
@@ -221,7 +232,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 ] );
                        }
                }
 
@@ -245,16 +256,17 @@ backsql_FreeRow( BACKSQL_ROW_NTS *row )
        }
 
        ber_bvarray_free( row->col_names );
-       charray_free( row->cols );
+       ldap_charray_free( row->cols );
        free( row->col_prec );
-       free( row->is_null );
+       free( row->value_len );
 
        return SQL_SUCCESS;
 }
 
 static int
-backsql_cmp_connid( backsql_db_conn *c1, backsql_db_conn *c2 )
+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;
        }
@@ -384,13 +396,18 @@ backsql_open_db_conn( backsql_info *si, int ldap_cid, backsql_db_conn **pdbc )
                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, NULL );
+       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 );
 
@@ -400,16 +417,15 @@ backsql_open_db_conn( backsql_info *si, int ldap_cid, backsql_db_conn **pdbc )
 }
 
 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 );
 
        /*
@@ -426,9 +442,9 @@ backsql_free_db_conn( Backend *be, Connection *ldapc )
 }
 
 int
-backsql_get_db_conn( Backend *be, Connection *ldapc, SQLHDBC *dbh )
+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;
@@ -438,16 +454,15 @@ backsql_get_db_conn( Backend *be, Connection *ldapc, SQLHDBC *dbh )
        assert( dbh );
        *dbh = SQL_NULL_HDBC;
 
-       tmp.ldap_cid = ldapc->c_connid;
+       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 ) {
-               rc = backsql_open_db_conn( si, ldapc->c_connid, &dbc );
+               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 "
@@ -463,7 +478,7 @@ backsql_get_db_conn( Backend *be, Connection *ldapc, SQLHDBC *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( be, ldapc );
+                       backsql_free_db_conn( op );
                        return rc;
                }
        }