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>
21 #include "schema-map.h"
29 backsql_cmp_oc_name( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
31 return BACKSQL_NCMP( &m1->name, &m2->name );
36 * Uses the pointer to the ObjectClass structure
39 backsql_cmp_oc( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
41 return ( m1->oc < m2->oc ? -1 : ( m1->oc > m2->oc ? 1 : 0 ) );
45 backsql_cmp_oc_id( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
47 return ( m1->id < m2->id ? -1 : ( m1->id > m2->id ? 1 : 0 ) );
55 backsql_cmp_attr_name( backsql_at_map_rec *m1, backsql_at_map_rec *m2 )
57 return BACKSQL_NCMP( &m1->name, &m2->name );
62 * Uses the pointer to the AttributeDescription structure
65 backsql_cmp_attr( backsql_at_map_rec *m1, backsql_at_map_rec *m2 )
67 return ( m1->ad < m2->ad ? -1 : ( m1->ad > m2->ad ? 1 : 0 ) );
71 backsql_make_attr_query(
72 backsql_oc_map_rec *oc_map,
73 backsql_at_map_rec *at_map )
75 struct berval tmps = { 0, NULL };
78 backsql_strcat( &tmps, &tmpslen, "SELECT ", at_map->sel_expr,
79 " AS ", at_map->name.bv_val,
80 " FROM ", at_map->from_tbls,
81 " WHERE ", oc_map->keytbl,".", oc_map->keycol,
83 if ( at_map->join_where != NULL ) {
84 backsql_strcat( &tmps, &tmpslen, " AND ",
85 at_map->join_where, NULL );
88 at_map->query = tmps.bv_val;
94 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
96 backsql_at_map_rec *at_map;
101 snprintf( s, sizeof( s ), "%ld", oc_map->id );
103 at_map = (backsql_at_map_rec *)ch_calloc(1,
104 sizeof( backsql_at_map_rec ) );
105 at_map->ad = slap_schema.si_ad_objectClass;
106 ber_dupbv( &at_map->name, &at_map->ad->ad_cname );
107 at_map->sel_expr = ch_strdup( "ldap_entry_objclasses.oc_name" );
108 at_map->from_tbls = ch_strdup( "ldap_entry_objclasses,ldap_entries" );
109 len = strlen( at_map->from_tbls );
110 backsql_merge_from_clause( &at_map->from_tbls, &len, oc_map->keytbl );
115 backsql_strcat( &bv, &len,
116 "ldap_entries.id=ldap_entry_objclasses.entry_id "
117 "and ldap_entries.keyval=",
118 oc_map->keytbl, ".", oc_map->keycol,
119 " and ldap_entries.oc_map_id=", s, NULL );
120 at_map->join_where = bv.bv_val;
122 at_map->add_proc = NULL;
123 at_map->delete_proc = NULL;
124 at_map->param_order = 0;
125 at_map->expect_return = 0;
126 backsql_make_attr_query( oc_map, at_map );
127 avl_insert( &oc_map->attrs, at_map,
128 (AVL_CMP)backsql_cmp_attr, NULL );
130 at_map = (backsql_at_map_rec *)ch_calloc( 1,
131 sizeof( backsql_at_map_rec ) );
132 at_map->ad = slap_schema.si_ad_ref;
133 ber_dupbv( &at_map->name, &at_map->ad->ad_cname );
134 at_map->sel_expr = ch_strdup( "ldap_referrals.url" );
135 at_map->from_tbls = ch_strdup( "ldap_referrals,ldap_entries" );
136 len = strlen( at_map->from_tbls );
137 backsql_merge_from_clause( &at_map->from_tbls, &len,oc_map->keytbl );
142 backsql_strcat( &bv, &len,
143 "ldap_entries.id=ldap_referrals.entry_id "
144 "and ldap_entries.keyval=",
145 oc_map->keytbl, ".", oc_map->keycol,
146 " and ldap_entries.oc_map_id=", s, NULL );
147 at_map->join_where = bv.bv_val;
149 at_map->add_proc = NULL;
150 at_map->delete_proc = NULL;
151 at_map->param_order = 0;
152 at_map->expect_return = 0;
153 backsql_make_attr_query( oc_map, at_map );
154 avl_insert( &oc_map->attrs, at_map,
155 (AVL_CMP)backsql_cmp_attr, NULL );
161 backsql_load_schema_map( backsql_info *si, SQLHDBC dbh )
163 SQLHSTMT oc_sth, at_sth;
165 BACKSQL_ROW_NTS oc_row, at_row;
167 backsql_oc_map_rec *oc_map;
168 backsql_at_map_rec *at_map;
172 Debug( LDAP_DEBUG_TRACE, "==>load_schema_map()\n", 0, 0, 0 );
175 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
177 if ( si->has_ldapinfo_dn_ru == -1 ) {
178 rc = backsql_Prepare( dbh, &oc_sth,
179 backsql_check_dn_ru_query, 0 );
180 if ( rc == SQL_SUCCESS ) {
181 si->has_ldapinfo_dn_ru = 1; /* Yes, the field exists */
182 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
183 "in the schema\n", 0, 0, 0 );
185 si->has_ldapinfo_dn_ru = 0; /* No such field exists */
188 SQLFreeStmt( oc_sth, SQL_DROP );
192 rc = backsql_Prepare( dbh, &oc_sth, si->oc_query, 0 );
193 if ( rc != SQL_SUCCESS ) {
194 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
195 "error preparing oc_query: '%s'\n",
196 si->oc_query, 0, 0 );
197 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
200 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n",
201 si->at_query, 0, 0 );
203 rc = backsql_Prepare( dbh, &at_sth, si->at_query, 0 );
204 if ( rc != SQL_SUCCESS ) {
205 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
206 "error preparing at_query: '%s'\n",
207 si->at_query, 0, 0 );
208 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
212 rc = backsql_BindParamID( at_sth, 1, &oc_id );
213 if ( rc != SQL_SUCCESS ) {
214 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
215 "error binding param for at_query: \n", 0, 0, 0 );
216 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
220 rc = SQLExecute( oc_sth );
221 if ( rc != SQL_SUCCESS ) {
222 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
223 "error executing oc_query: \n", 0, 0, 0 );
224 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
228 backsql_BindRowAsStrings( oc_sth, &oc_row );
229 rc = SQLFetch( oc_sth );
230 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( oc_sth ) ) {
231 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
232 sizeof( backsql_oc_map_rec ) );
234 oc_map->id = atoi( oc_row.cols[ 0 ] );
236 ber_str2bv( oc_row.cols[ 1 ], 0, 1, &oc_map->name );
237 oc_map->oc = oc_bvfind( &oc_map->name );
238 if ( oc_map->oc == NULL ) {
239 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
240 "objectClass '%s' is not defined in schema\n",
241 oc_map->name.bv_val, 0, 0 );
242 return LDAP_OTHER; /* undefined objectClass ? */
245 oc_map->keytbl = ch_strdup( oc_row.cols[ 2 ] );
246 oc_map->keycol = ch_strdup( oc_row.cols[ 3 ] );
247 oc_map->create_proc = ( oc_row.is_null[ 4 ] < 0 ) ? NULL
248 : ch_strdup( oc_row.cols[ 4 ] );
249 oc_map->delete_proc = ( oc_row.is_null[ 5 ] < 0 ) ? NULL
250 : ch_strdup( oc_row.cols[ 5 ] );
251 oc_map->expect_return = atoi( oc_row.cols[ 6 ] );
254 * FIXME: first attempt to check for offending
255 * instructions in {create|delete}_proc
258 oc_map->attrs = NULL;
259 avl_insert( &si->oc_by_oc, oc_map,
260 (AVL_CMP)backsql_cmp_oc, NULL );
261 avl_insert( &si->oc_by_id, oc_map,
262 (AVL_CMP)backsql_cmp_oc_id, NULL );
264 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
265 "objectClass '%s': keytbl='%s' keycol='%s'\n",
266 oc_map->name.bv_val, oc_map->keytbl, oc_map->keycol );
267 if ( oc_map->create_proc ) {
268 Debug( LDAP_DEBUG_TRACE, "create_proc='%s'\n",
269 oc_map->create_proc, 0, 0 );
271 if ( oc_map->delete_proc ) {
272 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
273 oc_map->delete_proc, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE, "expect_return: "
276 "add=%s, del=%s; attributes:\n",
277 BACKSQL_IS_ADD( oc_map->expect_return ),
278 BACKSQL_IS_DEL( oc_map->expect_return ), 0 );
280 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
281 "autoadding 'objectClass' and 'ref' mappings\n",
283 backsql_add_sysmaps( oc_map );
284 rc = SQLExecute( at_sth );
285 if ( rc != SQL_SUCCESS ) {
286 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
287 "error executing at_query: \n", 0, 0, 0 );
288 backsql_PrintErrors( SQL_NULL_HENV, dbh, at_sth, rc );
292 backsql_BindRowAsStrings( at_sth, &at_row );
293 rc = SQLFetch( at_sth );
294 for ( ; BACKSQL_SUCCESS(rc); rc = SQLFetch( at_sth ) ) {
295 const char *text = NULL;
297 Debug( LDAP_DEBUG_TRACE, "********'%s'\n",
298 at_row.cols[ 0 ], 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "name='%s',sel_expr='%s' from='%s'",
301 at_row.cols[ 0 ], at_row.cols[ 1 ],
303 Debug( LDAP_DEBUG_TRACE,
304 "join_where='%s',add_proc='%s'",
305 at_row.cols[ 3 ], at_row.cols[ 4 ], 0 );
306 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
307 at_row.cols[ 5 ], 0, 0 );
309 Debug( LDAP_DEBUG_TRACE, "sel_expr_u='%s'\n",
310 at_row.cols[ 8 ], 0, 0 );
311 at_map = (backsql_at_map_rec *)ch_calloc( 1,
312 sizeof( backsql_at_map_rec ) );
313 ber_str2bv( at_row.cols[ 0 ], 0, 1, &at_map->name );
314 rc = slap_bv2ad( &at_map->name, &at_map->ad, &text );
315 if ( rc != LDAP_SUCCESS ) {
316 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
317 "attribute '%s' for objectClass '%s' "
318 "is not defined in schema: %s\n",
320 oc_map->name.bv_val, text );
321 return LDAP_CONSTRAINT_VIOLATION;
324 at_map->sel_expr = ch_strdup( at_row.cols[ 1 ] );
325 at_map->sel_expr_u = ( at_row.is_null[ 8 ] < 0 ) ? NULL
326 : ch_strdup( at_row.cols[ 8 ] );
329 backsql_merge_from_clause( &tmps, &tmpslen,
331 at_map->from_tbls = tmps;
332 at_map->join_where = ( at_row.is_null[ 3 ] < 0 ) ? NULL
333 : ch_strdup( at_row.cols[ 3 ] );
334 at_map->add_proc = ( at_row.is_null[ 4 ] < 0 ) ? NULL
335 : ch_strdup( at_row.cols[4] );
336 at_map->delete_proc = ( at_row.is_null[ 5 ] < 0 ) ? NULL
337 : ch_strdup( at_row.cols[ 5 ] );
338 at_map->param_order = atoi( at_row.cols[ 6 ] );
339 at_map->expect_return = atoi( at_row.cols[ 7 ] );
340 backsql_make_attr_query( oc_map, at_map );
341 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
342 "preconstructed query '%s'\n",
343 at_map->query, 0, 0 );
344 avl_insert( &oc_map->attrs, at_map,
345 (AVL_CMP)backsql_cmp_attr, NULL );
347 backsql_FreeRow( &at_row );
348 SQLFreeStmt( at_sth, SQL_CLOSE );
350 backsql_FreeRow( &oc_row );
351 SQLFreeStmt( at_sth, SQL_DROP );
352 SQLFreeStmt( oc_sth, SQL_DROP );
353 si->schema_loaded = 1;
354 Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
359 backsql_oc2oc( backsql_info *si, ObjectClass *oc )
361 backsql_oc_map_rec tmp, *res;
364 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
365 "searching for objectclass with name='%s'\n",
370 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
371 (AVL_CMP)backsql_cmp_oc );
374 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
375 "found name='%s', id=%d\n", res->name, res->id, 0 );
377 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
378 "not found\n", 0, 0, 0 );
389 backsql_name2oc( backsql_info *si, struct berval *oc_name )
391 backsql_oc_map_rec tmp, *res;
394 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
395 "searching for objectclass with name='%s'\n",
399 tmp.oc = oc_bvfind( oc_name );
400 if ( tmp.oc == NULL ) {
404 res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp,
405 (AVL_CMP)backsql_cmp_oc );
408 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
409 "found name='%s', id=%d\n", res->name, res->id, 0 );
411 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
412 "not found\n", 0, 0, 0 );
420 backsql_id2oc( backsql_info *si, unsigned long id )
422 backsql_oc_map_rec tmp, *res;
425 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
426 "searching for objectclass with id='%d'\n", id, 0, 0 );
430 res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
431 (AVL_CMP)backsql_cmp_oc_id );
435 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
436 "found name='%s', id=%d\n", res->name, res->id, 0 );
438 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
439 "not found\n", 0, 0, 0 );
447 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
449 backsql_at_map_rec tmp, *res;
452 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
453 "searching for attribute '%s' for objectclass '%s'\n",
454 attr, objclass->name, 0 );
457 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
458 (AVL_CMP)backsql_cmp_attr );
462 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
463 "found name='%s', sel_expr='%s'\n",
464 res->name, res->sel_expr, 0 );
466 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
467 "not found\n", 0, 0, 0 );
478 backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr )
480 backsql_at_map_rec tmp, *res;
481 const char *text = NULL;
484 Debug( LDAP_DEBUG_TRACE, "==>backsql_name2at(): "
485 "searching for attribute '%s' for objectclass '%s'\n",
486 attr, objclass->name, 0 );
489 if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) {
493 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
494 (AVL_CMP)backsql_cmp_attr );
498 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
499 "found name='%s', sel_expr='%s'\n",
500 res->name, res->sel_expr, 0 );
502 Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): "
503 "not found\n", 0, 0, 0 );
511 backsql_free_attr( backsql_at_map_rec *at )
513 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n",
514 at->name.bv_val, 0, 0 );
515 ch_free( at->name.bv_val );
516 ch_free( at->sel_expr );
517 if ( at->from_tbls != NULL ) {
518 ch_free( at->from_tbls );
520 if ( at->join_where != NULL ) {
521 ch_free( at->join_where );
523 if ( at->add_proc != NULL ) {
524 ch_free( at->add_proc );
526 if ( at->delete_proc != NULL ) {
527 ch_free( at->delete_proc );
530 ch_free( at->query );
534 if ( at->sel_expr_u ) {
535 ch_free( at->sel_expr_u );
540 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
544 backsql_free_oc( backsql_oc_map_rec *oc )
546 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n",
547 oc->name.bv_val, 0, 0 );
548 avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
549 ch_free( oc->name.bv_val );
550 ch_free( oc->keytbl );
551 ch_free( oc->keycol );
552 if ( oc->create_proc != NULL ) {
553 ch_free( oc->create_proc );
555 if ( oc->delete_proc != NULL ) {
556 ch_free( oc->delete_proc );
560 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
564 backsql_destroy_schema_map( backsql_info *si )
566 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
567 avl_free( si->oc_by_oc, NULL );
568 avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
569 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
573 #endif /* SLAPD_SQL */