/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1999-2007 The OpenLDAP Foundation.
+ * Copyright 1999-2011 The OpenLDAP Foundation.
* Portions Copyright 1999 Dmitry Kovalev.
* Portions Copyright 2002 Pierangelo Masarati.
* Portions Copyright 2004 Mark Adamson.
#define MAX_ATTR_LEN 16384
-typedef struct backsql_db_conn {
- unsigned long ldap_cid;
- SQLHDBC dbh;
-} backsql_db_conn;
-
void
backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc )
{
}
RETCODE
-backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
+backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, const char *query, int timeout )
{
RETCODE rc;
} else {
SQLCHAR colname[ 64 ];
SQLSMALLINT name_len, col_type, col_scale, col_null;
- UDWORD col_prec;
+ SQLLEN col_prec;
int i;
#ifdef BACKSQL_TRACE
goto nomem;
}
- row->value_len = (SQLINTEGER *)ber_memcalloc_x( row->ncols,
- sizeof( SQLINTEGER ), ctx );
+ row->value_len = (SQLLEN *)ber_memcalloc_x( row->ncols,
+ sizeof( SQLLEN ), ctx );
if ( row->value_len == NULL ) {
goto nomem;
}
return backsql_FreeRow_x( row, NULL );
}
-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;
- }
-
- if ( c1->ldap_cid < c2->ldap_cid ) {
- return -1;
- }
-
- return 0;
-}
-
static void
-backsql_close_db_conn( void *v_conn )
+backsql_close_db_handle( SQLHDBC dbh )
{
- backsql_db_conn *conn = (backsql_db_conn *)v_conn;
- unsigned long cid = conn->ldap_cid;
+ if ( dbh == SQL_NULL_HDBC ) {
+ return;
+ }
- Debug( LDAP_DEBUG_TRACE, "==>backsql_close_db_conn(%lu)\n",
- cid, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "==>backsql_close_db_handle(%p)\n",
+ (void *)dbh, 0, 0 );
/*
* Default transact is SQL_ROLLBACK; commit is required only
*/
/* TimesTen */
- SQLTransact( SQL_NULL_HENV, conn->dbh, SQL_ROLLBACK );
- SQLDisconnect( conn->dbh );
- SQLFreeConnect( conn->dbh );
- ch_free( conn );
+ SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
+ SQLDisconnect( dbh );
+ SQLFreeConnect( dbh );
- Debug( LDAP_DEBUG_TRACE, "<==backsql_close_db_conn(%lu)\n",
- cid, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "<==backsql_close_db_handle(%p)\n",
+ (void *)dbh, 0, 0 );
}
int
backsql_conn_destroy(
backsql_info *bi )
{
- avl_free( bi->sql_db_conns, backsql_close_db_conn );
-
return 0;
}
}
static int
-backsql_open_db_conn( backsql_info *bi, unsigned long ldap_cid, backsql_db_conn **pdbc )
+backsql_open_db_handle(
+ backsql_info *bi,
+ SQLHDBC *dbhp )
{
/* TimesTen */
char DBMSName[ 32 ];
- backsql_db_conn *dbc;
int rc;
- assert( pdbc != NULL );
- *pdbc = NULL;
+ assert( dbhp != NULL );
+ *dbhp = SQL_NULL_HDBC;
- Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_conn(%lu)\n",
- ldap_cid, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_handle()\n",
+ 0, 0, 0 );
- dbc = (backsql_db_conn *)ch_calloc( 1, sizeof( backsql_db_conn ) );
- dbc->ldap_cid = ldap_cid;
- rc = SQLAllocConnect( bi->sql_db_env, &dbc->dbh );
+ rc = SQLAllocConnect( bi->sql_db_env, dbhp );
if ( !BACKSQL_SUCCESS( rc ) ) {
- Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(%lu): "
- "SQLAllocConnect() failed:\n", ldap_cid, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): "
+ "SQLAllocConnect() failed:\n",
+ 0, 0, 0 );
backsql_PrintErrors( bi->sql_db_env, SQL_NULL_HDBC,
- SQL_NULL_HENV, rc );
+ SQL_NULL_HENV, rc );
return LDAP_UNAVAILABLE;
}
- rc = SQLConnect( dbc->dbh,
- (SQLCHAR*)bi->sql_dbname, SQL_NTS,
- (SQLCHAR*)bi->sql_dbuser, SQL_NTS,
- (SQLCHAR*)bi->sql_dbpasswd, SQL_NTS );
+ rc = SQLConnect( *dbhp,
+ (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(%lu): "
+ Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): "
"SQLConnect() to database \"%s\" %s.\n",
- ldap_cid, bi->sql_dbname,
+ bi->sql_dbname,
rc == SQL_SUCCESS_WITH_INFO ?
- "succeeded with info" : "failed" );
- backsql_PrintErrors( bi->sql_db_env, dbc->dbh, SQL_NULL_HENV, rc );
+ "succeeded with info" : "failed",
+ 0 );
+ backsql_PrintErrors( bi->sql_db_env, *dbhp, SQL_NULL_HENV, rc );
if ( rc != SQL_SUCCESS_WITH_INFO ) {
+ SQLFreeConnect( *dbhp );
return LDAP_UNAVAILABLE;
}
}
* TimesTen : Turn off autocommit. We must explicitly
* commit any transactions.
*/
- SQLSetConnectOption( dbc->dbh, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF );
+ SQLSetConnectOption( *dbhp, SQL_AUTOCOMMIT,
+ BACKSQL_AUTOCOMMIT_ON( bi ) ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF );
/*
* See if this connection is to TimesTen. If it is,
/* Assume until proven otherwise */
bi->sql_flags &= ~BSQLF_USE_REVERSE_DN;
DBMSName[ 0 ] = '\0';
- rc = SQLGetInfo( dbc->dbh, SQL_DBMS_NAME, (PTR)&DBMSName,
+ rc = SQLGetInfo( *dbhp, SQL_DBMS_NAME, (PTR)&DBMSName,
sizeof( DBMSName ), NULL );
if ( rc == SQL_SUCCESS ) {
if ( strcmp( DBMSName, "TimesTen" ) == 0 ||
- strcmp( DBMSName, "Front-Tier" ) == 0 ) {
- Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(%lu): "
- "TimesTen database!\n", ldap_cid, 0, 0 );
+ strcmp( DBMSName, "Front-Tier" ) == 0 )
+ {
+ Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): "
+ "TimesTen database!\n",
+ 0, 0, 0 );
bi->sql_flags |= BSQLF_USE_REVERSE_DN;
}
+
} else {
- Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(%lu): "
- "SQLGetInfo() failed.\n", ldap_cid, 0, 0 );
- backsql_PrintErrors( bi->sql_db_env, dbc->dbh, SQL_NULL_HENV, rc );
- return rc;
+ Debug( LDAP_DEBUG_TRACE, "backsql_open_db_handle(): "
+ "SQLGetInfo() failed.\n",
+ 0, 0, 0 );
+ backsql_PrintErrors( bi->sql_db_env, *dbhp, SQL_NULL_HENV, rc );
+ SQLDisconnect( *dbhp );
+ SQLFreeConnect( *dbhp );
+ return LDAP_UNAVAILABLE;
}
/* end TimesTen */
- Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(%lu): "
- "connected, adding to tree.\n", ldap_cid, 0, 0 );
- 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(%lu): "
- "duplicate connection ID.\n", ldap_cid, 0, 0 );
- return LDAP_OTHER;
- }
- ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
- Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_conn(%lu)\n", ldap_cid, 0, 0 );
-
- *pdbc = dbc;
+ Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_handle()\n",
+ 0, 0, 0 );
return LDAP_SUCCESS;
}
-int
-backsql_free_db_conn( Operation *op )
+static void *backsql_db_conn_dummy;
+
+static void
+backsql_db_conn_keyfree(
+ void *key,
+ void *data )
{
- backsql_info *bi = (backsql_info *)op->o_bd->be_private;
- backsql_db_conn tmp = { 0 },
- *conn;
+ (void)backsql_close_db_handle( (SQLHDBC)data );
+}
+int
+backsql_free_db_conn( Operation *op, SQLHDBC dbh )
+{
Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_conn()\n", 0, 0, 0 );
- tmp.ldap_cid = op->o_connid;
- 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
- * get this out of critical section
- */
- if ( conn != NULL ) {
- Debug( LDAP_DEBUG_TRACE, "backsql_free_db_conn(): "
- "closing db connection %lu (%p)\n",
- op->o_connid, (void *)conn, 0 );
- backsql_close_db_conn( (void *)conn );
- }
+ (void)backsql_close_db_handle( dbh );
+ ldap_pvt_thread_pool_setkey( op->o_threadctx,
+ &backsql_db_conn_dummy, (void *)SQL_NULL_HDBC,
+ backsql_db_conn_keyfree, NULL, NULL );
Debug( LDAP_DEBUG_TRACE, "<==backsql_free_db_conn()\n", 0, 0, 0 );
- return conn ? SQL_SUCCESS : SQL_ERROR;
+ return LDAP_SUCCESS;
}
int
-backsql_get_db_conn( Operation *op, SQLHDBC *dbh )
+backsql_get_db_conn( Operation *op, SQLHDBC *dbhp )
{
- backsql_info *bi = (backsql_info *)op->o_bd->be_private;
- backsql_db_conn *dbc,
- tmp = { 0 };
- int rc = LDAP_SUCCESS;
+ backsql_info *bi = (backsql_info *)op->o_bd->be_private;
+ int rc = LDAP_SUCCESS;
+ SQLHDBC dbh = SQL_NULL_HDBC;
Debug( LDAP_DEBUG_TRACE, "==>backsql_get_db_conn()\n", 0, 0, 0 );
- assert( dbh != NULL );
- *dbh = SQL_NULL_HDBC;
+ assert( dbhp != NULL );
+ *dbhp = SQL_NULL_HDBC;
- tmp.ldap_cid = op->o_connid;
+ if ( op->o_threadctx ) {
+ void *data = NULL;
- /*
- * we have one thread per connection, as I understand --
- * so we do not need locking here
- */
- dbc = avl_find( bi->sql_db_conns, &tmp, backsql_cmp_connid );
- if ( !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 "
- "-- returning NULL\n", 0, 0, 0 );
- return rc;
- }
+ ldap_pvt_thread_pool_getkey( op->o_threadctx,
+ &backsql_db_conn_dummy, &data, NULL );
+ dbh = (SQLHDBC)data;
+
+ } else {
+ dbh = bi->sql_dbh;
}
- 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( bi, dbc->dbh );
+ if ( dbh == SQL_NULL_HDBC ) {
+ rc = backsql_open_db_handle( bi, &dbh );
if ( rc != LDAP_SUCCESS ) {
- ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
- backsql_free_db_conn( op );
return rc;
}
+
+ if ( op->o_threadctx ) {
+ void *data = (void *)dbh;
+
+ ldap_pvt_thread_pool_setkey( op->o_threadctx,
+ &backsql_db_conn_dummy, data,
+ backsql_db_conn_keyfree, NULL, NULL );
+
+ } else {
+ bi->sql_dbh = dbh;
+ }
}
- ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
- *dbh = dbc->dbh;
+ *dbhp = dbh;
Debug( LDAP_DEBUG_TRACE, "<==backsql_get_db_conn()\n", 0, 0, 0 );