2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
22 #include "schema-map.h"
26 * Uses the pointer to the ObjectClass structure
29 backsql_cmp_oc( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
31 return ( m1->oc < m2->oc ? -1 : ( m1->oc > m2->oc ? 1 : 0 ) );
35 backsql_cmp_oc_id( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
37 return ( m1->id < m2->id ? -1 : ( m1->id > m2->id ? 1 : 0 ) );
41 * Uses the pointer to the AttributeDescription structure
44 backsql_cmp_attr( backsql_at_map_rec *m1, backsql_at_map_rec *m2 )
46 return ( m1->ad < m2->ad ? -1 : ( m1->ad > m2->ad ? 1 : 0 ) );
50 backsql_make_attr_query(
51 backsql_oc_map_rec *oc_map,
52 backsql_at_map_rec *at_map )
54 struct berval tmps = BER_BVNULL;
55 ber_len_t tmpslen = 0;
57 backsql_strfcat( &tmps, &tmpslen, "lblblblbcbl",
58 (ber_len_t)sizeof( "SELECT " ) - 1, "SELECT ",
60 (ber_len_t)sizeof( " AS " ) - 1, " AS ",
61 &at_map->ad->ad_cname,
62 (ber_len_t)sizeof( " FROM " ) - 1, " FROM ",
64 (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
68 (ber_len_t)sizeof( "=?" ) - 1, "=?" );
70 if ( at_map->join_where.bv_val != NULL ) {
71 backsql_strfcat( &tmps, &tmpslen, "lb",
72 (ber_len_t)sizeof( " AND ") - 1, " AND ",
73 &at_map->join_where );
76 at_map->query = tmps.bv_val;
82 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
84 backsql_at_map_rec *at_map;
89 snprintf( s, sizeof( s ), "%ld", oc_map->id );
92 at_map = (backsql_at_map_rec *)ch_calloc(1,
93 sizeof( backsql_at_map_rec ) );
94 at_map->ad = slap_schema.si_ad_objectClass;
95 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1, &at_map->sel_expr );
96 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
98 len = at_map->from_tbls.bv_len + 1;
99 backsql_merge_from_clause( &at_map->from_tbls, &len, &oc_map->keytbl );
102 at_map->join_where.bv_val = NULL;
103 at_map->join_where.bv_len = 0;
104 backsql_strfcat( &at_map->join_where, &len, "lbcbll",
105 (ber_len_t)sizeof( "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=" ) - 1,
106 "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=",
110 (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1,
111 " and ldap_entries.oc_map_id=",
114 at_map->add_proc = NULL;
115 at_map->delete_proc = NULL;
116 at_map->param_order = 0;
117 at_map->expect_return = 0;
118 backsql_make_attr_query( oc_map, at_map );
119 avl_insert( &oc_map->attrs, at_map,
120 (AVL_CMP)backsql_cmp_attr, NULL );
122 at_map = (backsql_at_map_rec *)ch_calloc( 1,
123 sizeof( backsql_at_map_rec ) );
124 at_map->ad = slap_schema.si_ad_ref;
125 ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->sel_expr );
126 ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->from_tbls );
127 len = at_map->from_tbls.bv_len + 1;
128 backsql_merge_from_clause( &at_map->from_tbls, &len, &oc_map->keytbl );
131 at_map->join_where.bv_val = NULL;
132 at_map->join_where.bv_len = 0;
133 backsql_strfcat( &at_map->join_where, &len, "lbcbll",
134 (ber_len_t)sizeof( "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=" ) - 1,
135 "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=",
139 (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1,
140 " and ldap_entries.oc_map_id=",
143 at_map->add_proc = NULL;
144 at_map->delete_proc = NULL;
145 at_map->param_order = 0;
146 at_map->expect_return = 0;
147 backsql_make_attr_query( oc_map, at_map );
148 avl_insert( &oc_map->attrs, at_map,
149 (AVL_CMP)backsql_cmp_attr, NULL );
155 backsql_load_schema_map( backsql_info *si, SQLHDBC dbh )
157 SQLHSTMT oc_sth, at_sth;
159 BACKSQL_ROW_NTS oc_row, at_row;
161 backsql_oc_map_rec *oc_map;
162 backsql_at_map_rec *at_map;
164 Debug( LDAP_DEBUG_TRACE, "==>load_schema_map()\n", 0, 0, 0 );
167 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
169 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( si ) ) {
170 rc = backsql_Prepare( dbh, &oc_sth,
171 backsql_check_dn_ru_query, 0 );
172 if ( rc == SQL_SUCCESS ) {
173 /* Yes, the field exists */
174 si->bsql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
175 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
176 "in the schema\n", 0, 0, 0 );
178 /* No such field exists */
179 si->bsql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
182 SQLFreeStmt( oc_sth, SQL_DROP );
186 rc = backsql_Prepare( dbh, &oc_sth, si->oc_query, 0 );
187 if ( rc != SQL_SUCCESS ) {
188 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
189 "error preparing oc_query: '%s'\n",
190 si->oc_query, 0, 0 );
191 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
194 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n",
195 si->at_query, 0, 0 );
197 rc = backsql_Prepare( dbh, &at_sth, si->at_query, 0 );
198 if ( rc != SQL_SUCCESS ) {
199 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
200 "error preparing at_query: '%s'\n",
201 si->at_query, 0, 0 );
202 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
206 rc = backsql_BindParamID( at_sth, 1, &oc_id );
207 if ( rc != SQL_SUCCESS ) {
208 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
209 "error binding param for at_query: \n", 0, 0, 0 );
210 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
214 rc = SQLExecute( oc_sth );
215 if ( rc != SQL_SUCCESS ) {
216 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
217 "error executing oc_query: \n", 0, 0, 0 );
218 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
222 backsql_BindRowAsStrings( oc_sth, &oc_row );
223 rc = SQLFetch( oc_sth );
224 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( oc_sth ) ) {
227 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
228 sizeof( backsql_oc_map_rec ) );
230 oc_map->id = strtol( oc_row.cols[ 0 ], NULL, 0 );
232 oc_map->oc = oc_find( oc_row.cols[ 1 ] );
233 if ( oc_map->oc == NULL ) {
234 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
235 "objectClass '%s' is not defined in schema\n",
236 oc_row.cols[ 1 ], 0, 0 );
237 return LDAP_OTHER; /* undefined objectClass ? */
240 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->keytbl );
241 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->keycol );
242 oc_map->create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL
243 : ch_strdup( oc_row.cols[ 4 ] );
246 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
248 oc_map->create_keyval = ( oc_row.value_len[ 5 ] < 0 )
249 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
251 oc_map->delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL
252 : ch_strdup( oc_row.cols[ colnum ] );
253 oc_map->expect_return = strtol( oc_row.cols[ colnum + 1 ],
257 * FIXME: first attempt to check for offending
258 * instructions in {create|delete}_proc
261 oc_map->attrs = NULL;
262 avl_insert( &si->oc_by_oc, oc_map,
263 (AVL_CMP)backsql_cmp_oc, NULL );
264 avl_insert( &si->oc_by_id, oc_map,
265 (AVL_CMP)backsql_cmp_oc_id, NULL );
267 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
268 "objectClass '%s': keytbl='%s' keycol='%s'\n",
269 BACKSQL_OC_NAME( oc_map ),
270 oc_map->keytbl.bv_val, oc_map->keycol.bv_val );
271 if ( oc_map->create_proc ) {
272 Debug( LDAP_DEBUG_TRACE, "create_proc='%s'\n",
273 oc_map->create_proc, 0, 0 );
275 if ( oc_map->create_keyval ) {
276 Debug( LDAP_DEBUG_TRACE, "create_keyval='%s'\n",
277 oc_map->create_keyval, 0, 0 );
279 if ( oc_map->delete_proc ) {
280 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
281 oc_map->delete_proc, 0, 0 );
283 Debug( LDAP_DEBUG_TRACE, "expect_return: "
284 "add=%d, del=%d; attributes:\n",
285 BACKSQL_IS_ADD( oc_map->expect_return ),
286 BACKSQL_IS_DEL( oc_map->expect_return ), 0 );
288 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
289 "autoadding 'objectClass' and 'ref' mappings\n",
291 backsql_add_sysmaps( oc_map );
292 rc = SQLExecute( at_sth );
293 if ( rc != SQL_SUCCESS ) {
294 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
295 "error executing at_query: \n", 0, 0, 0 );
296 backsql_PrintErrors( SQL_NULL_HENV, dbh, at_sth, rc );
300 backsql_BindRowAsStrings( at_sth, &at_row );
301 rc = SQLFetch( at_sth );
302 for ( ; BACKSQL_SUCCESS(rc); rc = SQLFetch( at_sth ) ) {
303 const char *text = NULL;
307 Debug( LDAP_DEBUG_TRACE, "********'%s'\n",
308 at_row.cols[ 0 ], 0, 0 );
309 Debug( LDAP_DEBUG_TRACE,
310 "name='%s',sel_expr='%s' from='%s'",
311 at_row.cols[ 0 ], at_row.cols[ 1 ],
313 Debug( LDAP_DEBUG_TRACE,
314 "join_where='%s',add_proc='%s'",
315 at_row.cols[ 3 ], at_row.cols[ 4 ], 0 );
316 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
317 at_row.cols[ 5 ], 0, 0 );
319 Debug( LDAP_DEBUG_TRACE, "sel_expr_u='%s'\n",
320 at_row.cols[ 8 ], 0, 0 );
321 at_map = (backsql_at_map_rec *)ch_calloc( 1,
322 sizeof( backsql_at_map_rec ) );
323 rc = slap_str2ad( at_row.cols[ 0 ],
324 &at_map->ad, &text );
325 if ( rc != LDAP_SUCCESS ) {
326 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
327 "attribute '%s' for objectClass '%s' "
328 "is not defined in schema: %s\n",
329 at_map->ad->ad_cname.bv_val,
330 BACKSQL_OC_NAME( oc_map ), text );
331 return LDAP_CONSTRAINT_VIOLATION;
334 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->sel_expr );
335 if ( at_row.value_len[ 8 ] < 0 ) {
336 at_map->sel_expr_u.bv_val = NULL;
337 at_map->sel_expr_u.bv_len = 0;
339 ber_str2bv( at_row.cols[ 8 ], 0, 1,
340 &at_map->sel_expr_u );
343 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
344 backsql_merge_from_clause( &at_map->from_tbls,
346 if ( at_row.value_len[ 3 ] < 0 ) {
347 at_map->join_where.bv_val = NULL;
348 at_map->join_where.bv_len = 0;
350 ber_str2bv( at_row.cols[ 3 ], 0, 1,
351 &at_map->join_where );
353 at_map->add_proc = NULL;
354 if ( at_row.value_len[ 4 ] > 0 ) {
355 at_map->add_proc = ch_strdup( at_row.cols[4] );
357 at_map->delete_proc = NULL;
358 if ( at_row.value_len[ 5 ] > 0 ) {
360 = ch_strdup( at_row.cols[ 5 ] );
362 at_map->param_order = strtol( at_row.cols[ 6 ],
364 at_map->expect_return = strtol( at_row.cols[ 7 ],
366 backsql_make_attr_query( oc_map, at_map );
367 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
368 "preconstructed query '%s'\n",
369 at_map->query, 0, 0 );
370 avl_insert( &oc_map->attrs, at_map,
371 (AVL_CMP)backsql_cmp_attr, NULL );
373 backsql_FreeRow( &at_row );
374 SQLFreeStmt( at_sth, SQL_CLOSE );
376 backsql_FreeRow( &oc_row );
377 SQLFreeStmt( at_sth, SQL_DROP );
378 SQLFreeStmt( oc_sth, SQL_DROP );
379 si->bsql_flags |= BSQLF_SCHEMA_LOADED;
380 Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
385 backsql_oc2oc( backsql_info *si, ObjectClass *oc )
387 backsql_oc_map_rec tmp, *res;
390 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
391 "searching for objectclass with name='%s'\n",
393 #endif /* BACKSQL_TRACE */
396 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
397 (AVL_CMP)backsql_cmp_oc );
400 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
401 "found name='%s', id=%d\n",
402 BACKSQL_OC_NAME( res ), res->id, 0 );
404 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
405 "not found\n", 0, 0, 0 );
407 #endif /* BACKSQL_TRACE */
413 backsql_name2oc( backsql_info *si, struct berval *oc_name )
415 backsql_oc_map_rec tmp, *res;
418 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
419 "searching for objectclass with name='%s'\n",
421 #endif /* BACKSQL_TRACE */
423 tmp.oc = oc_bvfind( oc_name );
424 if ( tmp.oc == NULL ) {
428 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
429 (AVL_CMP)backsql_cmp_oc );
432 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
433 "found name='%s', id=%d\n",
434 BACKSQL_OC_NAME( res ), res->id, 0 );
436 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
437 "not found\n", 0, 0, 0 );
439 #endif /* BACKSQL_TRACE */
445 backsql_id2oc( backsql_info *si, unsigned long id )
447 backsql_oc_map_rec tmp, *res;
450 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
451 "searching for objectclass with id='%d'\n", id, 0, 0 );
452 #endif /* BACKSQL_TRACE */
455 res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
456 (AVL_CMP)backsql_cmp_oc_id );
460 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
461 "found name='%s', id=%d\n",
462 BACKSQL_OC_NAME( res ), res->id, 0 );
464 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
465 "not found\n", 0, 0, 0 );
467 #endif /* BACKSQL_TRACE */
473 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
475 backsql_at_map_rec tmp, *res;
478 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
479 "searching for attribute '%s' for objectclass '%s'\n",
480 attr, BACKSQL_OC_NAME( objclass ), 0 );
481 #endif /* BACKSQL_TRACE */
484 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
485 (AVL_CMP)backsql_cmp_attr );
489 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
490 "found name='%s', sel_expr='%s'\n",
491 res->ad->ad_cname.bv_val, res->sel_expr.bv_val, 0 );
493 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
494 "not found\n", 0, 0, 0 );
496 #endif /* BACKSQL_TRACE */
505 backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr )
507 backsql_at_map_rec tmp, *res;
508 const char *text = NULL;
511 Debug( LDAP_DEBUG_TRACE, "==>backsql_name2at(): "
512 "searching for attribute '%s' for objectclass '%s'\n",
513 attr, BACKSQL_OC_NAME( objclass ), 0 );
514 #endif /* BACKSQL_TRACE */
516 if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) {
520 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
521 (AVL_CMP)backsql_cmp_attr );
525 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
526 "found name='%s', sel_expr='%s'\n",
527 res->name, res->sel_expr.bv_val, 0 );
529 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
530 "not found\n", 0, 0, 0 );
532 #endif /* BACKSQL_TRACE */
538 backsql_free_attr( backsql_at_map_rec *at )
540 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n",
541 at->ad->ad_cname.bv_val, 0, 0 );
542 ch_free( at->sel_expr.bv_val );
543 if ( at->from_tbls.bv_val != NULL ) {
544 ch_free( at->from_tbls.bv_val );
546 if ( at->join_where.bv_val != NULL ) {
547 ch_free( at->join_where.bv_val );
549 if ( at->add_proc != NULL ) {
550 ch_free( at->add_proc );
552 if ( at->delete_proc != NULL ) {
553 ch_free( at->delete_proc );
556 ch_free( at->query );
560 if ( at->sel_expr_u.bv_val ) {
561 ch_free( at->sel_expr_u.bv_val );
566 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
570 backsql_free_oc( backsql_oc_map_rec *oc )
572 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n",
573 BACKSQL_OC_NAME( oc ), 0, 0 );
574 avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
575 ch_free( oc->keytbl.bv_val );
576 ch_free( oc->keycol.bv_val );
577 if ( oc->create_proc != NULL ) {
578 ch_free( oc->create_proc );
580 if ( oc->create_keyval != NULL ) {
581 ch_free( oc->create_keyval );
583 if ( oc->delete_proc != NULL ) {
584 ch_free( oc->delete_proc );
588 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
592 backsql_destroy_schema_map( backsql_info *si )
594 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
595 avl_free( si->oc_by_oc, NULL );
596 avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
597 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
601 #endif /* SLAPD_SQL */