2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * Portions Copyright 2004 Mark Adamson.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Dmitry Kovalev for inclusion
20 * by OpenLDAP Software. Additional significant contributors include
21 * Pierangelo Masarati and Mark Adamson.
27 #include <sys/types.h>
28 #include "ac/string.h"
31 #include "proto-sql.h"
33 #define BACKSQL_DUPLICATE (-1)
35 /* NOTE: by default, cannot just compare pointers because
36 * objectClass/attributeType order would be machine-dependent
37 * (and tests would fail!); however, if you don't want to run
38 * tests, or see attributeTypes written in the same order
39 * they are defined, define */
40 /* #undef BACKSQL_USE_PTR_CMP */
43 * Uses the pointer to the ObjectClass structure
46 backsql_cmp_oc( const void *v_m1, const void *v_m2 )
48 const backsql_oc_map_rec *m1 = v_m1,
51 #ifdef BACKSQL_USE_PTR_CMP
52 return SLAP_PTRCMP( m1->bom_oc, m2->bom_oc );
53 #else /* ! BACKSQL_USE_PTR_CMP */
54 return ber_bvcmp( &m1->bom_oc->soc_cname, &m2->bom_oc->soc_cname );
55 #endif /* ! BACKSQL_USE_PTR_CMP */
59 backsql_cmp_oc_id( const void *v_m1, const void *v_m2 )
61 const backsql_oc_map_rec *m1 = v_m1,
64 return ( m1->bom_id < m2->bom_id ? -1 : ( m1->bom_id > m2->bom_id ? 1 : 0 ) );
68 * Uses the pointer to the AttributeDescription structure
71 backsql_cmp_attr( const void *v_m1, const void *v_m2 )
73 const backsql_at_map_rec *m1 = v_m1,
76 #ifdef BACKSQL_USE_PTR_CMP
77 return SLAP_PTRCMP( m1->bam_ad, m2->bam_ad );
78 #else /* ! BACKSQL_USE_PTR_CMP */
79 return ber_bvcmp( &m1->bam_ad->ad_cname, &m2->bam_ad->ad_cname );
80 #endif /* ! BACKSQL_USE_PTR_CMP */
84 backsql_dup_attr( void *v_m1, void *v_m2 )
86 backsql_at_map_rec *m1 = v_m1,
89 assert( m1->bam_ad == m2->bam_ad );
91 /* duplicate definitions of attributeTypes are appended;
92 * this allows to define multiple rules for the same
93 * attributeType. Use with care! */
94 for ( ; m1->bam_next ; m1 = m1->bam_next );
98 return BACKSQL_DUPLICATE;
102 backsql_make_attr_query(
103 backsql_oc_map_rec *oc_map,
104 backsql_at_map_rec *at_map )
106 struct berbuf bb = BB_NULL;
108 #ifdef BACKSQL_ALIASING_QUOTE
109 backsql_strfcat( &bb, "lblcbclblbcbl",
110 (ber_len_t)STRLENOF( "SELECT " ), "SELECT ",
111 &at_map->bam_sel_expr,
112 (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ), " " BACKSQL_ALIASING,
113 BACKSQL_ALIASING_QUOTE,
114 &at_map->bam_ad->ad_cname,
115 BACKSQL_ALIASING_QUOTE,
116 (ber_len_t)STRLENOF( " FROM " ), " FROM ",
117 &at_map->bam_from_tbls,
118 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
122 (ber_len_t)STRLENOF( "=?" ), "=?" );
123 #else /* ! BACKSQL_ALIASING_QUOTE */
124 backsql_strfcat( &bb, "lblblblbcbl",
125 (ber_len_t)STRLENOF( "SELECT " ), "SELECT ",
126 &at_map->bam_sel_expr,
127 (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ), " " BACKSQL_ALIASING,
128 &at_map->bam_ad->ad_cname,
129 (ber_len_t)STRLENOF( " FROM " ), " FROM ",
130 &at_map->bam_from_tbls,
131 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
135 (ber_len_t)STRLENOF( "=?" ), "=?" );
136 #endif /* ! BACKSQL_ALIASING_QUOTE */
138 if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
139 backsql_strfcat( &bb, "lb",
140 (ber_len_t)STRLENOF( " AND " ), " AND ",
141 &at_map->bam_join_where );
144 #ifdef BACKSQL_ALIASING_QUOTE
145 backsql_strfcat( &bb, "lcbc",
146 (ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ",
147 BACKSQL_ALIASING_QUOTE,
148 &at_map->bam_sel_expr,
149 BACKSQL_ALIASING_QUOTE );
150 #else /* ! BACKSQL_ALIASING_QUOTE */
151 backsql_strfcat( &bb, "lb",
152 (ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ",
153 &at_map->bam_sel_expr );
154 #endif /* ! BACKSQL_ALIASING_QUOTE */
156 at_map->bam_query = bb.bb_val.bv_val;
158 #ifdef BACKSQL_COUNTQUERY
159 /* Query to count how many rows will be returned. */
160 BER_BVZERO( &bb.bb_val );
162 backsql_strfcat( &bb, "lblbcbl",
163 (ber_len_t)STRLENOF( "SELECT COUNT(*) FROM " ),
164 "SELECT COUNT(*) FROM ",
165 &at_map->bam_from_tbls,
166 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
170 (ber_len_t)STRLENOF( "=?" ), "=?" );
172 if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
173 backsql_strfcat( &bb, "lb",
174 (ber_len_t)STRLENOF( " AND " ), " AND ",
175 &at_map->bam_join_where );
178 at_map->bam_countquery = bb.bb_val.bv_val;
179 #endif /* BACKSQL_COUNTQUERY */
185 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
187 backsql_at_map_rec *at_map;
188 char s[] = "+9223372036854775807L";
193 snprintf( s, sizeof( s ), "%ld", oc_map->bom_id );
194 sbv.bv_len = strlen( s );
196 /* extra objectClasses */
197 at_map = (backsql_at_map_rec *)ch_calloc(1,
198 sizeof( backsql_at_map_rec ) );
199 at_map->bam_ad = slap_schema.si_ad_objectClass;
200 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
201 &at_map->bam_sel_expr );
202 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
203 &at_map->bam_from_tbls );
205 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
206 bb.bb_val = at_map->bam_from_tbls;
207 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
208 at_map->bam_from_tbls = bb.bb_val;
210 BER_BVZERO( &bb.bb_val );
212 backsql_strfcat( &bb, "lbcblb",
213 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
214 "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
218 (ber_len_t)STRLENOF( " and ldap_entries.oc_map_id=" ),
219 " and ldap_entries.oc_map_id=",
221 at_map->bam_join_where = bb.bb_val;
223 at_map->bam_oc = oc_map->bom_oc;
225 at_map->bam_add_proc = NULL;
228 "INSERT INTO ldap_entry_objclasses "
229 "(entry_id,oc_name) VALUES "
230 "((SELECT id FROM ldap_entries "
232 "18446744073709551615UL " /* 64 bit ULONG */
234 snprintf( tmp, sizeof(tmp),
235 "INSERT INTO ldap_entry_objclasses "
236 "(entry_id,oc_name) VALUES "
237 "((SELECT id FROM ldap_entries "
238 "WHERE oc_map_id=%lu "
239 "AND keyval=?),?)", oc_map->bom_id );
240 at_map->bam_add_proc = ch_strdup( tmp );
243 at_map->bam_delete_proc = NULL;
246 "DELETE FROM ldap_entry_objclasses "
247 "WHERE entry_id=(SELECT id FROM ldap_entries "
249 "18446744073709551615UL " /* 64 bit ULONG */
250 "AND keyval=?) AND oc_name=?";
251 snprintf( tmp, sizeof(tmp),
252 "DELETE FROM ldap_entry_objclasses "
253 "WHERE entry_id=(SELECT id FROM ldap_entries "
254 "WHERE oc_map_id=%lu"
255 "AND keyval=?) AND oc_name=?",
257 at_map->bam_delete_proc = ch_strdup( tmp );
260 at_map->bam_param_order = 0;
261 at_map->bam_expect_return = 0;
262 at_map->bam_next = NULL;
264 backsql_make_attr_query( oc_map, at_map );
265 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
266 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
267 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
268 at_map->bam_ad->ad_cname.bv_val,
269 oc_map->bom_oc->soc_cname.bv_val, 0 );
272 /* FIXME: we need to correct the objectClass join_where
273 * after the attribute query is built */
274 ch_free( at_map->bam_join_where.bv_val );
275 BER_BVZERO( &bb.bb_val );
277 backsql_strfcat( &bb, "lbcblb",
278 (ber_len_t)STRLENOF( /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=" ),
279 /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=",
283 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
284 " AND ldap_entries.oc_map_id=",
286 at_map->bam_join_where = bb.bb_val;
288 /* referral attribute */
289 at_map = (backsql_at_map_rec *)ch_calloc( 1,
290 sizeof( backsql_at_map_rec ) );
291 at_map->bam_ad = slap_schema.si_ad_ref;
292 ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->bam_sel_expr );
293 ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->bam_from_tbls );
295 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
296 bb.bb_val = at_map->bam_from_tbls;
297 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
298 at_map->bam_from_tbls = bb.bb_val;
300 BER_BVZERO( &bb.bb_val );
302 backsql_strfcat( &bb, "lbcblb",
303 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=" ),
304 "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=",
308 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
309 " AND ldap_entries.oc_map_id=",
312 at_map->bam_join_where = bb.bb_val;
314 at_map->bam_oc = NULL;
316 at_map->bam_add_proc = NULL;
319 "INSERT INTO ldap_referrals "
320 "(entry_id,url) VALUES "
321 "((SELECT id FROM ldap_entries "
323 "18446744073709551615UL " /* 64 bit ULONG */
325 snprintf( tmp, sizeof(tmp),
326 "INSERT INTO ldap_referrals "
327 "(entry_id,url) VALUES "
328 "((SELECT id FROM ldap_entries "
329 "WHERE oc_map_id=%lu "
330 "AND keyval=?),?)", oc_map->bom_id );
331 at_map->bam_add_proc = ch_strdup( tmp );
334 at_map->bam_delete_proc = NULL;
337 "DELETE FROM ldap_referrals "
338 "WHERE entry_id=(SELECT id FROM ldap_entries "
340 "18446744073709551615UL " /* 64 bit ULONG */
341 "AND keyval=?) and url=?";
342 snprintf( tmp, sizeof(tmp),
343 "DELETE FROM ldap_referrals "
344 "WHERE entry_id=(SELECT id FROM ldap_entries "
345 "WHERE oc_map_id=%lu"
346 "AND keyval=?) and url=?",
348 at_map->bam_delete_proc = ch_strdup( tmp );
351 at_map->bam_param_order = 0;
352 at_map->bam_expect_return = 0;
353 at_map->bam_next = NULL;
355 backsql_make_attr_query( oc_map, at_map );
356 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
357 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
358 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
359 at_map->bam_ad->ad_cname.bv_val,
360 oc_map->bom_oc->soc_cname.bv_val, 0 );
366 struct backsql_attr_schema_info {
367 backsql_info *bas_bi;
370 unsigned long *bas_oc_id;
375 backsql_oc_get_attr_mapping( void *v_oc, void *v_bas )
378 BACKSQL_ROW_NTS at_row;
379 backsql_oc_map_rec *oc_map = (backsql_oc_map_rec *)v_oc;
380 backsql_at_map_rec *at_map;
381 struct backsql_attr_schema_info *bas = (struct backsql_attr_schema_info *)v_bas;
383 /* bas->bas_oc_id has been bound to bas->bas_sth */
384 *bas->bas_oc_id = oc_map->bom_id;
386 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
387 "executing at_query\n"
389 " for objectClass \"%s\"\n"
390 " with param oc_id=\"%lu\"\n",
391 bas->bas_bi->sql_at_query,
392 BACKSQL_OC_NAME( oc_map ),
395 rc = SQLExecute( bas->bas_sth );
396 if ( rc != SQL_SUCCESS ) {
397 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
398 "error executing at_query\n"
400 " for objectClass \"%s\"\n"
401 " with param oc_id=\"%lu\"\n",
402 bas->bas_bi->sql_at_query,
403 BACKSQL_OC_NAME( oc_map ),
405 backsql_PrintErrors( bas->bas_bi->sql_db_env,
406 bas->bas_dbh, bas->bas_sth, rc );
407 bas->bas_rc = LDAP_OTHER;
408 return BACKSQL_AVL_STOP;
411 backsql_BindRowAsStrings( bas->bas_sth, &at_row );
412 for ( ; rc = SQLFetch( bas->bas_sth ), BACKSQL_SUCCESS( rc ); ) {
413 const char *text = NULL;
416 struct berbuf bb = BB_NULL;
418 Debug( LDAP_DEBUG_TRACE,
421 "\tsel_expr=\"%s\"\n"
423 at_row.cols[ 0 ], at_row.cols[ 1 ],
425 Debug( LDAP_DEBUG_TRACE,
426 "\tjoin_where=\"%s\"\n"
427 "\tadd_proc=\"%s\"\n"
428 "\tdelete_proc=\"%s\"\n",
429 at_row.cols[ 3 ], at_row.cols[ 4 ],
432 Debug( LDAP_DEBUG_TRACE, "\tsel_expr_u=\"%s\"\n",
433 at_row.cols[ 8 ], 0, 0 );
434 at_map = (backsql_at_map_rec *)ch_calloc( 1,
435 sizeof( backsql_at_map_rec ) );
436 rc = slap_str2ad( at_row.cols[ 0 ],
437 &at_map->bam_ad, &text );
438 if ( rc != LDAP_SUCCESS ) {
439 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
440 "attribute \"%s\" for objectClass \"%s\" "
441 "is not defined in schema: %s\n",
443 BACKSQL_OC_NAME( oc_map ), text );
444 bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
445 return BACKSQL_AVL_STOP;
448 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
449 if ( at_row.value_len[ 8 ] < 0 ) {
450 BER_BVZERO( &at_map->bam_sel_expr_u );
453 ber_str2bv( at_row.cols[ 8 ], 0, 1,
454 &at_map->bam_sel_expr_u );
457 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
458 backsql_merge_from_clause( &bb, &bv );
459 at_map->bam_from_tbls = bb.bb_val;
460 if ( at_row.value_len[ 3 ] < 0 ) {
461 BER_BVZERO( &at_map->bam_join_where );
464 ber_str2bv( at_row.cols[ 3 ], 0, 1,
465 &at_map->bam_join_where );
467 at_map->bam_add_proc = NULL;
468 if ( at_row.value_len[ 4 ] > 0 ) {
469 at_map->bam_add_proc = ch_strdup( at_row.cols[4] );
471 at_map->bam_delete_proc = NULL;
472 if ( at_row.value_len[ 5 ] > 0 ) {
473 at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
475 at_map->bam_param_order = strtol( at_row.cols[ 6 ],
477 if ( next == at_row.cols[ 6 ] || next[0] != '\0' ) {
480 at_map->bam_expect_return = strtol( at_row.cols[ 7 ],
482 if ( next == at_row.cols[ 7 ] || next[0] != '\0' ) {
485 backsql_make_attr_query( oc_map, at_map );
486 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
487 "preconstructed query \"%s\"\n",
488 at_map->bam_query, 0, 0 );
489 at_map->bam_next = NULL;
490 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
491 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
492 "duplicate attribute \"%s\" "
493 "in objectClass \"%s\" map\n",
494 at_map->bam_ad->ad_cname.bv_val,
495 oc_map->bom_oc->soc_cname.bv_val, 0 );
498 if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
499 BER_BVISNULL( &at_map->bam_sel_expr_u ) )
501 struct berbuf bb = BB_NULL;
503 backsql_strfcat( &bb, "bcbc",
504 &bas->bas_bi->sql_upper_func,
506 &at_map->bam_sel_expr,
508 at_map->bam_sel_expr_u = bb.bb_val;
511 backsql_FreeRow( &at_row );
512 SQLFreeStmt( bas->bas_sth, SQL_CLOSE );
514 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
515 "autoadding 'objectClass' and 'ref' mappings\n",
516 BACKSQL_OC_NAME( oc_map ), 0, 0 );
518 (void)backsql_add_sysmaps( oc_map );
520 return BACKSQL_AVL_CONTINUE;
525 backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
527 SQLHSTMT sth = SQL_NULL_HSTMT;
529 BACKSQL_ROW_NTS oc_row;
531 backsql_oc_map_rec *oc_map;
532 struct backsql_attr_schema_info bas;
534 Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n", 0, 0, 0 );
537 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
539 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
540 rc = backsql_Prepare( dbh, &sth,
541 backsql_check_dn_ru_query, 0 );
542 if ( rc == SQL_SUCCESS ) {
543 /* Yes, the field exists */
544 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
545 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
546 "in the schema\n", 0, 0, 0 );
548 /* No such field exists */
549 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
552 SQLFreeStmt( sth, SQL_DROP );
555 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n",
556 bi->sql_oc_query, 0, 0 );
558 rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
559 if ( rc != SQL_SUCCESS ) {
560 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
561 "error preparing oc_query: \"%s\"\n",
562 bi->sql_oc_query, 0, 0 );
563 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
567 rc = SQLExecute( sth );
568 if ( rc != SQL_SUCCESS ) {
569 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
570 "error executing oc_query: \n", 0, 0, 0 );
571 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
575 backsql_BindRowAsStrings( sth, &oc_row );
576 rc = SQLFetch( sth );
577 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
580 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
581 sizeof( backsql_oc_map_rec ) );
583 oc_map->bom_id = strtol( oc_row.cols[ 0 ], NULL, 0 );
585 oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
586 if ( oc_map->bom_oc == NULL ) {
587 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
588 "objectClass \"%s\" is not defined in schema\n",
589 oc_row.cols[ 1 ], 0, 0 );
590 return LDAP_OTHER; /* undefined objectClass ? */
593 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
594 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
595 oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL
596 : ch_strdup( oc_row.cols[ 4 ] );
599 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
601 oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] < 0 )
602 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
604 oc_map->bom_delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL
605 : ch_strdup( oc_row.cols[ colnum ] );
606 oc_map->bom_expect_return = strtol( oc_row.cols[ colnum + 1 ],
610 if ( ( oc_row.ncols > colnum ) &&
611 ( oc_row.value_len[ colnum ] > 0 ) )
615 oc_map->bom_create_hint = NULL;
616 rc = slap_str2ad( oc_row.cols[ colnum ],
617 &oc_map->bom_create_hint, &text );
618 if ( rc != SQL_SUCCESS ) {
619 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
621 "AttributeDescription %s "
622 "in create_hint: %s (%d)\n",
623 oc_row.cols[ colnum ],
625 backsql_PrintErrors( bi->sql_db_env, dbh,
632 * FIXME: first attempt to check for offending
633 * instructions in {create|delete}_proc
636 oc_map->bom_attrs = NULL;
637 if ( avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, avl_dup_error ) == -1 ) {
638 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
639 "duplicate objectClass \"%s\" in objectClass map\n",
640 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
643 if ( avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, avl_dup_error ) == -1 ) {
644 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
645 "duplicate objectClass \"%s\" in objectClass by ID map\n",
646 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
649 oc_id = oc_map->bom_id;
650 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
651 "objectClass \"%s\":\n keytbl=\"%s\" keycol=\"%s\"\n",
652 BACKSQL_OC_NAME( oc_map ),
653 oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
654 if ( oc_map->bom_create_proc ) {
655 Debug( LDAP_DEBUG_TRACE, " create_proc=\"%s\"\n",
656 oc_map->bom_create_proc, 0, 0 );
658 if ( oc_map->bom_create_keyval ) {
659 Debug( LDAP_DEBUG_TRACE, " create_keyval=\"%s\"\n",
660 oc_map->bom_create_keyval, 0, 0 );
662 if ( oc_map->bom_create_hint ) {
663 Debug( LDAP_DEBUG_TRACE, " create_hint=\"%s\"\n",
664 oc_map->bom_create_hint->ad_cname.bv_val,
667 if ( oc_map->bom_delete_proc ) {
668 Debug( LDAP_DEBUG_TRACE, " delete_proc=\"%s\"\n",
669 oc_map->bom_delete_proc, 0, 0 );
671 Debug( LDAP_DEBUG_TRACE, " expect_return: "
672 "add=%d, del=%d; attributes:\n",
673 BACKSQL_IS_ADD( oc_map->bom_expect_return ),
674 BACKSQL_IS_DEL( oc_map->bom_expect_return ), 0 );
677 backsql_FreeRow( &oc_row );
678 SQLFreeStmt( sth, SQL_DROP );
680 /* prepare for attribute fetching */
681 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): at_query \"%s\"\n",
682 bi->sql_at_query, 0, 0 );
684 rc = backsql_Prepare( dbh, &sth, bi->sql_at_query, 0 );
685 if ( rc != SQL_SUCCESS ) {
686 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
687 "error preparing at_query: \"%s\"\n",
688 bi->sql_at_query, 0, 0 );
689 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
693 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT, &oc_id );
694 if ( rc != SQL_SUCCESS ) {
695 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
696 "error binding param \"oc_id\" for at_query\n", 0, 0, 0 );
697 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
698 SQLFreeStmt( sth, SQL_DROP );
705 bas.bas_oc_id = &oc_id;
706 bas.bas_rc = LDAP_SUCCESS;
708 (void)avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
709 &bas, BACKSQL_AVL_STOP, AVL_INORDER );
711 SQLFreeStmt( sth, SQL_DROP );
713 bi->sql_flags |= BSQLF_SCHEMA_LOADED;
715 Debug( LDAP_DEBUG_TRACE, "<==backsql_load_schema_map()\n", 0, 0, 0 );
721 backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
723 backsql_oc_map_rec tmp, *res;
726 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
727 "searching for objectclass with name=\"%s\"\n",
728 oc->soc_cname.bv_val, 0, 0 );
729 #endif /* BACKSQL_TRACE */
732 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
735 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
736 "found name=\"%s\", id=%d\n",
737 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
739 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
740 "not found\n", 0, 0, 0 );
742 #endif /* BACKSQL_TRACE */
748 backsql_name2oc( backsql_info *bi, struct berval *oc_name )
750 backsql_oc_map_rec tmp, *res;
753 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
754 "searching for objectclass with name=\"%s\"\n",
755 oc_name->bv_val, 0, 0 );
756 #endif /* BACKSQL_TRACE */
758 tmp.bom_oc = oc_bvfind( oc_name );
759 if ( tmp.bom_oc == NULL ) {
763 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
766 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
767 "found name=\"%s\", id=%d\n",
768 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
770 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
771 "not found\n", 0, 0, 0 );
773 #endif /* BACKSQL_TRACE */
779 backsql_id2oc( backsql_info *bi, unsigned long id )
781 backsql_oc_map_rec tmp, *res;
784 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
785 "searching for objectclass with id='%d'\n", id, 0, 0 );
786 #endif /* BACKSQL_TRACE */
789 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_id, &tmp,
794 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
795 "found name=\"%s\", id=%d\n",
796 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
798 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
799 "not found\n", 0, 0, 0 );
801 #endif /* BACKSQL_TRACE */
807 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
809 backsql_at_map_rec tmp, *res;
812 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
813 "searching for attribute \"%s\" for objectclass \"%s\"\n",
814 ad->ad_cname.bv_val, BACKSQL_OC_NAME( objclass ), 0 );
815 #endif /* BACKSQL_TRACE */
818 res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp,
823 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
824 "found name=\"%s\", sel_expr=\"%s\"\n",
825 res->bam_ad->ad_cname.bv_val,
826 res->bam_sel_expr.bv_val, 0 );
828 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
829 "not found\n", 0, 0, 0 );
831 #endif /* BACKSQL_TRACE */
836 /* attributeType inheritance */
838 backsql_at_map_rec **ret;
839 AttributeDescription *ad;
843 #define SUPAD2AT_STOP (-1)
846 supad2at_f( void *v_at, void *v_arg )
848 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
849 struct supad2at_t *va = (struct supad2at_t *)v_arg;
851 if ( is_at_subtype( at->bam_ad->ad_type, va->ad->ad_type ) ) {
852 backsql_at_map_rec **ret = NULL;
855 /* if already listed, holler! (should never happen) */
857 for ( i = 0; i < va->n; i++ ) {
858 if ( va->ret[ i ]->bam_ad == at->bam_ad ) {
868 ret = ch_realloc( va->ret,
869 sizeof( backsql_at_map_rec * ) * ( va->n + 2 ) );
874 return SUPAD2AT_STOP;
887 * stores in *pret a NULL terminated array of pointers
888 * to backsql_at_map_rec whose attributeType is supad->ad_type
892 backsql_supad2at( backsql_oc_map_rec *objclass, AttributeDescription *supad,
893 backsql_at_map_rec ***pret )
895 struct supad2at_t va = { 0 };
906 rc = avl_apply( objclass->bom_attrs, supad2at_f, &va,
907 SUPAD2AT_STOP, AVL_INORDER );
908 if ( rc == SUPAD2AT_STOP ) {
918 backsql_free_attr( void *v_at )
920 backsql_at_map_rec *at = v_at;
922 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): \"%s\"\n",
923 at->bam_ad->ad_cname.bv_val, 0, 0 );
924 ch_free( at->bam_sel_expr.bv_val );
925 if ( !BER_BVISNULL( &at->bam_from_tbls ) ) {
926 ch_free( at->bam_from_tbls.bv_val );
928 if ( !BER_BVISNULL( &at->bam_join_where ) ) {
929 ch_free( at->bam_join_where.bv_val );
931 if ( at->bam_add_proc != NULL ) {
932 ch_free( at->bam_add_proc );
934 if ( at->bam_delete_proc != NULL ) {
935 ch_free( at->bam_delete_proc );
937 if ( at->bam_query != NULL ) {
938 ch_free( at->bam_query );
942 if ( !BER_BVISNULL( &at->bam_sel_expr_u ) ) {
943 ch_free( at->bam_sel_expr_u.bv_val );
946 if ( at->bam_next ) {
947 backsql_free_attr( at->bam_next );
952 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
956 backsql_free_oc( void *v_oc )
958 backsql_oc_map_rec *oc = v_oc;
960 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): \"%s\"\n",
961 BACKSQL_OC_NAME( oc ), 0, 0 );
962 avl_free( oc->bom_attrs, backsql_free_attr );
963 ch_free( oc->bom_keytbl.bv_val );
964 ch_free( oc->bom_keycol.bv_val );
965 if ( oc->bom_create_proc != NULL ) {
966 ch_free( oc->bom_create_proc );
968 if ( oc->bom_create_keyval != NULL ) {
969 ch_free( oc->bom_create_keyval );
971 if ( oc->bom_delete_proc != NULL ) {
972 ch_free( oc->bom_delete_proc );
976 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
980 backsql_destroy_schema_map( backsql_info *bi )
982 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
983 avl_free( bi->sql_oc_by_oc, 0 );
984 avl_free( bi->sql_oc_by_id, backsql_free_oc );
985 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );