schema-map.c 27.9 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1999-2021 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Portions Copyright 1999 Dmitry Kovalev.
6
 * Portions Copyright 2002 Pierangelo Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
 * Portions Copyright 2004 Mark Adamson.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
10
11
12
13
14
15
16
17
18
19
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Dmitry Kovalev for inclusion
20
21
 * by OpenLDAP Software.  Additional significant contributors include
 * Pierangelo Masarati and Mark Adamson.
22
23
24
25
26
27
 */

#include "portable.h"

#include <stdio.h>
#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
35
#define BACKSQL_DUPLICATE	(-1)

36
37
38
39
40
41
42
/* NOTE: by default, cannot just compare pointers because
 * objectClass/attributeType order would be machine-dependent
 * (and tests would fail!); however, if you don't want to run
 * tests, or see attributeTypes written in the same order
 * they are defined, define */
/* #undef BACKSQL_USE_PTR_CMP */

Pierangelo Masarati's avatar
Pierangelo Masarati committed
43
44
45
46
/*
 * Uses the pointer to the ObjectClass structure
 */
static int
47
backsql_cmp_oc( const void *v_m1, const void *v_m2 )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
48
{
49
50
	const backsql_oc_map_rec	*m1 = v_m1,
					*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 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
57
58
59
}

static int
60
backsql_cmp_oc_id( const void *v_m1, const void *v_m2 )
61
{
62
63
	const backsql_oc_map_rec	*m1 = v_m1,
					*m2 = v_m2;
64
65

	return ( m1->bom_id < m2->bom_id ? -1 : ( m1->bom_id > m2->bom_id ? 1 : 0 ) );
66
67
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
68
69
70
71
/*
 * Uses the pointer to the AttributeDescription structure
 */
static int
72
backsql_cmp_attr( const void *v_m1, const void *v_m2 )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
73
{
74
75
	const backsql_at_map_rec	*m1 = v_m1,
					*m2 = v_m2;
76

77
78
79
80
81
82
83
84
	if ( slap_ad_is_binary( m1->bam_ad ) || slap_ad_is_binary( m2->bam_ad ) ) {
#ifdef BACKSQL_USE_PTR_CMP
		return SLAP_PTRCMP( m1->bam_ad->ad_type, m2->bam_ad->ad_type );
#else /* ! BACKSQL_USE_PTR_CMP */
		return ber_bvcmp( &m1->bam_ad->ad_type->sat_cname, &m2->bam_ad->ad_type->sat_cname );
#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 */
90
91
92
93
94
}

int
backsql_dup_attr( void *v_m1, void *v_m2 )
{
95
96
	backsql_at_map_rec		*m1 = v_m1,
					*m2 = v_m2;
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
	if ( slap_ad_is_binary( m1->bam_ad ) || slap_ad_is_binary( m2->bam_ad ) ) {
#ifdef BACKSQL_USE_PTR_CMP
		assert( m1->bam_ad->ad_type == m2->bam_ad->ad_type );
#else /* ! BACKSQL_USE_PTR_CMP */
		assert( ber_bvcmp( &m1->bam_ad->ad_type->sat_cname, &m2->bam_ad->ad_type->sat_cname ) == 0 );
#endif /* ! BACKSQL_USE_PTR_CMP */

	} else {
#ifdef BACKSQL_USE_PTR_CMP
		assert( m1->bam_ad == m2->bam_ad );
#else /* ! BACKSQL_USE_PTR_CMP */
		assert( ber_bvcmp( &m1->bam_ad->ad_cname, &m2->bam_ad->ad_cname ) == 0 );
#endif /* ! BACKSQL_USE_PTR_CMP */
	}
112
113
114
115

	/* duplicate definitions of attributeTypes are appended;
	 * this allows to define multiple rules for the same 
	 * attributeType.  Use with care! */
116
	for ( ; m1->bam_next ; m1 = m1->bam_next );
117

118
119
120
121
	m1->bam_next = m2;
	m2->bam_next = NULL;

	return BACKSQL_DUPLICATE;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
122
123
124
}

static int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
125
backsql_make_attr_query( 
126
	backsql_info		*bi,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
127
128
	backsql_oc_map_rec 	*oc_map,
	backsql_at_map_rec 	*at_map )
Dmitry Kovalev's avatar
Dmitry Kovalev committed
129
{
130
	struct berbuf	bb = BB_NULL;
131

132
	backsql_strfcat_x( &bb, NULL, "lblbbbblblbcbl", 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
133
			(ber_len_t)STRLENOF( "SELECT " ), "SELECT ", 
134
			&at_map->bam_sel_expr, 
135
136
137
			(ber_len_t)STRLENOF( " " ), " ",
			&bi->sql_aliasing,
			&bi->sql_aliasing_quote, 
138
			&at_map->bam_ad->ad_cname,
139
140
			&bi->sql_aliasing_quote,
			(ber_len_t)STRLENOF( " FROM " ), " FROM ", 
141
			&at_map->bam_from_tbls, 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
142
			(ber_len_t)STRLENOF( " WHERE " ), " WHERE ", 
143
			&oc_map->bom_keytbl,
144
			'.', 
145
			&oc_map->bom_keycol,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
146
			(ber_len_t)STRLENOF( "=?" ), "=?" );
147

148
	if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
149
		backsql_strfcat_x( &bb, NULL, "lb",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
150
				(ber_len_t)STRLENOF( " AND " ), " AND ", 
151
				&at_map->bam_join_where );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
152
	}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
153

154
	backsql_strfcat_x( &bb, NULL, "lbbb", 
155
156
			(ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ",
			&bi->sql_aliasing_quote,
157
			&at_map->bam_ad->ad_cname,
158
			&bi->sql_aliasing_quote );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
159

160
	at_map->bam_query = bb.bb_val.bv_val;
161
162

#ifdef BACKSQL_COUNTQUERY
Pierangelo Masarati's avatar
Pierangelo Masarati committed
163
164
165
166
167
168
	/* Query to count how many rows will be returned.

	SELECT COUNT(*) FROM <from_tbls> WHERE <keytbl>.<keycol>=?
		[ AND <join_where> ]

	 */
169
170
	BER_BVZERO( &bb.bb_val );
	bb.bb_len = 0;
171
	backsql_strfcat_x( &bb, NULL, "lblbcbl", 
172
173
174
175
176
177
178
179
180
181
			(ber_len_t)STRLENOF( "SELECT COUNT(*) FROM " ),
				"SELECT COUNT(*) FROM ", 
			&at_map->bam_from_tbls, 
			(ber_len_t)STRLENOF( " WHERE " ), " WHERE ", 
			&oc_map->bom_keytbl,
			'.', 
			&oc_map->bom_keycol,
			(ber_len_t)STRLENOF( "=?" ), "=?" );

	if ( !BER_BVISNULL( &at_map->bam_join_where ) ) {
182
		backsql_strfcat_x( &bb, NULL, "lb",
183
184
185
186
187
188
189
				(ber_len_t)STRLENOF( " AND " ), " AND ", 
				&at_map->bam_join_where );
	}

	at_map->bam_countquery = bb.bb_val.bv_val;
#endif /* BACKSQL_COUNTQUERY */

Pierangelo Masarati's avatar
Pierangelo Masarati committed
190
	return 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
191
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
192

Pierangelo Masarati's avatar
Pierangelo Masarati committed
193
static int
194
backsql_add_sysmaps( backsql_info *bi, backsql_oc_map_rec *oc_map )
Dmitry Kovalev's avatar
Dmitry Kovalev committed
195
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
203

204
	/* extra objectClasses */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
205
206
	at_map = (backsql_at_map_rec *)ch_calloc(1, 
			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
210
	ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1,
			&at_map->bam_sel_expr );
211
	ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1, 
212
			&at_map->bam_from_tbls );
213
	
214
215
	bb.bb_len = at_map->bam_from_tbls.bv_len + 1;
	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
223
			(ber_len_t)STRLENOF( "ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=" ),
				"ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entries.keyval=",
224
			&oc_map->bom_keytbl, 
225
			'.', 
226
			&oc_map->bom_keycol,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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
238
			"(entry_id,oc_name) VALUES "
			"((SELECT id FROM ldap_entries "
239
240
			"WHERE oc_map_id= "
			"AND keyval=?),?)") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
241
242
243
244
		snprintf( tmp, sizeof(tmp), 
			"INSERT INTO ldap_entry_objclasses "
			"(entry_id,oc_name) VALUES "
			"((SELECT id FROM ldap_entries "
245
			"WHERE oc_map_id=" BACKSQL_IDNUMFMT " "
246
247
248
249
			"AND keyval=?),?)", oc_map->bom_id );
		at_map->bam_add_proc = ch_strdup( tmp );
	}

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
255
			"WHERE oc_map_id= "
			"AND keyval=?) AND oc_name=?") + LDAP_PVT_INTTYPE_CHARS(unsigned long)];
