2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
29 #include "ac/stdarg.h"
34 #include "schema-map.h"
37 #define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
38 #define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
40 #define BACKSQL_STR_GROW 256
42 char backsql_def_oc_query[] =
43 "SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
44 "FROM ldap_oc_mappings";
45 char backsql_def_needs_select_oc_query[] =
46 "SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
47 "expect_return FROM ldap_oc_mappings";
48 char backsql_def_at_query[] =
49 "SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
50 "param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
52 char backsql_def_delentry_query[] = "DELETE FROM ldap_entries WHERE id=?";
53 char backsql_def_insentry_query[] =
54 "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
56 char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
57 char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
58 char backsql_id_query[] = "SELECT id,keyval,oc_map_id FROM ldap_entries WHERE ";
59 /* better ?||? or cast(?||? as varchar) */
60 char backsql_def_concat_func[] = "CONCAT(?,?)";
63 char backsql_check_dn_ru_query[] = "SELECT dn_ru from ldap_entries";
66 backsql_strcat( struct berbuf *dest, ... )
69 ber_len_t cdlen, cslen, grow;
73 assert( dest->bb_val.bv_val == NULL
74 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
77 Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n" );
78 #endif /* BACKSQL_TRACE */
80 va_start( strs, dest );
81 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
82 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
84 dest->bb_val.bv_len = 0;
85 dest->bb_len = BACKSQL_STR_GROW;
87 cdlen = dest->bb_val.bv_len;
88 while ( ( cstr = va_arg( strs, char * ) ) != NULL ) {
89 cslen = strlen( cstr );
90 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
91 if ( dest->bb_len - cdlen <= cslen ) {
95 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
96 "buflen=%d, cdlen=%d, cslen=%d "
97 "-- reallocating dest\n",
98 dest->bb_len, cdlen + 1, cslen );
99 #endif /* BACKSQL_TRACE */
101 tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
102 ( dest->bb_len ) + grow * sizeof( char ) );
103 if ( tmp_dest == NULL ) {
104 Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
105 "could not reallocate string buffer.\n",
109 dest->bb_val.bv_val = tmp_dest;
110 dest->bb_len += grow;
113 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
114 "new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
115 #endif /* BACKSQL_TRACE */
117 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
123 Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest='%s')\n",
125 #endif /* BACKSQL_TRACE */
127 dest->bb_val.bv_len = cdlen;
133 backsql_strfcat( struct berbuf *dest, const char *fmt, ... )
140 assert( dest->bb_len == 0 || dest->bb_len > dest->bb_val.bv_len );
141 assert( dest->bb_val.bv_val == NULL
142 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
145 Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n" );
146 #endif /* BACKSQL_TRACE */
148 va_start( strs, fmt );
149 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
150 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
152 dest->bb_val.bv_len = 0;
153 dest->bb_len = BACKSQL_STR_GROW;
156 cdlen = dest->bb_val.bv_len;
157 for ( ; fmt[0]; fmt++ ) {
158 ber_len_t cslen, grow;
159 char *cstr, cc[ 2 ] = { '\0', '\0' };
162 switch ( fmt[ 0 ] ) {
166 cbv = va_arg( strs, struct berval * );
171 /* length + string */
173 cslen = va_arg( strs, ber_len_t );
174 cstr = va_arg( strs, char * );
179 cstr = va_arg( strs, char * );
180 cslen = strlen( cstr );
186 * `char' is promoted to `int' when passed through `...'
188 cc[0] = va_arg( strs, int );
197 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
198 if ( dest->bb_len - cdlen <= cslen ) {
202 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
203 "buflen=%d, cdlen=%d, cslen=%d "
204 "-- reallocating dest\n",
205 dest->bb_len, cdlen + 1, cslen );
206 #endif /* BACKSQL_TRACE */
208 tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
209 ( dest->bb_len ) + grow * sizeof( char ) );
210 if ( tmp_dest == NULL ) {
211 Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
212 "could not reallocate string buffer.\n",
216 dest->bb_val.bv_val = tmp_dest;
217 dest->bb_len += grow * sizeof( char );
220 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
221 "new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
222 #endif /* BACKSQL_TRACE */
227 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
234 Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest='%s')\n",
236 #endif /* BACKSQL_TRACE */
238 dest->bb_val.bv_len = cdlen;
244 backsql_entry_addattr(
246 struct berval *at_name,
247 struct berval *at_val,
250 AttributeDescription *ad;
255 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
256 "at_name='%s', at_val='%s'\n",
257 at_name->bv_val, at_val->bv_val, 0 );
258 #endif /* BACKSQL_TRACE */
261 rc = slap_bv2ad( at_name, &ad, &text );
262 if ( rc != LDAP_SUCCESS ) {
263 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
264 "failed to find AttributeDescription for '%s'\n",
265 at_name->bv_val, 0, 0 );
269 rc = attr_merge_normalize_one( e, ad, at_val, memctx );
272 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
273 "failed to merge value '%s' for attribute '%s'\n",
274 at_val->bv_val, at_name->bv_val, 0 );
279 Debug( LDAP_DEBUG_TRACE, "<==backsql_query_addattr()\n", 0, 0, 0 );
280 #endif /* BACKSQL_TRACE */
286 backsql_get_table_spec( char **p )
289 struct berbuf res = BB_NULL;
295 while ( **p && **p != ',' ) {
303 #define BACKSQL_NEXT_WORD { \
304 while ( *s && isspace( (unsigned char)*s ) ) s++; \
305 if ( !*s ) return res.bb_val.bv_val; \
307 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
308 if ( *q ) *q++='\0'; \
313 backsql_strcat( &res, s, NULL );
317 if ( !strcasecmp( s, "as" ) ) {
323 backsql_strcat( &res, " AS ", s, NULL );
324 /* oracle doesn't understand AS :( and other RDBMSes don't need it */
328 backsql_strfcat( &res, "cs", ' ', s );
330 return res.bb_val.bv_val;
334 backsql_merge_from_clause(
335 struct berbuf *dest_from,
336 struct berval *src_from )
338 char *s, *p, *srcc, *pos, e;
339 struct berbuf res = BB_NULL;
342 Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
343 "dest_from='%s',src_from='%s'\n",
344 dest_from ? dest_from->bb_val.bv_val : "<NULL>", src_from, 0 );
345 #endif /* BACKSQL_TRACE */
347 srcc = ch_strdup( src_from->bv_val );
350 if ( dest_from != NULL ) {
355 s = backsql_get_table_spec( &p );
358 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
359 "p='%s' s='%s'\n", p, s, 0 );
360 #endif /* BACKSQL_TRACE */
362 if ( res.bb_val.bv_val == NULL ) {
363 backsql_strcat( &res, s, NULL );
366 pos = strstr( res.bb_val.bv_val, s );
367 if ( pos == NULL || ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) ) {
368 backsql_strfcat( &res, "cs", ',', s );
378 Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
379 #endif /* BACKSQL_TRACE */
388 * splits a pattern in components separated by '?'
389 * (double ?? are turned into single ? and left in the string)
390 * expected contains the number of expected occurrences of '?'
391 * (a negative value means parse as many as possible)
395 backsql_split_pattern(
396 const char *_pattern,
397 BerVarray *split_pattern,
400 char *pattern, *start, *end;
404 #define SPLIT_CHAR '?'
407 assert( split_pattern );
409 pattern = ch_strdup( _pattern );
412 end = strchr( start, SPLIT_CHAR );
413 for ( ; start; expected-- ) {
414 char *real_end = end;
417 if ( real_end == NULL ) {
418 real_end = start + strlen( start );
420 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
422 AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
423 end = strchr( real_end + 1, SPLIT_CHAR );
427 real_len = real_end - start;
428 if ( real_len == 0 ) {
429 ber_str2bv( "", 0, 1, &bv );
431 ber_str2bv( start, real_len, 1, &bv );
434 ber_bvarray_add( split_pattern, &bv );
436 if ( expected == 0 ) {
446 end = strchr( start, SPLIT_CHAR );
458 backsql_prepare_pattern(
459 BerVarray split_pattern,
464 struct berbuf bb = BB_NULL;
468 for ( i = 0; values[i].bv_val; i++ ) {
469 if ( split_pattern[i].bv_val == NULL ) {
470 ch_free( bb.bb_val.bv_val );
473 backsql_strfcat( &bb, "b", &split_pattern[ i ] );
474 backsql_strfcat( &bb, "b", &values[ i ] );
477 if ( split_pattern[ i ].bv_val == NULL ) {
478 ch_free( bb.bb_val.bv_val );
482 backsql_strfcat( &bb, "b", &split_pattern[ i ] );
489 #endif /* SLAPD_SQL */