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