]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/schema-map.c
trace illegal condition in backsql_strfcat
[openldap] / servers / slapd / back-sql / schema-map.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 <sys/types.h>
16 #include <string.h>
17 #include "slap.h"
18 #include "lber_pvt.h"
19 #include "ldap_pvt.h"
20 #include "back-sql.h"
21 #include "sql-wrap.h"
22 #include "schema-map.h"
23 #include "util.h"
24
25 /*
26  * Uses the pointer to the ObjectClass structure
27  */
28 static int
29 backsql_cmp_oc( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
30 {
31         return ( m1->oc < m2->oc ? -1 : ( m1->oc > m2->oc ? 1 : 0 ) );
32 }
33
34 static int
35 backsql_cmp_oc_id( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
36 {
37         return ( m1->id < m2->id ? -1 : ( m1->id > m2->id ? 1 : 0 ) );
38 }
39
40 /*
41  * Uses the pointer to the AttributeDescription structure
42  */
43 static int
44 backsql_cmp_attr( backsql_at_map_rec *m1, backsql_at_map_rec *m2 )
45 {
46         return ( m1->ad < m2->ad ? -1 : ( m1->ad > m2->ad ? 1 : 0 ) );
47 }
48
49 static int
50 backsql_make_attr_query( 
51         backsql_oc_map_rec      *oc_map,
52         backsql_at_map_rec      *at_map )
53 {
54         struct berval   tmps = BER_BVNULL;
55         ber_len_t       tmpslen = 0;
56
57         backsql_strfcat( &tmps, &tmpslen, "lblblblbcbl", 
58                         (ber_len_t)sizeof( "SELECT " ) - 1, "SELECT ", 
59                         &at_map->sel_expr, 
60                         (ber_len_t)sizeof( " AS " ) - 1, " AS ", 
61                         &at_map->ad->ad_cname,
62                         (ber_len_t)sizeof( " FROM " ) - 1, " FROM ", 
63                         &at_map->from_tbls, 
64                         (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ", 
65                         &oc_map->keytbl,
66                         '.', 
67                         &oc_map->keycol,
68                         (ber_len_t)sizeof( "=?" ) - 1, "=?" );
69
70         if ( at_map->join_where.bv_val != NULL ) {
71                 backsql_strfcat( &tmps, &tmpslen, "lb",
72                                 (ber_len_t)sizeof( " AND ") - 1, " AND ", 
73                                 &at_map->join_where );
74         }
75
76         at_map->query = tmps.bv_val;
77         
78         return 0;
79 }
80
81 static int
82 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
83 {
84         backsql_at_map_rec      *at_map;
85         char                    s[ 30 ];
86         ber_len_t               len, slen;
87         
88
89         snprintf( s, sizeof( s ), "%ld", oc_map->id );
90         slen = strlen( s );
91
92         at_map = (backsql_at_map_rec *)ch_calloc(1, 
93                         sizeof( backsql_at_map_rec ) );
94         at_map->ad = slap_schema.si_ad_objectClass;
95         ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1, &at_map->sel_expr );
96         ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1, 
97                         &at_map->from_tbls );
98         len = at_map->from_tbls.bv_len + 1;
99         backsql_merge_from_clause( &at_map->from_tbls, &len, &oc_map->keytbl );
100
101         len = 0;
102         at_map->join_where.bv_val = NULL;
103         at_map->join_where.bv_len = 0;
104         backsql_strfcat( &at_map->join_where, &len, "lbcbll",
105                         (ber_len_t)sizeof( "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=" ) - 1,
106                                 "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=",
107                         &oc_map->keytbl, 
108                         '.', 
109                         &oc_map->keycol,
110                         (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1, 
111                                 " and ldap_entries.oc_map_id=", 
112                         slen, s );
113
114         at_map->add_proc = NULL;
115         at_map->delete_proc = NULL;
116         at_map->param_order = 0;
117         at_map->expect_return = 0;
118         backsql_make_attr_query( oc_map, at_map );
119         avl_insert( &oc_map->attrs, at_map, 
120                         (AVL_CMP)backsql_cmp_attr, NULL );
121
122         at_map = (backsql_at_map_rec *)ch_calloc( 1, 
123                         sizeof( backsql_at_map_rec ) );
124         at_map->ad = slap_schema.si_ad_ref;
125         ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->sel_expr );
126         ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->from_tbls );
127         len = at_map->from_tbls.bv_len + 1;
128         backsql_merge_from_clause( &at_map->from_tbls, &len, &oc_map->keytbl );
129
130         len = 0;
131         at_map->join_where.bv_val = NULL;
132         at_map->join_where.bv_len = 0;
133         backsql_strfcat( &at_map->join_where, &len, "lbcbll",
134                         (ber_len_t)sizeof( "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=" ) - 1,
135                                 "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=",
136                         &oc_map->keytbl, 
137                         '.', 
138                         &oc_map->keycol,
139                         (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1, 
140                                 " and ldap_entries.oc_map_id=", 
141                         slen, s );
142
143         at_map->add_proc = NULL;
144         at_map->delete_proc = NULL;
145         at_map->param_order = 0;
146         at_map->expect_return = 0;
147         backsql_make_attr_query( oc_map, at_map );
148         avl_insert( &oc_map->attrs, at_map, 
149                         (AVL_CMP)backsql_cmp_attr, NULL );
150
151         return 1;
152 }
153
154 int
155 backsql_load_schema_map( backsql_info *si, SQLHDBC dbh )
156 {
157         SQLHSTMT                oc_sth, at_sth;
158         RETCODE                 rc;
159         BACKSQL_ROW_NTS         oc_row, at_row;
160         unsigned long           oc_id;
161         backsql_oc_map_rec      *oc_map;
162         backsql_at_map_rec      *at_map;
163
164         Debug( LDAP_DEBUG_TRACE, "==>load_schema_map()\n", 0, 0, 0 );
165
166         /* 
167          * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
168          */
169         if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( si ) ) {
170                 rc = backsql_Prepare( dbh, &oc_sth, 
171                                 backsql_check_dn_ru_query, 0 );
172                 if ( rc == SQL_SUCCESS ) {
173                         /* Yes, the field exists */
174                         si->bsql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
175                         Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
176                                 "in the schema\n", 0, 0, 0 );
177                 } else {
178                         /* No such field exists */
179                         si->bsql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
180                 }
181
182                 SQLFreeStmt( oc_sth, SQL_DROP );
183         }
184
185
186         rc = backsql_Prepare( dbh, &oc_sth, si->oc_query, 0 );
187         if ( rc != SQL_SUCCESS ) {
188                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
189                         "error preparing oc_query: '%s'\n", 
190                         si->oc_query, 0, 0 );
191                 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
192                 return LDAP_OTHER;
193         }
194         Debug( LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n", 
195                         si->at_query, 0, 0 );
196
197         rc = backsql_Prepare( dbh, &at_sth, si->at_query, 0 );
198         if ( rc != SQL_SUCCESS ) {
199                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
200                         "error preparing at_query: '%s'\n", 
201                         si->at_query, 0, 0 );
202                 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
203                 return LDAP_OTHER;
204         }
205
206         rc = backsql_BindParamID( at_sth, 1, &oc_id );
207         if ( rc != SQL_SUCCESS ) {
208                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
209                         "error binding param for at_query: \n", 0, 0, 0 );
210                 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
211                 return LDAP_OTHER;
212         }
213
214         rc = SQLExecute( oc_sth );
215         if ( rc != SQL_SUCCESS ) {
216                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
217                         "error executing oc_query: \n", 0, 0, 0 );
218                 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
219                 return LDAP_OTHER;
220         }
221
222         backsql_BindRowAsStrings( oc_sth, &oc_row );
223         rc = SQLFetch( oc_sth );
224         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( oc_sth ) ) {
225                 int     colnum;
226
227                 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
228                                 sizeof( backsql_oc_map_rec ) );
229
230                 oc_map->id = strtol( oc_row.cols[ 0 ], NULL, 0 );
231
232                 oc_map->oc = oc_find( oc_row.cols[ 1 ] );
233                 if ( oc_map->oc == NULL ) {
234                         Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
235                                 "objectClass '%s' is not defined in schema\n", 
236                                 oc_row.cols[ 1 ], 0, 0 );
237                         return LDAP_OTHER;      /* undefined objectClass ? */
238                 }
239                 
240                 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->keytbl );
241                 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->keycol );
242                 oc_map->create_proc = ( oc_row.is_null[ 4 ] < 0 ) ? NULL 
243                         : ch_strdup( oc_row.cols[ 4 ] );
244
245                 colnum = 5;
246                 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
247                         colnum = 6;
248                         oc_map->create_keyval = ( oc_row.is_null[ 5 ] < 0 ) 
249                                 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
250                 }
251                 oc_map->delete_proc = ( oc_row.is_null[ colnum ] < 0 ) ? NULL 
252                         : ch_strdup( oc_row.cols[ colnum ] );
253                 oc_map->expect_return = strtol( oc_row.cols[ colnum + 1 ], 
254                                 NULL, 0 );
255
256                 /*
257                  * FIXME: first attempt to check for offending
258                  * instructions in {create|delete}_proc
259                  */
260
261                 oc_map->attrs = NULL;
262                 avl_insert( &si->oc_by_oc, oc_map,
263                                 (AVL_CMP)backsql_cmp_oc, NULL );
264                 avl_insert( &si->oc_by_id, oc_map,
265                                 (AVL_CMP)backsql_cmp_oc_id, NULL );
266                 oc_id = oc_map->id;
267                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
268                         "objectClass '%s': keytbl='%s' keycol='%s'\n",
269                         BACKSQL_OC_NAME( oc_map ),
270                         oc_map->keytbl.bv_val, oc_map->keycol.bv_val );
271                 if ( oc_map->create_proc ) {
272                         Debug( LDAP_DEBUG_TRACE, "create_proc='%s'\n",
273                                 oc_map->create_proc, 0, 0 );
274                 }
275                 if ( oc_map->create_keyval ) {
276                         Debug( LDAP_DEBUG_TRACE, "create_keyval='%s'\n",
277                                 oc_map->create_keyval, 0, 0 );
278                 }
279                 if ( oc_map->delete_proc ) {
280                         Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n", 
281                                 oc_map->delete_proc, 0, 0 );
282                 }
283                 Debug( LDAP_DEBUG_TRACE, "expect_return: "
284                         "add=%d, del=%d; attributes:\n",
285                         BACKSQL_IS_ADD( oc_map->expect_return ), 
286                         BACKSQL_IS_DEL( oc_map->expect_return ), 0 );
287
288                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
289                         "autoadding 'objectClass' and 'ref' mappings\n",
290                         0, 0, 0 );
291                 backsql_add_sysmaps( oc_map );
292                 rc = SQLExecute( at_sth );
293                 if ( rc != SQL_SUCCESS ) {
294                         Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
295                                 "error executing at_query: \n", 0, 0, 0 );
296                         backsql_PrintErrors( SQL_NULL_HENV, dbh, at_sth, rc );
297                         return LDAP_OTHER;
298                 }
299
300                 backsql_BindRowAsStrings( at_sth, &at_row );
301                 rc = SQLFetch( at_sth );
302                 for ( ; BACKSQL_SUCCESS(rc); rc = SQLFetch( at_sth ) ) {
303                         const char      *text = NULL;
304                         struct berval   bv;
305                         ber_len_t       tmpslen;
306
307                         Debug( LDAP_DEBUG_TRACE, "********'%s'\n",
308                                 at_row.cols[ 0 ], 0, 0 );
309                         Debug( LDAP_DEBUG_TRACE, 
310                                 "name='%s',sel_expr='%s' from='%s'",
311                                 at_row.cols[ 0 ], at_row.cols[ 1 ],
312                                 at_row.cols[ 2 ] );
313                         Debug( LDAP_DEBUG_TRACE, 
314                                 "join_where='%s',add_proc='%s'",
315                                 at_row.cols[ 3 ], at_row.cols[ 4 ], 0 );
316                         Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
317                                         at_row.cols[ 5 ], 0, 0 );
318                         /* TimesTen */
319                         Debug( LDAP_DEBUG_TRACE, "sel_expr_u='%s'\n",
320                                         at_row.cols[ 8 ], 0, 0 );
321                         at_map = (backsql_at_map_rec *)ch_calloc( 1,
322                                         sizeof( backsql_at_map_rec ) );
323                         rc = slap_str2ad( at_row.cols[ 0 ], 
324                                         &at_map->ad, &text );
325                         if ( rc != LDAP_SUCCESS ) {
326                                 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
327                                         "attribute '%s' for objectClass '%s' "
328                                         "is not defined in schema: %s\n", 
329                                         at_map->ad->ad_cname.bv_val, 
330                                         BACKSQL_OC_NAME( oc_map ), text );
331                                 return LDAP_CONSTRAINT_VIOLATION;
332                         }
333                                 
334                         ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->sel_expr );
335                         if ( at_row.is_null[ 8 ] < 0 ) {
336                                 at_map->sel_expr_u.bv_val = NULL;
337                                 at_map->sel_expr_u.bv_len = 0;
338                         } else {
339                                 ber_str2bv( at_row.cols[ 8 ], 0, 1, 
340                                                 &at_map->sel_expr_u );
341                         }
342                         tmpslen = 0;
343                         ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
344                         backsql_merge_from_clause( &at_map->from_tbls, 
345                                         &tmpslen, &bv );
346                         if ( at_row.is_null[ 3 ] < 0 ) {
347                                 at_map->join_where.bv_val = NULL;
348                                 at_map->join_where.bv_len = 0;
349                         } else {
350                                 ber_str2bv( at_row.cols[ 3 ], 0, 1, 
351                                                 &at_map->join_where );
352                         }
353                         at_map->add_proc = ( at_row.is_null[ 4 ] < 0 ) ? NULL
354                                 : ch_strdup( at_row.cols[4] );
355                         at_map->delete_proc = ( at_row.is_null[ 5 ] < 0 ) ? NULL
356                                 : ch_strdup( at_row.cols[ 5 ] );
357                         at_map->param_order = strtol( at_row.cols[ 6 ], 
358                                         NULL, 0 );
359                         at_map->expect_return = strtol( at_row.cols[ 7 ],
360                                         NULL, 0 );
361                         backsql_make_attr_query( oc_map, at_map );
362                         Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
363                                 "preconstructed query '%s'\n",
364                                 at_map->query, 0, 0 );
365                         avl_insert( &oc_map->attrs, at_map, 
366                                         (AVL_CMP)backsql_cmp_attr, NULL );
367                 }
368                 backsql_FreeRow( &at_row );
369                 SQLFreeStmt( at_sth, SQL_CLOSE );
370         }
371         backsql_FreeRow( &oc_row );
372         SQLFreeStmt( at_sth, SQL_DROP );
373         SQLFreeStmt( oc_sth, SQL_DROP );
374         si->bsql_flags |= BSQLF_SCHEMA_LOADED;
375         Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
376         return LDAP_SUCCESS;
377 }
378
379 backsql_oc_map_rec *
380 backsql_oc2oc( backsql_info *si, ObjectClass *oc )
381 {
382         backsql_oc_map_rec      tmp, *res;
383
384 #ifdef BACKSQL_TRACE
385         Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
386                 "searching for objectclass with name='%s'\n",
387                 objclass, 0, 0 );
388 #endif /* BACKSQL_TRACE */
389
390         tmp.oc = oc;
391         res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
392                         (AVL_CMP)backsql_cmp_oc );
393 #ifdef BACKSQL_TRACE
394         if ( res != NULL ) {
395                 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
396                         "found name='%s', id=%d\n", 
397                         BACKSQL_OC_NAME( res ), res->id, 0 );
398         } else {
399                 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
400                         "not found\n", 0, 0, 0 );
401         }
402 #endif /* BACKSQL_TRACE */
403  
404         return res;
405 }
406
407 backsql_oc_map_rec *
408 backsql_name2oc( backsql_info *si, struct berval *oc_name )
409 {
410         backsql_oc_map_rec      tmp, *res;
411
412 #ifdef BACKSQL_TRACE
413         Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
414                 "searching for objectclass with name='%s'\n",
415                 objclass, 0, 0 );
416 #endif /* BACKSQL_TRACE */
417
418         tmp.oc = oc_bvfind( oc_name );
419         if ( tmp.oc == NULL ) {
420                 return NULL;
421         }
422
423         res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
424                         (AVL_CMP)backsql_cmp_oc );
425 #ifdef BACKSQL_TRACE
426         if ( res != NULL ) {
427                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
428                         "found name='%s', id=%d\n", 
429                         BACKSQL_OC_NAME( res ), res->id, 0 );
430         } else {
431                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
432                         "not found\n", 0, 0, 0 );
433         }
434 #endif /* BACKSQL_TRACE */
435  
436         return res;
437 }
438
439 backsql_oc_map_rec *
440 backsql_id2oc( backsql_info *si, unsigned long id )
441 {
442         backsql_oc_map_rec      tmp, *res;
443  
444 #ifdef BACKSQL_TRACE
445         Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
446                 "searching for objectclass with id='%d'\n", id, 0, 0 );
447 #endif /* BACKSQL_TRACE */
448
449         tmp.id = id;
450         res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
451                         (AVL_CMP)backsql_cmp_oc_id );
452
453 #ifdef BACKSQL_TRACE
454         if ( res != NULL ) {
455                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
456                         "found name='%s', id=%d\n",
457                         BACKSQL_OC_NAME( res ), res->id, 0 );
458         } else {
459                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
460                         "not found\n", 0, 0, 0 );
461         }
462 #endif /* BACKSQL_TRACE */
463         
464         return res;
465 }
466
467 backsql_at_map_rec *
468 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
469 {
470         backsql_at_map_rec      tmp, *res;
471  
472 #ifdef BACKSQL_TRACE
473         Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
474                 "searching for attribute '%s' for objectclass '%s'\n",
475                 attr, BACKSQL_OC_NAME( objclass ), 0 );
476 #endif /* BACKSQL_TRACE */
477
478         tmp.ad = ad;
479         res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
480                         (AVL_CMP)backsql_cmp_attr );
481
482 #ifdef BACKSQL_TRACE
483         if ( res != NULL ) {
484                 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
485                         "found name='%s', sel_expr='%s'\n",
486                         res->ad->ad_cname.bv_val, res->sel_expr.bv_val, 0 );
487         } else {
488                 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
489                         "not found\n", 0, 0, 0 );
490         }
491 #endif /* BACKSQL_TRACE */
492
493         return res;
494 }
495
496 /*
497  * Deprecated
498  */
499 backsql_at_map_rec *
500 backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr )
501 {
502         backsql_at_map_rec      tmp, *res;
503         const char              *text = NULL;
504  
505 #ifdef BACKSQL_TRACE
506         Debug( LDAP_DEBUG_TRACE, "==>backsql_name2at(): "
507                 "searching for attribute '%s' for objectclass '%s'\n",
508                 attr, BACKSQL_OC_NAME( objclass ), 0 );
509 #endif /* BACKSQL_TRACE */
510
511         if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) {
512                 return NULL;
513         }
514
515         res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
516                         (AVL_CMP)backsql_cmp_attr );
517
518 #ifdef BACKSQL_TRACE
519         if ( res != NULL ) {
520                 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
521                         "found name='%s', sel_expr='%s'\n",
522                         res->name, res->sel_expr.bv_val, 0 );
523         } else {
524                 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
525                         "not found\n", 0, 0, 0 );
526         }
527 #endif /* BACKSQL_TRACE */
528
529         return res;
530 }
531
532 static void
533 backsql_free_attr( backsql_at_map_rec *at )
534 {
535         Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n", 
536                         at->ad->ad_cname.bv_val, 0, 0 );
537         ch_free( at->sel_expr.bv_val );
538         if ( at->from_tbls.bv_val != NULL ) {
539                 ch_free( at->from_tbls.bv_val );
540         }
541         if ( at->join_where.bv_val != NULL ) {
542                 ch_free( at->join_where.bv_val );
543         }
544         if ( at->add_proc != NULL ) {
545                 ch_free( at->add_proc );
546         }
547         if ( at->delete_proc != NULL ) {
548                 ch_free( at->delete_proc );
549         }
550         if ( at->query ) {
551                 ch_free( at->query );
552         }
553
554         /* TimesTen */
555         if ( at->sel_expr_u.bv_val ) {
556                 ch_free( at->sel_expr_u.bv_val );
557         }
558         
559         ch_free( at );
560
561         Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
562 }
563
564 static void
565 backsql_free_oc( backsql_oc_map_rec *oc )
566 {
567         Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n", 
568                         BACKSQL_OC_NAME( oc ), 0, 0 );
569         avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
570         ch_free( oc->keytbl.bv_val );
571         ch_free( oc->keycol.bv_val );
572         if ( oc->create_proc != NULL ) {
573                 ch_free( oc->create_proc );
574         }
575         if ( oc->create_keyval != NULL ) {
576                 ch_free( oc->create_keyval );
577         }
578         if ( oc->delete_proc != NULL ) {
579                 ch_free( oc->delete_proc );
580         }
581         ch_free( oc );
582
583         Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
584 }
585
586 int
587 backsql_destroy_schema_map( backsql_info *si )
588 {
589         Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
590         avl_free( si->oc_by_oc, NULL );
591         avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
592         Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
593         return 0;
594 }
595
596 #endif /* SLAPD_SQL */
597