]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/schema-map.c
a0194561d05580797daa6b23bd099eef1c53f593
[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.value_len[ 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.value_len[ 5 ] < 0 ) 
249                                 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
250                 }
251                 oc_map->delete_proc = ( oc_row.value_len[ 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.value_len[ 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.value_len[ 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 = NULL;
354                         if ( at_row.value_len[ 4 ] > 0 ) {
355                                 at_map->add_proc = ch_strdup( at_row.cols[4] );
356                         }
357                         at_map->delete_proc = NULL;
358                         if ( at_row.value_len[ 5 ] > 0 ) {
359                                 at_map->delete_proc
360                                         = ch_strdup( at_row.cols[ 5 ] );
361                         }
362                         at_map->param_order = strtol( at_row.cols[ 6 ], 
363                                         NULL, 0 );
364                         at_map->expect_return = strtol( at_row.cols[ 7 ],
365                                         NULL, 0 );
366                         backsql_make_attr_query( oc_map, at_map );
367                         Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
368                                 "preconstructed query '%s'\n",
369                                 at_map->query, 0, 0 );
370                         avl_insert( &oc_map->attrs, at_map, 
371                                         (AVL_CMP)backsql_cmp_attr, NULL );
372                 }
373                 backsql_FreeRow( &at_row );
374                 SQLFreeStmt( at_sth, SQL_CLOSE );
375         }
376         backsql_FreeRow( &oc_row );
377         SQLFreeStmt( at_sth, SQL_DROP );
378         SQLFreeStmt( oc_sth, SQL_DROP );
379         si->bsql_flags |= BSQLF_SCHEMA_LOADED;
380         Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
381         return LDAP_SUCCESS;
382 }
383
384 backsql_oc_map_rec *
385 backsql_oc2oc( backsql_info *si, ObjectClass *oc )
386 {
387         backsql_oc_map_rec      tmp, *res;
388
389 #ifdef BACKSQL_TRACE
390         Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
391                 "searching for objectclass with name='%s'\n",
392                 objclass, 0, 0 );
393 #endif /* BACKSQL_TRACE */
394
395         tmp.oc = oc;
396         res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
397                         (AVL_CMP)backsql_cmp_oc );
398 #ifdef BACKSQL_TRACE
399         if ( res != NULL ) {
400                 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
401                         "found name='%s', id=%d\n", 
402                         BACKSQL_OC_NAME( res ), res->id, 0 );
403         } else {
404                 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
405                         "not found\n", 0, 0, 0 );
406         }
407 #endif /* BACKSQL_TRACE */
408  
409         return res;
410 }
411
412 backsql_oc_map_rec *
413 backsql_name2oc( backsql_info *si, struct berval *oc_name )
414 {
415         backsql_oc_map_rec      tmp, *res;
416
417 #ifdef BACKSQL_TRACE
418         Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
419                 "searching for objectclass with name='%s'\n",
420                 objclass, 0, 0 );
421 #endif /* BACKSQL_TRACE */
422
423         tmp.oc = oc_bvfind( oc_name );
424         if ( tmp.oc == NULL ) {
425                 return NULL;
426         }
427
428         res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
429                         (AVL_CMP)backsql_cmp_oc );
430 #ifdef BACKSQL_TRACE
431         if ( res != NULL ) {
432                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
433                         "found name='%s', id=%d\n", 
434                         BACKSQL_OC_NAME( res ), res->id, 0 );
435         } else {
436                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
437                         "not found\n", 0, 0, 0 );
438         }
439 #endif /* BACKSQL_TRACE */
440  
441         return res;
442 }
443
444 backsql_oc_map_rec *
445 backsql_id2oc( backsql_info *si, unsigned long id )
446 {
447         backsql_oc_map_rec      tmp, *res;
448  
449 #ifdef BACKSQL_TRACE
450         Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
451                 "searching for objectclass with id='%d'\n", id, 0, 0 );
452 #endif /* BACKSQL_TRACE */
453
454         tmp.id = id;
455         res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
456                         (AVL_CMP)backsql_cmp_oc_id );
457
458 #ifdef BACKSQL_TRACE
459         if ( res != NULL ) {
460                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
461                         "found name='%s', id=%d\n",
462                         BACKSQL_OC_NAME( res ), res->id, 0 );
463         } else {
464                 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
465                         "not found\n", 0, 0, 0 );
466         }
467 #endif /* BACKSQL_TRACE */
468         
469         return res;
470 }
471
472 backsql_at_map_rec *
473 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
474 {
475         backsql_at_map_rec      tmp, *res;
476  
477 #ifdef BACKSQL_TRACE
478         Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
479                 "searching for attribute '%s' for objectclass '%s'\n",
480                 attr, BACKSQL_OC_NAME( objclass ), 0 );
481 #endif /* BACKSQL_TRACE */
482
483         tmp.ad = ad;
484         res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
485                         (AVL_CMP)backsql_cmp_attr );
486
487 #ifdef BACKSQL_TRACE
488         if ( res != NULL ) {
489                 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
490                         "found name='%s', sel_expr='%s'\n",
491                         res->ad->ad_cname.bv_val, res->sel_expr.bv_val, 0 );
492         } else {
493                 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
494                         "not found\n", 0, 0, 0 );
495         }
496 #endif /* BACKSQL_TRACE */
497
498         return res;
499 }
500
501 /*
502  * Deprecated
503  */
504 backsql_at_map_rec *
505 backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr )
506 {
507         backsql_at_map_rec      tmp, *res;
508         const char              *text = NULL;
509  
510 #ifdef BACKSQL_TRACE
511         Debug( LDAP_DEBUG_TRACE, "==>backsql_name2at(): "
512                 "searching for attribute '%s' for objectclass '%s'\n",
513                 attr, BACKSQL_OC_NAME( objclass ), 0 );
514 #endif /* BACKSQL_TRACE */
515
516         if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) {
517                 return NULL;
518         }
519
520         res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
521                         (AVL_CMP)backsql_cmp_attr );
522
523 #ifdef BACKSQL_TRACE
524         if ( res != NULL ) {
525                 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
526                         "found name='%s', sel_expr='%s'\n",
527                         res->name, res->sel_expr.bv_val, 0 );
528         } else {
529                 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
530                         "not found\n", 0, 0, 0 );
531         }
532 #endif /* BACKSQL_TRACE */
533
534         return res;
535 }
536
537 static void
538 backsql_free_attr( backsql_at_map_rec *at )
539 {
540         Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n", 
541                         at->ad->ad_cname.bv_val, 0, 0 );
542         ch_free( at->sel_expr.bv_val );
543         if ( at->from_tbls.bv_val != NULL ) {
544                 ch_free( at->from_tbls.bv_val );
545         }
546         if ( at->join_where.bv_val != NULL ) {
547                 ch_free( at->join_where.bv_val );
548         }
549         if ( at->add_proc != NULL ) {
550                 ch_free( at->add_proc );
551         }
552         if ( at->delete_proc != NULL ) {
553                 ch_free( at->delete_proc );
554         }
555         if ( at->query ) {
556                 ch_free( at->query );
557         }
558
559         /* TimesTen */
560         if ( at->sel_expr_u.bv_val ) {
561                 ch_free( at->sel_expr_u.bv_val );
562         }
563         
564         ch_free( at );
565
566         Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
567 }
568
569 static void
570 backsql_free_oc( backsql_oc_map_rec *oc )
571 {
572         Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n", 
573                         BACKSQL_OC_NAME( oc ), 0, 0 );
574         avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
575         ch_free( oc->keytbl.bv_val );
576         ch_free( oc->keycol.bv_val );
577         if ( oc->create_proc != NULL ) {
578                 ch_free( oc->create_proc );
579         }
580         if ( oc->create_keyval != NULL ) {
581                 ch_free( oc->create_keyval );
582         }
583         if ( oc->delete_proc != NULL ) {
584                 ch_free( oc->delete_proc );
585         }
586         ch_free( oc );
587
588         Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
589 }
590
591 int
592 backsql_destroy_schema_map( backsql_info *si )
593 {
594         Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
595         avl_free( si->oc_by_oc, NULL );
596         avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
597         Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
598         return 0;
599 }
600
601 #endif /* SLAPD_SQL */
602