256
257
258
		snprintf( tmp, sizeof(tmp), 
			"DELETE FROM ldap_entry_objclasses "
			"WHERE entry_id=(SELECT id FROM ldap_entries "
259
			"WHERE oc_map_id=" BACKSQL_IDNUMFMT " "
260
261
262
263
264
			"AND keyval=?) AND oc_name=?",
			oc_map->bom_id );
		at_map->bam_delete_proc = ch_strdup( tmp );
	}

265
266
267
	at_map->bam_param_order = 0;
	at_map->bam_expect_return = 0;
	at_map->bam_next = NULL;
268

269
	backsql_make_attr_query( bi, oc_map, at_map );
270
271
272
273
	if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
		Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): "
				"duplicate attribute \"%s\" in objectClass \"%s\" map\n",
				at_map->bam_ad->ad_cname.bv_val,
274
				oc_map->bom_oc->soc_cname.bv_val );
275
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
276

277
	/* FIXME: we need to correct the objectClass join_where 
278
279
280
281
	 * after the attribute query is built */
	ch_free( at_map->bam_join_where.bv_val );
	BER_BVZERO( &bb.bb_val );
	bb.bb_len = 0;
282
	backsql_strfcat_x( &bb, NULL, "lbcblb",
283
284
			(ber_len_t)STRLENOF( /* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=" ),
				/* "ldap_entries.id=ldap_entry_objclasses.entry_id AND " */ "ldap_entries.keyval=",
285
286
287
288
289
290
291
292
			&oc_map->bom_keytbl, 
			'.', 
			&oc_map->bom_keycol,
			(ber_len_t)STRLENOF( " AND ldap_entries.oc_map_id=" ), 
				" AND ldap_entries.oc_map_id=", 
			&sbv );
	at_map->bam_join_where = bb.bb_val;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
293
	return 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
294
295
}

