Debian Codegraph @

servers/slapd/back-sql/schema-map.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * Portions Copyright 2004 Mark Adamson.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
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>.
17 */
18 /* ACKNOWLEDGEMENTS:
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.
22 */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include "ac/string.h"
29
30 #include "lutil.h"
31 #include "slap.h"
32 #include "proto-sql.h"
33
34 #define BACKSQL_DUPLICATE (-1)
35
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 */
42
43 /*
44 * Uses the pointer to the ObjectClass structure
45 */
46 static int
47 backsql_cmp_oc( const void *v_m1, const void *v_m2 )
48 {
49 const backsql_oc_map_rec *m1 = v_m1,
50 *m2 = v_m2;
51
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 */
57 }
58
59 static int
60 backsql_cmp_oc_id( const void *v_m1, const void *v_m2 )
61 {
62 const backsql_oc_map_rec *m1 = v_m1,
63 *m2 = v_m2;
64
65 return ( m1->bom_id < m2->bom_id ? -1 : ( m1->bom_id > m2->bom_id ? 1 : 0 ) );
66 }
67
68 /*
69 * Uses the pointer to the AttributeDescription structure
70 */
71 static int
72 backsql_cmp_attr( const void *v_m1, const void *v_m2 )
73 {
74 const backsql_at_map_rec *m1 = v_m1,
75 *m2 = v_m2;
76
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 */
83 }
84
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 */
90 }
91
92 int
93 backsql_dup_attr( void *v_m1, void *v_m2 )
94 {
95 backsql_at_map_rec *m1 = v_m1,
96 *m2 = v_m2;
97
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 */
104
105 } else {
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 */
111 }
112
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 );
117
118 m1->bam_next = m2;
119 m2->bam_next = NULL;
120
121 return BACKSQL_DUPLICATE;
122 }
123
124 static int
125 backsql_make_attr_query(
126 backsql_info *bi,
127 backsql_oc_map_rec *oc_map,
128 backsql_at_map_rec *at_map )
129 {
130 struct berbuf bb = BB_NULL;
131
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( " " ), " ",
136 &bi->sql_aliasing,
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 ",
143 &oc_map->bom_keytbl,
144 '.',
145 &oc_map->bom_keycol,
146 (ber_len_t)STRLENOF( "=?" ), "=?" );
147
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 );
152 }
153
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 );
159
160 at_map->bam_query = bb.bb_val.bv_val;
161
162 #ifdef BACKSQL_COUNTQUERY
163 /* Query to count how many rows will be returned.
164
165 SELECT COUNT(*) FROM <from_tbls> WHERE <keytbl>.<keycol>=?
166 [ AND <join_where> ]
167
168 */
169 BER_BVZERO( &bb.bb_val );
170 bb.bb_len = 0;
171 backsql_strfcat_x( &bb, NULL, "lblbcbl",
172 (ber_len_t)STRLENOF( "SELECT COUNT(*) FROM " ),
173 "SELECT COUNT(*) FROM ",
174 &at_map->bam_from_tbls,
175 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
176 &oc_map->bom_keytbl,
177 '.',
178 &oc_map->bom_keycol,
179 (ber_len_t)STRLENOF( "=?" ), "=?" );
180
181 if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
182 backsql_strfcat_x( &bb, NULL, "lb",
183 (ber_len_t)STRLENOF( " AND " ), " AND ",
184 &at_map->bam_join_where );
185 }
186
187 at_map->bam_countquery = bb.bb_val.bv_val;
188 #endif /* BACKSQL_COUNTQUERY */
189
190 return 0;
191 }
192
193 static int
194 backsql_add_sysmaps( backsql_info *bi, backsql_oc_map_rec *oc_map )
195 {
196 backsql_at_map_rec *at_map;
197 char s[LDAP_PVT_INTTYPE_CHARS(long)];
198 struct berval sbv;
199 struct berbuf bb;
200
201 sbv.bv_val = s;
202 sbv.bv_len = snprintf( s, sizeof( s ), BACKSQL_IDNUMFMT, oc_map->bom_id );
203
204 /* extra objectClasses */
205 at_map = (backsql_at_map_rec *)ch_calloc(1,
206 sizeof( backsql_at_map_rec ) );
207 at_map->bam_ad = slap_schema.si_ad_objectClass;
208 at_map->bam_true_ad = slap_schema.si_ad_objectClass;
209 ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
210 &at_map->bam_sel_expr );
211 ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1,
212 &at_map->bam_from_tbls );
213
214 bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
215 bb.bb_val = at_map->bam_from_tbls;
216 backsql_merge_from_clause( bi, &bb, &oc_map->bom_keytbl );
217 at_map->bam_from_tbls = bb.bb_val;
218
219 BER_BVZERO( &bb.bb_val );
220 bb.bb_len = 0;
221 backsql_strfcat_x( &bb, NULL, "lbcblb",
222 (ber_len_t)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
223 "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
224 &oc_map->bom_keytbl,
225 '.',
226 &oc_map->bom_keycol,
227 (ber_len_t)STRLENOF( " and ldap_entries.oc_map_id=" ),
228 " and ldap_entries.oc_map_id=",
229 &sbv );
230 at_map->bam_join_where = bb.bb_val;
231
232 at_map->bam_oc = oc_map->bom_oc;
233
234 at_map->bam_add_proc = NULL;
235 {
236 char tmp[STRLENOF("INSERT INTO ldap_entry_objclasses "
237 "(entry_id,oc_name) VALUES "
238 "((SELECT id FROM ldap_entries "
239 "WHERE oc_map_id= "
240 "AND keyval=?),?)") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
241 snprintf( tmp, sizeof(tmp),
242 "INSERT INTO ldap_entry_objclasses "
243 "(entry_id,oc_name) VALUES "
244 "((SELECT id FROM ldap_entries "
245 "WHERE oc_map_id=" BACKSQL_IDNUMFMT " "
246 "AND keyval=?),?)", oc_map->bom_id );
247 at_map->bam_add_proc = ch_strdup( tmp );
248 }
249
250 at_map->bam_delete_proc = NULL;
251 {
252 char tmp[STRLENOF("DELETE FROM ldap_entry_objclasses "
253 "WHERE entry_id=(SELECT id FROM ldap_entries "
254 "WHERE oc_map_id= "
255 "AND keyval=?) AND oc_name=?") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
256 snprintf( tmp, sizeof(tmp),
257 "DELETE FROM ldap_entry_objclasses "
258 "WHERE entry_id=(SELECT id FROM ldap_entries "
259 "WHERE oc_map_id=" BACKSQL_IDNUMFMT " "
260 "AND keyval=?) AND oc_name=?",
261 oc_map->bom_id );
262 at_map->bam_delete_proc = ch_strdup( tmp );
263 }
264
265 at_map->bam_param_order = 0;
266 at_map->bam_expect_return = 0;
267 at_map->bam_next = NULL;
268
269 backsql_make_attr_query( bi, oc_map, at_map );
270 if ( ldap_avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
271 Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
272 "duplicate attribute \"%s\" in objectClass \"%s\" map\n",
273 at_map->bam_ad->ad_cname.bv_val,
274 oc_map->bom_oc->soc_cname.bv_val );
275 }
276
277 /* FIXME: we need to correct the objectClass join_where
278 * after the attribute query is built */
279 ch_free( at_map->bam_join_where.bv_val );
280 BER_BVZERO( &bb.bb_val );
281 bb.bb_len = 0;
282 backsql_strfcat_x( &bb, NULL, "lbcblb",
283 (ber_len_t)STRLENOF( /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=" ),
284 /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=",
285 &oc_map->bom_keytbl,
286 '.',
287 &oc_map->bom_keycol,
288 (ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ),
289 " AND ldap_entries.oc_map_id=",
290 &sbv );
291 at_map->bam_join_where = bb.bb_val;
292
293 return 1;
294 }
295
296 struct backsql_attr_schema_info {
297 backsql_info *bas_bi;
298 SQLHDBC bas_dbh;
299 SQLHSTMT bas_sth;
300 backsql_key_t *bas_oc_id;
301 int bas_rc;
302 };
303
304 static int
305 backsql_oc_get_attr_mapping( void *v_oc, void *v_bas )
306 {
307 RETCODE rc;
308 BACKSQL_ROW_NTS at_row;
309 backsql_oc_map_rec *oc_map = (backsql_oc_map_rec *)v_oc;
310 backsql_at_map_rec *at_map;
311 struct backsql_attr_schema_info *bas = (struct backsql_attr_schema_info *)v_bas;
312
313 /* bas->bas_oc_id has been bound to bas->bas_sth */
314 *bas->bas_oc_id = oc_map->bom_id;
315
316 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
317 "executing at_query\n"
318 " \"%s\"\n"
319 " for objectClass \"%s\"\n"
320 " with param oc_id=" BACKSQL_IDNUMFMT "\n",
321 bas->bas_bi->sql_at_query,
322 BACKSQL_OC_NAME( oc_map ),
323 *bas->bas_oc_id );
324
325 rc = SQLExecute( bas->bas_sth );
326 if ( rc != SQL_SUCCESS ) {
327 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
328 "error executing at_query\n"
329 " \"%s\"\n"
330 " for objectClass \"%s\"\n"
331 " with param oc_id=" BACKSQL_IDNUMFMT "\n",
332 bas->bas_bi->sql_at_query,
333 BACKSQL_OC_NAME( oc_map ),
334 *bas->bas_oc_id );
335 backsql_PrintErrors( bas->bas_bi->sql_db_env,
336 bas->bas_dbh, bas->bas_sth, rc );
337 bas->bas_rc = LDAP_OTHER;
338 return BACKSQL_AVL_STOP;
339 }
340
341 backsql_BindRowAsStrings( bas->bas_sth, &at_row );
342 for ( ; rc = SQLFetch( bas->bas_sth ), BACKSQL_SUCCESS( rc ); ) {
343 const char *text = NULL;
344 struct berval bv;
345 struct berbuf bb = BB_NULL;
346 AttributeDescription *ad = NULL;
347
348 {
349 struct {
350 int idx;
351 char *name;
352 } required[] = {
353 { 0, "name" },
354 { 1, "sel_expr" },
355 { 2, "from" },
356 { -1, NULL },
357 };
358 int i;
359
360 for ( i = 0; required[ i ].name != NULL; i++ ) {
361 if ( at_row.value_len[ i ] <= 0 ) {
362 Debug( LDAP_DEBUG_ANY,
363 "backsql_oc_get_attr_mapping(): "
364 "required column #%d \"%s\" is empty\n",
365 required[ i ].idx, required[ i ].name );
366 bas->bas_rc = LDAP_OTHER;
367 return BACKSQL_AVL_STOP;
368 }
369 }
370 }
371
372 Debug(LDAP_DEBUG_TRACE,
373 "attributeType: " "name=\"%s\" " "sel_expr=\"%s\" " "from=\"%s\" " "join_where=\"%s\" " "add_proc=\"%s\" " "delete_proc=\"%s\" " "sel_expr_u=\"%s\"\n",
374 at_row.cols[0], at_row.cols[1], at_row.cols[2],
375 at_row.cols[3] ? at_row.cols[3] : "",
376 at_row.cols[4] ? at_row.cols[4] : "",
377 at_row.cols[5] ? at_row.cols[5] : "",
378 at_row.cols[8] ? at_row.cols[8] : "" );
379
380 rc = slap_str2ad( at_row.cols[ 0 ], &ad, &text );
381 if ( rc != LDAP_SUCCESS ) {
382 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
383 "attribute \"%s\" for objectClass \"%s\" "
384 "is not defined in schema: %s\n",
385 at_row.cols[ 0 ],
386 BACKSQL_OC_NAME( oc_map ), text );
387 bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
388 return BACKSQL_AVL_STOP;
389 }
390 at_map = (backsql_at_map_rec *)ch_calloc( 1,
391 sizeof( backsql_at_map_rec ) );
392 at_map->bam_ad = ad;
393 at_map->bam_true_ad = ad;
394 if ( slap_syntax_is_binary( ad->ad_type->sat_syntax )
395 && !slap_ad_is_binary( ad ) )
396 {
397 char buf[ SLAP_TEXT_BUFLEN ];
398 struct berval bv;
399 const char *text = NULL;
400
401 bv.bv_val = buf;
402 bv.bv_len = snprintf( buf, sizeof( buf ), "%s;binary",
403 ad->ad_cname.bv_val );
404 at_map->bam_true_ad = NULL;
405 bas->bas_rc = slap_bv2ad( &bv, &at_map->bam_true_ad, &text );
406 if ( bas->bas_rc != LDAP_SUCCESS ) {
407 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
408 "unable to fetch attribute \"%s\": %s (%d)\n",
409 buf, text, rc );
410 ch_free( at_map );
411 return BACKSQL_AVL_STOP;
412 }
413 }
414
415 ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
416 if ( at_row.value_len[ 8 ] <= 0 ) {
417 BER_BVZERO( &at_map->bam_sel_expr_u );
418
419 } else {
420 ber_str2bv( at_row.cols[ 8 ], 0, 1,
421 &at_map->bam_sel_expr_u );
422 }
423
424 ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv );
425 backsql_merge_from_clause( bas->bas_bi, &bb, &bv );
426 at_map->bam_from_tbls = bb.bb_val;
427 if ( at_row.value_len[ 3 ] <= 0 ) {
428 BER_BVZERO( &at_map->bam_join_where );
429
430 } else {
431 ber_str2bv( at_row.cols[ 3 ], 0, 1,
432 &at_map->bam_join_where );
433 }
434 at_map->bam_add_proc = NULL;
435 if ( at_row.value_len[ 4 ] > 0 ) {
436 at_map->bam_add_proc = ch_strdup( at_row.cols[ 4 ] );
437 }
438 at_map->bam_delete_proc = NULL;
439 if ( at_row.value_len[ 5 ] > 0 ) {
440 at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
441 }
442 if ( lutil_atoix( &at_map->bam_param_order, at_row.cols[ 6 ], 0 ) != 0 ) {
443 /* error */
444 }
445 if ( lutil_atoix( &at_map->bam_expect_return, at_row.cols[ 7 ], 0 ) != 0 ) {
446 /* error */
447 }
448 backsql_make_attr_query( bas->bas_bi, oc_map, at_map );
449 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
450 "preconstructed query \"%s\"\n",
451 at_map->bam_query );
452 at_map->bam_next = NULL;
453 if ( ldap_avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
454 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
455 "duplicate attribute \"%s\" "
456 "in objectClass \"%s\" map\n",
457 at_map->bam_ad->ad_cname.bv_val,
458 oc_map->bom_oc->soc_cname.bv_val );
459 ch_free( at_map );
460 }
461
462 if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
463 BER_BVISNULL( &at_map->bam_sel_expr_u ) )
464 {
465 struct berbuf bb = BB_NULL;
466
467 backsql_strfcat_x( &bb, NULL, "bcbc",
468 &bas->bas_bi->sql_upper_func,
469 '(' /* ) */ ,
470 &at_map->bam_sel_expr,
471 /* ( */ ')' );
472 at_map->bam_sel_expr_u = bb.bb_val;
473 }
474 }
475 backsql_FreeRow( &at_row );
476 SQLFreeStmt( bas->bas_sth, SQL_CLOSE );
477
478 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
479 "autoadding 'objectClass' and 'ref' mappings\n",
480 BACKSQL_OC_NAME( oc_map ) );
481
482 (void)backsql_add_sysmaps( bas->bas_bi, oc_map );
483
484 return BACKSQL_AVL_CONTINUE;
485 }
486
487
488 int
489 backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
490 {
491 SQLHSTMT sth = SQL_NULL_HSTMT;
492 RETCODE rc;
493 BACKSQL_ROW_NTS oc_row;
494 backsql_key_t oc_id;
495 backsql_oc_map_rec *oc_map;
496 struct backsql_attr_schema_info bas;
497
498 int delete_proc_idx = 5;
499 int create_hint_idx = delete_proc_idx + 2;
500
501 Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n" );
502
503 /*
504 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
505 */
506 if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
507 rc = backsql_Prepare( dbh, &sth,
508 backsql_check_dn_ru_query, 0 );
509 if ( rc == SQL_SUCCESS ) {
510 /* Yes, the field exists */
511 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
512 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
513 "in the schema\n" );
514 } else {
515 /* No such field exists */
516 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
517 }
518
519 SQLFreeStmt( sth, SQL_DROP );
520 }
521
522 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n",
523 bi->sql_oc_query );
524
525 rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
526 if ( rc != SQL_SUCCESS ) {
527 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
528 "error preparing oc_query: \"%s\"\n",
529 bi->sql_oc_query );
530 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
531 return LDAP_OTHER;
532 }
533
534 rc = SQLExecute( sth );
535 if ( rc != SQL_SUCCESS ) {
536 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
537 "error executing oc_query: \n" );
538 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
539 return LDAP_OTHER;
540 }
541
542 backsql_BindRowAsStrings( sth, &oc_row );
543 rc = SQLFetch( sth );
544
545 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
546 delete_proc_idx++;
547 create_hint_idx++;
548 }
549
550 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
551 {
552 struct {
553 int idx;
554 char *name;
555 } required[] = {
556 { 0, "id" },
557 { 1, "name" },
558 { 2, "keytbl" },
559 { 3, "keycol" },
560 { -1, "expect_return" },
561 { -1, NULL },
562 };
563 int i;
564
565 required[4].idx = delete_proc_idx + 1;
566
567 for ( i = 0; required[ i ].name != NULL; i++ ) {
568 if ( oc_row.value_len[ required[ i ].idx ] <= 0 ) {
569 Debug( LDAP_DEBUG_ANY,
570 "backsql_load_schema_map(): "
571 "required column #%d \"%s\" is empty\n",
572 required[ i ].idx, required[ i ].name );
573 return LDAP_OTHER;
574 }
575 }
576 }
577
578 Debug(LDAP_DEBUG_TRACE,
579 "objectClass: " "id=\"%s\" " "name=\"%s\" " "keytbl=\"%s\" " "keycol=\"%s\" " "create_proc=\"%s\" " "create_keyval=\"%s\" " "delete_proc=\"%s\" " "expect_return=\"%s\"" "create_hint=\"%s\" \n",
580 oc_row.cols[0], oc_row.cols[1], oc_row.cols[2],
581 oc_row.cols[3],
582 oc_row.cols[4] ? oc_row.cols[4] : "",
583 (BACKSQL_CREATE_NEEDS_SELECT(bi) && oc_row.cols[5]) ? oc_row.cols[5] : "",
584 oc_row.cols[delete_proc_idx] ? oc_row.cols[delete_proc_idx] : "",
585 oc_row.cols[delete_proc_idx + 1],
586 ((oc_row.ncols > create_hint_idx) && oc_row.cols[create_hint_idx]) ? oc_row.cols[create_hint_idx] : "" );
587
588 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
589 sizeof( backsql_oc_map_rec ) );
590
591 if ( BACKSQL_STR2ID( &oc_map->bom_id, oc_row.cols[ 0 ], 0 ) != 0 ) {
592 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
593 "unable to parse id=\"%s\"\n",
594 oc_row.cols[ 0 ] );
595 ch_free( oc_map );
596 return LDAP_OTHER;
597 }
598
599 oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
600 if ( oc_map->bom_oc == NULL ) {
601 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
602 "objectClass \"%s\" is not defined in schema\n",
603 oc_row.cols[ 1 ] );
604 ch_free( oc_map );
605 return LDAP_OTHER; /* undefined objectClass ? */
606 }
607
608 ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
609 ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
610 oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] <= 0 ) ? NULL
611 : ch_strdup( oc_row.cols[ 4 ] );
612
613 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
614 oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] <= 0 )
615 ? NULL : ch_strdup( oc_row.cols[ 5 ] );
616 }
617 oc_map->bom_delete_proc = ( oc_row.value_len[ delete_proc_idx ] <= 0 ) ? NULL
618 : ch_strdup( oc_row.cols[ delete_proc_idx ] );
619 if ( lutil_atoix( &oc_map->bom_expect_return, oc_row.cols[ delete_proc_idx + 1 ], 0 ) != 0 ) {
620 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
621 "unable to parse expect_return=\"%s\" for objectClass \"%s\"\n",
622 oc_row.cols[ delete_proc_idx + 1 ], oc_row.cols[ 1 ] );
623 ch_free( oc_map );
624 return LDAP_OTHER;
625 }
626
627 if ( ( oc_row.ncols > create_hint_idx ) &&
628 ( oc_row.value_len[ create_hint_idx ] > 0 ) )
629 {
630 const char *text;
631
632 oc_map->bom_create_hint = NULL;
633 rc = slap_str2ad( oc_row.cols[ create_hint_idx ],
634 &oc_map->bom_create_hint, &text );
635 if ( rc != SQL_SUCCESS ) {
636 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
637 "error matching "
638 "AttributeDescription %s "
639 "in create_hint: %s (%d)\n",
640 oc_row.cols[ create_hint_idx ],
641 text, rc );
642 backsql_PrintErrors( bi->sql_db_env, dbh,
643 sth, rc );
644 ch_free( oc_map );
645 return LDAP_OTHER;
646 }
647 }
648
649 /*
650 * FIXME: first attempt to check for offending
651 * instructions in {create|delete}_proc
652 */
653
654 oc_map->bom_attrs = NULL;
655 if ( ldap_avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, ldap_avl_dup_error ) == -1 ) {
656 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
657 "duplicate objectClass \"%s\" in objectClass map\n",
658 oc_map->bom_oc->soc_cname.bv_val );
659 ch_free( oc_map );
660 return LDAP_OTHER;
661 }
662 if ( ldap_avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, ldap_avl_dup_error ) == -1 ) {
663 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
664 "duplicate objectClass \"%s\" in objectClass by ID map\n",
665 oc_map->bom_oc->soc_cname.bv_val );
666 return LDAP_OTHER;
667 }
668 oc_id = oc_map->bom_id;
669 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
670 "objectClass \"%s\":\n keytbl=\"%s\" keycol=\"%s\"\n",
671 BACKSQL_OC_NAME( oc_map ),
672 oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
673 if ( oc_map->bom_create_proc ) {
674 Debug( LDAP_DEBUG_TRACE, " create_proc=\"%s\"\n",
675 oc_map->bom_create_proc );
676 }
677 if ( oc_map->bom_create_keyval ) {
678 Debug( LDAP_DEBUG_TRACE, " create_keyval=\"%s\"\n",
679 oc_map->bom_create_keyval );
680 }
681 if ( oc_map->bom_create_hint ) {
682 Debug( LDAP_DEBUG_TRACE, " create_hint=\"%s\"\n",
683 oc_map->bom_create_hint->ad_cname.bv_val );
684 }
685 if ( oc_map->bom_delete_proc ) {
686 Debug( LDAP_DEBUG_TRACE, " delete_proc=\"%s\"\n",
687 oc_map->bom_delete_proc );
688 }
689 Debug( LDAP_DEBUG_TRACE, " expect_return: "
690 "add=%d, del=%d; attributes:\n",
691 BACKSQL_IS_ADD( oc_map->bom_expect_return ),
692 BACKSQL_IS_DEL( oc_map->bom_expect_return ) );
693 }
694
695 backsql_FreeRow( &oc_row );
696 SQLFreeStmt( sth, SQL_DROP );
697
698 /* prepare for attribute fetching */
699 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): at_query \"%s\"\n",
700 bi->sql_at_query );
701
702 rc = backsql_Prepare( dbh, &sth, bi->sql_at_query, 0 );
703 if ( rc != SQL_SUCCESS ) {
704 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
705 "error preparing at_query: \"%s\"\n",
706 bi->sql_at_query );
707 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
708 return LDAP_OTHER;
709 }
710
711 rc = backsql_BindParamNumID( sth, 1, SQL_PARAM_INPUT, &oc_id );
712 if ( rc != SQL_SUCCESS ) {
713 Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
714 "error binding param \"oc_id\" for at_query\n" );
715 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
716 SQLFreeStmt( sth, SQL_DROP );
717 return LDAP_OTHER;
718 }
719
720 bas.bas_bi = bi;
721 bas.bas_dbh = dbh;
722 bas.bas_sth = sth;
723 bas.bas_oc_id = &oc_id;
724 bas.bas_rc = LDAP_SUCCESS;
725
726 (void)ldap_avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
727 &bas, BACKSQL_AVL_STOP, AVL_INORDER );
728
729 SQLFreeStmt( sth, SQL_DROP );
730
731 bi->sql_flags |= BSQLF_SCHEMA_LOADED;
732
733 Debug( LDAP_DEBUG_TRACE, "<==backsql_load_schema_map()\n" );
734
735 return bas.bas_rc;
736 }
737
738 backsql_oc_map_rec *
739 backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
740 {
741 backsql_oc_map_rec tmp, *res;
742
743 #ifdef BACKSQL_TRACE
744 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc2oc(): "
745 "searching for objectclass with name=\"%s\"\n",
746 oc->soc_cname.bv_val );
747 #endif /* BACKSQL_TRACE */
748
749 tmp.bom_oc = oc;
750 res = (backsql_oc_map_rec *)ldap_avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
751 #ifdef BACKSQL_TRACE
752 if ( res != NULL ) {
753 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
754 "found name=\"%s\", id=%d\n",
755 BACKSQL_OC_NAME( res ), res->bom_id );
756 } else {
757 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
758 "not found\n" );
759 }
760 #endif /* BACKSQL_TRACE */
761
762 return res;
763 }
764
765 backsql_oc_map_rec *
766 backsql_name2oc( backsql_info *bi, struct berval *oc_name )
767 {
768 backsql_oc_map_rec tmp, *res;
769
770 #ifdef BACKSQL_TRACE
771 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
772 "searching for objectclass with name=\"%s\"\n",
773 oc_name->bv_val );
774 #endif /* BACKSQL_TRACE */
775
776 tmp.bom_oc = oc_bvfind( oc_name );
777 if ( tmp.bom_oc == NULL ) {
778 return NULL;
779 }
780
781 res = (backsql_oc_map_rec *)ldap_avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
782 #ifdef BACKSQL_TRACE
783 if ( res != NULL ) {
784 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
785 "found name=\"%s\", id=%d\n",
786 BACKSQL_OC_NAME( res ), res->bom_id );
787 } else {
788 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
789 "not found\n" );
790 }
791 #endif /* BACKSQL_TRACE */
792
793 return res;
794 }
795
796 backsql_oc_map_rec *
797 backsql_id2oc( backsql_info *bi, unsigned long id )
798 {
799 backsql_oc_map_rec tmp, *res;
800
801 #ifdef BACKSQL_TRACE
802 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
803 "searching for objectclass with id=%lu\n", id );
804 #endif /* BACKSQL_TRACE */
805
806 tmp.bom_id = id;
807 res = (backsql_oc_map_rec *)ldap_avl_find( bi->sql_oc_by_id, &tmp,
808 backsql_cmp_oc_id );
809
810 #ifdef BACKSQL_TRACE
811 if ( res != NULL ) {
812 Debug( LDAP_DEBUG_TRACE, "<==oc_with_id(): "
813 "found name=\"%s\", id=%lu\n",
814 BACKSQL_OC_NAME( res ), res->bom_id );
815 } else {
816 Debug( LDAP_DEBUG_TRACE, "<==oc_with_id(): "
817 "id=%lu not found\n", res->bom_id );
818 }
819 #endif /* BACKSQL_TRACE */
820
821 return res;
822 }
823
824 backsql_at_map_rec *
825 backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )
826 {
827 backsql_at_map_rec tmp = { 0 }, *res;
828
829 #ifdef BACKSQL_TRACE
830 Debug( LDAP_DEBUG_TRACE, "==>backsql_ad2at(): "
831 "searching for attribute \"%s\" for objectclass \"%s\"\n",
832 ad->ad_cname.bv_val, BACKSQL_OC_NAME( objclass ) );
833 #endif /* BACKSQL_TRACE */
834
835 tmp.bam_ad = ad;
836 res = (backsql_at_map_rec *)ldap_avl_find( objclass->bom_attrs, &tmp,
837 backsql_cmp_attr );
838
839 #ifdef BACKSQL_TRACE
840 if ( res != NULL ) {
841 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
842 "found name=\"%s\", sel_expr=\"%s\"\n",
843 res->bam_ad->ad_cname.bv_val,
844 res->bam_sel_expr.bv_val );
845 } else {
846 Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): "
847 "not found\n" );
848 }
849 #endif /* BACKSQL_TRACE */
850
851 return res;
852 }
853
854 /* attributeType inheritance */
855 struct supad2at_t {
856 backsql_at_map_rec **ret;
857 AttributeDescription *ad;
858 unsigned n;
859 };
860
861 #define SUPAD2AT_STOP (-1)
862
863 static int
864 supad2at_f( void *v_at, void *v_arg )
865 {
866 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
867 struct supad2at_t *va = (struct supad2at_t *)v_arg;
868
869 if ( is_at_subtype( at->bam_ad->ad_type, va->ad->ad_type ) ) {
870 backsql_at_map_rec **ret = NULL;
871 unsigned i;
872
873 /* if already listed, holler! (should never happen) */
874 if ( va->ret ) {
875 for ( i = 0; i < va->n; i++ ) {
876 if ( va->ret[ i ]->bam_ad == at->bam_ad ) {
877 break;
878 }
879 }
880
881 if ( i < va->n ) {
882 return 0;
883 }
884 }
885
886 ret = ch_realloc( va->ret,
887 sizeof( backsql_at_map_rec * ) * ( va->n + 2 ) );
888 if ( ret == NULL ) {
889 ch_free( va->ret );
890 va->ret = NULL;
891 va->n = 0;
892 return SUPAD2AT_STOP;
893 }
894
895 ret[ va->n ] = at;
896 va->n++;
897 ret[ va->n ] = NULL;
898 va->ret = ret;
899 }
900
901 return 0;
902 }
903
904 /*
905 * stores in *pret a NULL terminated array of pointers
906 * to backsql_at_map_rec whose attributeType is supad->ad_type
907 * or derived from it
908 */
909 int
910 backsql_supad2at( backsql_oc_map_rec *objclass, AttributeDescription *supad,
911 backsql_at_map_rec ***pret )
912 {
913 struct supad2at_t va = { 0 };
914 int rc;
915
916 assert( objclass != NULL );
917 assert( supad != NULL );
918 assert( pret != NULL );
919
920 *pret = NULL;
921
922 va.ad = supad;
923
924 rc = ldap_avl_apply( objclass->bom_attrs, supad2at_f, &va,
925 SUPAD2AT_STOP, AVL_INORDER );
926 if ( rc == SUPAD2AT_STOP ) {
927 return -1;
928 }
929
930 *pret = va.ret;
931
932 return 0;
933 }
934
935 static void
936 backsql_free_attr( void *v_at )
937 {
938 backsql_at_map_rec *at = v_at;
939
940 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): \"%s\"\n",
941 at->bam_ad->ad_cname.bv_val );
942 ch_free( at->bam_sel_expr.bv_val );
943 if ( !BER_BVISNULL( &at->bam_from_tbls ) ) {
944 ch_free( at->bam_from_tbls.bv_val );
945 }
946 if ( !BER_BVISNULL( &at->bam_join_where ) ) {
947 ch_free( at->bam_join_where.bv_val );
948 }
949 if ( at->bam_add_proc != NULL ) {
950 ch_free( at->bam_add_proc );
951 }
952 if ( at->bam_delete_proc != NULL ) {
953 ch_free( at->bam_delete_proc );
954 }
955 if ( at->bam_query != NULL ) {
956 ch_free( at->bam_query );
957 }
958
959 #ifdef BACKSQL_COUNTQUERY
960 if ( at->bam_countquery != NULL ) {
961 ch_free( at->bam_countquery );
962 }
963 #endif /* BACKSQL_COUNTQUERY */
964
965 /* TimesTen */
966 if ( !BER_BVISNULL( &at->bam_sel_expr_u ) ) {
967 ch_free( at->bam_sel_expr_u.bv_val );
968 }
969
970 if ( at->bam_next ) {
971 backsql_free_attr( at->bam_next );
972 }
973
974 ch_free( at );
975
976 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n" );
977 }
978
979 static void
980 backsql_free_oc( void *v_oc )
981 {
982 backsql_oc_map_rec *oc = v_oc;
983
984 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): \"%s\"\n",
985 BACKSQL_OC_NAME( oc ) );
986 ldap_avl_free( oc->bom_attrs, backsql_free_attr );
987 ch_free( oc->bom_keytbl.bv_val );
988 ch_free( oc->bom_keycol.bv_val );
989 if ( oc->bom_create_proc != NULL ) {
990 ch_free( oc->bom_create_proc );
991 }
992 if ( oc->bom_create_keyval != NULL ) {
993 ch_free( oc->bom_create_keyval );
994 }
995 if ( oc->bom_delete_proc != NULL ) {
996 ch_free( oc->bom_delete_proc );
997 }
998 ch_free( oc );
999
1000 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n" );
1001 }
1002
1003 int
1004 backsql_destroy_schema_map( backsql_info *bi )
1005 {
1006 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n" );
1007 ldap_avl_free( bi->sql_oc_by_oc, 0 );
1008 ldap_avl_free( bi->sql_oc_by_id, backsql_free_oc );
1009 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n" );
1010 return 0;
1011 }
1012