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.
16 #include <sys/types.h>
19 #include "sql-types.h"
21 #include "schema-map.h"
23 #define MAX_ATTR_LEN 16384
25 typedef struct backsql_conn
31 int backsql_dummy(void *,void *);
33 void backsql_PrintErrors(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth,int rc)
35 SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH]; /* msg. buffer */
36 SQLCHAR state[SQL_SQLSTATE_SIZE]; /* statement buf. */
37 SDWORD iSqlCode; /* return code */
38 SWORD len=SQL_MAX_MESSAGE_LENGTH-1; /* return length */
41 Debug(LDAP_DEBUG_TRACE,"Return code: %d\n", rc,0,0);
43 while((rc=SQLError(henv,hdbc,sth,state,&iSqlCode,msg,
44 SQL_MAX_MESSAGE_LENGTH - 1, &len)) == SQL_SUCCESS
45 || rc == SQL_SUCCESS_WITH_INFO
48 Debug(LDAP_DEBUG_TRACE,"Native error code: %d\n",(int) iSqlCode,0,0);
49 Debug(LDAP_DEBUG_TRACE,"SQL engine state: %s\n", state,0,0);
50 Debug(LDAP_DEBUG_TRACE,"Message: %s\n",msg,0,0);
54 RETCODE backsql_Prepare(SQLHDBC dbh,SQLHSTMT *sth,char* query,int timeout)
61 rc=SQLAllocStmt(dbh,sth);
62 if (rc != SQL_SUCCESS)
65 /*Debug(LDAP_DEBUG_TRACE,"==>_SQLPrepare()\n", 0,0,0);*/
66 SQLGetInfo(dbh,SQL_DRIVER_NAME,drv_name,30,&len);
67 /*Debug(LDAP_DEBUG_TRACE,"_SQLPrepare(): driver name='%s'\n", drv_name,0,0);*/
68 if (!strncmp(ldap_pvt_str2upper(drv_name),"SQLSRV32.DLL",30))
70 /*stupid default result set in MS SQL Server does not support multiple active statements
71 *on the same connection -- so we are trying to make it not to use default result set...
73 Debug(LDAP_DEBUG_TRACE,"_SQLprepare(): enabling MS SQL Server default result set workaround\n", 0,0,0);
74 rc=SQLSetStmtOption(*sth,SQL_CONCURRENCY,SQL_CONCUR_ROWVER);
75 if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
77 Debug(LDAP_DEBUG_TRACE,"_SQLPrepare(): SQLSetStmtOption(SQL_CONCURRENCY,SQL_CONCUR_ROWVER) failed:\n", 0,0,0);
78 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
83 Debug(LDAP_DEBUG_TRACE,"_SQLprepare(): setting query timeout to %d sec.\n", timeout,0,0);
84 if ((rc=SQLSetStmtOption(*sth,SQL_QUERY_TIMEOUT,timeout)) != SQL_SUCCESS)
86 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
90 /*Debug(LDAP_DEBUG_TRACE,"<==_SQLPrepare() calling SQLPrepare()\n", 0,0,0);*/
91 return SQLPrepare(*sth,query,SQL_NTS);
94 RETCODE backsql_BindParamStr(SQLHSTMT sth,int par_ind,char *str,int maxlen)
97 SQLINTEGER len=SQL_NTS;
99 rc=SQLBindParameter(sth,(SQLUSMALLINT)par_ind,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,
100 (SQLUINTEGER)maxlen,0,(SQLPOINTER)str,(SQLUINTEGER)maxlen,NULL);
104 RETCODE backsql_BindParamID(SQLHSTMT sth,int par_ind,unsigned long *id)
107 return SQLBindParameter(sth,(SQLUSMALLINT)par_ind,SQL_PARAM_INPUT,SQL_C_ULONG,SQL_INTEGER,
108 0,0,(SQLPOINTER)id,0,(SQLINTEGER*)NULL);
111 RETCODE backsql_BindRowAsStrings(SQLHSTMT sth,BACKSQL_ROW_NTS *row)
115 SQLSMALLINT name_len,col_type,col_scale,col_null;
122 /*Debug(LDAP_DEBUG_TRACE,"==> backsql_BindRowAsStrings()\n",0,0,0);*/
123 rc=SQLNumResultCols(sth,&row->ncols);
124 if (rc != SQL_SUCCESS)
126 /*Debug(LDAP_DEBUG_TRACE,"_SQLBindRowAsStrings(): SQLNumResultCols() failed:\n",0,0,0);*/
127 backsql_PrintErrors(SQL_NULL_HENV,SQL_NULL_HDBC,sth,rc);
131 /*Debug(LDAP_DEBUG_TRACE,"backsql_BindRowAsStrings: ncols=%d\n",(int)row->ncols,0,0);*/
132 row->col_names=(char**)ch_calloc(row->ncols,sizeof(char*));
133 row->cols=(char**)ch_calloc(row->ncols,sizeof(char*));
134 row->col_prec=(UDWORD*)ch_calloc(row->ncols,sizeof(UDWORD));
135 row->is_null=(SQLINTEGER*)ch_calloc(row->ncols,sizeof(SQLINTEGER));
136 for (i=1;i<=row->ncols;i++)
138 rc=SQLDescribeCol(sth,(SQLSMALLINT)i,&colname[0],(SQLUINTEGER)sizeof(colname)-1,&name_len,&col_type,
139 (UDWORD*) &col_prec,&col_scale,&col_null);
140 row->col_names[i-1]=ch_strdup(colname);
141 /*Debug(LDAP_DEBUG_TRACE,"backsql_BindRowAsStrings: col_name=%s, col_prec[%d]=%d\n",colname,(int)i,(int)col_prec);*/
142 if (col_type == SQL_LONGVARCHAR || col_type== SQL_LONGVARBINARY)
144 /*row->cols[i-1]=NULL;
145 *row->col_prec[i-1]=-1;
146 *such fields must be handled in some other way since they return 2G
147 *as their precision (at least it does so with MS SQL Server w/native driver)
148 *for now, we just set fixed precision for such fields - dirty hack, but...
149 *no time to deal with SQLGetData()
151 col_prec=MAX_ATTR_LEN;
152 row->cols[i-1]=(char*)ch_calloc((col_prec+1),sizeof(char));
153 row->col_prec[i-1]=col_prec;
154 rc=SQLBindCol(sth,(SQLUSMALLINT)i,SQL_C_CHAR,(SQLPOINTER)row->cols[i-1],col_prec+1,
159 row->cols[i-1]=(char*)ch_calloc((col_prec+1),sizeof(char));
160 row->col_prec[i-1]=col_prec;
161 rc=SQLBindCol(sth,(SQLUSMALLINT)i,SQL_C_CHAR,(SQLPOINTER)row->cols[i-1],col_prec+1,
166 /*Debug(LDAP_DEBUG_TRACE,"<== backsql_BindRowAsStrings()\n",0,0,0);*/
170 RETCODE backsql_FreeRow(BACKSQL_ROW_NTS *row)
174 if (row->cols == NULL)
176 for(i=0;i<row->ncols;i++)
180 free(row->col_names);
187 int backsql_cmp_connid(backsql_db_conn *c1,backsql_db_conn *c2)
189 if (c1->ldap_cid > c2->ldap_cid)
191 if (c1->ldap_cid < c2->ldap_cid)
197 int backsql_close_db_conn(backsql_db_conn *conn)
199 Debug(LDAP_DEBUG_TRACE,"==>backsql_close_db_conn()\n",0,0,0);
200 SQLTransact(NULL, conn->dbh, SQL_COMMIT); /* TimesTen */
201 SQLDisconnect(conn->dbh);
202 SQLFreeConnect(conn->dbh);
203 Debug(LDAP_DEBUG_TRACE,"<==backsql_close_db_conn()\n",0,0,0);
207 int backsql_init_db_env(backsql_info *si)
210 Debug(LDAP_DEBUG_TRACE,"==>backsql_init_db_env()\n",0,0,0);
211 if ((rc=SQLAllocEnv(&si->db_env)) != SQL_SUCCESS)
213 Debug(LDAP_DEBUG_TRACE,"init_db_env: SQLAllocEnv failed:\n",0,0,0);
214 backsql_PrintErrors(SQL_NULL_HENV,SQL_NULL_HDBC,SQL_NULL_HENV,rc);
216 Debug(LDAP_DEBUG_TRACE,"<==backsql_init_db_env()\n",0,0,0);
220 int backsql_free_db_env(backsql_info *si)
222 Debug(LDAP_DEBUG_TRACE,"==>backsql_free_db_env()\n",0,0,0);
223 /*Debug(LDAP_DEBUG_TRACE,"free_db_env(): delete AVL tree here!!!\n",0,0,0);*/
225 /*stop, if frontend waits for all threads to shutdown before calling this --
226 *then what we are going to delete?? everything is deleted already...
228 Debug(LDAP_DEBUG_TRACE,"<==backsql_free_db_env()\n",0,0,0);
232 backsql_db_conn* backsql_open_db_conn(backsql_info *si,int ldap_cid)
234 char DBMSName[32]; /* TimesTen*/
236 backsql_db_conn *dbc=(backsql_db_conn*)ch_calloc(1,sizeof(backsql_db_conn));
239 Debug(LDAP_DEBUG_TRACE,"==>backsql_open_db_conn()\n",0,0,0);
240 dbc->ldap_cid=ldap_cid;
241 if ((rc=SQLAllocConnect(si->db_env,&dbc->dbh)) != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
243 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: SQLAllocConnect() failed:\n",0,0,0);
244 backsql_PrintErrors(si->db_env,SQL_NULL_HDBC,SQL_NULL_HENV,rc);
247 if ((rc=SQLConnect(dbc->dbh,si->dbname,SQL_NTS,si->dbuser,SQL_NTS,
248 si->dbpasswd,SQL_NTS) != SQL_SUCCESS))
250 if (rc != SQL_SUCCESS_WITH_INFO)
251 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: SQLConnect() failed:\n",0,0,0);
253 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: SQLConnect() succeeded with info:\n",0,0,0);
254 backsql_PrintErrors(si->db_env,dbc->dbh,SQL_NULL_HENV,rc);
255 if (rc != SQL_SUCCESS_WITH_INFO)
259 /* TimesTen : Turn off autocommit. We must explicitly commit any transactions. */
261 SQLSetConnectOption(dbc->dbh, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
263 /* See if this connection is to TimesTen. If it is,
264 remember that fact for later use. */
266 si->isTimesTen = 0; /* Assume until proven otherwise */
269 rc = SQLGetInfo(dbc->dbh, SQL_DBMS_NAME, (PTR) &DBMSName,
270 sizeof(DBMSName), NULL);
271 if (rc == SQL_SUCCESS) {
272 if (strcmp(DBMSName, "TimesTen") == 0 ||
273 strcmp(DBMSName, "Front-Tier") == 0) {
274 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: TimesTen database!\n",0,0,0);
279 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: SQLGetInfo() failed:\n",0,0,0);
280 backsql_PrintErrors(si->db_env,dbc->dbh,SQL_NULL_HENV,rc);
284 Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn(): connected, adding to tree\n",0,0,0);
285 ldap_pvt_thread_mutex_lock(&si->dbconn_mutex);
286 avl_insert(&si->db_conns,dbc,(AVL_CMP)backsql_cmp_connid,backsql_dummy);
287 ldap_pvt_thread_mutex_unlock(&si->dbconn_mutex);
288 Debug(LDAP_DEBUG_TRACE,"<==backsql_open_db_conn()\n",0,0,0);
292 int backsql_free_db_conn(Backend *be,Connection *ldapc)
294 backsql_info *si=(backsql_info*)be->be_private;
295 backsql_db_conn tmp,*conn;
297 Debug(LDAP_DEBUG_TRACE,"==>backsql_free_db_conn()\n",0,0,0);
298 tmp.ldap_cid=ldapc->c_connid;
299 ldap_pvt_thread_mutex_lock(&si->dbconn_mutex);
300 conn=(backsql_db_conn*)avl_delete(&si->db_conns,&tmp,(AVL_CMP)backsql_cmp_connid);
301 ldap_pvt_thread_mutex_unlock(&si->dbconn_mutex);
302 /*we have one thread per connection, as I understand -- so we can
303 *get this out of critical section
307 Debug(LDAP_DEBUG_TRACE,"backsql_free_db_conn(): closing db connection\n",0,0,0);
308 backsql_close_db_conn(conn);
310 Debug(LDAP_DEBUG_TRACE,"<==backsql_free_db_conn()\n",0,0,0);
314 SQLHDBC backsql_get_db_conn(Backend *be,Connection *ldapc)
316 backsql_info *si=(backsql_info*)be->be_private;
317 backsql_db_conn *dbc;
320 Debug(LDAP_DEBUG_TRACE,"==>backsql_get_db_conn()\n",0,0,0);
322 tmp.ldap_cid=ldapc->c_connid;
323 /*we have one thread per connection, as I understand -- so we do not need
326 dbc=(backsql_db_conn*)avl_find(si->db_conns,&tmp,(AVL_CMP)backsql_cmp_connid);
328 dbc=backsql_open_db_conn(si,ldapc->c_connid);
332 Debug(LDAP_DEBUG_TRACE,"backsql_get_db_conn(): could not get connection handle -- returning NULL\n",0,0,0);
335 ldap_pvt_thread_mutex_lock(&si->schema_mutex);
336 if (!si->schema_loaded)
338 Debug(LDAP_DEBUG_TRACE,"backsql_get_db_conn(): first call -- reading schema map\n",0,0,0);
339 backsql_load_schema_map(si,dbc->dbh);
341 ldap_pvt_thread_mutex_unlock(&si->schema_mutex);
343 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_db_conn()\n",0,0,0);
347 #endif /* SLAPD_SQL */