296
struct backsql_attr_schema_info {
297
	backsql_info	*bas_bi;
298
299
	SQLHDBC		bas_dbh;
	SQLHSTMT	bas_sth;
300
	backsql_key_t	*bas_oc_id;
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
	int		bas_rc;
};

static int
backsql_oc_get_attr_mapping( void *v_oc, void *v_bas )
{
	RETCODE				rc;
	BACKSQL_ROW_NTS			at_row;
	backsql_oc_map_rec		*oc_map = (backsql_oc_map_rec *)v_oc;
	backsql_at_map_rec		*at_map;
	struct backsql_attr_schema_info	*bas = (struct backsql_attr_schema_info *)v_bas;

	/* bas->bas_oc_id has been bound to bas->bas_sth */
	*bas->bas_oc_id = oc_map->bom_id;

	Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
		"executing at_query\n"
		"    \"%s\"\n"
		"    for objectClass \"%s\"\n"
320
		"    with param oc_id=" BACKSQL_IDNUMFMT "\n",
321
		bas->bas_bi->sql_at_query,
322
323
324
325
326
327
328
329
330
		BACKSQL_OC_NAME( oc_map ),
		*bas->bas_oc_id );

	rc = SQLExecute( bas->bas_sth );
	if ( rc != SQL_SUCCESS ) {
		Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
			"error executing at_query\n"
			"    \"%s\"\n"
			"    for objectClass \"%s\"\n"
331
			"    with param oc_id=" BACKSQL_IDNUMFMT "\n",
332
			bas->bas_bi->sql_at_query,
333
334
			BACKSQL_OC_NAME( oc_map ),
			*bas->bas_oc_id );
335
336
		backsql_PrintErrors( bas->bas_bi->sql_db_env,
				bas->bas_dbh, bas->bas_sth, rc );
