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;
162 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
164 backsql_at_map_rec *at_map;
165 char s[] = "+9223372036854775807L";
170 snprintf( s, sizeof( s ), "%ld", oc_map->bom_id );
171 sbv.bv_len = strlen( s );
173 /* extra objectClasses */
174 at_map = (backsql_at_map_rec *)ch_calloc(1,
175 sizeof( backsql_at_map_rec ) );
176 at_map->bam_ad = slap_schema.si_ad_objectClass;
177 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
178 &at_map->bam_sel_expr );
179 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
180 &at_map->bam_from_tbls );
182 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
183 bb.bb_val = at_map->bam_from_tbls;
184 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
185 at_map->bam_from_tbls = bb.bb_val;
187 BER_BVZERO( &bb.bb_val );
189 backsql_strfcat( &bb, "lbcblb",
190 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
191 "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
195 (ber_len_t)STRLENOF( " and ldap_entries.oc_map_id=" ),
196 " and ldap_entries.oc_map_id=",
198 at_map->bam_join_where = bb.bb_val;
200 at_map->bam_oc = oc_map->bom_oc;
202 at_map->bam_add_proc = NULL;
205 "INSERT INTO ldap_entry_objclasses "
206 "(entry_id,oc_name) VALUES "
207 "((SELECT id FROM ldap_entries "
209 "18446744073709551615UL " /* 64 bit ULONG */
211 snprintf( tmp, sizeof(tmp),
212 "INSERT INTO ldap_entry_objclasses "
213 "(entry_id,oc_name) VALUES "
214 "((SELECT id FROM ldap_entries "
215 "WHERE oc_map_id=%lu "
216 "AND keyval=?),?)", oc_map->bom_id );
217 at_map->bam_add_proc = ch_strdup( tmp );
220 at_map->bam_delete_proc = NULL;
223 "DELETE FROM ldap_entry_objclasses "
224 "WHERE entry_id=(SELECT id FROM ldap_entries "
226 "18446744073709551615UL " /* 64 bit ULONG */
227 "AND keyval=?) AND oc_name=?";
228 snprintf( tmp, sizeof(tmp),
229 "DELETE FROM ldap_entry_objclasses "
230 "WHERE entry_id=(SELECT id FROM ldap_entries "
231 "WHERE oc_map_id=%lu"
232 "AND keyval=?) AND oc_name=?",
234 at_map->bam_delete_proc = ch_strdup( tmp );
237 at_map->bam_param_order = 0;
238 at_map->bam_expect_return = 0;
239 at_map->bam_next = NULL;
241 backsql_make_attr_query( oc_map, at_map );
242 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
243 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
244 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
245 at_map->bam_ad->ad_cname.bv_val,
246 oc_map->bom_oc->soc_cname.bv_val, 0 );
249 /* FIXME: we need to correct the objectClass join_where
250 * after the attribute query is built */
251 ch_free( at_map->bam_join_where.bv_val );
252 BER_BVZERO( &bb.bb_val );
254 backsql_strfcat( &bb, "lbcblb",
255 (ber_len_t)STRLENOF( /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=" ),
256 /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=",
260 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
261 " AND ldap_entries.oc_map_id=",
263 at_map->bam_join_where = bb.bb_val;
265 /* referral attribute */
266 at_map = (backsql_at_map_rec *)ch_calloc( 1,
267 sizeof( backsql_at_map_rec ) );
268 at_map->bam_ad = slap_schema.si_ad_ref;
269 ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->bam_sel_expr );
270 ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->bam_from_tbls );
272 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
273 bb.bb_val = at_map->bam_from_tbls;
274 backsql_merge_from_clause( &bb, &oc_map->bom_keytbl );
275 at_map->bam_from_tbls = bb.bb_val;
277 BER_BVZERO( &bb.bb_val );
279 backsql_strfcat( &bb, "lbcblb",
280 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=" ),
281 "ldap_entries.id=ldap_referrals.entry_id AND ldap_entries.keyval=",
285 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
286 " AND ldap_entries.oc_map_id=",
289 at_map->bam_join_where = bb.bb_val;
291 at_map->bam_oc = NULL;
293 at_map->bam_add_proc = NULL;
296 "INSERT INTO ldap_referrals "
297 "(entry_id,url) VALUES "
298 "((SELECT id FROM ldap_entries "
300 "18446744073709551615UL " /* 64 bit ULONG */
302 snprintf( tmp, sizeof(tmp),
303 "INSERT INTO ldap_referrals "
304 "(entry_id,url) VALUES "
305 "((SELECT id FROM ldap_entries "
306 "WHERE oc_map_id=%lu "
307 "AND keyval=?),?)", oc_map->bom_id );
308 at_map->bam_add_proc = ch_strdup( tmp );
311 at_map->bam_delete_proc = NULL;
314 "DELETE FROM ldap_referrals "
315 "WHERE entry_id=(SELECT id FROM ldap_entries "
317 "18446744073709551615UL " /* 64 bit ULONG */
318 "AND keyval=?) and url=?";
319 snprintf( tmp, sizeof(tmp),
320 "DELETE FROM ldap_referrals "
321 "WHERE entry_id=(SELECT id FROM ldap_entries "
322 "WHERE oc_map_id=%lu"
323 "AND keyval=?) and url=?",
325 at_map->bam_delete_proc = ch_strdup( tmp );
328 at_map->bam_param_order = 0;
329 at_map->bam_expect_return = 0;
330 at_map->bam_next = NULL;
332 backsql_make_attr_query( oc_map, at_map );
333 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
334 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
335 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
336 at_map->bam_ad->ad_cname.bv_val,
337 oc_map->bom_oc->soc_cname.bv_val, 0 );
343 struct backsql_attr_schema_info {
344 backsql_info *bas_bi;
347 unsigned long *bas_oc_id;
352 backsql_oc_get_attr_mapping( void *v_oc, void *v_bas )
355 BACKSQL_ROW_NTS at_row;
356 backsql_oc_map_rec *oc_map = (backsql_oc_map_rec *)v_oc;
357 backsql_at_map_rec *at_map;
358 struct backsql_attr_schema_info *bas = (struct backsql_attr_schema_info *)v_bas;
360 /* bas->bas_oc_id has been bound to bas->bas_sth */
361 *bas->bas_oc_id = oc_map->bom_id;
363 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
364 "executing at_query\n"
366 " for objectClass \"%s\"\n"
367 " with param oc_id=\"%lu\"\n",
368 bas->bas_bi->sql_at_query,
369 BACKSQL_OC_NAME( oc_map ),
372 rc = SQLExecute( bas->bas_sth );
373 if ( rc != SQL_SUCCESS ) {
374 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
375 "error executing at_query\n"
377 " for objectClass \"%s\"\n"
378 " with param oc_id=\"%lu\"\n",
379 bas->bas_bi->sql_at_query,
380 BACKSQL_OC_NAME( oc_map ),
382 backsql_PrintErrors( bas->bas_bi->sql_db_env,
383 bas->bas_dbh, bas->bas_sth, rc );
384 bas->bas_rc = LDAP_OTHER;
385 return BACKSQL_AVL_STOP;
388 backsql_BindRowAsStrings( bas->bas_sth, &at_row );
389 for ( ; rc = SQLFetch( bas->bas_sth ), BACKSQL_SUCCESS( rc ); ) {
390 const char *text = NULL;
393 struct berbuf bb = BB_NULL;
395 Debug( LDAP_DEBUG_TRACE,
398 "\tsel_expr=\"%s\"\n"
400 at_row.cols[ 0 ], at_row.cols[ 1 ],
402 Debug( LDAP_DEBUG_TRACE,
403 "\tjoin_where=\"%s\"\n"
404 "\tadd_proc=\"%s\"\n"
405 "\tdelete_proc=\"%s\"\n",
406 at_row.cols[ 3 ], at_row.cols[ 4 ],
409 Debug( LDAP_DEBUG_TRACE, "\tsel_expr_u=\"%s\"\n",
410 at_row.cols[ 8 ], 0, 0 );
411 at_map = (backsql_at_map_rec *)ch_calloc( 1,
412 sizeof( backsql_at_map_rec ) );
413 rc = slap_str2ad( at_row.cols[ 0 ],
414 &at_map->bam_ad, &text );
415 if ( rc != LDAP_SUCCESS ) {
416 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
417 "attribute \"%s\" for objectClass \"%s\" "
418 "is not defined in schema: %s\n",
420 BACKSQL_OC_NAME( oc_map ), text );
421 bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
422 return BACKSQL_AVL_STOP;
425 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
426 if ( at_row.value_len[ 8 ] < 0 ) {
427 BER_BVZERO( &at_map->bam_sel_expr_u );
430 ber_str2bv( at_row.cols[ 8 ], 0, 1,
431 &at_map->bam_sel_expr_u );
434 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
435 backsql_merge_from_clause( &bb, &bv );
436 at_map->bam_from_tbls = bb.bb_val;
437 if ( at_row.value_len[ 3 ] < 0 ) {
438 BER_BVZERO( &at_map->bam_join_where );
441 ber_str2bv( at_row.cols[ 3 ], 0, 1,
442 &at_map->bam_join_where );
444 at_map->bam_add_proc = NULL;
445 if ( at_row.value_len[ 4 ] > 0 ) {
446 at_map->bam_add_proc = ch_strdup( at_row.cols[4] );
448 at_map->bam_delete_proc = NULL;
449 if ( at_row.value_len[ 5 ] > 0 ) {
450 at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
452 at_map->bam_param_order = strtol( at_row.cols[ 6 ],
454 if ( next == at_row.cols[ 6 ] || next[0] != '\0' ) {
457 at_map->bam_expect_return = strtol( at_row.cols[ 7 ],
459 if ( next == at_row.cols[ 7 ] || next[0] != '\0' ) {
462 backsql_make_attr_query( oc_map, at_map );
463 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
464 "preconstructed query \"%s\"\n",
465 at_map->bam_query, 0, 0 );
466 at_map->bam_next = NULL;
467 if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
468 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
469 "duplicate attribute \"%s\" "
470 "in objectClass \"%s\" map\n",
471 at_map->bam_ad->ad_cname.bv_val,
472 oc_map->bom_oc->soc_cname.bv_val, 0 );
475 if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
476 BER_BVISNULL( &at_map->bam_sel_expr_u ) )
478 struct berbuf bb = BB_NULL;
480 backsql_strfcat( &bb, "bcbc",
481 &bas->bas_bi->sql_upper_func,
483 &at_map->bam_sel_expr,
485 at_map->bam_sel_expr_u = bb.bb_val;
488 backsql_FreeRow( &at_row );
489 SQLFreeStmt( bas->bas_sth, SQL_CLOSE );
491 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
492 "autoadding 'objectClass' and 'ref' mappings\n",
493 BACKSQL_OC_NAME( oc_map ), 0, 0 );
495 (void)backsql_add_sysmaps( oc_map );
497 return BACKSQL_AVL_CONTINUE;
502 backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
504 SQLHSTMT sth = SQL_NULL_HSTMT;
506 BACKSQL_ROW_NTS oc_row;
508 backsql_oc_map_rec *oc_map;
509 struct backsql_attr_schema_info bas;
511 Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n", 0, 0, 0 );
514 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
516 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
517 rc = backsql_Prepare( dbh, &sth,
518 backsql_check_dn_ru_query, 0 );
519 if ( rc == SQL_SUCCESS ) {
520 /* Yes, the field exists */
521 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
522 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
523 "in the schema\n", 0, 0, 0 );
525 /* No such field exists */
526 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
529 SQLFreeStmt( sth, SQL_DROP );
532 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n",
533 bi->sql_oc_query, 0, 0 );
535 rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
536 if ( rc != SQL_SUCCESS ) {
537 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
538 "error preparing oc_query: \"%s\"\n",
539 bi->sql_oc_query, 0, 0 );
540 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
544 rc = SQLExecute( sth );
545 if ( rc != SQL_SUCCESS ) {
546 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
547 "error executing oc_query: \n", 0, 0, 0 );
548 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
552 backsql_BindRowAsStrings( sth, &oc_row );
553 rc = SQLFetch( sth );
554 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
557 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
558 sizeof( backsql_oc_map_rec ) );
560 oc_map->bom_id = strtol( oc_row.cols[ 0 ], NULL, 0 );
562 oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
563 if ( oc_map->bom_oc == NULL ) {
564 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
565 "objectClass \"%s\" is not defined in schema\n",
566 oc_row.cols[ 1 ], 0, 0 );
567 return LDAP_OTHER; /* undefined objectClass ? */
570 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
571 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
572 oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL
573 : ch_strdup( oc_row.cols[ 4 ] );
576 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
578 oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] < 0 )
579 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
581 oc_map->bom_delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL
582 : ch_strdup( oc_row.cols[ colnum ] );
583 oc_map->bom_expect_return = strtol( oc_row.cols[ colnum + 1 ],
587 if ( ( oc_row.ncols > colnum ) &&
588 ( oc_row.value_len[ colnum ] > 0 ) )
592 oc_map->bom_create_hint = NULL;
593 rc = slap_str2ad( oc_row.cols[ colnum ],
594 &oc_map->bom_create_hint, &text );
595 if ( rc != SQL_SUCCESS ) {
596 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
598 "AttributeDescription %s "
599 "in create_hint: %s (%d)\n",
600 oc_row.cols[ colnum ],
602 backsql_PrintErrors( bi->sql_db_env, dbh,
609 * FIXME: first attempt to check for offending
610 * instructions in {create|delete}_proc
613 oc_map->bom_attrs = NULL;
614 if ( avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, avl_dup_error ) == -1 ) {
615 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
616 "duplicate objectClass \"%s\" in objectClass map\n",
617 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
620 if ( avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, avl_dup_error ) == -1 ) {
621 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
622 "duplicate objectClass \"%s\" in objectClass by ID map\n",
623 oc_map->bom_oc->soc_cname.bv_val, 0, 0 );
626 oc_id = oc_map->bom_id;
627 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
628 "objectClass \"%s\":\n keytbl=\"%s\" keycol=\"%s\"\n",
629 BACKSQL_OC_NAME( oc_map ),
630 oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
631 if ( oc_map->bom_create_proc ) {
632 Debug( LDAP_DEBUG_TRACE, " create_proc=\"%s\"\n",
633 oc_map->bom_create_proc, 0, 0 );
635 if ( oc_map->bom_create_keyval ) {
636 Debug( LDAP_DEBUG_TRACE, " create_keyval=\"%s\"\n",
637 oc_map->bom_create_keyval, 0, 0 );
639 if ( oc_map->bom_create_hint ) {
640 Debug( LDAP_DEBUG_TRACE, " create_hint=\"%s\"\n",
641 oc_map->bom_create_hint->ad_cname.bv_val,
644 if ( oc_map->bom_delete_proc ) {
645 Debug( LDAP_DEBUG_TRACE, " delete_proc=\"%s\"\n",
646 oc_map->bom_delete_proc, 0, 0 );
648 Debug( LDAP_DEBUG_TRACE, " expect_return: "
649 "add=%d, del=%d; attributes:\n",
650 BACKSQL_IS_ADD( oc_map->bom_expect_return ),
651 BACKSQL_IS_DEL( oc_map->bom_expect_return ), 0 );
654 backsql_FreeRow( &oc_row );
655 SQLFreeStmt( sth, SQL_DROP );
657 /* prepare for attribute fetching */
658 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): at_query \"%s\"\n",
659 bi->sql_at_query, 0, 0 );
661 rc = backsql_Prepare( dbh, &sth, bi->sql_at_query, 0 );
662 if ( rc != SQL_SUCCESS ) {
663 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
664 "error preparing at_query: \"%s\"\n",
665 bi->sql_at_query, 0, 0 );
666 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
670 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT, &oc_id );
671 if ( rc != SQL_SUCCESS ) {
672 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
673 "error binding param \"oc_id\" for at_query\n", 0, 0, 0 );
674 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
675 SQLFreeStmt( sth, SQL_DROP );
682 bas.bas_oc_id = &oc_id;
683 bas.bas_rc = LDAP_SUCCESS;
685 (void)avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
686 &bas, BACKSQL_AVL_STOP, AVL_INORDER );
688 SQLFreeStmt( sth, SQL_DROP );
690 bi->sql_flags |= BSQLF_SCHEMA_LOADED;
692 Debug( LDAP_DEBUG_TRACE, "<==backsql_load_schema_map()\n", 0, 0, 0 );
698 backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
700 backsql_oc_map_rec tmp, *res;
703 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
704 "searching for objectclass with name=\"%s\"\n",
705 oc->soc_cname.bv_val, 0, 0 );
706 #endif /* BACKSQL_TRACE */
709 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
712 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
713 "found name=\"%s\", id=%d\n",
714 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
716 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
717 "not found\n", 0, 0, 0 );
719 #endif /* BACKSQL_TRACE */
725 backsql_name2oc( backsql_info *bi, struct berval *oc_name )
727 backsql_oc_map_rec tmp, *res;
730 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
731 "searching for objectclass with name=\"%s\"\n",
732 oc_name->bv_val, 0, 0 );
733 #endif /* BACKSQL_TRACE */
735 tmp.bom_oc = oc_bvfind( oc_name );
736 if ( tmp.bom_oc == NULL ) {
740 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
743 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
744 "found name=\"%s\", id=%d\n",
745 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
747 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
748 "not found\n", 0, 0, 0 );
750 #endif /* BACKSQL_TRACE */
756 backsql_id2oc( backsql_info *bi, unsigned long id )
758 backsql_oc_map_rec tmp, *res;
761 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
762 "searching for objectclass with id='%d'\n", id, 0, 0 );
763 #endif /* BACKSQL_TRACE */
766 res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_id, &tmp,
771 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
772 "found name=\"%s\", id=%d\n",
773 BACKSQL_OC_NAME( res ), res->bom_id, 0 );
775 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
776 "not found\n", 0, 0, 0 );
778 #endif /* BACKSQL_TRACE */
784 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
786 backsql_at_map_rec tmp, *res;
789 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
790 "searching for attribute \"%s\" for objectclass \"%s\"\n",
791 ad->ad_cname.bv_val, BACKSQL_OC_NAME( objclass ), 0 );
792 #endif /* BACKSQL_TRACE */
795 res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp,
800 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
801 "found name=\"%s\", sel_expr=\"%s\"\n",
802 res->bam_ad->ad_cname.bv_val,
803 res->bam_sel_expr.bv_val, 0 );
805 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
806 "not found\n", 0, 0, 0 );
808 #endif /* BACKSQL_TRACE */
813 /* attributeType inheritance */
815 backsql_at_map_rec **ret;
816 AttributeDescription *ad;
820 #define SUPAD2AT_STOP (-1)
823 supad2at_f( void *v_at, void *v_arg )
825 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
826 struct supad2at_t *va = (struct supad2at_t *)v_arg;
828 if ( is_at_subtype( at->bam_ad->ad_type, va->ad->ad_type ) ) {
829 backsql_at_map_rec **ret;
832 /* if already listed, holler! (should never happen) */
834 for ( i = 0; i < va->n; i++ ) {
835 if ( va->ret[ i ]->bam_ad == at->bam_ad ) {
845 ret = ch_realloc( va->ret,
846 sizeof( backsql_at_map_rec *) * ( va->n + 2 ) );
849 return SUPAD2AT_STOP;
862 * stores in *pret a NULL terminated array of pointers
863 * to backsql_at_map_rec whose attributeType is supad->ad_type
867 backsql_supad2at( backsql_oc_map_rec *objclass, AttributeDescription *supad,
868 backsql_at_map_rec ***pret )
870 struct supad2at_t va;
883 rc = avl_apply( objclass->bom_attrs, supad2at_f, &va,
884 SUPAD2AT_STOP, AVL_INORDER );
885 if ( rc == SUPAD2AT_STOP ) {
895 backsql_free_attr( void *v_at )
897 backsql_at_map_rec *at = v_at;
899 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): \"%s\"\n",
900 at->bam_ad->ad_cname.bv_val, 0, 0 );
901 ch_free( at->bam_sel_expr.bv_val );
902 if ( !BER_BVISNULL( &at->bam_from_tbls ) ) {
903 ch_free( at->bam_from_tbls.bv_val );
905 if ( !BER_BVISNULL( &at->bam_join_where ) ) {
906 ch_free( at->bam_join_where.bv_val );
908 if ( at->bam_add_proc != NULL ) {
909 ch_free( at->bam_add_proc );
911 if ( at->bam_delete_proc != NULL ) {
912 ch_free( at->bam_delete_proc );
914 if ( at->bam_query != NULL ) {
915 ch_free( at->bam_query );
919 if ( !BER_BVISNULL( &at->bam_sel_expr_u ) ) {
920 ch_free( at->bam_sel_expr_u.bv_val );
923 if ( at->bam_next ) {
924 backsql_free_attr( at->bam_next );
929 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
933 backsql_free_oc( void *v_oc )
935 backsql_oc_map_rec *oc = v_oc;
937 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): \"%s\"\n",
938 BACKSQL_OC_NAME( oc ), 0, 0 );
939 avl_free( oc->bom_attrs, backsql_free_attr );
940 ch_free( oc->bom_keytbl.bv_val );
941 ch_free( oc->bom_keycol.bv_val );
942 if ( oc->bom_create_proc != NULL ) {
943 ch_free( oc->bom_create_proc );
945 if ( oc->bom_create_keyval != NULL ) {
946 ch_free( oc->bom_create_keyval );
948 if ( oc->bom_delete_proc != NULL ) {
949 ch_free( oc->bom_delete_proc );
953 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
957 backsql_destroy_schema_map( backsql_info *bi )
959 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
960 avl_free( bi->sql_oc_by_oc, 0 );
961 avl_free( bi->sql_oc_by_id, backsql_free_oc );
962 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );