1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/schema-map.c,v 1.59.2.6 2008/02/11 23:26:48 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 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"
32 #include "proto-sql.h"
34 #define BACKSQL_DUPLICATE (-1)
36 /* NOTE: by default, cannot just compare pointers because
37 * objectClass/attributeType order would be machine-dependent
38 * (and tests would fail!); however, if you don't want to run
39 * tests, or see attributeTypes written in the same order
40 * they are defined, define */
41 /* #undef BACKSQL_USE_PTR_CMP */
44 * Uses the pointer to the ObjectClass structure
47 backsql_cmp_oc( const void *v_m1
, const void *v_m2
)
49 const backsql_oc_map_rec
*m1
= v_m1
,
52 #ifdef BACKSQL_USE_PTR_CMP
53 return SLAP_PTRCMP( m1
->bom_oc
, m2
->bom_oc
);
54 #else /* ! BACKSQL_USE_PTR_CMP */
55 return ber_bvcmp( &m1
->bom_oc
->soc_cname
, &m2
->bom_oc
->soc_cname
);
56 #endif /* ! BACKSQL_USE_PTR_CMP */
60 backsql_cmp_oc_id( const void *v_m1
, const void *v_m2
)
62 const backsql_oc_map_rec
*m1
= v_m1
,
65 return ( m1
->bom_id
< m2
->bom_id
? -1 : ( m1
->bom_id
> m2
->bom_id
? 1 : 0 ) );
69 * Uses the pointer to the AttributeDescription structure
72 backsql_cmp_attr( const void *v_m1
, const void *v_m2
)
74 const backsql_at_map_rec
*m1
= v_m1
,
77 if ( slap_ad_is_binary( m1
->bam_ad
) || slap_ad_is_binary( m2
->bam_ad
) ) {
78 #ifdef BACKSQL_USE_PTR_CMP
79 return SLAP_PTRCMP( m1
->bam_ad
->ad_type
, m2
->bam_ad
->ad_type
);
80 #else /* ! BACKSQL_USE_PTR_CMP */
81 return ber_bvcmp( &m1
->bam_ad
->ad_type
->sat_cname
, &m2
->bam_ad
->ad_type
->sat_cname
);
82 #endif /* ! BACKSQL_USE_PTR_CMP */
85 #ifdef BACKSQL_USE_PTR_CMP
86 return SLAP_PTRCMP( m1
->bam_ad
, m2
->bam_ad
);
87 #else /* ! BACKSQL_USE_PTR_CMP */
88 return ber_bvcmp( &m1
->bam_ad
->ad_cname
, &m2
->bam_ad
->ad_cname
);
89 #endif /* ! BACKSQL_USE_PTR_CMP */
93 backsql_dup_attr( void *v_m1
, void *v_m2
)
95 backsql_at_map_rec
*m1
= v_m1
,
98 if ( slap_ad_is_binary( m1
->bam_ad
) || slap_ad_is_binary( m2
->bam_ad
) ) {
99 #ifdef BACKSQL_USE_PTR_CMP
100 assert( m1
->bam_ad
->ad_type
== m2
->bam_ad
->ad_type
);
101 #else /* ! BACKSQL_USE_PTR_CMP */
102 assert( ber_bvcmp( &m1
->bam_ad
->ad_type
->sat_cname
, &m2
->bam_ad
->ad_type
->sat_cname
) == 0 );
103 #endif /* ! BACKSQL_USE_PTR_CMP */
106 #ifdef BACKSQL_USE_PTR_CMP
107 assert( m1
->bam_ad
== m2
->bam_ad
);
108 #else /* ! BACKSQL_USE_PTR_CMP */
109 assert( ber_bvcmp( &m1
->bam_ad
->ad_cname
, &m2
->bam_ad
->ad_cname
) == 0 );
110 #endif /* ! BACKSQL_USE_PTR_CMP */
113 /* duplicate definitions of attributeTypes are appended;
114 * this allows to define multiple rules for the same
115 * attributeType. Use with care! */
116 for ( ; m1
->bam_next
; m1
= m1
->bam_next
);
121 return BACKSQL_DUPLICATE
;
125 backsql_make_attr_query(
127 backsql_oc_map_rec
*oc_map
,
128 backsql_at_map_rec
*at_map
)
130 struct berbuf bb
= BB_NULL
;
132 backsql_strfcat_x( &bb
, NULL
, "lblbbbblblbcbl",
133 (ber_len_t
)STRLENOF( "SELECT " ), "SELECT ",
134 &at_map
->bam_sel_expr
,
135 (ber_len_t
)STRLENOF( " " ), " ",
137 &bi
->sql_aliasing_quote
,
138 &at_map
->bam_ad
->ad_cname
,
139 &bi
->sql_aliasing_quote
,
140 (ber_len_t
)STRLENOF( " FROM " ), " FROM ",
141 &at_map
->bam_from_tbls
,
142 (ber_len_t
)STRLENOF( " WHERE " ), " WHERE ",
146 (ber_len_t
)STRLENOF( "=?" ), "=?" );
148 if ( !BER_BVISNULL( &at_map
->bam_join_where
) ) {
149 backsql_strfcat_x( &bb
, NULL
, "lb",
150 (ber_len_t
)STRLENOF( " AND " ), " AND ",
151 &at_map
->bam_join_where
);
154 backsql_strfcat_x( &bb
, NULL
, "lbbb",
155 (ber_len_t
)STRLENOF( " ORDER BY " ), " ORDER BY ",
156 &bi
->sql_aliasing_quote
,
157 &at_map
->bam_ad
->ad_cname
,
158 &bi
->sql_aliasing_quote
);
160 at_map
->bam_query
= bb
.bb_val
.bv_val
;
162 #ifdef BACKSQL_COUNTQUERY
163 /* Query to count how many rows will be returned. */
164 BER_BVZERO( &bb
.bb_val
);
166 backsql_strfcat_x( &bb
, NULL
, "lblbcbl",
167 (ber_len_t
)STRLENOF( "SELECT COUNT(*) FROM " ),
168 "SELECT COUNT(*) FROM ",
169 &at_map
->bam_from_tbls
,
170 (ber_len_t
)STRLENOF( " WHERE " ), " WHERE ",
174 (ber_len_t
)STRLENOF( "=?" ), "=?" );
176 if ( !BER_BVISNULL( &at_map
->bam_join_where
) ) {
177 backsql_strfcat_x( &bb
, NULL
, "lb",
178 (ber_len_t
)STRLENOF( " AND " ), " AND ",
179 &at_map
->bam_join_where
);
182 at_map
->bam_countquery
= bb
.bb_val
.bv_val
;
183 #endif /* BACKSQL_COUNTQUERY */
189 backsql_add_sysmaps( backsql_info
*bi
, backsql_oc_map_rec
*oc_map
)
191 backsql_at_map_rec
*at_map
;
192 char s
[LDAP_PVT_INTTYPE_CHARS(long)];
197 sbv
.bv_len
= snprintf( s
, sizeof( s
), "%ld", oc_map
->bom_id
);
199 /* extra objectClasses */
200 at_map
= (backsql_at_map_rec
*)ch_calloc(1,
201 sizeof( backsql_at_map_rec
) );
202 at_map
->bam_ad
= slap_schema
.si_ad_objectClass
;
203 at_map
->bam_true_ad
= slap_schema
.si_ad_objectClass
;
204 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
205 &at_map
->bam_sel_expr
);
206 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
207 &at_map
->bam_from_tbls
);
209 bb
.bb_len
= at_map
->bam_from_tbls
.bv_len
+ 1;
210 bb
.bb_val
= at_map
->bam_from_tbls
;
211 backsql_merge_from_clause( bi
, &bb
, &oc_map
->bom_keytbl
);
212 at_map
->bam_from_tbls
= bb
.bb_val
;
214 BER_BVZERO( &bb
.bb_val
);
216 backsql_strfcat_x( &bb
, NULL
, "lbcblb",
217 (ber_len_t
)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
218 "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
222 (ber_len_t
)STRLENOF( " and ldap_entries.oc_map_id=" ),
223 " and ldap_entries.oc_map_id=",
225 at_map
->bam_join_where
= bb
.bb_val
;
227 at_map
->bam_oc
= oc_map
->bom_oc
;
229 at_map
->bam_add_proc
= NULL
;
231 char tmp
[STRLENOF("INSERT INTO ldap_entry_objclasses "
232 "(entry_id,oc_name) VALUES "
233 "((SELECT id FROM ldap_entries "
235 "AND keyval=?),?)") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
236 snprintf( tmp
, sizeof(tmp
),
237 "INSERT INTO ldap_entry_objclasses "
238 "(entry_id,oc_name) VALUES "
239 "((SELECT id FROM ldap_entries "
240 "WHERE oc_map_id=%lu "
241 "AND keyval=?),?)", oc_map
->bom_id
);
242 at_map
->bam_add_proc
= ch_strdup( tmp
);
245 at_map
->bam_delete_proc
= NULL
;
247 char tmp
[STRLENOF("DELETE FROM ldap_entry_objclasses "
248 "WHERE entry_id=(SELECT id FROM ldap_entries "
250 "AND keyval=?) AND oc_name=?") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
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( bi
, 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_x( &bb
, NULL
, "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
;
291 struct backsql_attr_schema_info
{
292 backsql_info
*bas_bi
;
295 unsigned long *bas_oc_id
;
300 backsql_oc_get_attr_mapping( void *v_oc
, void *v_bas
)
303 BACKSQL_ROW_NTS at_row
;
304 backsql_oc_map_rec
*oc_map
= (backsql_oc_map_rec
*)v_oc
;
305 backsql_at_map_rec
*at_map
;
306 struct backsql_attr_schema_info
*bas
= (struct backsql_attr_schema_info
*)v_bas
;
308 /* bas->bas_oc_id has been bound to bas->bas_sth */
309 *bas
->bas_oc_id
= oc_map
->bom_id
;
311 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
312 "executing at_query\n"
314 " for objectClass \"%s\"\n"
315 " with param oc_id=\"%lu\"\n",
316 bas
->bas_bi
->sql_at_query
,
317 BACKSQL_OC_NAME( oc_map
),
320 rc
= SQLExecute( bas
->bas_sth
);
321 if ( rc
!= SQL_SUCCESS
) {
322 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
323 "error executing at_query\n"
325 " for objectClass \"%s\"\n"
326 " with param oc_id=\"%lu\"\n",
327 bas
->bas_bi
->sql_at_query
,
328 BACKSQL_OC_NAME( oc_map
),
330 backsql_PrintErrors( bas
->bas_bi
->sql_db_env
,
331 bas
->bas_dbh
, bas
->bas_sth
, rc
);
332 bas
->bas_rc
= LDAP_OTHER
;
333 return BACKSQL_AVL_STOP
;
336 backsql_BindRowAsStrings( bas
->bas_sth
, &at_row
);
337 for ( ; rc
= SQLFetch( bas
->bas_sth
), BACKSQL_SUCCESS( rc
); ) {
338 const char *text
= NULL
;
340 struct berbuf bb
= BB_NULL
;
341 AttributeDescription
*ad
= NULL
;
343 Debug( LDAP_DEBUG_TRACE
,
346 "\tsel_expr=\"%s\"\n"
348 at_row
.cols
[ 0 ], at_row
.cols
[ 1 ],
350 Debug( LDAP_DEBUG_TRACE
,
351 "\tjoin_where=\"%s\"\n"
352 "\tadd_proc=\"%s\"\n"
353 "\tdelete_proc=\"%s\"\n",
354 at_row
.cols
[ 3 ], at_row
.cols
[ 4 ],
357 Debug( LDAP_DEBUG_TRACE
, "\tsel_expr_u=\"%s\"\n",
358 at_row
.cols
[ 8 ], 0, 0 );
359 rc
= slap_str2ad( at_row
.cols
[ 0 ], &ad
, &text
);
360 if ( rc
!= LDAP_SUCCESS
) {
361 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
362 "attribute \"%s\" for objectClass \"%s\" "
363 "is not defined in schema: %s\n",
365 BACKSQL_OC_NAME( oc_map
), text
);
366 bas
->bas_rc
= LDAP_CONSTRAINT_VIOLATION
;
367 return BACKSQL_AVL_STOP
;
369 at_map
= (backsql_at_map_rec
*)ch_calloc( 1,
370 sizeof( backsql_at_map_rec
) );
372 at_map
->bam_true_ad
= ad
;
373 if ( slap_syntax_is_binary( ad
->ad_type
->sat_syntax
)
374 && !slap_ad_is_binary( ad
) )
378 const char *text
= NULL
;
381 bv
.bv_len
= snprintf( buf
, sizeof( buf
), "%s;binary",
382 ad
->ad_cname
.bv_val
);
383 at_map
->bam_true_ad
= NULL
;
384 bas
->bas_rc
= slap_bv2ad( &bv
, &at_map
->bam_true_ad
, &text
);
385 if ( bas
->bas_rc
!= LDAP_SUCCESS
) {
386 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
387 "unable to fetch attribute \"%s\": %s (%d)\n",
389 return BACKSQL_AVL_STOP
;
393 ber_str2bv( at_row
.cols
[ 1 ], 0, 1, &at_map
->bam_sel_expr
);
394 if ( at_row
.value_len
[ 8 ] <= 0 ) {
395 BER_BVZERO( &at_map
->bam_sel_expr_u
);
398 ber_str2bv( at_row
.cols
[ 8 ], 0, 1,
399 &at_map
->bam_sel_expr_u
);
402 ber_str2bv( at_row
.cols
[ 2 ], 0, 0, &bv
);
403 backsql_merge_from_clause( bas
->bas_bi
, &bb
, &bv
);
404 at_map
->bam_from_tbls
= bb
.bb_val
;
405 if ( at_row
.value_len
[ 3 ] <= 0 ) {
406 BER_BVZERO( &at_map
->bam_join_where
);
409 ber_str2bv( at_row
.cols
[ 3 ], 0, 1,
410 &at_map
->bam_join_where
);
412 at_map
->bam_add_proc
= NULL
;
413 if ( at_row
.value_len
[ 4 ] > 0 ) {
414 at_map
->bam_add_proc
= ch_strdup( at_row
.cols
[4] );
416 at_map
->bam_delete_proc
= NULL
;
417 if ( at_row
.value_len
[ 5 ] > 0 ) {
418 at_map
->bam_delete_proc
= ch_strdup( at_row
.cols
[ 5 ] );
420 if ( lutil_atoix( &at_map
->bam_param_order
, at_row
.cols
[ 6 ], 0 ) != 0 ) {
423 if ( lutil_atoix( &at_map
->bam_expect_return
, at_row
.cols
[ 7 ], 0 ) != 0 ) {
426 backsql_make_attr_query( bas
->bas_bi
, oc_map
, at_map
);
427 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
428 "preconstructed query \"%s\"\n",
429 at_map
->bam_query
, 0, 0 );
430 at_map
->bam_next
= NULL
;
431 if ( avl_insert( &oc_map
->bom_attrs
, at_map
, backsql_cmp_attr
, backsql_dup_attr
) == BACKSQL_DUPLICATE
) {
432 Debug( LDAP_DEBUG_TRACE
, "backsql_oc_get_attr_mapping(): "
433 "duplicate attribute \"%s\" "
434 "in objectClass \"%s\" map\n",
435 at_map
->bam_ad
->ad_cname
.bv_val
,
436 oc_map
->bom_oc
->soc_cname
.bv_val
, 0 );
439 if ( !BER_BVISNULL( &bas
->bas_bi
->sql_upper_func
) &&
440 BER_BVISNULL( &at_map
->bam_sel_expr_u
) )
442 struct berbuf bb
= BB_NULL
;
444 backsql_strfcat_x( &bb
, NULL
, "bcbc",
445 &bas
->bas_bi
->sql_upper_func
,
447 &at_map
->bam_sel_expr
,
449 at_map
->bam_sel_expr_u
= bb
.bb_val
;
452 backsql_FreeRow( &at_row
);
453 SQLFreeStmt( bas
->bas_sth
, SQL_CLOSE
);
455 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(\"%s\"): "
456 "autoadding 'objectClass' and 'ref' mappings\n",
457 BACKSQL_OC_NAME( oc_map
), 0, 0 );
459 (void)backsql_add_sysmaps( bas
->bas_bi
, oc_map
);
461 return BACKSQL_AVL_CONTINUE
;
466 backsql_load_schema_map( backsql_info
*bi
, SQLHDBC dbh
)
468 SQLHSTMT sth
= SQL_NULL_HSTMT
;
470 BACKSQL_ROW_NTS oc_row
;
472 backsql_oc_map_rec
*oc_map
;
473 struct backsql_attr_schema_info bas
;
475 Debug( LDAP_DEBUG_TRACE
, "==>backsql_load_schema_map()\n", 0, 0, 0 );
478 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
480 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi
) ) {
481 rc
= backsql_Prepare( dbh
, &sth
,
482 backsql_check_dn_ru_query
, 0 );
483 if ( rc
== SQL_SUCCESS
) {
484 /* Yes, the field exists */
485 bi
->sql_flags
|= BSQLF_HAS_LDAPINFO_DN_RU
;
486 Debug( LDAP_DEBUG_TRACE
, "ldapinfo.dn_ru field exists "
487 "in the schema\n", 0, 0, 0 );
489 /* No such field exists */
490 bi
->sql_flags
&= ~BSQLF_HAS_LDAPINFO_DN_RU
;
493 SQLFreeStmt( sth
, SQL_DROP
);
496 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): oc_query \"%s\"\n",
497 bi
->sql_oc_query
, 0, 0 );
499 rc
= backsql_Prepare( dbh
, &sth
, bi
->sql_oc_query
, 0 );
500 if ( rc
!= SQL_SUCCESS
) {
501 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
502 "error preparing oc_query: \"%s\"\n",
503 bi
->sql_oc_query
, 0, 0 );
504 backsql_PrintErrors( bi
->sql_db_env
, dbh
, sth
, rc
);
508 rc
= SQLExecute( sth
);
509 if ( rc
!= SQL_SUCCESS
) {
510 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
511 "error executing oc_query: \n", 0, 0, 0 );
512 backsql_PrintErrors( bi
->sql_db_env
, dbh
, sth
, rc
);
516 backsql_BindRowAsStrings( sth
, &oc_row
);
517 rc
= SQLFetch( sth
);
518 for ( ; BACKSQL_SUCCESS( rc
); rc
= SQLFetch( sth
) ) {
521 oc_map
= (backsql_oc_map_rec
*)ch_calloc( 1,
522 sizeof( backsql_oc_map_rec
) );
524 if ( lutil_atoulx( &oc_map
->bom_id
, oc_row
.cols
[ 0 ], 0 ) != 0 ) {
525 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
526 "unable to parse id=\"%s\"\n",
527 oc_row
.cols
[ 0 ], 0, 0 );
531 oc_map
->bom_oc
= oc_find( oc_row
.cols
[ 1 ] );
532 if ( oc_map
->bom_oc
== NULL
) {
533 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
534 "objectClass \"%s\" is not defined in schema\n",
535 oc_row
.cols
[ 1 ], 0, 0 );
536 return LDAP_OTHER
; /* undefined objectClass ? */
539 ber_str2bv( oc_row
.cols
[ 2 ], 0, 1, &oc_map
->bom_keytbl
);
540 ber_str2bv( oc_row
.cols
[ 3 ], 0, 1, &oc_map
->bom_keycol
);
541 oc_map
->bom_create_proc
= ( oc_row
.value_len
[ 4 ] <= 0 ) ? NULL
542 : ch_strdup( oc_row
.cols
[ 4 ] );
545 if ( BACKSQL_CREATE_NEEDS_SELECT( bi
) ) {
547 oc_map
->bom_create_keyval
= ( oc_row
.value_len
[ 5 ] <= 0 )
548 ? NULL
: ch_strdup( oc_row
.cols
[ 5 ] );
550 oc_map
->bom_delete_proc
= ( oc_row
.value_len
[ colnum
] <= 0 ) ? NULL
551 : ch_strdup( oc_row
.cols
[ colnum
] );
552 if ( lutil_atoix( &oc_map
->bom_expect_return
, oc_row
.cols
[ colnum
+ 1 ], 0 ) != 0 ) {
553 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
554 "unable to parse expect_return=\"%s\" for objectClass \"%s\"\n",
555 oc_row
.cols
[ colnum
+ 1 ], oc_row
.cols
[ 1 ], 0 );
560 if ( ( oc_row
.ncols
> colnum
) &&
561 ( oc_row
.value_len
[ colnum
] > 0 ) )
565 oc_map
->bom_create_hint
= NULL
;
566 rc
= slap_str2ad( oc_row
.cols
[ colnum
],
567 &oc_map
->bom_create_hint
, &text
);
568 if ( rc
!= SQL_SUCCESS
) {
569 Debug( LDAP_DEBUG_TRACE
, "load_schema_map(): "
571 "AttributeDescription %s "
572 "in create_hint: %s (%d)\n",
573 oc_row
.cols
[ colnum
],
575 backsql_PrintErrors( bi
->sql_db_env
, dbh
,
582 * FIXME: first attempt to check for offending
583 * instructions in {create|delete}_proc
586 oc_map
->bom_attrs
= NULL
;
587 if ( avl_insert( &bi
->sql_oc_by_oc
, oc_map
, backsql_cmp_oc
, avl_dup_error
) == -1 ) {
588 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
589 "duplicate objectClass \"%s\" in objectClass map\n",
590 oc_map
->bom_oc
->soc_cname
.bv_val
, 0, 0 );
593 if ( avl_insert( &bi
->sql_oc_by_id
, oc_map
, backsql_cmp_oc_id
, avl_dup_error
) == -1 ) {
594 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
595 "duplicate objectClass \"%s\" in objectClass by ID map\n",
596 oc_map
->bom_oc
->soc_cname
.bv_val
, 0, 0 );
599 oc_id
= oc_map
->bom_id
;
600 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
601 "objectClass \"%s\":\n keytbl=\"%s\" keycol=\"%s\"\n",
602 BACKSQL_OC_NAME( oc_map
),
603 oc_map
->bom_keytbl
.bv_val
, oc_map
->bom_keycol
.bv_val
);
604 if ( oc_map
->bom_create_proc
) {
605 Debug( LDAP_DEBUG_TRACE
, " create_proc=\"%s\"\n",
606 oc_map
->bom_create_proc
, 0, 0 );
608 if ( oc_map
->bom_create_keyval
) {
609 Debug( LDAP_DEBUG_TRACE
, " create_keyval=\"%s\"\n",
610 oc_map
->bom_create_keyval
, 0, 0 );
612 if ( oc_map
->bom_create_hint
) {
613 Debug( LDAP_DEBUG_TRACE
, " create_hint=\"%s\"\n",
614 oc_map
->bom_create_hint
->ad_cname
.bv_val
,
617 if ( oc_map
->bom_delete_proc
) {
618 Debug( LDAP_DEBUG_TRACE
, " delete_proc=\"%s\"\n",
619 oc_map
->bom_delete_proc
, 0, 0 );
621 Debug( LDAP_DEBUG_TRACE
, " expect_return: "
622 "add=%d, del=%d; attributes:\n",
623 BACKSQL_IS_ADD( oc_map
->bom_expect_return
),
624 BACKSQL_IS_DEL( oc_map
->bom_expect_return
), 0 );
627 backsql_FreeRow( &oc_row
);
628 SQLFreeStmt( sth
, SQL_DROP
);
630 /* prepare for attribute fetching */
631 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): at_query \"%s\"\n",
632 bi
->sql_at_query
, 0, 0 );
634 rc
= backsql_Prepare( dbh
, &sth
, bi
->sql_at_query
, 0 );
635 if ( rc
!= SQL_SUCCESS
) {
636 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
637 "error preparing at_query: \"%s\"\n",
638 bi
->sql_at_query
, 0, 0 );
639 backsql_PrintErrors( bi
->sql_db_env
, dbh
, sth
, rc
);
643 rc
= backsql_BindParamInt( sth
, 1, SQL_PARAM_INPUT
, &oc_id
);
644 if ( rc
!= SQL_SUCCESS
) {
645 Debug( LDAP_DEBUG_TRACE
, "backsql_load_schema_map(): "
646 "error binding param \"oc_id\" for at_query\n", 0, 0, 0 );
647 backsql_PrintErrors( bi
->sql_db_env
, dbh
, sth
, rc
);
648 SQLFreeStmt( sth
, SQL_DROP
);
655 bas
.bas_oc_id
= &oc_id
;
656 bas
.bas_rc
= LDAP_SUCCESS
;
658 (void)avl_apply( bi
->sql_oc_by_oc
, backsql_oc_get_attr_mapping
,
659 &bas
, BACKSQL_AVL_STOP
, AVL_INORDER
);
661 SQLFreeStmt( sth
, SQL_DROP
);
663 bi
->sql_flags
|= BSQLF_SCHEMA_LOADED
;
665 Debug( LDAP_DEBUG_TRACE
, "<==backsql_load_schema_map()\n", 0, 0, 0 );
671 backsql_oc2oc( backsql_info
*bi
, ObjectClass
*oc
)
673 backsql_oc_map_rec tmp
, *res
;
676 Debug( LDAP_DEBUG_TRACE
, "==>backsql_oc2oc(): "
677 "searching for objectclass with name=\"%s\"\n",
678 oc
->soc_cname
.bv_val
, 0, 0 );
679 #endif /* BACKSQL_TRACE */
682 res
= (backsql_oc_map_rec
*)avl_find( bi
->sql_oc_by_oc
, &tmp
, backsql_cmp_oc
);
685 Debug( LDAP_DEBUG_TRACE
, "<==backsql_oc2oc(): "
686 "found name=\"%s\", id=%d\n",
687 BACKSQL_OC_NAME( res
), res
->bom_id
, 0 );
689 Debug( LDAP_DEBUG_TRACE
, "<==backsql_oc2oc(): "
690 "not found\n", 0, 0, 0 );
692 #endif /* BACKSQL_TRACE */
698 backsql_name2oc( backsql_info
*bi
, struct berval
*oc_name
)
700 backsql_oc_map_rec tmp
, *res
;
703 Debug( LDAP_DEBUG_TRACE
, "==>oc_with_name(): "
704 "searching for objectclass with name=\"%s\"\n",
705 oc_name
->bv_val
, 0, 0 );
706 #endif /* BACKSQL_TRACE */
708 tmp
.bom_oc
= oc_bvfind( oc_name
);
709 if ( tmp
.bom_oc
== NULL
) {
713 res
= (backsql_oc_map_rec
*)avl_find( bi
->sql_oc_by_oc
, &tmp
, backsql_cmp_oc
);
716 Debug( LDAP_DEBUG_TRACE
, "<==oc_with_name(): "
717 "found name=\"%s\", id=%d\n",
718 BACKSQL_OC_NAME( res
), res
->bom_id
, 0 );
720 Debug( LDAP_DEBUG_TRACE
, "<==oc_with_name(): "
721 "not found\n", 0, 0, 0 );
723 #endif /* BACKSQL_TRACE */
729 backsql_id2oc( backsql_info
*bi
, unsigned long id
)
731 backsql_oc_map_rec tmp
, *res
;
734 Debug( LDAP_DEBUG_TRACE
, "==>oc_with_id(): "
735 "searching for objectclass with id='%d'\n", id
, 0, 0 );
736 #endif /* BACKSQL_TRACE */
739 res
= (backsql_oc_map_rec
*)avl_find( bi
->sql_oc_by_id
, &tmp
,
744 Debug( LDAP_DEBUG_TRACE
, "<==oc_with_name(): "
745 "found name=\"%s\", id=%d\n",
746 BACKSQL_OC_NAME( res
), res
->bom_id
, 0 );
748 Debug( LDAP_DEBUG_TRACE
, "<==oc_with_name(): "
749 "not found\n", 0, 0, 0 );
751 #endif /* BACKSQL_TRACE */
757 backsql_ad2at( backsql_oc_map_rec
* objclass
, AttributeDescription
*ad
)
759 backsql_at_map_rec tmp
= { 0 }, *res
;
762 Debug( LDAP_DEBUG_TRACE
, "==>backsql_ad2at(): "
763 "searching for attribute \"%s\" for objectclass \"%s\"\n",
764 ad
->ad_cname
.bv_val
, BACKSQL_OC_NAME( objclass
), 0 );
765 #endif /* BACKSQL_TRACE */
768 res
= (backsql_at_map_rec
*)avl_find( objclass
->bom_attrs
, &tmp
,
773 Debug( LDAP_DEBUG_TRACE
, "<==backsql_ad2at(): "
774 "found name=\"%s\", sel_expr=\"%s\"\n",
775 res
->bam_ad
->ad_cname
.bv_val
,
776 res
->bam_sel_expr
.bv_val
, 0 );
778 Debug( LDAP_DEBUG_TRACE
, "<==backsql_ad2at(): "
779 "not found\n", 0, 0, 0 );
781 #endif /* BACKSQL_TRACE */
786 /* attributeType inheritance */
788 backsql_at_map_rec
**ret
;
789 AttributeDescription
*ad
;
793 #define SUPAD2AT_STOP (-1)
796 supad2at_f( void *v_at
, void *v_arg
)
798 backsql_at_map_rec
*at
= (backsql_at_map_rec
*)v_at
;
799 struct supad2at_t
*va
= (struct supad2at_t
*)v_arg
;
801 if ( is_at_subtype( at
->bam_ad
->ad_type
, va
->ad
->ad_type
) ) {
802 backsql_at_map_rec
**ret
= NULL
;
805 /* if already listed, holler! (should never happen) */
807 for ( i
= 0; i
< va
->n
; i
++ ) {
808 if ( va
->ret
[ i
]->bam_ad
== at
->bam_ad
) {
818 ret
= ch_realloc( va
->ret
,
819 sizeof( backsql_at_map_rec
* ) * ( va
->n
+ 2 ) );
824 return SUPAD2AT_STOP
;
837 * stores in *pret a NULL terminated array of pointers
838 * to backsql_at_map_rec whose attributeType is supad->ad_type
842 backsql_supad2at( backsql_oc_map_rec
*objclass
, AttributeDescription
*supad
,
843 backsql_at_map_rec
***pret
)
845 struct supad2at_t va
= { 0 };
848 assert( objclass
!= NULL
);
849 assert( supad
!= NULL
);
850 assert( pret
!= NULL
);
856 rc
= avl_apply( objclass
->bom_attrs
, supad2at_f
, &va
,
857 SUPAD2AT_STOP
, AVL_INORDER
);
858 if ( rc
== SUPAD2AT_STOP
) {
868 backsql_free_attr( void *v_at
)
870 backsql_at_map_rec
*at
= v_at
;
872 Debug( LDAP_DEBUG_TRACE
, "==>free_attr(): \"%s\"\n",
873 at
->bam_ad
->ad_cname
.bv_val
, 0, 0 );
874 ch_free( at
->bam_sel_expr
.bv_val
);
875 if ( !BER_BVISNULL( &at
->bam_from_tbls
) ) {
876 ch_free( at
->bam_from_tbls
.bv_val
);
878 if ( !BER_BVISNULL( &at
->bam_join_where
) ) {
879 ch_free( at
->bam_join_where
.bv_val
);
881 if ( at
->bam_add_proc
!= NULL
) {
882 ch_free( at
->bam_add_proc
);
884 if ( at
->bam_delete_proc
!= NULL
) {
885 ch_free( at
->bam_delete_proc
);
887 if ( at
->bam_query
!= NULL
) {
888 ch_free( at
->bam_query
);
891 #ifdef BACKSQL_COUNTQUERY
892 if ( at
->bam_countquery
!= NULL
) {
893 ch_free( at
->bam_countquery
);
895 #endif /* BACKSQL_COUNTQUERY */
898 if ( !BER_BVISNULL( &at
->bam_sel_expr_u
) ) {
899 ch_free( at
->bam_sel_expr_u
.bv_val
);
902 if ( at
->bam_next
) {
903 backsql_free_attr( at
->bam_next
);
908 Debug( LDAP_DEBUG_TRACE
, "<==free_attr()\n", 0, 0, 0 );
912 backsql_free_oc( void *v_oc
)
914 backsql_oc_map_rec
*oc
= v_oc
;
916 Debug( LDAP_DEBUG_TRACE
, "==>free_oc(): \"%s\"\n",
917 BACKSQL_OC_NAME( oc
), 0, 0 );
918 avl_free( oc
->bom_attrs
, backsql_free_attr
);
919 ch_free( oc
->bom_keytbl
.bv_val
);
920 ch_free( oc
->bom_keycol
.bv_val
);
921 if ( oc
->bom_create_proc
!= NULL
) {
922 ch_free( oc
->bom_create_proc
);
924 if ( oc
->bom_create_keyval
!= NULL
) {
925 ch_free( oc
->bom_create_keyval
);
927 if ( oc
->bom_delete_proc
!= NULL
) {
928 ch_free( oc
->bom_delete_proc
);
932 Debug( LDAP_DEBUG_TRACE
, "<==free_oc()\n", 0, 0, 0 );
936 backsql_destroy_schema_map( backsql_info
*bi
)
938 Debug( LDAP_DEBUG_TRACE
, "==>destroy_schema_map()\n", 0, 0, 0 );
939 avl_free( bi
->sql_oc_by_oc
, 0 );
940 avl_free( bi
->sql_oc_by_id
, backsql_free_oc
);
941 Debug( LDAP_DEBUG_TRACE
, "<==destroy_schema_map()\n", 0, 0, 0 );