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>
20 #include "schema-map.h"
23 int backsql_dummy( void *, void * );
26 backsql_cmp_oc_name( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
28 return strcasecmp( m1->name, m2->name );
32 backsql_cmp_oc_id( backsql_oc_map_rec *m1, backsql_oc_map_rec *m2 )
34 if ( m1->id < m2->id ) {
37 if ( m1->id > m2->id ) {
45 backsql_at_map_rec *m1,
46 backsql_at_map_rec *m2 )
48 return strcasecmp( m1->name, m2->name );
52 backsql_make_attr_query(
53 backsql_oc_map_rec *oc_map,
54 backsql_at_map_rec *at_map )
56 struct berval tmps = { 0, NULL };
59 backsql_strcat( &tmps, &tmpslen, "SELECT ", at_map->sel_expr,
60 " AS ", at_map->name, " FROM ", at_map->from_tbls,
61 " WHERE ", oc_map->keytbl,".", oc_map->keycol,
63 if ( at_map->join_where != NULL ) {
64 backsql_strcat( &tmps, &tmpslen, " AND ",
65 at_map->join_where, NULL );
68 at_map->query = tmps.bv_val;
74 backsql_add_sysmaps( backsql_oc_map_rec *oc_map )
76 backsql_at_map_rec *at_map;
81 snprintf( s, sizeof( s ), "%ld", oc_map->id );
83 at_map = (backsql_at_map_rec *)ch_calloc(1,
84 sizeof( backsql_at_map_rec ) );
85 at_map->name = ch_strdup( "objectClass" );
86 at_map->sel_expr = ch_strdup( "ldap_entry_objclasses.oc_name" );
87 at_map->from_tbls = ch_strdup( "ldap_entry_objclasses,ldap_entries" );
88 len = strlen( at_map->from_tbls );
89 backsql_merge_from_clause( &at_map->from_tbls, &len, oc_map->keytbl );
93 backsql_strcat( &bv, &len,
94 "ldap_entries.id=ldap_entry_objclasses.entry_id "
95 "and ldap_entries.keyval=",
96 oc_map->keytbl, ".", oc_map->keycol,
97 " and ldap_entries.oc_map_id=", s, NULL );
98 at_map->join_where = bv.bv_val;
99 at_map->add_proc = NULL;
100 at_map->delete_proc = NULL;
101 at_map->param_order = 0;
102 at_map->expect_return = 0;
103 backsql_make_attr_query( oc_map, at_map );
104 avl_insert( &oc_map->attrs, at_map,
105 (AVL_CMP)backsql_cmp_attr, backsql_dummy );
107 at_map = (backsql_at_map_rec *)ch_calloc( 1,
108 sizeof( backsql_at_map_rec ) );
109 at_map->name = ch_strdup( "ref" );
110 at_map->sel_expr = ch_strdup( "ldap_referrals.url" );
111 at_map->from_tbls = ch_strdup( "ldap_referrals,ldap_entries" );
112 len = strlen( at_map->from_tbls );
113 backsql_merge_from_clause( &at_map->from_tbls, &len,oc_map->keytbl );
115 /* FIXME: no free? */
116 at_map->join_where = NULL;
121 backsql_strcat( &bv, &len,
122 "ldap_entries.id=ldap_referrals.entry_id "
123 "and ldap_entries.keyval=",
124 oc_map->keytbl, ".", oc_map->keycol,
125 " and ldap_entries.oc_map_id=", s, NULL );
126 at_map->join_where = bv.bv_val;
127 at_map->add_proc = NULL;
128 at_map->delete_proc = NULL;
129 at_map->param_order = 0;
130 at_map->expect_return = 0;
131 backsql_make_attr_query( oc_map, at_map );
132 avl_insert( &oc_map->attrs, at_map,
133 (AVL_CMP)backsql_cmp_attr, backsql_dummy );
139 backsql_load_schema_map( backsql_info *si, SQLHDBC dbh )
141 SQLHSTMT oc_sth, at_sth;
143 BACKSQL_ROW_NTS oc_row, at_row;
145 backsql_oc_map_rec *oc_map;
146 backsql_at_map_rec *at_map;
150 Debug( LDAP_DEBUG_TRACE, "==>load_schema_map()\n", 0, 0, 0 );
153 * TimesTen : See if the ldap_entries.dn_ru field exists in the schema
155 if ( si->has_ldapinfo_dn_ru == -1 ) {
156 rc = backsql_Prepare( dbh, &oc_sth,
157 backsql_check_dn_ru_query, 0 );
158 if ( rc == SQL_SUCCESS ) {
159 si->has_ldapinfo_dn_ru = 1; /* Yes, the field exists */
160 Debug( LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists "
161 "in the schema\n", 0, 0, 0 );
163 si->has_ldapinfo_dn_ru = 0; /* No such field exists */
166 SQLFreeStmt( oc_sth, SQL_DROP );
170 rc = backsql_Prepare( dbh, &oc_sth, si->oc_query, 0 );
171 if ( rc != SQL_SUCCESS ) {
172 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
173 "error preparing oc_query: '%s'\n",
174 si->oc_query, 0, 0 );
175 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
178 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n",
179 si->at_query, 0, 0 );
181 rc = backsql_Prepare( dbh, &at_sth, si->at_query, 0 );
182 if ( rc != SQL_SUCCESS ) {
183 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
184 "error preparing at_query: '%s'\n",
185 si->at_query, 0, 0 );
186 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
190 rc = backsql_BindParamID( at_sth, 1, &oc_id );
191 if ( rc != SQL_SUCCESS ) {
192 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
193 "error binding param for at_query: \n", 0, 0, 0 );
194 backsql_PrintErrors( si->db_env, dbh, at_sth, rc );
198 rc = SQLExecute( oc_sth );
199 if ( rc != SQL_SUCCESS ) {
200 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
201 "error executing oc_query: \n", 0, 0, 0 );
202 backsql_PrintErrors( si->db_env, dbh, oc_sth, rc );
206 backsql_BindRowAsStrings( oc_sth, &oc_row );
207 rc = SQLFetch( oc_sth );
208 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( oc_sth ) ) {
209 oc_map = (backsql_oc_map_rec *)ch_calloc( 1,
210 sizeof( backsql_oc_map_rec ) );
211 oc_map->id = atoi( oc_row.cols[ 0 ] );
212 oc_map->name = ch_strdup( oc_row.cols[ 1 ] );
213 oc_map->keytbl = ch_strdup( oc_row.cols[ 2 ] );
214 oc_map->keycol = ch_strdup( oc_row.cols[ 3 ] );
215 oc_map->create_proc = ( oc_row.is_null[ 4 ] < 0 ) ? NULL
216 : ch_strdup( oc_row.cols[ 4 ] );
217 oc_map->delete_proc = ( oc_row.is_null[ 5 ] < 0 ) ? NULL
218 : ch_strdup( oc_row.cols[ 5 ] );
219 oc_map->expect_return = atoi( oc_row.cols[ 6 ] );
222 * FIXME: first attempt to check for offending
223 * instructions in {create|delete}_proc
226 oc_map->attrs = NULL;
227 avl_insert( &si->oc_by_name, oc_map,
228 (AVL_CMP)backsql_cmp_oc_name, backsql_dummy );
229 avl_insert( &si->oc_by_id, oc_map,
230 (AVL_CMP)backsql_cmp_oc_id, backsql_dummy );
232 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
233 "objectClass '%s': keytbl='%s' keycol='%s'\n",
234 oc_map->name, oc_map->keytbl, oc_map->keycol );
235 if ( oc_map->delete_proc ) {
236 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
237 oc_map->delete_proc, 0, 0 );
239 if ( oc_map->create_proc ) {
240 Debug( LDAP_DEBUG_TRACE, "create_proc='%s'\n",
241 oc_map->create_proc, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "expect_return=%d; attributes:\n",
244 oc_map->expect_return, 0, 0 );
246 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
247 "autoadding 'objectClass' and 'ref' mappings\n",
249 backsql_add_sysmaps( oc_map );
250 rc = SQLExecute( at_sth );
251 if ( rc != SQL_SUCCESS ) {
252 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
253 "error executing at_query: \n", 0, 0, 0 );
254 backsql_PrintErrors( SQL_NULL_HENV, dbh, at_sth, rc );
258 backsql_BindRowAsStrings( at_sth, &at_row );
259 rc = SQLFetch( at_sth );
260 for ( ; BACKSQL_SUCCESS(rc); rc = SQLFetch( at_sth ) ) {
261 Debug( LDAP_DEBUG_TRACE, "********'%s'\n",
262 at_row.cols[ 0 ], 0, 0 );
263 Debug( LDAP_DEBUG_TRACE,
264 "name='%s',sel_expr='%s' from='%s'",
265 at_row.cols[ 0 ], at_row.cols[ 1 ],
267 Debug( LDAP_DEBUG_TRACE,
268 "join_where='%s',add_proc='%s'",
269 at_row.cols[ 3 ], at_row.cols[ 4 ], 0 );
270 Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n",
271 at_row.cols[ 5 ], 0, 0 );
273 Debug( LDAP_DEBUG_TRACE, "sel_expr_u='%s'\n",
274 at_row.cols[ 8 ], 0, 0 );
275 at_map = (backsql_at_map_rec *)ch_calloc( 1,
276 sizeof( backsql_at_map_rec ) );
277 at_map->name = ch_strdup( at_row.cols[ 0 ] );
278 at_map->sel_expr = ch_strdup( at_row.cols[ 1 ] );
279 at_map->sel_expr_u = ( at_row.is_null[ 8 ] < 0 ) ? NULL
280 : ch_strdup( at_row.cols[ 8 ] );
283 backsql_merge_from_clause( &tmps, &tmpslen,
285 at_map->from_tbls = tmps;
286 at_map->join_where = ( at_row.is_null[ 3 ] < 0 ) ? NULL
287 : ch_strdup( at_row.cols[ 3 ] );
288 at_map->add_proc = ( at_row.is_null[ 4 ] < 0 ) ? NULL
289 : ch_strdup( at_row.cols[4] );
290 at_map->delete_proc = ( at_row.is_null[ 5 ] < 0 ) ? NULL
291 : ch_strdup( at_row.cols[ 5 ] );
292 at_map->param_order = atoi( at_row.cols[ 6 ] );
293 at_map->expect_return = atoi( at_row.cols[ 7 ] );
294 backsql_make_attr_query( oc_map, at_map );
295 Debug( LDAP_DEBUG_TRACE, "load_schema_map(): "
296 "preconstructed query '%s'\n",
297 at_map->query, 0, 0 );
298 avl_insert( &oc_map->attrs, at_map,
299 (AVL_CMP)backsql_cmp_attr,
302 backsql_FreeRow( &at_row );
303 SQLFreeStmt( at_sth, SQL_CLOSE );
305 backsql_FreeRow( &oc_row );
306 SQLFreeStmt( at_sth, SQL_DROP );
307 SQLFreeStmt( oc_sth, SQL_DROP );
308 si->schema_loaded = 1;
309 Debug( LDAP_DEBUG_TRACE, "<==load_schema_map()\n", 0, 0, 0 );
314 backsql_oc_with_name( backsql_info *si, char *objclass )
316 backsql_oc_map_rec tmp, *res;
319 Debug( LDAP_DEBUG_TRACE, "==>oc_with_name(): "
320 "searching for objectclass with name='%s'\n",
325 res = (backsql_oc_map_rec *)avl_find( si->oc_by_name, &tmp,
326 (AVL_CMP)backsql_cmp_oc_name );
329 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
330 "found name='%s', id=%d\n", res->name, res->id, 0 );
332 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
333 "not found\n", 0, 0, 0 );
341 backsql_oc_with_id( backsql_info *si, unsigned long id )
343 backsql_oc_map_rec tmp, *res;
346 Debug( LDAP_DEBUG_TRACE, "==>oc_with_id(): "
347 "searching for objectclass with id='%d'\n", id, 0, 0 );
351 res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp,
352 (AVL_CMP)backsql_cmp_oc_id );
356 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
357 "found name='%s', id=%d\n", res->name, res->id, 0 );
359 Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): "
360 "not found\n", 0, 0, 0 );
368 backsql_at_with_name( backsql_oc_map_rec* objclass, char *attr )
370 backsql_at_map_rec tmp, *res;
373 Debug( LDAP_DEBUG_TRACE, "==>at_with_name(): "
374 "searching for attribute '%s' for objectclass '%s'\n",
375 attr, objclass->name, 0 );
378 res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp,
379 (AVL_CMP)backsql_cmp_attr );
383 Debug( LDAP_DEBUG_TRACE, "<==at_with_name(): "
384 "found name='%s', sel_expr='%s'\n",
385 res->name, res->sel_expr, 0 );
387 Debug( LDAP_DEBUG_TRACE, "<==at_with_name(): "
388 "not found\n", 0, 0, 0 );
396 backsql_free_attr( backsql_at_map_rec *at )
398 Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n", at->name, 0, 0 );
400 ch_free( at->sel_expr );
401 if ( at->from_tbls != NULL ) {
402 ch_free( at->from_tbls );
404 if ( at->join_where != NULL ) {
405 ch_free( at->join_where );
407 if ( at->add_proc != NULL ) {
408 ch_free( at->add_proc );
410 if ( at->delete_proc != NULL ) {
411 ch_free( at->delete_proc );
414 ch_free( at->query );
419 if ( at->sel_expr_u ) {
420 ch_free( at->sel_expr_u );
423 Debug( LDAP_DEBUG_TRACE, "<==free_attr()\n", 0, 0, 0 );
429 backsql_free_oc( backsql_oc_map_rec *oc )
431 Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n", oc->name, 0, 0 );
432 avl_free( oc->attrs, (AVL_FREE)backsql_free_attr );
434 ch_free( oc->keytbl );
435 ch_free( oc->keycol );
436 if ( oc->create_proc != NULL ) {
437 ch_free( oc->create_proc );
439 if ( oc->delete_proc != NULL ) {
440 ch_free( oc->delete_proc );
444 Debug( LDAP_DEBUG_TRACE, "<==free_oc()\n", 0, 0, 0 );
449 backsql_destroy_schema_map( backsql_info *si )
451 Debug( LDAP_DEBUG_TRACE, "==>destroy_schema_map()\n", 0, 0, 0 );
452 avl_free( si->oc_by_id, (AVL_FREE)backsql_free_oc );
453 avl_free( si->oc_by_name, (AVL_FREE)backsql_dummy );
454 Debug( LDAP_DEBUG_TRACE, "<==destroy_schema_map()\n", 0, 0, 0 );
458 #endif /* SLAPD_SQL */