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"
32 #include "proto-sql.h"
34 #define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
35 #define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
37 #define BACKSQL_STR_GROW 256
39 char backsql_def_oc_query[] =
40 "SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
41 "FROM ldap_oc_mappings";
42 char backsql_def_needs_select_oc_query[] =
43 "SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
44 "expect_return FROM ldap_oc_mappings";
45 char backsql_def_at_query[] =
46 "SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
47 "param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
49 char backsql_def_delentry_query[] = "DELETE FROM ldap_entries WHERE id=?";
50 char backsql_def_insentry_query[] =
51 "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
53 char backsql_def_delobjclasses_query[] = "DELETE FROM ldap_entry_objclasses "
55 char backsql_def_delreferrals_query[] = "DELETE FROM ldap_referrals "
57 char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
58 char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
59 char backsql_id_query[] = "SELECT id,keyval,oc_map_id,dn FROM ldap_entries WHERE ";
60 /* better ?||? or cast(?||? as varchar) */
61 char backsql_def_concat_func[] = "CONCAT(?,?)";
64 char backsql_check_dn_ru_query[] = "SELECT dn_ru FROM ldap_entries";
67 backsql_strcat( struct berbuf *dest, ... )
70 ber_len_t cdlen, cslen, grow;
74 assert( dest->bb_val.bv_val == NULL
75 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
78 Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n", 0, 0, 0 );
79 #endif /* BACKSQL_TRACE */
81 va_start( strs, dest );
82 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
83 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
85 dest->bb_val.bv_len = 0;
86 dest->bb_len = BACKSQL_STR_GROW;
88 cdlen = dest->bb_val.bv_len;
89 while ( ( cstr = va_arg( strs, char * ) ) != NULL ) {
90 cslen = strlen( cstr );
91 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
92 if ( dest->bb_len - cdlen <= cslen ) {
96 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
97 "buflen=%d, cdlen=%d, cslen=%d "
98 "-- reallocating dest\n",
99 dest->bb_len, cdlen + 1, cslen );
100 #endif /* BACKSQL_TRACE */
102 tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
103 ( dest->bb_len ) + grow * sizeof( char ) );
104 if ( tmp_dest == NULL ) {
105 Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
106 "could not reallocate string buffer.\n",
110 dest->bb_val.bv_val = tmp_dest;
111 dest->bb_len += grow;
114 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
115 "new buflen=%d, dest=%p\n",
116 dest->bb_len, dest, 0 );
117 #endif /* BACKSQL_TRACE */
119 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
125 Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest=\"%s\")\n",
126 dest->bb_val.bv_val, 0, 0 );
127 #endif /* BACKSQL_TRACE */
129 dest->bb_val.bv_len = cdlen;
135 backsql_strfcat( struct berbuf *dest, const char *fmt, ... )
142 assert( dest->bb_len == 0 || dest->bb_len > dest->bb_val.bv_len );
143 assert( dest->bb_val.bv_val == NULL
144 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
147 Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n", 0, 0, 0 );
148 #endif /* BACKSQL_TRACE */
150 va_start( strs, fmt );
151 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
152 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW,
154 dest->bb_val.bv_len = 0;
155 dest->bb_len = BACKSQL_STR_GROW;
158 cdlen = dest->bb_val.bv_len;
159 for ( ; fmt[0]; fmt++ ) {
160 ber_len_t cslen, grow;
161 char *cstr, cc[ 2 ] = { '\0', '\0' };
164 switch ( fmt[ 0 ] ) {
168 cbv = va_arg( strs, struct berval * );
173 /* length + string */
175 cslen = va_arg( strs, ber_len_t );
176 cstr = va_arg( strs, char * );
181 cstr = va_arg( strs, char * );
182 cslen = strlen( cstr );
188 * `char' is promoted to `int' when passed through `...'
190 cc[0] = va_arg( strs, int );
199 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
200 if ( dest->bb_len - cdlen <= cslen ) {
204 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
205 "buflen=%d, cdlen=%d, cslen=%d "
206 "-- reallocating dest\n",
207 dest->bb_len, cdlen + 1, cslen );
208 #endif /* BACKSQL_TRACE */
210 tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
211 ( dest->bb_len ) + grow * sizeof( char ) );
212 if ( tmp_dest == NULL ) {
213 Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
214 "could not reallocate string buffer.\n",
218 dest->bb_val.bv_val = tmp_dest;
219 dest->bb_len += grow * sizeof( char );
222 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
223 "new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
224 #endif /* BACKSQL_TRACE */
229 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
236 Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest=\"%s\")\n",
237 dest->bb_val.bv_val, 0, 0 );
238 #endif /* BACKSQL_TRACE */
240 dest->bb_val.bv_len = cdlen;
246 backsql_entry_addattr(
248 struct berval *at_name,
249 struct berval *at_val,
252 AttributeDescription *ad;
257 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
258 "at_name=\"%s\", at_val=\"%s\"\n",
259 at_name->bv_val, at_val->bv_val, 0 );
260 #endif /* BACKSQL_TRACE */
263 rc = slap_bv2ad( at_name, &ad, &text );
264 if ( rc != LDAP_SUCCESS ) {
265 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
266 "failed to find AttributeDescription for \"%s\"\n",
267 at_name->bv_val, 0, 0 );
271 rc = attr_merge_normalize_one( e, ad, at_val, memctx );
274 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
275 "failed to merge value \"%s\" for attribute \"%s\"\n",
276 at_val->bv_val, at_name->bv_val, 0 );
281 Debug( LDAP_DEBUG_TRACE, "<==backsql_query_addattr()\n", 0, 0, 0 );
282 #endif /* BACKSQL_TRACE */
288 backsql_get_table_spec( char **p )
291 struct berbuf res = BB_NULL;
297 while ( **p && **p != ',' ) {
305 #define BACKSQL_NEXT_WORD { \
306 while ( *s && isspace( (unsigned char)*s ) ) s++; \
307 if ( !*s ) return res.bb_val.bv_val; \
309 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
310 if ( *q ) *q++='\0'; \
315 backsql_strcat( &res, s, NULL );
319 if ( strcasecmp( s, "AS" ) == 0 ) {
324 /* oracle doesn't understand "AS" :( and other RDBMSes don't need it */
325 #ifdef BACKSQL_ALIASING_QUOTE
326 backsql_strfcat( &res, "scsc", " " BACKSQL_ALIASING,
327 BACKSQL_ALIASING_QUOTE, s, BACKSQL_ALIASING_QUOTE );
328 #else /* ! BACKSQL_ALIASING */
329 backsql_strcat( &res, " " BACKSQL_ALIASING, s, NULL );
330 #endif /* ! BACKSQL_ALIASING */
332 return res.bb_val.bv_val;
336 backsql_merge_from_clause(
337 struct berbuf *dest_from,
338 struct berval *src_from )
340 char *s, *p, *srcc, *pos, e;
341 struct berbuf res = BB_NULL;
344 Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
345 "dest_from=\"%s\",src_from=\"%s\"\n",
346 dest_from ? dest_from->bb_val.bv_val : "<NULL>",
347 src_from->bv_val, 0 );
348 #endif /* BACKSQL_TRACE */
350 srcc = ch_strdup( src_from->bv_val );
353 if ( dest_from != NULL ) {
358 s = backsql_get_table_spec( &p );
361 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
362 "p=\"%s\" s=\"%s\"\n", p, s, 0 );
363 #endif /* BACKSQL_TRACE */
365 if ( res.bb_val.bv_val == NULL ) {
366 backsql_strcat( &res, s, NULL );
369 pos = strstr( res.bb_val.bv_val, s );
370 if ( pos == NULL || ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) ) {
371 backsql_strfcat( &res, "cs", ',', s );
381 Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
382 #endif /* BACKSQL_TRACE */
391 * splits a pattern in components separated by '?'
392 * (double ?? are turned into single ? and left in the string)
393 * expected contains the number of expected occurrences of '?'
394 * (a negative value means parse as many as possible)
398 backsql_split_pattern(
399 const char *_pattern,
400 BerVarray *split_pattern,
403 char *pattern, *start, *end;
407 #define SPLIT_CHAR '?'
410 assert( split_pattern );
412 pattern = ch_strdup( _pattern );
415 end = strchr( start, SPLIT_CHAR );
416 for ( ; start; expected-- ) {
417 char *real_end = end;
420 if ( real_end == NULL ) {
421 real_end = start + strlen( start );
423 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
425 AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
426 end = strchr( real_end + 1, SPLIT_CHAR );
430 real_len = real_end - start;
431 if ( real_len == 0 ) {
432 ber_str2bv( "", 0, 1, &bv );
434 ber_str2bv( start, real_len, 1, &bv );
437 ber_bvarray_add( split_pattern, &bv );
439 if ( expected == 0 ) {
449 end = strchr( start, SPLIT_CHAR );
461 backsql_prepare_pattern(
462 BerVarray split_pattern,
467 struct berbuf bb = BB_NULL;
471 for ( i = 0; values[i].bv_val; i++ ) {
472 if ( split_pattern[i].bv_val == NULL ) {
473 ch_free( bb.bb_val.bv_val );
476 backsql_strfcat( &bb, "b", &split_pattern[ i ] );
477 backsql_strfcat( &bb, "b", &values[ i ] );
480 if ( split_pattern[ i ].bv_val == NULL ) {
481 ch_free( bb.bb_val.bv_val );
485 backsql_strfcat( &bb, "b", &split_pattern[ i ] );
492 #endif /* SLAPD_SQL */