2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include "ac/string.h"
16 #include <sys/types.h>
20 #include "sql-types.h"
22 #include "schema-map.h"
24 #define MAX_ATTR_LEN 16384
26 typedef struct backsql_conn {
31 int backsql_dummy( void *, void * );
34 backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc )
36 SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH]; /* msg. buffer */
37 SQLCHAR state[SQL_SQLSTATE_SIZE]; /* statement buf. */
38 SDWORD iSqlCode; /* return code */
39 SWORD len = SQL_MAX_MESSAGE_LENGTH - 1; /* return length */
41 Debug( LDAP_DEBUG_TRACE, "Return code: %d\n", rc, 0, 0 );
44 rc = SQLError( henv, hdbc, sth, state, &iSqlCode, msg,
45 SQL_MAX_MESSAGE_LENGTH - 1, &len );
46 for ( ; BACKSQL_SUCCESS( rc ); ) {
47 Debug( LDAP_DEBUG_TRACE, "Native error code: %d\n",
48 (int)iSqlCode, 0, 0 );
49 Debug( LDAP_DEBUG_TRACE, "SQL engine state: %s\n",
51 Debug( LDAP_DEBUG_TRACE, "Message: %s\n", msg, 0, 0 );
52 rc = SQLError( henv, hdbc, sth, state, &iSqlCode, msg,
53 SQL_MAX_MESSAGE_LENGTH - 1, &len );
58 backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char *query, int timeout )
64 rc = SQLAllocStmt( dbh, sth );
65 if ( rc != SQL_SUCCESS ) {
70 Debug( LDAP_DEBUG_TRACE, "==>_SQLPrepare()\n", 0, 0, 0 );
73 SQLGetInfo( dbh, SQL_DRIVER_NAME, drv_name, 30, &len );
76 Debug( LDAP_DEBUG_TRACE, "_SQLPrepare(): driver name='%s'\n",
80 if ( !strncmp( ldap_pvt_str2upper( drv_name ), "SQLSRV32.DLL", 30 ) ) {
82 * stupid default result set in MS SQL Server
83 * does not support multiple active statements
84 * on the same connection -- so we are trying
85 * to make it not to use default result set...
87 Debug( LDAP_DEBUG_TRACE, "_SQLprepare(): "
88 "enabling MS SQL Server default result "
89 "set workaround\n", 0, 0, 0 );
90 rc = SQLSetStmtOption( *sth, SQL_CONCURRENCY,
92 if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) {
93 Debug( LDAP_DEBUG_TRACE, "_SQLPrepare(): "
94 "SQLSetStmtOption(SQL_CONCURRENCY,SQL_CONCUR_ROWVER) failed:\n",
96 backsql_PrintErrors( SQL_NULL_HENV, dbh, *sth, rc );
101 Debug( LDAP_DEBUG_TRACE, "_SQLprepare(): "
102 "setting query timeout to %d sec.\n",
104 rc = SQLSetStmtOption( *sth, SQL_QUERY_TIMEOUT, timeout );
105 if ( rc != SQL_SUCCESS ) {
106 backsql_PrintErrors( SQL_NULL_HENV, dbh, *sth, rc );
111 Debug( LDAP_DEBUG_TRACE, "<==_SQLPrepare() calling SQLPrepare()\n",
115 return SQLPrepare( *sth, query, SQL_NTS );
120 backsql_BindParamStr( SQLHSTMT sth, int par_ind, char *str, int maxlen )
124 rc = SQLBindParameter( sth, (SQLUSMALLINT)par_ind, SQL_PARAM_INPUT,
125 SQL_C_CHAR, SQL_VARCHAR,
126 (SQLUINTEGER)maxlen, 0, (SQLPOINTER)str,
127 (SQLUINTEGER)maxlen, NULL );
132 backsql_BindParamID( SQLHSTMT sth, int par_ind, unsigned long *id )
134 return SQLBindParameter( sth, (SQLUSMALLINT)par_ind,
135 SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
136 0, 0, (SQLPOINTER)id, 0, (SQLINTEGER*)NULL );
141 backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
144 SQLCHAR colname[ 64 ];
145 SQLSMALLINT name_len, col_type, col_scale, col_null;
154 Debug( LDAP_DEBUG_TRACE, "==> backsql_BindRowAsStrings()\n", 0, 0, 0 );
157 rc = SQLNumResultCols( sth, &row->ncols );
158 if ( rc != SQL_SUCCESS ) {
160 Debug( LDAP_DEBUG_TRACE, "_SQLBindRowAsStrings(): "
161 "SQLNumResultCols() failed:\n", 0, 0, 0 );
164 backsql_PrintErrors( SQL_NULL_HENV, SQL_NULL_HDBC, sth, rc );
167 Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
168 "ncols=%d\n", (int)row->ncols, 0, 0 );
171 row->col_names = (char **)ch_calloc( row->ncols,
173 row->cols = (char **)ch_calloc( row->ncols, sizeof( char * ) );
174 row->col_prec = (UDWORD *)ch_calloc( row->ncols,
176 row->is_null = (SQLINTEGER *)ch_calloc( row->ncols,
177 sizeof( SQLINTEGER ) );
178 for ( i = 1; i <= row->ncols; i++ ) {
179 rc = SQLDescribeCol( sth, (SQLSMALLINT)i, &colname[ 0 ],
180 (SQLUINTEGER)sizeof( colname ) - 1,
181 &name_len, &col_type,
182 &col_prec, &col_scale, &col_null );
183 row->col_names[ i - 1 ] = ch_strdup( colname );
185 Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
186 "col_name=%s, col_prec[%d]=%d\n",
187 colname, (int)i, (int)col_prec );
189 if ( col_type == SQL_LONGVARCHAR
190 || col_type == SQL_LONGVARBINARY) {
192 row->cols[ i - 1 ] = NULL;
193 row->col_prec[ i - 1 ] = -1;
196 * such fields must be handled
197 * in some other way since they return 2G
198 * as their precision (at least it does so
199 * with MS SQL Server w/native driver)
200 * for now, we just set fixed precision
201 * for such fields - dirty hack, but...
202 * no time to deal with SQLGetData()
205 col_prec = MAX_ATTR_LEN;
206 row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
207 row->col_prec[ i - 1 ] = col_prec;
208 rc = SQLBindCol( sth, (SQLUSMALLINT)i,
210 (SQLPOINTER)row->cols[ i - 1 ],
212 &row->is_null[ i - 1 ] );
214 row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
215 row->col_prec[ i - 1 ] = col_prec;
216 rc = SQLBindCol( sth, (SQLUSMALLINT)i,
218 (SQLPOINTER)row->cols[ i - 1 ],
220 &row->is_null[ i - 1 ] );
225 Debug( LDAP_DEBUG_TRACE, "<== backsql_BindRowAsStrings()\n", 0, 0, 0 );
232 backsql_FreeRow( BACKSQL_ROW_NTS *row )
236 if ( row->cols == NULL ) {
240 for ( i = 0; i < row->ncols; i++ ) {
242 * FIXME: we need to free the col_names as well, don't we?
244 free( row->cols[ i ] );
247 free( row->col_names );
248 free( row->col_prec );
250 free( row->is_null );
256 backsql_cmp_connid( backsql_db_conn *c1, backsql_db_conn *c2 )
258 if ( c1->ldap_cid > c2->ldap_cid ) {
262 if ( c1->ldap_cid < c2->ldap_cid ) {
270 backsql_close_db_conn( backsql_db_conn *conn )
272 Debug( LDAP_DEBUG_TRACE, "==>backsql_close_db_conn()\n", 0, 0, 0 );
275 SQLTransact( SQL_NULL_HENV, conn->dbh, SQL_COMMIT );
276 SQLDisconnect( conn->dbh );
277 SQLFreeConnect( conn->dbh );
278 Debug( LDAP_DEBUG_TRACE, "<==backsql_close_db_conn()\n", 0, 0, 0 );
283 backsql_init_db_env( backsql_info *si )
287 Debug( LDAP_DEBUG_TRACE, "==>backsql_init_db_env()\n", 0, 0, 0 );
288 rc = SQLAllocEnv( &si->db_env );
289 if ( rc != SQL_SUCCESS ) {
290 Debug( LDAP_DEBUG_TRACE, "init_db_env: SQLAllocEnv failed:\n",
292 backsql_PrintErrors( SQL_NULL_HENV, SQL_NULL_HDBC,
295 Debug( LDAP_DEBUG_TRACE, "<==backsql_init_db_env()\n", 0, 0, 0 );
300 backsql_free_db_env( backsql_info *si )
302 Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_env()\n", 0, 0, 0 );
304 Debug( LDAP_DEBUG_TRACE, "free_db_env(): delete AVL tree here!!!\n",
309 * stop, if frontend waits for all threads to shutdown
310 * before calling this -- then what are we going to delete??
311 * everything is already deleted...
313 Debug( LDAP_DEBUG_TRACE, "<==backsql_free_db_env()\n", 0, 0, 0 );
318 backsql_open_db_conn( backsql_info *si, int ldap_cid )
322 backsql_db_conn *dbc;
325 Debug( LDAP_DEBUG_TRACE, "==>backsql_open_db_conn()\n", 0, 0, 0 );
326 dbc = (backsql_db_conn *)ch_calloc( 1, sizeof( backsql_db_conn ) );
327 dbc->ldap_cid = ldap_cid;
328 rc = SQLAllocConnect( si->db_env, &dbc->dbh );
329 if (!BACKSQL_SUCCESS( rc ) ) {
330 Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
331 "SQLAllocConnect() failed:\n", 0, 0, 0 );
332 backsql_PrintErrors( si->db_env, SQL_NULL_HDBC,
337 rc = SQLConnect( dbc->dbh, si->dbname, SQL_NTS, si->dbuser,
338 SQL_NTS, si->dbpasswd, SQL_NTS );
339 if ( rc != SQL_SUCCESS ) {
340 Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
341 "SQLConnect() to database '%s' as user '%s' "
342 "%s:\n", si->dbname, si->dbuser,
343 rc == SQL_SUCCESS_WITH_INFO ?
344 "succeeded with info" : "failed" );
345 backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc );
346 if ( rc != SQL_SUCCESS_WITH_INFO ) {
352 * TimesTen : Turn off autocommit. We must explicitly
353 * commit any transactions.
355 SQLSetConnectOption( dbc->dbh, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF );
358 * See if this connection is to TimesTen. If it is,
359 * remember that fact for later use.
361 si->isTimesTen = 0; /* Assume until proven otherwise */
362 DBMSName[ 0 ] = '\0';
363 rc = SQLGetInfo( dbc->dbh, SQL_DBMS_NAME, (PTR)&DBMSName,
364 sizeof( DBMSName ), NULL );
365 if ( rc == SQL_SUCCESS ) {
366 if ( strcmp( DBMSName, "TimesTen" ) == 0 ||
367 strcmp( DBMSName, "Front-Tier" ) == 0 ) {
368 Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
369 "TimesTen database!\n", 0, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn: "
374 "SQLGetInfo() failed:\n", 0, 0, 0 );
375 backsql_PrintErrors( si->db_env, dbc->dbh, SQL_NULL_HENV, rc );
379 Debug( LDAP_DEBUG_TRACE, "backsql_open_db_conn(): "
380 "connected, adding to tree\n", 0, 0, 0 );
381 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
382 avl_insert( &si->db_conns, dbc, (AVL_CMP)backsql_cmp_connid,
384 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
385 Debug( LDAP_DEBUG_TRACE, "<==backsql_open_db_conn()\n", 0, 0, 0 );
391 backsql_free_db_conn( Backend *be, Connection *ldapc )
393 backsql_info *si = (backsql_info *)be->be_private;
394 backsql_db_conn tmp, *conn;
396 Debug( LDAP_DEBUG_TRACE, "==>backsql_free_db_conn()\n", 0, 0, 0 );
397 tmp.ldap_cid = ldapc->c_connid;
398 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
399 conn = (backsql_db_conn *)avl_delete( &si->db_conns, &tmp,
400 (AVL_CMP)backsql_cmp_connid );
401 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
404 * we have one thread per connection, as I understand -- so we can
405 * get this out of critical section
407 if ( conn != NULL ) {
408 Debug( LDAP_DEBUG_TRACE, "backsql_free_db_conn(): "
409 "closing db connection\n", 0, 0, 0 );
410 backsql_close_db_conn( conn );
412 Debug( LDAP_DEBUG_TRACE, "<==backsql_free_db_conn()\n", 0, 0, 0 );
417 backsql_get_db_conn( Backend *be, Connection *ldapc )
419 backsql_info *si = (backsql_info *)be->be_private;
420 backsql_db_conn *dbc;
423 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_db_conn()\n", 0, 0, 0 );
425 tmp.ldap_cid = ldapc->c_connid;
428 * we have one thread per connection, as I understand --
429 * so we do not need locking here
431 dbc = (backsql_db_conn *)avl_find( si->db_conns, &tmp,
432 (AVL_CMP)backsql_cmp_connid );
434 dbc = backsql_open_db_conn( si, ldapc->c_connid );
438 Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
439 "could not get connection handle -- returning NULL\n",
441 return SQL_NULL_HDBC;
443 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
444 if ( !si->schema_loaded ) {
445 Debug( LDAP_DEBUG_TRACE, "backsql_get_db_conn(): "
446 "first call -- reading schema map\n", 0, 0, 0 );
447 backsql_load_schema_map( si, dbc->dbh );
449 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
451 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_db_conn()\n", 0, 0, 0 );
455 #endif /* SLAPD_SQL */