337
338
339
340
341
342
343
344
345
		bas->bas_rc = LDAP_OTHER;
		return BACKSQL_AVL_STOP;
	}

	backsql_BindRowAsStrings( bas->bas_sth, &at_row );
	for ( ; rc = SQLFetch( bas->bas_sth ), BACKSQL_SUCCESS( rc ); ) {
		const char	*text = NULL;
		struct berval	bv;
		struct berbuf	bb = BB_NULL;
Howard Chu's avatar
Howard Chu committed
346
		AttributeDescription *ad = NULL;
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
		{
			struct {
				int idx;
				char *name;
			} required[] = {
				{ 0, "name" },
				{ 1, "sel_expr" },
				{ 2, "from" },
				{ -1, NULL },
			};
			int i;

			for ( i = 0; required[ i ].name != NULL; i++ ) {
				if ( at_row.value_len[ i ] <= 0 ) {
					Debug( LDAP_DEBUG_ANY,
						"backsql_oc_get_attr_mapping(): "
						"required column #%d \"%s\" is empty\n",
365
						required[ i ].idx, required[ i ].name );
366
367
368
369
370
371
					bas->bas_rc = LDAP_OTHER;
					return BACKSQL_AVL_STOP;
				}
			}
		}

372
373
374
375
376
377
378
		Debug(LDAP_DEBUG_TRACE,
		      "attributeType: " "name=\"%s\" " "sel_expr=\"%s\" " "from=\"%s\" " "join_where=\"%s\" " "add_proc=\"%s\" " "delete_proc=\"%s\" " "sel_expr_u=\"%s\"\n",
		      at_row.cols[0], at_row.cols[1], at_row.cols[2],
		      at_row.cols[3] ? at_row.cols[3] : "",
		      at_row.cols[4] ? at_row.cols[4] : "",
		      at_row.cols[5] ? at_row.cols[5] : "",
		      at_row.cols[8] ? at_row.cols[8] : "" );
379

Howard Chu's avatar
Howard Chu committed
380
		rc = slap_str2ad( at_row.cols[ 0 ], &ad, &text );
381
382
383
384
385
386
387
388
389
		if ( rc != LDAP_SUCCESS ) {
			Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
				"attribute \"%s\" for objectClass \"%s\" "
				"is not defined in schema: %s\n", 
				at_row.cols[ 0 ],
				BACKSQL_OC_NAME( oc_map ), text );
			bas->bas_rc = LDAP_CONSTRAINT_VIOLATION;
			return BACKSQL_AVL_STOP;
		}
Howard Chu's avatar
Howard Chu committed
390
391
392
		at_map = (backsql_at_map_rec *)ch_calloc( 1,
				sizeof( backsql_at_map_rec ) );
		at_map->bam_ad = ad;
393
394
395
396
		at_map->bam_true_ad = ad;
		if ( slap_syntax_is_binary( ad->ad_type->sat_syntax )
			&& !slap_ad_is_binary( ad ) )
		{
397
			char		buf[ SLAP_TEXT_BUFLEN ];
398
399
400
401
402
403
404
405
406
407
408
409
			struct berval	bv;
			const char	*text = NULL;

			bv.bv_val = buf;
			bv.bv_len = snprintf( buf, sizeof( buf ), "%s;binary",
				ad->ad_cname.bv_val );
			at_map->bam_true_ad = NULL;
			bas->bas_rc = slap_bv2ad( &bv, &at_map->bam_true_ad, &text );
			if ( bas->bas_rc != LDAP_SUCCESS ) {
				Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
					"unable to fetch attribute \"%s\": %s (%d)\n",
					buf, text, rc );
Howard Chu's avatar
Howard Chu committed
410
				ch_free( at_map );
411
412
413
				return BACKSQL_AVL_STOP;
			}
		}
414
415

		ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
416
		if ( at_row.value_len[ 8 ] <= 0 ) {
417
			BER_BVZERO( &at_map->bam_sel_expr_u );
418
419
420
421
422
423
424

		} else {
			ber_str2bv( at_row.cols[ 8 ], 0, 1, 
					&at_map->bam_sel_expr_u );
		}

		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;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
427
		if ( at_row.value_len[ 3 ] <= 0 ) {
428
			BER_BVZERO( &at_map->bam_join_where );
429
430
431
432
433
434
435

		} else {
			ber_str2bv( at_row.cols[ 3 ], 0, 1, 
					&at_map->bam_join_where );
		}
		at_map->bam_add_proc = NULL;
		if ( at_row.value_len[ 4 ] > 0 ) {
436
			at_map->bam_add_proc = ch_strdup( at_row.cols[ 4 ] );
437
438
439
440
441
		}
		at_map->bam_delete_proc = NULL;
		if ( at_row.value_len[ 5 ] > 0 ) {
			at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] );
		}
442
		if ( lutil_atoix( &at_map->bam_param_order, at_row.cols[ 6 ], 0 ) != 0 ) {
443
444
			/* error */
		}
445
		if ( lutil_atoix( &at_map->bam_expect_return, at_row.cols[ 7 ], 0 ) != 0 ) {
446
447
			/* error */
		}
448
		backsql_make_attr_query( bas->bas_bi, oc_map, at_map );
449
450
		Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
			"preconstructed query \"%s\"\n",
451
			at_map->bam_query );
452
453
454
455
456
457
		at_map->bam_next = NULL;
		if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) {
			Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): "
					"duplicate attribute \"%s\" "
					"in objectClass \"%s\" map\n",
					at_map->bam_ad->ad_cname.bv_val,
458
					oc_map->bom_oc->soc_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
459
			ch_free( at_map );
460
461
		}

462
		if ( !BER_BVISNULL( &bas->bas_bi->sql_upper_func ) &&
463
464
				BER_BVISNULL( &at_map->bam_sel_expr_u ) )
		{
465
466
			struct berbuf	bb = BB_NULL;

467
			backsql_strfcat_x( &bb, NULL, "bcbc",
468
					&bas->bas_bi->sql_upper_func,
469
470
471
472
473
474
475
476
477
					'(' /* ) */ ,
					&at_map->bam_sel_expr,
					/* ( */ ')' );
			at_map->bam_sel_expr_u = bb.bb_val;
		}
	}
	backsql_FreeRow( &at_row );
	SQLFreeStmt( bas->bas_sth, SQL_CLOSE );

478
479
	Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(\"%s\"): "
		"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
485
486
487
	return BACKSQL_AVL_CONTINUE;
}


Pierangelo Masarati's avatar
Pierangelo Masarati committed
488
int
489
backsql_load_schema_map( backsql_info *bi, SQLHDBC dbh )
490
{
491
	SQLHSTMT 			sth = SQL_NULL_HSTMT;
492
493
	RETCODE				rc;
	BACKSQL_ROW_NTS			oc_row;
494
	backsql_key_t			oc_id;
495
496
	backsql_oc_map_rec		*oc_map;
	struct backsql_attr_schema_info	bas;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
497

498
499
500
	int				delete_proc_idx = 5;
	int				create_hint_idx = delete_proc_idx + 2;

501
	Debug( LDAP_DEBUG_TRACE, "==>backsql_load_schema_map()\n" );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
502
503
504
505

	/* 
	 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
	 */
506
	if ( !BACKSQL_DONTCHECK_LDAPINFO_DN_RU( bi ) ) {
507
		rc = backsql_Prepare( dbh, &sth, 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
508
509
				backsql_check_dn_ru_query, 0 );
		if ( rc == SQL_SUCCESS ) {
510
			/* Yes, the field exists */
511
			bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
512
   			Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
513
				"in the schema\n" );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
514
		} else {
515
			/* No such field exists */
516
			bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
517
518
		}

519
		SQLFreeStmt( sth, SQL_DROP );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
520
521
	}

522
	Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): oc_query \"%s\"\n", 
523
			bi->sql_oc_query );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
524

525
	rc = backsql_Prepare( dbh, &sth, bi->sql_oc_query, 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
526
	if ( rc != SQL_SUCCESS ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
531
		return LDAP_OTHER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
532
533
	}

534
	rc = SQLExecute( sth );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
535
	if ( rc != SQL_SUCCESS ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
539
		return LDAP_OTHER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
540
541
	}

542
543
	backsql_BindRowAsStrings( sth, &oc_row );
	rc = SQLFetch( sth );
544
545
546
547
548
549

	if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
		delete_proc_idx++;
		create_hint_idx++;
	}

550
	for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
551
552
553
554
555
556
557
558
559
		{
			struct {
				int idx;
				char *name;
			} required[] = {
				{ 0, "id" },
				{ 1, "name" },
				{ 2, "keytbl" },
				{ 3, "keycol" },
560
				{ -1, "expect_return" },
561
562
563
564
				{ -1, NULL },
			};
			int i;

565
566
			required[4].idx = delete_proc_idx + 1;

567
			for ( i = 0; required[ i ].name != NULL; i++ ) {
568
				if ( oc_row.value_len[ required[ i ].idx ] <= 0 ) {
569
570
571
					Debug( LDAP_DEBUG_ANY,
						"backsql_load_schema_map(): "
						"required column #%d \"%s\" is empty\n",
572
						required[ i ].idx, required[ i ].name );
573
574
575
576
577
					return LDAP_OTHER;
				}
			}
		}

578
579
580
581
582
583
584
585
586
		Debug(LDAP_DEBUG_TRACE,
		      "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",
		      oc_row.cols[0], oc_row.cols[1], oc_row.cols[2],
		      oc_row.cols[3],
		      oc_row.cols[4] ? oc_row.cols[4] : "",
		      (BACKSQL_CREATE_NEEDS_SELECT(bi) && oc_row.cols[5]) ? oc_row.cols[5] : "",
		      oc_row.cols[delete_proc_idx] ? oc_row.cols[delete_proc_idx] : "",
		      oc_row.cols[delete_proc_idx + 1],
		      ((oc_row.ncols > create_hint_idx) && oc_row.cols[create_hint_idx]) ? oc_row.cols[create_hint_idx] : "" );
587

Pierangelo Masarati's avatar
Pierangelo Masarati committed
588
589
		oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
				sizeof( backsql_oc_map_rec ) );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
590

591
		if ( BACKSQL_STR2ID( &oc_map->bom_id, oc_row.cols[ 0 ], 0 ) != 0 ) {
592
593
			Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
				"unable to parse id=\"%s\"\n", 
594
				oc_row.cols[ 0 ] );
Howard Chu's avatar
Howard Chu committed
595
			ch_free( oc_map );
596
597
			return LDAP_OTHER;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
598

599
600
		oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] );
		if ( oc_map->bom_oc == NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
601
			Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
602
				"objectClass \"%s\" is not defined in schema\n", 
603
				oc_row.cols[ 1 ] );
Howard Chu's avatar
Howard Chu committed
604
			ch_free( oc_map );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
605
606
607
			return LDAP_OTHER;	/* undefined objectClass ? */
		}
		
608
609
		ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl );
		ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
610
		oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] <= 0 ) ? NULL 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
611
			: ch_strdup( oc_row.cols[ 4 ] );
612

613
		if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
614
			oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] <= 0 ) 
615
616
				? NULL : ch_strdup( oc_row.cols[ 5 ] );
		}
617
618
619
		oc_map->bom_delete_proc = ( oc_row.value_len[ delete_proc_idx ] <= 0 ) ? NULL 
			: ch_strdup( oc_row.cols[ delete_proc_idx ] );
		if ( lutil_atoix( &oc_map->bom_expect_return, oc_row.cols[ delete_proc_idx + 1 ], 0 ) != 0 ) {
620
621
			Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
				"unable to parse expect_return=\"%s\" for objectClass \"%s\"\n", 
622
				oc_row.cols[ delete_proc_idx + 1 ], oc_row.cols[ 1 ] );
Howard Chu's avatar
Howard Chu committed
623
			ch_free( oc_map );
624
625
			return LDAP_OTHER;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
626

627
628
		if ( ( oc_row.ncols > create_hint_idx ) &&
				( oc_row.value_len[ create_hint_idx ] > 0 ) )
629
630
631
632
		{
			const char	*text;

			oc_map->bom_create_hint = NULL;
633
			rc = slap_str2ad( oc_row.cols[ create_hint_idx ],
634
635
636
637
638
639
					&oc_map->bom_create_hint, &text );
			if ( rc != SQL_SUCCESS ) {
				Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
						"error matching "
						"AttributeDescription %s "
						"in create_hint: %s (%d)\n",
640
						oc_row.cols[ create_hint_idx ],
641
642
643
						text, rc );
				backsql_PrintErrors( bi->sql_db_env, dbh,
						sth, rc );
Howard Chu's avatar
Howard Chu committed
644
				ch_free( oc_map );
645
646
647
648
				return LDAP_OTHER;
			}
		}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
649
650
651
652
653
		/*
		 * FIXME: first attempt to check for offending
		 * instructions in {create|delete}_proc
		 */

654
		oc_map->bom_attrs = NULL;
655
		if ( avl_insert( &bi->sql_oc_by_oc, oc_map, backsql_cmp_oc, avl_dup_error ) == -1 ) {
656
657
			Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
					"duplicate objectClass \"%s\" in objectClass map\n",
658
					oc_map->bom_oc->soc_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
659
			ch_free( oc_map );
660
661
			return LDAP_OTHER;
		}
662
		if ( avl_insert( &bi->sql_oc_by_id, oc_map, backsql_cmp_oc_id, avl_dup_error ) == -1 ) {
663
664
			Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
					"duplicate objectClass \"%s\" in objectClass by ID map\n",
665
					oc_map->bom_oc->soc_cname.bv_val );
666
667
668
			return LDAP_OTHER;
		}
		oc_id = oc_map->bom_id;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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
673
			oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val );
		if ( oc_map->bom_create_proc ) {
674
			Debug( LDAP_DEBUG_TRACE, "    create_proc=\"%s\"\n",
675
				oc_map->bom_create_proc );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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
682
		if ( oc_map->bom_create_hint ) {
			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 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
694

695
696
	backsql_FreeRow( &oc_row );
	SQLFreeStmt( sth, SQL_DROP );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
697

698
699
	/* prepare for attribute fetching */
	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
704
705
	if ( rc != SQL_SUCCESS ) {
		Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): "
			"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;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
709
	}
710

711
	rc = backsql_BindParamNumID( sth, 1, SQL_PARAM_INPUT, &oc_id );
712
713
	if ( rc != SQL_SUCCESS ) {
		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
718
719
		return LDAP_OTHER;
	}

720
	bas.bas_bi = bi;
721
722
723
724
725
	bas.bas_dbh = dbh;
	bas.bas_sth = sth;
	bas.bas_oc_id = &oc_id;
	bas.bas_rc = LDAP_SUCCESS;

726
	(void)avl_apply( bi->sql_oc_by_oc, backsql_oc_get_attr_mapping,
727
728
729
730
			&bas, BACKSQL_AVL_STOP, AVL_INORDER );

	SQLFreeStmt( sth, SQL_DROP );

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
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
738
backsql_oc_map_rec *
739
backsql_oc2oc( backsql_info *bi, ObjectClass *oc )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
740
741
742
{
	backsql_oc_map_rec	tmp, *res;

743
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
748

749
	tmp.bom_oc = oc;
750
	res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
751
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
752
753
	if ( res != NULL ) {
		Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
754
			"found name=\"%s\", id=%d\n", 
755
			BACKSQL_OC_NAME( res ), res->bom_id );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
756
757
	} else {
		Debug( LDAP_DEBUG_TRACE, "<==backsql_oc2oc(): "
758
			"not found\n" );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
759
	}
760
#endif /* BACKSQL_TRACE */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
761
762
763
764
765
 
	return res;
}

backsql_oc_map_rec *
766
backsql_name2oc( backsql_info *bi, struct berval *oc_name )
767
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
768
769
	backsql_oc_map_rec	tmp, *res;

770
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
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 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
775

776
777
	tmp.bom_oc = oc_bvfind( oc_name );
	if ( tmp.bom_oc == NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
778
779
780
		return NULL;
	}

781
	res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_oc, &tmp, backsql_cmp_oc );
782
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
783
784
	if ( res != NULL ) {
		Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
785
			"found name=\"%s\", id=%d\n", 
786
			BACKSQL_OC_NAME( res ), res->bom_id );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
787
788
	} else {
		Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
789
			"not found\n" );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
790
	}
791
#endif /* BACKSQL_TRACE */
792
 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
793
	return res;
794
795
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
796
backsql_oc_map_rec *
797
backsql_id2oc( backsql_info *bi, unsigned long id )
798
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
799
	backsql_oc_map_rec	tmp, *res;
800
 
801
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
802
	Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
803
		"searching for objectclass with id=%lu\n", id );
804
#endif /* BACKSQL_TRACE */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
805

806
	tmp.bom_id = id;
807
	res = (backsql_oc_map_rec *)avl_find( bi->sql_oc_by_id, &tmp,
808
			backsql_cmp_oc_id );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
809

810
#ifdef BACKSQL_TRACE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
811
	if ( res != NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
812
813
		Debug( LDAP_DEBUG_TRACE, "<==oc_with_id(): "
			"found name=\"%s\", id=%lu\n",
814
			BACKSQL_OC_NAME( res ), res->bom_id );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
815
	} else {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
816
		Debug( LDAP_DEBUG_TRACE, "<==oc_with_id(): "
817
			"id=%lu not found\n", res->bom_id );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
818
	}
819
#endif /* BACKSQL_TRACE */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
820
821
	
	return res;
822
823
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
824
backsql_at_map_rec *
Pierangelo Masarati's avatar
Pierangelo Masarati committed
825
backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad )