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;
390 struct berbuf bb = BB_NULL;
392 Debug( LDAP_DEBUG_TRACE,
395 "\tsel_expr=\"%s\"\n"
397 at_row.cols[ 0 ], at_row.cols[ 1 ],
399 Debug( LDAP_DEBUG_TRACE,
400 "\tjoin_where=\"%s\"\n"
401 "\tadd_proc=\"%s\"\n"
402 "\tdelete_proc=\"%s\"\n",
403 at_row.cols[ 3 ], at_row.cols[ 4 ],
406 Debug( LDAP_DEBUG_TRACE, "\tsel_expr_u=\"%s\"\n",
407 at_row.cols[ 8 ], 0, 0 );
408 at_map = (backsql_at_map_rec *)ch_calloc( 1,
409 sizeof( backsql_at_map_rec ) );
410 rc = slap_str2ad( at_row.cols[ 0 ],
411 &at_map->bam_ad, &text );
412 if ( rc != LDAP_SUCCESS ) {
413 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
414 "attribute \"%s\" for objectClass \"%s\" "
415 "is not defined in schema: %s\n",
417 BACKSQL_OC_NAME( oc_map ), text );
418 bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
419 return BACKSQL_AVL_STOP;
422 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
423 if ( at_row.value_len[ 8 ] < 0 ) {
424 BER_BVZERO( &at_map->bam_sel_expr_u );
427 ber_str2bv( at_row.cols[ 8 ], 0, 1,
428 &at_map->bam_sel_expr_u );
431 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
432 backsql_merge_from_clause( &bb, &bv );
433 at_map->bam_from_tbls = bb.bb_val;
434 if ( at_row.value_len[ 3 ] < 0 ) {
435 BER_BVZERO( &at_map->bam_join_where );
438 ber_str2bv( at_row.cols[ 3 ], 0, 1,
439 &at_map->bam_join_where );
441 at_map->bam_add_proc = NULL;
442 if ( at_row.value_len[ 4 ] > 0 ) {
443 at_map->bam_add_proc = ch_strdup( at_row.cols[4] );
445 at_map->bam_delete_proc = NULL;
446 if ( at_row.value_len[ 5 ] > 0 ) {
447 at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
449 at_map->bam_param_order = strtol( at_row.cols[ 6 ],
451 if ( next == at_row.cols[ 6 ] || next[0] != '\0' ) {
454 at_map->bam_expect_return = strtol( at_row.cols[ 7 ],
456 if ( next == at_row.cols[ 7 ] || next[0] != '\0' ) {
459 backsql_make_attr_query( oc_map, at_map );
460 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
461 "preconstructed query \"%s\"\n",
462 at_map->bam_query, 0, 0 );
463 at_map->bam_next = NULL;
464 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
465 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
466 "duplicate attribute \"%s\" "
467 "in objectClass \"%s\" map\n",
468 at_map->bam_ad->ad_cname.bv_val,
469 oc_map->bom_oc->soc_cname.bv_val, 0 );
472 if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
473 BER_BVISNULL( &at_map->bam_sel_expr_u ) )
475 struct berbuf bb = BB_NULL;
477 backsql_strfcat( &bb, "bcbc",
478 &bas->bas_bi->sql_upper_func,
480 &at_map->bam_sel_expr,
482 at_map->bam_sel_expr_u = bb.bb_val;
485 backsql_FreeRow( &at_row );
486 SQLFreeStmt( bas->bas_sth, SQL_CLOSE );
488 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
489 "autoadding 'objectClass' and 'ref' mappings\n",
490 BACKSQL_OC_NAME( oc_map ), 0, 0 );
492 (void)backsql_add_sysmaps( oc_map );
494 return BACKSQL_AVL_CONTINUE;
499 backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
501 SQLHSTMT sth = SQL_NULL_HSTMT;
503 BACKSQL_ROW_NTS oc_row;
505 backsql_oc_map_rec *oc_map;
506 struct backsql_attr_schema_info bas;
508 Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n", 0, 0, 0 );
511 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
513 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
514 rc = backsql_Prepare( dbh, &sth,
515 backsql_check_dn_ru_query, 0 );
516 if ( rc == SQL_SUCCESS ) {
517 /* Yes, the field exists */
518 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
519 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
520 "in the schema\n", 0, 0, 0 );
522 /* No such field exists */
523 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
526 SQLFreeStmt( sth, SQL_DROP );
529 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n",
530 bi->sql_oc_query, 0, 0 );
532 rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
533 if ( rc != SQL_SUCCESS ) {
534 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
535 "error preparing oc_query: \"%s\"\n",
536 bi->sql_oc_query, 0, 0 );
537 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
541 rc = SQLExecute( sth );
542 if ( rc != SQL_SUCCESS ) {
543 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
544 "error executing oc_query: \n", 0, 0, 0 );
545 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
549 backsql_BindRowAsStrings( sth, &oc_row );
550 rc = SQLFetch( sth );
551 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
554 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
555 sizeof( backsql_oc_map_rec ) );
557 oc_map->bom_id = strtol( oc_row.cols[ 0 ], NULL, 0 );
559 oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
560 if ( oc_map->bom_oc == NULL ) {
561 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
562 "objectClass \"%s\" is not defined in schema\n",
563 oc_row.cols[ 1 ], 0, 0 );
564 return LDAP_OTHER; /* undefined objectClass ? */
567 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
568 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
569 oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL
570 : ch_strdup( oc_row.cols[ 4 ] );
573 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
575 oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] < 0 )
576 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
578 oc_map->bom_delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL
579 : ch_strdup( oc_row.cols[ colnum ] );
580 oc_map->bom_expect_return = strtol( oc_row.cols[ colnum + 1 ],
584 if ( ( oc_row.ncols > colnum ) &&
585 ( oc_row.value_len[ colnum ] > 0 ) )
589 oc_map->bom_create_hint = NULL;
590 rc = slap_str2ad( oc_row.cols[ colnum ],
591 &oc_map->bom_create_hint, &text );
592 if ( rc != SQL_SUCCESS ) {
593 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
595 "AttributeDescription %s "
596 "in create_hint: %s (%d)\n",
597 oc_row.cols[ colnum ],
599 backsql_PrintErrors( bi->sql_db_env, dbh,
606 * FIXME: first attempt to check for offending
607 * instructions in {create|delete}_proc
610 oc_map->bom_attrs = NULL;
611 if ( avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, avl_dup_error ) == -1 ) {
612 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
613 "duplicate objectClass \"%s\" in objectClass map\n",
614 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
617 if ( avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, avl_dup_error ) == -1 ) {
618 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
619 "duplicate objectClass \"%s\" in objectClass by ID map\n",
620 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
623 oc_id = oc_map->bom_id;
624 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
625 "objectClass \"%s\":\n keytbl=\"%s\" keycol=\"%s\"\n",
626 BACKSQL_OC_NAME( oc_map ),
627 oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
628 if ( oc_map->bom_create_proc ) {
629 Debug( LDAP_DEBUG_TRACE, " create_proc=\"%s\"\n",
630 oc_map->bom_create_proc, 0, 0 );
632 if ( oc_map->bom_create_keyval ) {
633 Debug( LDAP_DEBUG_TRACE, " create_keyval=\"%s\"\n",
634 oc_map->bom_create_keyval, 0, 0 );
636 if ( oc_map->bom_create_hint ) {
637 Debug( LDAP_DEBUG_TRACE, " create_hint=\"%s\"\n",
638 oc_map->bom_create_hint->ad_cname.bv_val,
641 if ( oc_map->bom_delete_proc ) {
642 Debug( LDAP_DEBUG_TRACE, " delete_proc=\"%s\"\n",
643 oc_map->bom_delete_proc, 0, 0 );
645 Debug( LDAP_DEBUG_TRACE, " expect_return: "
646 "add=%d, del=%d; attributes:\n",
647 BACKSQL_IS_ADD( oc_map->bom_expect_return ),
648 BACKSQL_IS_DEL( oc_map->bom_expect_return ), 0 );
651 backsql_FreeRow( &oc_row );
652 SQLFreeStmt( sth, SQL_DROP );
654 /* prepare for attribute fetching */
655 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): at_query \"%s\"\n",
656 bi->sql_at_query, 0, 0 );
658 rc = backsql_Prepare( dbh, &sth, bi->sql_at_query, 0 );
659 if ( rc != SQL_SUCCESS ) {
660 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
661 "error preparing at_query: \"%s\"\n",
662 bi->sql_at_query, 0, 0 );
663 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
667 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT, &oc_id );
668 if ( rc != SQL_SUCCESS ) {
669 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
670 "error binding param \"oc_id\" for at_query\n", 0, 0, 0 );
671 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
672 SQLFreeStmt( sth, SQL_DROP );
679 bas.bas_oc_id = &oc_id;
680 bas.bas_rc = LDAP_SUCCESS;
682 (void)avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
683 &bas, BACKSQL_AVL_STOP, AVL_INORDER );
685 SQLFreeStmt( sth, SQL_DROP );
687 bi->sql_flags |= BSQLF_SCHEMA_LOADED;
689 Debug( LDAP_DEBUG_TRACE, "<==backsql_load_schema_map()\n", 0, 0, 0 );
695 backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
697 backsql_oc_map_rec tmp, *res;
700 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
701 "searching for objectclass with name=\"%s\"\n",
702 oc->soc_cname.bv_val, 0, 0 );
703 #endif /* BACKSQL_TRACE */
706 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
709 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
710 "found name=\"%s\", id=%d\n",
711 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
713 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
714 "not found\n", 0, 0, 0 );
716 #endif /* BACKSQL_TRACE */
722 backsql_name2oc( backsql_info *bi, struct berval *oc_name )
724 backsql_oc_map_rec tmp, *res;
727 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
728 "searching for objectclass with name=\"%s\"\n",
729 oc_name->bv_val, 0, 0 );
730 #endif /* BACKSQL_TRACE */
732 tmp.bom_oc = oc_bvfind( oc_name );
733 if ( tmp.bom_oc == NULL ) {
737 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
740 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
741 "found name=\"%s\", id=%d\n",
742 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
744 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
745 "not found\n", 0, 0, 0 );
747 #endif /* BACKSQL_TRACE */
753 backsql_id2oc( backsql_info *bi, unsigned long id )
755 backsql_oc_map_rec tmp, *res;
758 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
759 "searching for objectclass with id='%d'\n", id, 0, 0 );
760 #endif /* BACKSQL_TRACE */
763 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_id, &tmp,
768 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
769 "found name=\"%s\", id=%d\n",
770 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
772 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
773 "not found\n", 0, 0, 0 );
775 #endif /* BACKSQL_TRACE */
781 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
783 backsql_at_map_rec tmp, *res;
786 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
787 "searching for attribute \"%s\" for objectclass \"%s\"\n",
788 ad->ad_cname.bv_val, BACKSQL_OC_NAME( objclass ), 0 );
789 #endif /* BACKSQL_TRACE */
792 res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp,
797 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
798 "found name=\"%s\", sel_expr=\"%s\"\n",
799 res->bam_ad->ad_cname.bv_val,
800 res->bam_sel_expr.bv_val, 0 );
802 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
803 "not found\n", 0, 0, 0 );
805 #endif /* BACKSQL_TRACE */
810 /* attributeType inheritance */
812 backsql_at_map_rec **ret;
813 AttributeDescription *ad;
817 #define SUPAD2AT_STOP (-1)
820 supad2at_f( void *v_at, void *v_arg )
822 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
823 struct supad2at_t *va = (struct supad2at_t *)v_arg;
825 if ( is_at_subtype( at->bam_ad->ad_type, va->ad->ad_type ) ) {
826 backsql_at_map_rec **ret;
829 /* if already listed, holler! (should never happen) */
831 for ( i = 0; i < va->n; i++ ) {
832 if ( va->ret[ i ]->bam_ad == at->bam_ad ) {
842 ret = ch_realloc( va->ret,
843 sizeof( backsql_at_map_rec *) * ( va->n + 2 ) );
846 return SUPAD2AT_STOP;
859 * stores in *pret a NULL terminated array of pointers
860 * to backsql_at_map_rec whose attributeType is supad->ad_type
864 backsql_supad2at( backsql_oc_map_rec *objclass, AttributeDescription *supad,
865 backsql_at_map_rec ***pret )
867 struct supad2at_t va;
880 rc = avl_apply( objclass->bom_attrs, supad2at_f, &va,
881 SUPAD2AT_STOP, AVL_INORDER );
882 if ( rc == SUPAD2AT_STOP ) {
892 backsql_free_attr( void *v_at )
894 backsql_at_map_rec *at = v_at;
896 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): \"%s\"\n",
897 at->bam_ad->ad_cname.bv_val, 0, 0 );
898 ch_free( at->bam_sel_expr.bv_val );
899 if ( !BER_BVISNULL( &at->bam_from_tbls ) ) {
900 ch_free( at->bam_from_tbls.bv_val );
902 if ( !BER_BVISNULL( &at->bam_join_where ) ) {
903 ch_free( at->bam_join_where.bv_val );
905 if ( at->bam_add_proc != NULL ) {
906 ch_free( at->bam_add_proc );
908 if ( at->bam_delete_proc != NULL ) {
909 ch_free( at->bam_delete_proc );
911 if ( at->bam_query != NULL ) {
912 ch_free( at->bam_query );
916 if ( !BER_BVISNULL( &at->bam_sel_expr_u ) ) {
917 ch_free( at->bam_sel_expr_u.bv_val );
920 if ( at->bam_next ) {
921 backsql_free_attr( at->bam_next );
926 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
930 backsql_free_oc( void *v_oc )
932 backsql_oc_map_rec *oc = v_oc;
934 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): \"%s\"\n",
935 BACKSQL_OC_NAME( oc ), 0, 0 );
936 avl_free( oc->bom_attrs, backsql_free_attr );
937 ch_free( oc->bom_keytbl.bv_val );
938 ch_free( oc->bom_keycol.bv_val );
939 if ( oc->bom_create_proc != NULL ) {
940 ch_free( oc->bom_create_proc );
942 if ( oc->bom_create_keyval != NULL ) {
943 ch_free( oc->bom_create_keyval );
945 if ( oc->bom_delete_proc != NULL ) {
946 ch_free( oc->bom_delete_proc );
950 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
954 backsql_destroy_schema_map( backsql_info *bi )
956 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
957 avl_free( bi->sql_oc_by_oc, 0 );
958 avl_free( bi->sql_oc_by_id, backsql_free_oc );
959 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );