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