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.is_null[ 4 ] < 0 ) ? NULL
243 : ch_strdup( oc_row.cols[ 4 ] );
246 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
248 oc_map->create_keyval = ( oc_row.is_null[ 5 ] < 0 )
249 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
251 oc_map->delete_proc = ( oc_row.is_null[ 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.is_null[ 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.is_null[ 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 = ( at_row.is_null[ 4 ] < 0 ) ? NULL
354 : ch_strdup( at_row.cols[4] );
355 at_map->delete_proc = ( at_row.is_null[ 5 ] < 0 ) ? NULL
356 : ch_strdup( at_row.cols[ 5 ] );
357 at_map->param_order = strtol( at_row.cols[ 6 ],
359 at_map->expect_return = strtol( at_row.cols[ 7 ],
361 backsql_make_attr_query( oc_map, at_map );
362 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
363 "preconstructed query '%s'\n",
364 at_map->query, 0, 0 );
365 avl_insert( &oc_map->attrs, at_map,
366 (AVL_CMP)backsql_cmp_attr, NULL );
368 backsql_FreeRow( &at_row );
369 SQLFreeStmt( at_sth, SQL_CLOSE );
371 backsql_FreeRow( &oc_row );
372 SQLFreeStmt( at_sth, SQL_DROP );
373 SQLFreeStmt( oc_sth, SQL_DROP );
374 si->bsql_flags |= BSQLF_SCHEMA_LOADED;
375 Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
380 backsql_oc2oc( backsql_info *si, ObjectClass *oc )
382 backsql_oc_map_rec tmp, *res;
385 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
386 "searching for objectclass with name='%s'\n",
388 #endif /* BACKSQL_TRACE */
391 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
392 (AVL_CMP)backsql_cmp_oc );
395 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
396 "found name='%s', id=%d\n",
397 BACKSQL_OC_NAME( res ), res->id, 0 );
399 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
400 "not found\n", 0, 0, 0 );
402 #endif /* BACKSQL_TRACE */
408 backsql_name2oc( backsql_info *si, struct berval *oc_name )
410 backsql_oc_map_rec tmp, *res;
413 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
414 "searching for objectclass with name='%s'\n",
416 #endif /* BACKSQL_TRACE */
418 tmp.oc = oc_bvfind( oc_name );
419 if ( tmp.oc == NULL ) {
423 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
424 (AVL_CMP)backsql_cmp_oc );
427 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
428 "found name='%s', id=%d\n",
429 BACKSQL_OC_NAME( res ), res->id, 0 );
431 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
432 "not found\n", 0, 0, 0 );
434 #endif /* BACKSQL_TRACE */
440 backsql_id2oc( backsql_info *si, unsigned long id )
442 backsql_oc_map_rec tmp, *res;
445 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
446 "searching for objectclass with id='%d'\n", id, 0, 0 );
447 #endif /* BACKSQL_TRACE */
450 res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
451 (AVL_CMP)backsql_cmp_oc_id );
455 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
456 "found name='%s', id=%d\n",
457 BACKSQL_OC_NAME( res ), res->id, 0 );
459 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
460 "not found\n", 0, 0, 0 );
462 #endif /* BACKSQL_TRACE */
468 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
470 backsql_at_map_rec tmp, *res;
473 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
474 "searching for attribute '%s' for objectclass '%s'\n",
475 attr, BACKSQL_OC_NAME( objclass ), 0 );
476 #endif /* BACKSQL_TRACE */
479 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
480 (AVL_CMP)backsql_cmp_attr );
484 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
485 "found name='%s', sel_expr='%s'\n",
486 res->ad->ad_cname.bv_val, res->sel_expr.bv_val, 0 );
488 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
489 "not found\n", 0, 0, 0 );
491 #endif /* BACKSQL_TRACE */
500 backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr )
502 backsql_at_map_rec tmp, *res;
503 const char *text = NULL;
506 Debug( LDAP_DEBUG_TRACE, "==>backsql_name2at(): "
507 "searching for attribute '%s' for objectclass '%s'\n",
508 attr, BACKSQL_OC_NAME( objclass ), 0 );
509 #endif /* BACKSQL_TRACE */
511 if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) {
515 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
516 (AVL_CMP)backsql_cmp_attr );
520 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
521 "found name='%s', sel_expr='%s'\n",
522 res->name, res->sel_expr.bv_val, 0 );
524 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
525 "not found\n", 0, 0, 0 );
527 #endif /* BACKSQL_TRACE */
533 backsql_free_attr( backsql_at_map_rec *at )
535 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n",
536 at->ad->ad_cname.bv_val, 0, 0 );
537 ch_free( at->sel_expr.bv_val );
538 if ( at->from_tbls.bv_val != NULL ) {
539 ch_free( at->from_tbls.bv_val );
541 if ( at->join_where.bv_val != NULL ) {
542 ch_free( at->join_where.bv_val );
544 if ( at->add_proc != NULL ) {
545 ch_free( at->add_proc );
547 if ( at->delete_proc != NULL ) {
548 ch_free( at->delete_proc );
551 ch_free( at->query );
555 if ( at->sel_expr_u.bv_val ) {
556 ch_free( at->sel_expr_u.bv_val );
561 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
565 backsql_free_oc( backsql_oc_map_rec *oc )
567 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n",
568 BACKSQL_OC_NAME( oc ), 0, 0 );
569 avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
570 ch_free( oc->keytbl.bv_val );
571 ch_free( oc->keycol.bv_val );
572 if ( oc->create_proc != NULL ) {
573 ch_free( oc->create_proc );
575 if ( oc->create_keyval != NULL ) {
576 ch_free( oc->create_keyval );
578 if ( oc->delete_proc != NULL ) {
579 ch_free( oc->delete_proc );
583 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
587 backsql_destroy_schema_map( backsql_info *si )
589 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
590 avl_free( si->oc_by_oc, NULL );
591 avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
592 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
596 #endif /* SLAPD_SQL */