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>
16 #include "ac/string.h"
18 #include "ac/stdarg.h"
23 #include "schema-map.h"
26 #define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
27 #define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
29 #define BACKSQL_STR_GROW 256
31 char backsql_def_oc_query[] =
32 "SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
33 "FROM ldap_oc_mappings";
34 char backsql_def_needs_select_oc_query[] =
35 "SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
36 "expect_return FROM ldap_oc_mappings";
37 char backsql_def_at_query[] =
38 "SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
39 "param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
41 char backsql_def_delentry_query[] = "DELETE FROM ldap_entries WHERE id=?";
42 char backsql_def_insentry_query[] =
43 "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
45 char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
46 char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
47 char backsql_id_query[] = "SELECT id,keyval,oc_map_id FROM ldap_entries WHERE ";
48 /* better ?||? or cast(?||? as varchar) */
49 char backsql_def_concat_func[] = "CONCAT(?,?)";
52 char backsql_check_dn_ru_query[] = "SELECT dn_ru from ldap_entries";
55 backsql_strcat( struct berval *dest, ber_len_t *buflen, ... )
58 ber_len_t cdlen, cslen, grow;
62 assert( dest->bv_val == NULL
63 || dest->bv_len == strlen( dest->bv_val ) );
66 Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n" );
67 #endif /* BACKSQL_TRACE */
69 va_start( strs, buflen );
70 if ( dest->bv_val == NULL || *buflen == 0 ) {
71 dest->bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
74 *buflen = BACKSQL_STR_GROW;
77 while ( ( cstr = va_arg( strs, char * ) ) != NULL ) {
78 cslen = strlen( cstr );
79 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
80 if ( *buflen - cdlen <= cslen ) {
84 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
85 "buflen=%d, cdlen=%d, cslen=%d "
86 "-- reallocating dest\n",
87 *buflen, cdlen + 1, cslen );
88 #endif /* BACKSQL_TRACE */
90 tmp_dest = (char *)ch_realloc( dest->bv_val,
91 ( *buflen ) + grow * sizeof( char ) );
92 if ( tmp_dest == NULL ) {
93 Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
94 "could not reallocate string buffer.\n",
98 dest->bv_val = tmp_dest;
102 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
103 "new buflen=%d, dest=%p\n", *buflen, dest, 0 );
104 #endif /* BACKSQL_TRACE */
106 AC_MEMCPY( dest->bv_val + cdlen, cstr, cslen + 1 );
112 Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest='%s')\n",
114 #endif /* BACKSQL_TRACE */
116 dest->bv_len = cdlen;
122 backsql_strfcat( struct berval *dest, ber_len_t *buflen, const char *fmt, ... )
130 assert( *buflen == 0 || *buflen > dest->bv_len );
131 assert( dest->bv_val == NULL
132 || dest->bv_len == strlen( dest->bv_val ) );
135 Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n" );
136 #endif /* BACKSQL_TRACE */
138 va_start( strs, fmt );
139 if ( dest->bv_val == NULL || *buflen == 0 ) {
140 dest->bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
143 *buflen = BACKSQL_STR_GROW;
146 cdlen = dest->bv_len;
147 for ( ; fmt[0]; fmt++ ) {
148 ber_len_t cslen, grow;
149 char *cstr, cc[ 2 ] = { '\0', '\0' };
152 switch ( fmt[ 0 ] ) {
156 cbv = va_arg( strs, struct berval * );
161 /* length + string */
163 cslen = va_arg( strs, ber_len_t );
164 cstr = va_arg( strs, char * );
169 cstr = va_arg( strs, char * );
170 cslen = strlen( cstr );
176 * `char' is promoted to `int' when passed through `...'
178 cc[0] = va_arg( strs, int );
187 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
188 if ( *buflen - cdlen <= cslen ) {
192 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
193 "buflen=%d, cdlen=%d, cslen=%d "
194 "-- reallocating dest\n",
195 *buflen, cdlen + 1, cslen );
196 #endif /* BACKSQL_TRACE */
198 tmp_dest = (char *)ch_realloc( dest->bv_val,
199 ( *buflen ) + grow * sizeof( char ) );
200 if ( tmp_dest == NULL ) {
201 Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
202 "could not reallocate string buffer.\n",
206 dest->bv_val = tmp_dest;
207 *buflen += grow * sizeof( char );
210 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
211 "new buflen=%d, dest=%p\n", *buflen, dest, 0 );
212 #endif /* BACKSQL_TRACE */
217 AC_MEMCPY( dest->bv_val + cdlen, cstr, cslen + 1 );
224 Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest='%s')\n",
226 #endif /* BACKSQL_TRACE */
228 dest->bv_len = cdlen;
234 backsql_entry_addattr(
236 struct berval *at_name,
237 struct berval *at_val )
239 AttributeDescription *ad;
244 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
245 "at_name='%s', at_val='%s'\n",
246 at_name->bv_val, at_val->bv_val, 0 );
247 #endif /* BACKSQL_TRACE */
250 rc = slap_bv2ad( at_name, &ad, &text );
251 if ( rc != LDAP_SUCCESS ) {
252 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
253 "failed to find AttributeDescription for '%s'\n",
254 at_name->bv_val, 0, 0 );
258 rc = attr_mergeit_one( e, ad, at_val );
261 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
262 "failed to merge value '%s' for attribute '%s'\n",
263 at_val->bv_val, at_name->bv_val, 0 );
268 Debug( LDAP_DEBUG_TRACE, "<==backsql_query_addattr()\n", 0, 0, 0 );
269 #endif /* BACKSQL_TRACE */
275 backsql_get_table_spec( char **p )
278 struct berval res = BER_BVNULL;
279 ber_len_t res_len = 0;
285 while ( **p && **p != ',' ) {
293 #define BACKSQL_NEXT_WORD { \
294 while ( *s && isspace( (unsigned char)*s ) ) s++; \
295 if ( !*s ) return res.bv_val; \
297 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
298 if ( *q ) *q++='\0'; \
303 backsql_strcat( &res, &res_len, s, NULL );
307 if ( !strcasecmp( s, "as" ) ) {
313 backsql_strcat( &res, &res_len, " AS ", s, NULL );
314 /* oracle doesn't understand AS :( */
318 backsql_strfcat( &res, &res_len, "cs", ' ', s );
324 backsql_merge_from_clause(
325 struct berval *dest_from,
327 struct berval *src_from )
329 char *s, *p, *srcc, *pos, e;
330 struct berval res = { 0 , NULL };
333 Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
334 "dest_from='%s',src_from='%s'\n",
335 dest_from ? dest_from->bv_val : "<NULL>", src_from, 0 );
336 #endif /* BACKSQL_TRACE */
338 srcc = ch_strdup( src_from->bv_val );
341 if ( dest_from != NULL ) {
346 s = backsql_get_table_spec( &p );
349 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
350 "p='%s' s='%s'\n", p, s, 0 );
351 #endif /* BACKSQL_TRACE */
353 if ( res.bv_val == NULL ) {
354 backsql_strcat( &res, dest_len, s, NULL );
357 pos = strstr( res.bv_val, s );
359 backsql_strfcat( &res, dest_len, "cs", ',', s );
360 } else if ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) {
361 backsql_strfcat( &res, dest_len, "cs", ',', s );
371 Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
372 #endif /* BACKSQL_TRACE */
381 * splits a pattern in components separated by '?'
382 * (double ?? are turned into single ? and left in the string)
383 * expected contains the number of expected occurrences of '?'
384 * (a negative value means parse as many as possible)
388 backsql_split_pattern(
389 const char *_pattern,
390 BerVarray *split_pattern,
393 char *pattern, *start, *end;
397 #define SPLIT_CHAR '?'
400 assert( split_pattern );
402 pattern = ch_strdup( _pattern );
405 end = strchr( start, SPLIT_CHAR );
406 for ( ; start; expected-- ) {
407 char *real_end = end;
410 if ( real_end == NULL ) {
411 real_end = start + strlen( start );
413 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
415 AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
416 end = strchr( real_end + 1, SPLIT_CHAR );
420 real_len = real_end - start;
421 if ( real_len == 0 ) {
422 ber_str2bv( "", 0, 1, &bv );
424 ber_str2bv( start, real_len, 1, &bv );
427 ber_bvarray_add( split_pattern, &bv );
429 if ( expected == 0 ) {
439 end = strchr( start, SPLIT_CHAR );
451 backsql_prepare_pattern(
452 BerVarray split_pattern,
462 for ( i = 0; values[i].bv_val; i++ ) {
463 if ( split_pattern[i].bv_val == NULL ) {
466 backsql_strfcat( res, &len, "b", &split_pattern[ i ] );
467 backsql_strfcat( res, &len, "b", &values[ i ] );
470 if ( split_pattern[ i ].bv_val == NULL ) {
474 backsql_strfcat( res, &len, "b", &split_pattern[ i ] );
479 #endif /* SLAPD_SQL */