2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
24 #include <sys/types.h>
25 #include "ac/string.h"
28 #include "proto-sql.h"
30 #define BACKSQL_DUPLICATE (-1)
32 /* NOTE: by default, cannot just compare pointers because
33 * objectClass/attributeType order would be machine-dependent
34 * (and tests would fail!); however, if you don't want to run
35 * tests, or see attributeTypes written in the same order
36 * they are defined, define */
37 /* #undef BACKSQL_USE_PTR_CMP */
40 * Uses the pointer to the ObjectClass structure
43 backsql_cmp_oc( const void *v_m1, const void *v_m2 )
45 const backsql_oc_map_rec *m1 = v_m1,
48 #ifdef BACKSQL_USE_PTR_CMP
49 return SLAP_PTRCMP( m1->bom_oc, m2->bom_oc );
50 #else /* ! BACKSQL_USE_PTR_CMP */
51 return ber_bvcmp( &m1->bom_oc->soc_cname, &m2->bom_oc->soc_cname );
52 #endif /* ! BACKSQL_USE_PTR_CMP */
56 backsql_cmp_oc_id( const void *v_m1, const void *v_m2 )
58 const backsql_oc_map_rec *m1 = v_m1,
61 return ( m1->bom_id < m2->bom_id ? -1 : ( m1->bom_id > m2->bom_id ? 1 : 0 ) );
65 * Uses the pointer to the AttributeDescription structure
68 backsql_cmp_attr( const void *v_m1, const void *v_m2 )
70 const backsql_at_map_rec *m1 = v_m1,
73 #ifdef BACKSQL_USE_PTR_CMP
74 return SLAP_PTRCMP( m1->bam_ad, m2->bam_ad );
75 #else /* ! BACKSQL_USE_PTR_CMP */
76 return ber_bvcmp( &m1->bam_ad->ad_cname, &m2->bam_ad->ad_cname );
77 #endif /* ! BACKSQL_USE_PTR_CMP */
81 backsql_dup_attr( void *v_m1, void *v_m2 )
83 backsql_at_map_rec *m1 = v_m1,
86 assert( m1->bam_ad == m2->bam_ad );
88 /* duplicate definitions of attributeTypes are appended;
89 * this allows to define multiple rules for the same
90 * attributeType. Use with care! */
91 for ( ; m1->bam_next ; m1 = m1->bam_next );
95 return BACKSQL_DUPLICATE;
99 backsql_make_attr_query(
100 backsql_oc_map_rec *oc_map,
101 backsql_at_map_rec *at_map )
103 struct berbuf bb = BB_NULL;
105 #ifdef BACKSQL_ALIASING_QUOTE
106 backsql_strfcat( &bb, "lblcbclblbcbl",
107 (ber_len_t)STRLENOF( "SELECT " ), "SELECT ",
108 &at_map->bam_sel_expr,
109 (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ), " " BACKSQL_ALIASING,
110 BACKSQL_ALIASING_QUOTE,
111 &at_map->bam_ad->ad_cname,
112 BACKSQL_ALIASING_QUOTE,
113 (ber_len_t)STRLENOF( " FROM " ), " FROM ",
114 &at_map->bam_from_tbls,
115 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
119 (ber_len_t)STRLENOF( "=?" ), "=?" );
120 #else /* ! BACKSQL_ALIASING_QUOTE */
121 backsql_strfcat( &bb, "lblblblbcbl",
122 (ber_len_t)STRLENOF( "SELECT " ), "SELECT ",
123 &at_map->bam_sel_expr,
124 (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ), " " BACKSQL_ALIASING,
125 &at_map->bam_ad->ad_cname,
126 (ber_len_t)STRLENOF( " FROM " ), " FROM ",
127 &at_map->bam_from_tbls,
128 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
132 (ber_len_t)STRLENOF( "=?" ), "=?" );
133 #endif /* ! BACKSQL_ALIASING_QUOTE */
135 if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
136 backsql_strfcat( &bb, "lb",
137 (ber_len_t)STRLENOF( " AND " ), " AND ",
138 &at_map->bam_join_where );
141 #ifdef BACKSQL_ALIASING_QUOTE
142 backsql_strfcat( &bb, "lcbc",
143 (ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ",
144 BACKSQL_ALIASING_QUOTE,
145 &at_map->bam_sel_expr,
146 BACKSQL_ALIASING_QUOTE );
147 #else /* ! BACKSQL_ALIASING_QUOTE */
148 backsql_strfcat( &bb, "lb",
149 (ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ",
150 &at_map->bam_sel_expr );
151 #endif /* ! BACKSQL_ALIASING_QUOTE */
153 at_map->bam_query = bb.bb_val.bv_val;
159 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
161 backsql_at_map_rec *at_map;
162 char s[] = "+9223372036854775807L";
167 snprintf( s, sizeof( s ), "%ld", oc_map->bom_id );
168 sbv.bv_len = strlen( s );
170 /* extra objectClasses */
171 at_map = (backsql_at_map_rec *)ch_calloc(1,
172 sizeof( backsql_at_map_rec ) );
173 at_map->bam_ad = slap_schema.si_ad_objectClass;
174 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
175 &at_map->bam_sel_expr );
176 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
177 &at_map->bam_from_tbls );
179 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
180 bb.bb_val = at_map->bam_from_tbls;
181 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
182 at_map->bam_from_tbls = bb.bb_val;
184 BER_BVZERO( &bb.bb_val );
186 backsql_strfcat( &bb, "lbcblb",
187 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
188 "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
192 (ber_len_t)STRLENOF( " and ldap_entries.oc_map_id=" ),
193 " and ldap_entries.oc_map_id=",
195 at_map->bam_join_where = bb.bb_val;
197 at_map->bam_oc = oc_map->bom_oc;
199 at_map->bam_add_proc = NULL;
202 "INSERT INTO ldap_entry_objclasses "
203 "(entry_id,oc_name) VALUES "
204 "((SELECT id FROM ldap_entries "
206 "18446744073709551615UL " /* 64 bit ULONG */
208 snprintf( tmp, sizeof(tmp),
209 "INSERT INTO ldap_entry_objclasses "
210 "(entry_id,oc_name) VALUES "
211 "((SELECT id FROM ldap_entries "
212 "WHERE oc_map_id=%lu "
213 "AND keyval=?),?)", oc_map->bom_id );
214 at_map->bam_add_proc = ch_strdup( tmp );
217 at_map->bam_delete_proc = NULL;
220 "DELETE FROM ldap_entry_objclasses "
221 "WHERE entry_id=(SELECT id FROM ldap_entries "
223 "18446744073709551615UL " /* 64 bit ULONG */
224 "AND keyval=?) AND oc_name=?";
225 snprintf( tmp, sizeof(tmp),
226 "DELETE FROM ldap_entry_objclasses "
227 "WHERE entry_id=(SELECT id FROM ldap_entries "
228 "WHERE oc_map_id=%lu"
229 "AND keyval=?) AND oc_name=?",
231 at_map->bam_delete_proc = ch_strdup( tmp );
234 at_map->bam_param_order = 0;
235 at_map->bam_expect_return = 0;
236 at_map->bam_next = NULL;
238 backsql_make_attr_query( oc_map, at_map );
239 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
240 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
241 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
242 at_map->bam_ad->ad_cname.bv_val,
243 oc_map->bom_oc->soc_cname.bv_val, 0 );
246 /* FIXME: we need to correct the objectClass join_where
247 * after the attribute query is built */
248 ch_free( at_map->bam_join_where.bv_val );
249 BER_BVZERO( &bb.bb_val );
251 backsql_strfcat( &bb, "lbcblb",
252 (ber_len_t)STRLENOF( /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=" ),
253 /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=",
257 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
258 " AND ldap_entries.oc_map_id=",
260 at_map->bam_join_where = bb.bb_val;
262 /* referral attribute */
263 at_map = (backsql_at_map_rec *)ch_calloc( 1,
264 sizeof( backsql_at_map_rec ) );
265 at_map->bam_ad = slap_schema.si_ad_ref;
266 ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->bam_sel_expr );
267 ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->bam_from_tbls );
269 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
270 bb.bb_val = at_map->bam_from_tbls;
271 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
272 at_map->bam_from_tbls = bb.bb_val;
274 BER_BVZERO( &bb.bb_val );
276 backsql_strfcat( &bb, "lbcblb",
277 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=" ),
278 "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=",
282 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
283 " AND ldap_entries.oc_map_id=",
286 at_map->bam_join_where = bb.bb_val;
288 at_map->bam_oc = NULL;
290 at_map->bam_add_proc = NULL;
293 "INSERT INTO ldap_referrals "
294 "(entry_id,url) VALUES "
295 "((SELECT id FROM ldap_entries "
297 "18446744073709551615UL " /* 64 bit ULONG */
299 snprintf( tmp, sizeof(tmp),
300 "INSERT INTO ldap_referrals "
301 "(entry_id,url) VALUES "
302 "((SELECT id FROM ldap_entries "
303 "WHERE oc_map_id=%lu "
304 "AND keyval=?),?)", oc_map->bom_id );
305 at_map->bam_add_proc = ch_strdup( tmp );
308 at_map->bam_delete_proc = NULL;
311 "DELETE FROM ldap_referrals "
312 "WHERE entry_id=(SELECT id FROM ldap_entries "
314 "18446744073709551615UL " /* 64 bit ULONG */
315 "AND keyval=?) and url=?";
316 snprintf( tmp, sizeof(tmp),
317 "DELETE FROM ldap_referrals "
318 "WHERE entry_id=(SELECT id FROM ldap_entries "
319 "WHERE oc_map_id=%lu"
320 "AND keyval=?) and url=?",
322 at_map->bam_delete_proc = ch_strdup( tmp );
325 at_map->bam_param_order = 0;
326 at_map->bam_expect_return = 0;
327 at_map->bam_next = NULL;
329 backsql_make_attr_query( oc_map, at_map );
330 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
331 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
332 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
333 at_map->bam_ad->ad_cname.bv_val,
334 oc_map->bom_oc->soc_cname.bv_val, 0 );
340 struct backsql_attr_schema_info {
341 backsql_info *bas_bi;
344 unsigned long *bas_oc_id;
349 backsql_oc_get_attr_mapping( void *v_oc, void *v_bas )
352 BACKSQL_ROW_NTS at_row;
353 backsql_oc_map_rec *oc_map = (backsql_oc_map_rec *)v_oc;
354 backsql_at_map_rec *at_map;
355 struct backsql_attr_schema_info *bas = (struct backsql_attr_schema_info *)v_bas;
357 /* bas->bas_oc_id has been bound to bas->bas_sth */
358 *bas->bas_oc_id = oc_map->bom_id;
360 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
361 "executing at_query\n"
363 " for objectClass \"%s\"\n"
364 " with param oc_id=\"%lu\"\n",
365 bas->bas_bi->sql_at_query,
366 BACKSQL_OC_NAME( oc_map ),
369 rc = SQLExecute( bas->bas_sth );
370 if ( rc != SQL_SUCCESS ) {
371 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
372 "error executing at_query\n"
374 " for objectClass \"%s\"\n"
375 " with param oc_id=\"%lu\"\n",
376 bas->bas_bi->sql_at_query,
377 BACKSQL_OC_NAME( oc_map ),
379 backsql_PrintErrors( bas->bas_bi->sql_db_env,
380 bas->bas_dbh, bas->bas_sth, rc );
381 bas->bas_rc = LDAP_OTHER;
382 return BACKSQL_AVL_STOP;
385 backsql_BindRowAsStrings( bas->bas_sth, &at_row );
386 for ( ; rc = SQLFetch( bas->bas_sth ), BACKSQL_SUCCESS( rc ); ) {
387 const char *text = NULL;
389 struct berbuf bb = BB_NULL;
391 Debug( LDAP_DEBUG_TRACE,
394 "\tsel_expr=\"%s\"\n"
396 at_row.cols[ 0 ], at_row.cols[ 1 ],
398 Debug( LDAP_DEBUG_TRACE,
399 "\tjoin_where=\"%s\"\n"
400 "\tadd_proc=\"%s\"\n"
401 "\tdelete_proc=\"%s\"\n",
402 at_row.cols[ 3 ], at_row.cols[ 4 ],
405 Debug( LDAP_DEBUG_TRACE, "\tsel_expr_u=\"%s\"\n",
406 at_row.cols[ 8 ], 0, 0 );
407 at_map = (backsql_at_map_rec *)ch_calloc( 1,
408 sizeof( backsql_at_map_rec ) );
409 rc = slap_str2ad( at_row.cols[ 0 ],
410 &at_map->bam_ad, &text );
411 if ( rc != LDAP_SUCCESS ) {
412 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
413 "attribute \"%s\" for objectClass \"%s\" "
414 "is not defined in schema: %s\n",
416 BACKSQL_OC_NAME( oc_map ), text );
417 bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
418 return BACKSQL_AVL_STOP;
421 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
422 if ( at_row.value_len[ 8 ] < 0 ) {
423 BER_BVZERO( &at_map->bam_sel_expr_u );
426 ber_str2bv( at_row.cols[ 8 ], 0, 1,
427 &at_map->bam_sel_expr_u );
430 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
431 backsql_merge_from_clause( &bb, &bv );
432 at_map->bam_from_tbls = bb.bb_val;
433 if ( at_row.value_len[ 3 ] < 0 ) {
434 BER_BVZERO( &at_map->bam_join_where );
437 ber_str2bv( at_row.cols[ 3 ], 0, 1,
438 &at_map->bam_join_where );
440 at_map->bam_add_proc = NULL;
441 if ( at_row.value_len[ 4 ] > 0 ) {
442 at_map->bam_add_proc = ch_strdup( at_row.cols[4] );
444 at_map->bam_delete_proc = NULL;
445 if ( at_row.value_len[ 5 ] > 0 ) {
446 at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
448 at_map->bam_param_order = strtol( at_row.cols[ 6 ],
450 at_map->bam_expect_return = strtol( at_row.cols[ 7 ],
452 backsql_make_attr_query( oc_map, at_map );
453 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
454 "preconstructed query \"%s\"\n",
455 at_map->bam_query, 0, 0 );
456 at_map->bam_next = NULL;
457 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
458 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
459 "duplicate attribute \"%s\" "
460 "in objectClass \"%s\" map\n",
461 at_map->bam_ad->ad_cname.bv_val,
462 oc_map->bom_oc->soc_cname.bv_val, 0 );
465 if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
466 BER_BVISNULL( &at_map->bam_sel_expr_u ) )
468 struct berbuf bb = BB_NULL;
470 backsql_strfcat( &bb, "bcbc",
471 &bas->bas_bi->sql_upper_func,
473 &at_map->bam_sel_expr,
475 at_map->bam_sel_expr_u = bb.bb_val;
478 backsql_FreeRow( &at_row );
479 SQLFreeStmt( bas->bas_sth, SQL_CLOSE );
481 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
482 "autoadding 'objectClass' and 'ref' mappings\n",
483 BACKSQL_OC_NAME( oc_map ), 0, 0 );
485 (void)backsql_add_sysmaps( oc_map );
487 return BACKSQL_AVL_CONTINUE;
492 backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
496 BACKSQL_ROW_NTS oc_row;
498 backsql_oc_map_rec *oc_map;
499 struct backsql_attr_schema_info bas;
501 Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n", 0, 0, 0 );
504 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
506 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
507 rc = backsql_Prepare( dbh, &sth,
508 backsql_check_dn_ru_query, 0 );
509 if ( rc == SQL_SUCCESS ) {
510 /* Yes, the field exists */
511 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
512 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
513 "in the schema\n", 0, 0, 0 );
515 /* No such field exists */
516 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
519 SQLFreeStmt( sth, SQL_DROP );
522 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n",
523 bi->sql_oc_query, 0, 0 );
525 rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
526 if ( rc != SQL_SUCCESS ) {
527 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
528 "error preparing oc_query: \"%s\"\n",
529 bi->sql_oc_query, 0, 0 );
530 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
534 rc = SQLExecute( sth );
535 if ( rc != SQL_SUCCESS ) {
536 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
537 "error executing oc_query: \n", 0, 0, 0 );
538 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
542 backsql_BindRowAsStrings( sth, &oc_row );
543 rc = SQLFetch( sth );
544 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
547 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
548 sizeof( backsql_oc_map_rec ) );
550 oc_map->bom_id = strtol( oc_row.cols[ 0 ], NULL, 0 );
552 oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
553 if ( oc_map->bom_oc == NULL ) {
554 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
555 "objectClass \"%s\" is not defined in schema\n",
556 oc_row.cols[ 1 ], 0, 0 );
557 return LDAP_OTHER; /* undefined objectClass ? */
560 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
561 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
562 oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL
563 : ch_strdup( oc_row.cols[ 4 ] );
566 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
568 oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] < 0 )
569 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
571 oc_map->bom_delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL
572 : ch_strdup( oc_row.cols[ colnum ] );
573 oc_map->bom_expect_return = strtol( oc_row.cols[ colnum + 1 ],
577 * FIXME: first attempt to check for offending
578 * instructions in {create|delete}_proc
581 oc_map->bom_attrs = NULL;
582 if ( avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, avl_dup_error ) == -1 ) {
583 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
584 "duplicate objectClass \"%s\" in objectClass map\n",
585 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
588 if ( avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, avl_dup_error ) == -1 ) {
589 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
590 "duplicate objectClass \"%s\" in objectClass by ID map\n",
591 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
594 oc_id = oc_map->bom_id;
595 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
596 "objectClass \"%s\": keytbl=\"%s\" keycol=\"%s\"\n",
597 BACKSQL_OC_NAME( oc_map ),
598 oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
599 if ( oc_map->bom_create_proc ) {
600 Debug( LDAP_DEBUG_TRACE, "create_proc=\"%s\"\n",
601 oc_map->bom_create_proc, 0, 0 );
603 if ( oc_map->bom_create_keyval ) {
604 Debug( LDAP_DEBUG_TRACE, "create_keyval=\"%s\"\n",
605 oc_map->bom_create_keyval, 0, 0 );
607 if ( oc_map->bom_delete_proc ) {
608 Debug( LDAP_DEBUG_TRACE, "delete_proc=\"%s\"\n",
609 oc_map->bom_delete_proc, 0, 0 );
611 Debug( LDAP_DEBUG_TRACE, "expect_return: "
612 "add=%d, del=%d; attributes:\n",
613 BACKSQL_IS_ADD( oc_map->bom_expect_return ),
614 BACKSQL_IS_DEL( oc_map->bom_expect_return ), 0 );
617 backsql_FreeRow( &oc_row );
618 SQLFreeStmt( sth, SQL_DROP );
620 /* prepare for attribute fetching */
621 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): at_query \"%s\"\n",
622 bi->sql_at_query, 0, 0 );
624 rc = backsql_Prepare( dbh, &sth, bi->sql_at_query, 0 );
625 if ( rc != SQL_SUCCESS ) {
626 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
627 "error preparing at_query: \"%s\"\n",
628 bi->sql_at_query, 0, 0 );
629 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
633 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT, &oc_id );
634 if ( rc != SQL_SUCCESS ) {
635 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
636 "error binding param \"oc_id\" for at_query\n", 0, 0, 0 );
637 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
638 SQLFreeStmt( sth, SQL_DROP );
645 bas.bas_oc_id = &oc_id;
646 bas.bas_rc = LDAP_SUCCESS;
648 (void)avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
649 &bas, BACKSQL_AVL_STOP, AVL_INORDER );
651 SQLFreeStmt( sth, SQL_DROP );
653 bi->sql_flags |= BSQLF_SCHEMA_LOADED;
655 Debug( LDAP_DEBUG_TRACE, "<==backsql_load_schema_map()\n", 0, 0, 0 );
661 backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
663 backsql_oc_map_rec tmp, *res;
666 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
667 "searching for objectclass with name=\"%s\"\n",
668 oc->soc_cname.bv_val, 0, 0 );
669 #endif /* BACKSQL_TRACE */
672 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
675 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
676 "found name=\"%s\", id=%d\n",
677 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
679 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
680 "not found\n", 0, 0, 0 );
682 #endif /* BACKSQL_TRACE */
688 backsql_name2oc( backsql_info *bi, struct berval *oc_name )
690 backsql_oc_map_rec tmp, *res;
693 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
694 "searching for objectclass with name=\"%s\"\n",
695 oc_name->bv_val, 0, 0 );
696 #endif /* BACKSQL_TRACE */
698 tmp.bom_oc = oc_bvfind( oc_name );
699 if ( tmp.bom_oc == NULL ) {
703 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
706 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
707 "found name=\"%s\", id=%d\n",
708 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
710 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
711 "not found\n", 0, 0, 0 );
713 #endif /* BACKSQL_TRACE */
719 backsql_id2oc( backsql_info *bi, unsigned long id )
721 backsql_oc_map_rec tmp, *res;
724 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
725 "searching for objectclass with id='%d'\n", id, 0, 0 );
726 #endif /* BACKSQL_TRACE */
729 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_id, &tmp,
734 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
735 "found name=\"%s\", id=%d\n",
736 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
738 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
739 "not found\n", 0, 0, 0 );
741 #endif /* BACKSQL_TRACE */
747 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
749 backsql_at_map_rec tmp, *res;
752 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
753 "searching for attribute \"%s\" for objectclass \"%s\"\n",
754 ad->ad_cname.bv_val, BACKSQL_OC_NAME( objclass ), 0 );
755 #endif /* BACKSQL_TRACE */
758 res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp,
763 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
764 "found name=\"%s\", sel_expr=\"%s\"\n",
765 res->bam_ad->ad_cname.bv_val,
766 res->bam_sel_expr.bv_val, 0 );
768 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
769 "not found\n", 0, 0, 0 );
771 #endif /* BACKSQL_TRACE */
776 /* attributeType inheritance */
778 backsql_at_map_rec **ret;
779 AttributeDescription *ad;
783 #define SUPAD2AT_STOP (-1)
786 supad2at_f( void *v_at, void *v_arg )
788 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
789 struct supad2at_t *va = (struct supad2at_t *)v_arg;
791 if ( is_at_subtype( at->bam_ad->ad_type, va->ad->ad_type ) ) {
792 backsql_at_map_rec **ret;
795 /* if already listed, holler! (should never happen) */
797 for ( i = 0; i < va->n; i++ ) {
798 if ( va->ret[ i ]->bam_ad == at->bam_ad ) {
808 ret = ch_realloc( va->ret,
809 sizeof( backsql_at_map_rec *) * ( va->n + 2 ) );
812 return SUPAD2AT_STOP;
825 * stores in *pret a NULL terminated array of pointers
826 * to backsql_at_map_rec whose attributeType is supad->ad_type
830 backsql_supad2at( backsql_oc_map_rec *objclass, AttributeDescription *supad,
831 backsql_at_map_rec ***pret )
833 struct supad2at_t va;
846 rc = avl_apply( objclass->bom_attrs, supad2at_f, &va,
847 SUPAD2AT_STOP, AVL_INORDER );
848 if ( rc == SUPAD2AT_STOP ) {
858 backsql_free_attr( void *v_at )
860 backsql_at_map_rec *at = v_at;
862 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): \"%s\"\n",
863 at->bam_ad->ad_cname.bv_val, 0, 0 );
864 ch_free( at->bam_sel_expr.bv_val );
865 if ( !BER_BVISNULL( &at->bam_from_tbls ) ) {
866 ch_free( at->bam_from_tbls.bv_val );
868 if ( !BER_BVISNULL( &at->bam_join_where ) ) {
869 ch_free( at->bam_join_where.bv_val );
871 if ( at->bam_add_proc != NULL ) {
872 ch_free( at->bam_add_proc );
874 if ( at->bam_delete_proc != NULL ) {
875 ch_free( at->bam_delete_proc );
877 if ( at->bam_query != NULL ) {
878 ch_free( at->bam_query );
882 if ( !BER_BVISNULL( &at->bam_sel_expr_u ) ) {
883 ch_free( at->bam_sel_expr_u.bv_val );
886 if ( at->bam_next ) {
887 backsql_free_attr( at->bam_next );
892 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
896 backsql_free_oc( void *v_oc )
898 backsql_oc_map_rec *oc = v_oc;
900 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): \"%s\"\n",
901 BACKSQL_OC_NAME( oc ), 0, 0 );
902 avl_free( oc->bom_attrs, backsql_free_attr );
903 ch_free( oc->bom_keytbl.bv_val );
904 ch_free( oc->bom_keycol.bv_val );
905 if ( oc->bom_create_proc != NULL ) {
906 ch_free( oc->bom_create_proc );
908 if ( oc->bom_create_keyval != NULL ) {
909 ch_free( oc->bom_create_keyval );
911 if ( oc->bom_delete_proc != NULL ) {
912 ch_free( oc->bom_delete_proc );
916 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
920 backsql_destroy_schema_map( backsql_info *bi )
922 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
923 avl_free( bi->sql_oc_by_oc, 0 );
924 avl_free( bi->sql_oc_by_id, backsql_free_oc );
925 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );