]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/sql-wrap.c
Fixup bdb_entry_release now that entry_decode uses two memory blocks
[openldap] / servers / slapd / back-sql / sql-wrap.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
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.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include "slap.h"
18 #include "back-sql.h"
19 #include "sql-types.h"
20 #include "sql-wrap.h"
21 #include "schema-map.h"
22
23 #define MAX_ATTR_LEN 16384
24
25 typedef struct backsql_conn
26 {
27  int ldap_cid;
28  SQLHDBC dbh;
29 }backsql_db_conn;
30
31 int backsql_dummy(void *,void *);
32
33 void backsql_PrintErrors(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth,int rc)
34 {
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  */
39
40  
41  Debug(LDAP_DEBUG_TRACE,"Return code: %d\n", rc,0,0);
42   
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
46        )
47  {
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);
51  }
52 }
53
54 RETCODE backsql_Prepare(SQLHDBC dbh,SQLHSTMT *sth,char* query,int timeout)
55 {
56  RETCODE rc;
57  char drv_name[30];
58  SWORD len;
59  int i;
60  
61  rc=SQLAllocStmt(dbh,sth);
62  if (rc != SQL_SUCCESS)
63   return rc;
64  
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))
69   {
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...
72    */
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)
76    {
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);
79    }
80   }
81  if (timeout>0)
82   {
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)
85     {
86      backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
87     }
88   }
89  
90  /*Debug(LDAP_DEBUG_TRACE,"<==_SQLPrepare() calling SQLPrepare()\n", 0,0,0);*/
91  return SQLPrepare(*sth,query,SQL_NTS);
92 }
93
94 RETCODE backsql_BindParamStr(SQLHSTMT sth,int par_ind,char *str,int maxlen)
95 {
96  RETCODE rc;
97  SQLINTEGER len=SQL_NTS;
98  
99  rc=SQLBindParameter(sth,(SQLUSMALLINT)par_ind,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,
100          (SQLUINTEGER)maxlen,0,(SQLPOINTER)str,(SQLUINTEGER)maxlen,NULL);
101  return rc;
102 }
103
104 RETCODE backsql_BindParamID(SQLHSTMT sth,int par_ind,unsigned long *id)
105 {
106  
107  return SQLBindParameter(sth,(SQLUSMALLINT)par_ind,SQL_PARAM_INPUT,SQL_C_ULONG,SQL_INTEGER,
108          0,0,(SQLPOINTER)id,0,(SQLINTEGER*)NULL);
109 }
110
111 RETCODE backsql_BindRowAsStrings(SQLHSTMT sth,BACKSQL_ROW_NTS *row)
112 {
113  RETCODE rc;
114  SQLCHAR colname[64];
115  SQLSMALLINT name_len,col_type,col_scale,col_null;
116  UDWORD col_prec;
117  int i;
118  
119  if (row == NULL)
120   return SQL_ERROR;
121  
122  /*Debug(LDAP_DEBUG_TRACE,"==> backsql_BindRowAsStrings()\n",0,0,0);*/
123  rc=SQLNumResultCols(sth,&row->ncols);
124  if (rc != SQL_SUCCESS)
125  {
126   /*Debug(LDAP_DEBUG_TRACE,"_SQLBindRowAsStrings(): SQLNumResultCols() failed:\n",0,0,0);*/
127   backsql_PrintErrors(SQL_NULL_HENV,SQL_NULL_HDBC,sth,rc);
128  }
129  else
130  {
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++)
137   {
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)
143    {
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()
150         */
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,
155                                                        &row->is_null[i-1]);
156    }
157    else
158    {
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,
162                                                        &row->is_null[i-1]);
163    }
164   }
165  }
166  /*Debug(LDAP_DEBUG_TRACE,"<== backsql_BindRowAsStrings()\n",0,0,0);*/
167  return rc;
168 }
169
170 RETCODE backsql_FreeRow(BACKSQL_ROW_NTS *row)
171 {
172  int i;
173  
174  if (row->cols == NULL)
175   return SQL_ERROR;
176  for(i=0;i<row->ncols;i++)
177   {
178    free(row->cols[i]);
179   }
180  free(row->col_names);
181  free(row->col_prec);
182  free(row->cols);
183  free(row->is_null);
184  return SQL_SUCCESS;
185 }
186
187 int backsql_cmp_connid(backsql_db_conn *c1,backsql_db_conn *c2)
188 {
189  if (c1->ldap_cid > c2->ldap_cid)
190   return 1; 
191  if (c1->ldap_cid < c2->ldap_cid)
192   return -1; 
193  return 0;
194 }
195
196
197 int backsql_close_db_conn(backsql_db_conn *conn)
198 {
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);
204  return 1;
205 }
206
207 int backsql_init_db_env(backsql_info *si)
208 {
209  RETCODE rc;
210  Debug(LDAP_DEBUG_TRACE,"==>backsql_init_db_env()\n",0,0,0);
211  if ((rc=SQLAllocEnv(&si->db_env)) != SQL_SUCCESS)
212   {
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);
215   }
216  Debug(LDAP_DEBUG_TRACE,"<==backsql_init_db_env()\n",0,0,0);
217  return SQL_SUCCESS;
218 }
219
220 int backsql_free_db_env(backsql_info *si)
221 {
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);*/
224  
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...
227 */
228  Debug(LDAP_DEBUG_TRACE,"<==backsql_free_db_env()\n",0,0,0);
229  return SQL_SUCCESS;
230 }
231
232 backsql_db_conn* backsql_open_db_conn(backsql_info *si,int ldap_cid)
233 {
234  char DBMSName[32]; /* TimesTen*/
235
236  backsql_db_conn *dbc=(backsql_db_conn*)ch_calloc(1,sizeof(backsql_db_conn));
237  int rc;
238  
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)
242   {
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);
245    return NULL;
246   }
247  if ((rc=SQLConnect(dbc->dbh,si->dbname,SQL_NTS,si->dbuser,SQL_NTS,
248                       si->dbpasswd,SQL_NTS) != SQL_SUCCESS))
249   {
250    if (rc != SQL_SUCCESS_WITH_INFO)
251     Debug(LDAP_DEBUG_TRACE,"backsql_open_db_conn: SQLConnect() failed:\n",0,0,0);
252    else
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)
256     return NULL;
257   }
258
259  /* TimesTen : Turn off autocommit.  We must explicitly commit any transactions. */
260
261  SQLSetConnectOption(dbc->dbh, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
262
263  /* See if this connection is to TimesTen.  If it is,
264     remember that fact for later use. */
265
266  si->isTimesTen = 0;        /* Assume until proven otherwise */
267
268  DBMSName[0] = '\0';
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);
275      si->isTimesTen = 1;
276    }
277  }
278  else {
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);
281  }  
282  /* end TimesTen */
283  
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);
289  return dbc;
290 }
291
292 int backsql_free_db_conn(Backend *be,Connection *ldapc)
293 {
294  backsql_info *si=(backsql_info*)be->be_private;
295  backsql_db_conn tmp,*conn;
296  
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
304 */
305  if (conn!=NULL)
306   { 
307    Debug(LDAP_DEBUG_TRACE,"backsql_free_db_conn(): closing db connection\n",0,0,0);
308    backsql_close_db_conn(conn);
309   }
310  Debug(LDAP_DEBUG_TRACE,"<==backsql_free_db_conn()\n",0,0,0);
311  return SQL_SUCCESS;
312 }
313
314 SQLHDBC backsql_get_db_conn(Backend *be,Connection *ldapc)
315 {
316  backsql_info *si=(backsql_info*)be->be_private;
317  backsql_db_conn *dbc;
318  backsql_db_conn tmp;
319  
320  Debug(LDAP_DEBUG_TRACE,"==>backsql_get_db_conn()\n",0,0,0);
321  
322  tmp.ldap_cid=ldapc->c_connid;
323  /*we have one thread per connection, as I understand -- so we do not need
324   * locking here
325 */
326  dbc=(backsql_db_conn*)avl_find(si->db_conns,&tmp,(AVL_CMP)backsql_cmp_connid);
327  if (!dbc)
328   dbc=backsql_open_db_conn(si,ldapc->c_connid);
329  
330  if (!dbc)
331  {
332    Debug(LDAP_DEBUG_TRACE,"backsql_get_db_conn(): could not get connection handle -- returning NULL\n",0,0,0);
333    return NULL;
334  }
335  ldap_pvt_thread_mutex_lock(&si->schema_mutex);
336  if (!si->schema_loaded)
337   {
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);
340   }
341  ldap_pvt_thread_mutex_unlock(&si->schema_mutex);
342  
343  Debug(LDAP_DEBUG_TRACE,"<==backsql_get_db_conn()\n",0,0,0);
344  return dbc->dbh;
345 }
346
347 #endif /* SLAPD_SQL */