2003-12-08 03:19:18 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
2000-03-17 03:46:21 +08:00
|
|
|
*
|
2008-01-08 08:19:56 +08:00
|
|
|
* Copyright 1999-2008 The OpenLDAP Foundation.
|
2003-12-08 03:19:18 +08:00
|
|
|
* Portions Copyright 1999 Dmitry Kovalev.
|
2005-01-03 18:51:59 +08:00
|
|
|
* Portions Copyright 2002 Pierangelo Masarati.
|
2003-12-08 03:19:18 +08:00
|
|
|
* 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
|
2005-01-03 18:51:59 +08:00
|
|
|
* by OpenLDAP Software. Additional significant contributors include
|
|
|
|
* Pierangelo Masarati.
|
2000-03-17 03:46:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
2002-08-14 01:12:27 +08:00
|
|
|
#include "ac/string.h"
|
|
|
|
#include "ac/ctype.h"
|
|
|
|
#include "ac/stdarg.h"
|
2004-01-20 06:13:15 +08:00
|
|
|
|
2000-03-17 03:46:21 +08:00
|
|
|
#include "slap.h"
|
2004-01-20 06:13:15 +08:00
|
|
|
#include "proto-sql.h"
|
2004-10-04 09:19:53 +08:00
|
|
|
#include "lutil.h"
|
2000-03-17 03:46:21 +08:00
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
|
|
|
|
|
|
|
|
#define BACKSQL_STR_GROW 256
|
2000-03-17 03:46:21 +08:00
|
|
|
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_oc_query[] =
|
2002-08-14 01:12:27 +08:00
|
|
|
"SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
|
|
|
|
"FROM ldap_oc_mappings";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_needs_select_oc_query[] =
|
2002-08-23 16:54:08 +08:00
|
|
|
"SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
|
|
|
|
"expect_return FROM ldap_oc_mappings";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_at_query[] =
|
2002-08-14 01:12:27 +08:00
|
|
|
"SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
|
|
|
|
"param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
|
|
|
|
"WHERE oc_map_id=?";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_delentry_stmt[] = "DELETE FROM ldap_entries WHERE id=?";
|
|
|
|
const char backsql_def_renentry_stmt[] =
|
2005-01-18 03:32:28 +08:00
|
|
|
"UPDATE ldap_entries SET dn=?,parent=?,keyval=? WHERE id=?";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_insentry_stmt[] =
|
2002-08-14 01:12:27 +08:00
|
|
|
"INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
|
|
|
|
"VALUES (?,?,?,?)";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_delobjclasses_stmt[] = "DELETE FROM ldap_entry_objclasses "
|
2004-08-25 01:08:30 +08:00
|
|
|
"WHERE entry_id=?";
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
|
|
|
|
const char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
|
|
|
|
const char backsql_id_query[] = "SELECT id,keyval,oc_map_id,dn FROM ldap_entries WHERE ";
|
2002-08-23 16:54:08 +08:00
|
|
|
/* better ?||? or cast(?||? as varchar) */
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_def_concat_func[] = "CONCAT(?,?)";
|
2002-08-14 01:12:27 +08:00
|
|
|
|
|
|
|
/* TimesTen */
|
2007-12-15 23:23:23 +08:00
|
|
|
const char backsql_check_dn_ru_query[] = "SELECT dn_ru FROM ldap_entries";
|
A big bunch of improvements, contributed by Sam Drake and Raj Damani.
Summary of changes is cited below.
The patch still needs some cosmetic changes to be made, but is ready for testing.
-----Original Message-----
From: Sam Drake [mailto:drake@timesten.com]
Sent: Saturday, April 07, 2001 10:40 PM
To: 'mitya@seismic.ru'
Cc: openldap-devel@OpenLDAP.org
Subject: RE: Slapd frontend performance issues
FYI, here is a short description of the changes I made. I'll package up the
changes asap, but it may take a couple of days.
The performance numbers quoted in this report were seen at my location with
a 100,000 object database ... the slower numbers I mentioned earlier were
reported by a customer with a 1,000,000 object database.
I also can't explain the very poor performance I saw with OpenLDAP and LDBM
with a 100,000 object database.
...Sam Drake / TimesTen Performance Software
----------
Work Performed
OpenLDAP 2.0.9, including back-sql, was built successfully on Solaris
8 using gcc. The LDAP server itself, slapd, passed all tests bundled
with OpenLDAP. OpenLDAP was built using Sleepycat LDBM release 3.1.17
as the "native" storage manager.
The experimental back-sql facility in slapd was also built
successfully. It was built using Oracle release 8.1.7 and the Oracle
ODBC driver and ODBC Driver Manager from Merant. Rudimentary testing
was performed with the data and examples provided with back-sql, and
back-sql was found to be functional.
Slapd and back-sql were then tested with TimesTen, using TimesTen
4.1.1. Back-sql was not immediately functional with TimesTen due to a
number of SQL limitations in the TimesTen product.
Functional issues encountered were:
1. Back-sql issued SELECT statements including the construct,
"UPPER(?)". While TimesTen supports UPPER, it does not support the
use of parameters as input to builtin functions. Back-sql was
modified to convert the parameter to upper case prior to giving it
to the underlying database ... a change that is appropriate for all
databases.
2. Back-sql issued SELECT statements using the SQL CONCAT function.
TimesTen does not support this function. Back-sql was modified to
concatentate the necessary strings itself (in "C" code) prior to
passing the parameters to SQL. This change is also appropriate for
all databases, not just TimesTen.
Once these two issues were resolved, back-sql could successfully
process LDAP searches using the sample data and examples provided with
back-sql.
While performance was not measured at this point, numerous serious
performance problems were observed with the back-sql code and the
generated SQL. In particular:
1. In the process of implementing an LDAP search, back-sql will
generate and execute a SQL query for all object classes stored in
back-sql. During the source of generating each SQL query, it is
common for back-sql to determine that a particular object class can
not possibly have any members satisfying the search. For example,
this can occur if the query searches an attribute of the LDAP
object that does not exist in the SQL schema. In this case,
back-sql would generate and issue the SQL query anyway, including a
clause such as "WHERE 1=0" in the generated SELECT. The overhead
of parsing, optimizing and executing the query is non-trivial, and
the answer (the empty set) is known in advance. Solution: Back-sql
was modified to stop executing a SQL query when it can be
predetermined that the query will return no rows.
2. Searches in LDAP are fundamentally case-insensitive ("abc" is equal
to "aBc"). However, in SQL this is not normally the case.
Back-sql thus generated SQL SELECT statements including clauses of
the form, "WHERE UPPER(attribute) = 'JOE'". Even if an index is
defined on the attribute in the relational database, the index can
not be used to satisfy the query, as the index is case sensitive.
The relational database then is forced to scan all rows in the
table in order to satisfy the query ... an expensive and
non-scalable proposition. Solution: Back-sql was modified to allow
the schema designer to add additional "upper cased" columns to the
SQL schema. These columns, if present, contain an upper cased
version of the "standard" field, and will be used preferentially
for searching. Such columns can be provided for all searchable
columns, some columns, or no columns. An application using
database "triggers" or similar mechanisms can automatically
maintain these upper cased columns when the standard column is
changed.
3. In order to implement the hierarchical nature of LDAP object
hierarchies, OpenLDAP uses suffix searches in SQL. For example, to
find all objects in the subtree "o=TimesTen,c=us", a SQL SELECT
statement of the form, "WHERE UPPER(dn) LIKE '%O=TIMESTEN,C=US'"
would be employed. Aside from the UPPER issue discussed above, a
second performance problem in this query is the use of suffix
search. In TimesTen (and most relational databases), indexes can
be used to optimize exact-match searches and prefix searches.
However, suffix searches must be performed by scanning every row in
the table ... an expensive and non-scalable proposition. Solution:
Back-sql was modified to optionally add a new "dn_ru" column to the
ldap_entries table. This additional column, if present, contains a
byte-reversed and upper cased version of the DN. This allows
back-sql to generate indexable prefix searches. This column is
also easily maintained automatically through the use of triggers.
Results
A simple database schema was generated holding the LDAP objects and
attributes specified by our customer. An application was written to
generate test databases. Both TimesTen and Oracle 8.1.7 were
populated with 100,000 entry databases.
Load Times
Using "slapadd" followed by "slapindex", loading and indexing 100,000
entries in an LDBM database ran for 19 minutes 10 seconds.
Using a C++ application that used ODBC, loading 100,000 entries into
a disk based RDBMS took 17 minutes 53 seconds.
Using a C++ application that used ODBC, loading 100,000 entries into
TimesTen took 1 minute 40 seconds.
Search Times
The command, "timex timesearch.sh '(cn=fname210100*)'" was used to
test search times. This command issues the same LDAP search 4000
times over a single LDAP connection. Both the client and server
(slapd) were run on the same machine.
With TimesTen as the database, 4000 queries took 14.93 seconds, for a
rate of 267.9 per second.
With a disk based RDBMS as the database, 4000 queries took 77.79 seconds,
for a
rate of 51.42 per second.
With LDBM as the database, 1 query takes 76 seconds, or 0.076 per
second. Something is clearly broken.
2001-08-03 01:28:59 +08:00
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf *
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strcat_x( struct berbuf *dest, void *memctx, ... )
|
2000-03-17 03:46:21 +08:00
|
|
|
{
|
2002-08-14 01:12:27 +08:00
|
|
|
va_list strs;
|
2002-08-23 16:54:08 +08:00
|
|
|
ber_len_t cdlen, cslen, grow;
|
2002-08-14 01:12:27 +08:00
|
|
|
char *cstr;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( dest != NULL );
|
2003-04-16 05:55:25 +08:00
|
|
|
assert( dest->bb_val.bv_val == NULL
|
|
|
|
|| dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
|
2000-03-17 03:46:21 +08:00
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#ifdef BACKSQL_TRACE
|
2004-06-15 04:04:36 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n", 0, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
2002-08-14 01:12:27 +08:00
|
|
|
|
2005-11-14 05:51:13 +08:00
|
|
|
va_start( strs, memctx );
|
2003-04-16 05:55:25 +08:00
|
|
|
if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
|
2005-11-14 05:51:13 +08:00
|
|
|
dest->bb_val.bv_val = (char *)ber_memalloc_x( BACKSQL_STR_GROW * sizeof( char ), memctx );
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_len = 0;
|
|
|
|
dest->bb_len = BACKSQL_STR_GROW;
|
2002-08-14 01:12:27 +08:00
|
|
|
}
|
2003-04-16 05:55:25 +08:00
|
|
|
cdlen = dest->bb_val.bv_len;
|
2002-08-14 01:12:27 +08:00
|
|
|
while ( ( cstr = va_arg( strs, char * ) ) != NULL ) {
|
|
|
|
cslen = strlen( cstr );
|
|
|
|
grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
|
2003-04-16 05:55:25 +08:00
|
|
|
if ( dest->bb_len - cdlen <= cslen ) {
|
2002-08-14 01:12:27 +08:00
|
|
|
char *tmp_dest;
|
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#ifdef BACKSQL_TRACE
|
2002-08-14 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
|
|
|
|
"buflen=%d, cdlen=%d, cslen=%d "
|
|
|
|
"-- reallocating dest\n",
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_len, cdlen + 1, cslen );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2005-11-14 05:51:13 +08:00
|
|
|
tmp_dest = (char *)ber_memrealloc_x( dest->bb_val.bv_val,
|
|
|
|
dest->bb_len + grow * sizeof( char ), memctx );
|
2002-08-14 01:12:27 +08:00
|
|
|
if ( tmp_dest == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
|
|
|
|
"could not reallocate string buffer.\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_val = tmp_dest;
|
|
|
|
dest->bb_len += grow;
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2002-08-14 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
|
2004-08-20 20:06:03 +08:00
|
|
|
"new buflen=%d, dest=%p\n",
|
|
|
|
dest->bb_len, dest, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
2002-08-14 01:12:27 +08:00
|
|
|
}
|
2003-04-16 05:55:25 +08:00
|
|
|
AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
|
2002-08-14 01:12:27 +08:00
|
|
|
cdlen += cslen;
|
|
|
|
}
|
|
|
|
va_end( strs );
|
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#ifdef BACKSQL_TRACE
|
2004-04-10 17:33:55 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest=\"%s\")\n",
|
2004-08-20 20:06:03 +08:00
|
|
|
dest->bb_val.bv_val, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_len = cdlen;
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf *
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... )
|
2002-08-23 16:54:08 +08:00
|
|
|
{
|
|
|
|
va_list strs;
|
|
|
|
ber_len_t cdlen;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( dest != NULL );
|
|
|
|
assert( fmt != NULL );
|
2003-04-16 05:55:25 +08:00
|
|
|
assert( dest->bb_len == 0 || dest->bb_len > dest->bb_val.bv_len );
|
|
|
|
assert( dest->bb_val.bv_val == NULL
|
|
|
|
|| dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2004-06-15 04:04:36 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n", 0, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
|
|
|
va_start( strs, fmt );
|
2003-04-16 05:55:25 +08:00
|
|
|
if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
|
2005-11-14 05:51:13 +08:00
|
|
|
dest->bb_val.bv_val = (char *)ber_memalloc_x( BACKSQL_STR_GROW * sizeof( char ), memctx );
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_len = 0;
|
|
|
|
dest->bb_len = BACKSQL_STR_GROW;
|
2002-08-23 16:54:08 +08:00
|
|
|
}
|
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
cdlen = dest->bb_val.bv_len;
|
2002-08-23 16:54:08 +08:00
|
|
|
for ( ; fmt[0]; fmt++ ) {
|
|
|
|
ber_len_t cslen, grow;
|
|
|
|
char *cstr, cc[ 2 ] = { '\0', '\0' };
|
|
|
|
struct berval *cbv;
|
|
|
|
|
|
|
|
switch ( fmt[ 0 ] ) {
|
|
|
|
|
|
|
|
/* berval */
|
|
|
|
case 'b':
|
|
|
|
cbv = va_arg( strs, struct berval * );
|
|
|
|
cstr = cbv->bv_val;
|
|
|
|
cslen = cbv->bv_len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* length + string */
|
|
|
|
case 'l':
|
|
|
|
cslen = va_arg( strs, ber_len_t );
|
|
|
|
cstr = va_arg( strs, char * );
|
|
|
|
break;
|
2004-08-24 18:31:02 +08:00
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
/* string */
|
|
|
|
case 's':
|
|
|
|
cstr = va_arg( strs, char * );
|
|
|
|
cslen = strlen( cstr );
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* char */
|
|
|
|
case 'c':
|
|
|
|
/*
|
|
|
|
* `char' is promoted to `int' when passed through `...'
|
|
|
|
*/
|
|
|
|
cc[0] = va_arg( strs, int );
|
|
|
|
cstr = cc;
|
|
|
|
cslen = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
|
2003-04-16 05:55:25 +08:00
|
|
|
if ( dest->bb_len - cdlen <= cslen ) {
|
2002-08-23 16:54:08 +08:00
|
|
|
char *tmp_dest;
|
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
|
|
|
|
"buflen=%d, cdlen=%d, cslen=%d "
|
|
|
|
"-- reallocating dest\n",
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_len, cdlen + 1, cslen );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2005-11-14 05:51:13 +08:00
|
|
|
tmp_dest = (char *)ber_memrealloc_x( dest->bb_val.bv_val,
|
|
|
|
( dest->bb_len ) + grow * sizeof( char ), memctx );
|
2002-08-23 16:54:08 +08:00
|
|
|
if ( tmp_dest == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
|
|
|
|
"could not reallocate string buffer.\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_val = tmp_dest;
|
|
|
|
dest->bb_len += grow * sizeof( char );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
|
2003-04-16 05:55:25 +08:00
|
|
|
"new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
}
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( cstr != NULL );
|
2002-08-31 18:37:42 +08:00
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
|
2002-08-23 16:54:08 +08:00
|
|
|
cdlen += cslen;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end( strs );
|
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2004-04-10 17:33:55 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest=\"%s\")\n",
|
2004-08-20 20:06:03 +08:00
|
|
|
dest->bb_val.bv_val, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
2002-08-14 01:12:27 +08:00
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
dest->bb_val.bv_len = cdlen;
|
2002-08-14 01:12:27 +08:00
|
|
|
|
|
|
|
return dest;
|
2000-03-17 03:46:21 +08:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:12:27 +08:00
|
|
|
int
|
|
|
|
backsql_entry_addattr(
|
2005-01-17 07:12:36 +08:00
|
|
|
Entry *e,
|
|
|
|
AttributeDescription *ad,
|
|
|
|
struct berval *val,
|
|
|
|
void *memctx )
|
2000-03-17 03:46:21 +08:00
|
|
|
{
|
2002-08-14 01:12:27 +08:00
|
|
|
int rc;
|
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#ifdef BACKSQL_TRACE
|
2005-01-17 07:12:36 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(\"%s\"): %s=%s\n",
|
2006-12-22 06:05:49 +08:00
|
|
|
e->e_name.bv_val, ad->ad_cname.bv_val, val->bv_val );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
2002-08-14 01:12:27 +08:00
|
|
|
|
2005-01-17 07:12:36 +08:00
|
|
|
rc = attr_merge_normalize_one( e, ad, val, memctx );
|
2002-08-14 01:12:27 +08:00
|
|
|
|
2005-01-17 07:12:36 +08:00
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(\"%s\"): "
|
2004-04-10 17:33:55 +08:00
|
|
|
"failed to merge value \"%s\" for attribute \"%s\"\n",
|
2005-01-17 07:12:36 +08:00
|
|
|
e->e_name.bv_val, val->bv_val, ad->ad_cname.bv_val );
|
|
|
|
return rc;
|
2002-08-14 01:12:27 +08:00
|
|
|
}
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2005-01-17 07:12:36 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<==backsql_entry_addattr(\"%s\")\n",
|
|
|
|
e->e_name.bv_val, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2005-01-17 07:12:36 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-03-17 03:46:21 +08:00
|
|
|
}
|
|
|
|
|
2004-01-20 06:13:15 +08:00
|
|
|
static char *
|
2005-06-09 17:43:20 +08:00
|
|
|
backsql_get_table_spec( backsql_info *bi, char **p )
|
2000-03-17 03:46:21 +08:00
|
|
|
{
|
2002-08-14 01:12:27 +08:00
|
|
|
char *s, *q;
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf res = BB_NULL;
|
2002-08-23 16:54:08 +08:00
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( p != NULL );
|
|
|
|
assert( *p != NULL );
|
2002-08-14 01:12:27 +08:00
|
|
|
|
|
|
|
s = *p;
|
|
|
|
while ( **p && **p != ',' ) {
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( **p ) {
|
|
|
|
*(*p)++ = '\0';
|
|
|
|
}
|
|
|
|
|
2002-04-16 04:42:42 +08:00
|
|
|
#define BACKSQL_NEXT_WORD { \
|
2002-08-14 01:12:27 +08:00
|
|
|
while ( *s && isspace( (unsigned char)*s ) ) s++; \
|
2003-04-16 05:55:25 +08:00
|
|
|
if ( !*s ) return res.bb_val.bv_val; \
|
2002-08-14 01:12:27 +08:00
|
|
|
q = s; \
|
|
|
|
while ( *q && !isspace( (unsigned char)*q ) ) q++; \
|
|
|
|
if ( *q ) *q++='\0'; \
|
|
|
|
}
|
|
|
|
|
|
|
|
BACKSQL_NEXT_WORD;
|
|
|
|
/* table name */
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strcat_x( &res, NULL, s, NULL );
|
2002-08-14 01:12:27 +08:00
|
|
|
s = q;
|
|
|
|
|
|
|
|
BACKSQL_NEXT_WORD;
|
2004-08-25 18:41:13 +08:00
|
|
|
if ( strcasecmp( s, "AS" ) == 0 ) {
|
2002-08-14 01:12:27 +08:00
|
|
|
s = q;
|
|
|
|
BACKSQL_NEXT_WORD;
|
|
|
|
}
|
2002-08-23 16:54:08 +08:00
|
|
|
|
2004-08-25 18:41:13 +08:00
|
|
|
/* oracle doesn't understand "AS" :( and other RDBMSes don't need it */
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strfcat_x( &res, NULL, "lbbsb",
|
2005-06-09 17:43:20 +08:00
|
|
|
STRLENOF( " " ), " ",
|
|
|
|
&bi->sql_aliasing,
|
|
|
|
&bi->sql_aliasing_quote,
|
|
|
|
s,
|
|
|
|
&bi->sql_aliasing_quote );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
2003-04-16 05:55:25 +08:00
|
|
|
return res.bb_val.bv_val;
|
2000-03-17 03:46:21 +08:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:12:27 +08:00
|
|
|
int
|
2002-08-23 16:54:08 +08:00
|
|
|
backsql_merge_from_clause(
|
2005-06-09 17:43:20 +08:00
|
|
|
backsql_info *bi,
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf *dest_from,
|
2002-08-23 16:54:08 +08:00
|
|
|
struct berval *src_from )
|
2000-03-17 03:46:21 +08:00
|
|
|
{
|
2002-08-14 01:12:27 +08:00
|
|
|
char *s, *p, *srcc, *pos, e;
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf res = BB_NULL;
|
2002-08-14 01:12:27 +08:00
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
#ifdef BACKSQL_TRACE
|
2002-08-14 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
|
2004-04-10 17:33:55 +08:00
|
|
|
"dest_from=\"%s\",src_from=\"%s\"\n",
|
2004-08-20 20:06:03 +08:00
|
|
|
dest_from ? dest_from->bb_val.bv_val : "<NULL>",
|
|
|
|
src_from->bv_val, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
|
|
|
srcc = ch_strdup( src_from->bv_val );
|
2002-08-14 01:12:27 +08:00
|
|
|
p = srcc;
|
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
if ( dest_from != NULL ) {
|
|
|
|
res = *dest_from;
|
2002-08-14 01:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( *p ) {
|
2005-06-09 17:43:20 +08:00
|
|
|
s = backsql_get_table_spec( bi, &p );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2002-08-14 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
|
2004-04-10 17:33:55 +08:00
|
|
|
"p=\"%s\" s=\"%s\"\n", p, s, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2005-11-14 05:51:13 +08:00
|
|
|
if ( BER_BVISNULL( &res.bb_val ) ) {
|
|
|
|
backsql_strcat_x( &res, NULL, s, NULL );
|
2002-08-14 01:12:27 +08:00
|
|
|
|
|
|
|
} else {
|
2003-04-16 05:55:25 +08:00
|
|
|
pos = strstr( res.bb_val.bv_val, s );
|
|
|
|
if ( pos == NULL || ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) ) {
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strfcat_x( &res, NULL, "cs", ',', s );
|
2002-08-14 01:12:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( s ) {
|
|
|
|
ch_free( s );
|
|
|
|
}
|
|
|
|
}
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_TRACE
|
2002-08-14 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
|
2002-08-23 16:54:08 +08:00
|
|
|
#endif /* BACKSQL_TRACE */
|
|
|
|
|
2002-08-14 01:12:27 +08:00
|
|
|
free( srcc );
|
2002-08-23 16:54:08 +08:00
|
|
|
*dest_from = res;
|
2002-08-14 01:12:27 +08:00
|
|
|
|
|
|
|
return 1;
|
2000-03-17 03:46:21 +08:00
|
|
|
}
|
2000-03-19 14:18:27 +08:00
|
|
|
|
2002-08-23 16:54:08 +08:00
|
|
|
/*
|
|
|
|
* splits a pattern in components separated by '?'
|
|
|
|
* (double ?? are turned into single ? and left in the string)
|
|
|
|
* expected contains the number of expected occurrences of '?'
|
|
|
|
* (a negative value means parse as many as possible)
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
backsql_split_pattern(
|
|
|
|
const char *_pattern,
|
|
|
|
BerVarray *split_pattern,
|
|
|
|
int expected )
|
|
|
|
{
|
|
|
|
char *pattern, *start, *end;
|
|
|
|
struct berval bv;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
#define SPLIT_CHAR '?'
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( _pattern != NULL );
|
|
|
|
assert( split_pattern != NULL );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
pattern = ch_strdup( _pattern );
|
|
|
|
|
|
|
|
start = pattern;
|
|
|
|
end = strchr( start, SPLIT_CHAR );
|
|
|
|
for ( ; start; expected-- ) {
|
|
|
|
char *real_end = end;
|
|
|
|
ber_len_t real_len;
|
|
|
|
|
|
|
|
if ( real_end == NULL ) {
|
|
|
|
real_end = start + strlen( start );
|
|
|
|
|
|
|
|
} else if ( real_end[ 1 ] == SPLIT_CHAR ) {
|
|
|
|
expected++;
|
|
|
|
AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
|
|
|
|
end = strchr( real_end + 1, SPLIT_CHAR );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_len = real_end - start;
|
|
|
|
if ( real_len == 0 ) {
|
|
|
|
ber_str2bv( "", 0, 1, &bv );
|
|
|
|
} else {
|
|
|
|
ber_str2bv( start, real_len, 1, &bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_bvarray_add( split_pattern, &bv );
|
|
|
|
|
|
|
|
if ( expected == 0 ) {
|
|
|
|
if ( end != NULL ) {
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( end != NULL ) {
|
|
|
|
start = end + 1;
|
|
|
|
end = strchr( start, SPLIT_CHAR );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:;
|
|
|
|
|
|
|
|
ch_free( pattern );
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
backsql_prepare_pattern(
|
|
|
|
BerVarray split_pattern,
|
|
|
|
BerVarray values,
|
|
|
|
struct berval *res )
|
|
|
|
{
|
|
|
|
int i;
|
2003-04-16 05:55:25 +08:00
|
|
|
struct berbuf bb = BB_NULL;
|
2002-08-23 16:54:08 +08:00
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( res != NULL );
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
for ( i = 0; values[i].bv_val; i++ ) {
|
|
|
|
if ( split_pattern[i].bv_val == NULL ) {
|
2003-04-16 05:55:25 +08:00
|
|
|
ch_free( bb.bb_val.bv_val );
|
2002-08-23 16:54:08 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strfcat_x( &bb, NULL, "b", &split_pattern[ i ] );
|
|
|
|
backsql_strfcat_x( &bb, NULL, "b", &values[ i ] );
|
2002-08-23 16:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( split_pattern[ i ].bv_val == NULL ) {
|
2003-04-16 05:55:25 +08:00
|
|
|
ch_free( bb.bb_val.bv_val );
|
2002-08-23 16:54:08 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-14 05:51:13 +08:00
|
|
|
backsql_strfcat_x( &bb, NULL, "b", &split_pattern[ i ] );
|
2003-04-16 05:55:25 +08:00
|
|
|
|
|
|
|
*res = bb.bb_val;
|
2002-08-23 16:54:08 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-04 09:19:53 +08:00
|
|
|
int
|
|
|
|
backsql_entryUUID(
|
|
|
|
backsql_info *bi,
|
|
|
|
backsql_entryID *id,
|
|
|
|
struct berval *entryUUID,
|
|
|
|
void *memctx )
|
|
|
|
{
|
|
|
|
char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
|
|
|
|
struct berval uuid;
|
|
|
|
#ifdef BACKSQL_ARBITRARY_KEY
|
|
|
|
int i;
|
|
|
|
ber_len_t l, lmax;
|
|
|
|
#endif /* BACKSQL_ARBITRARY_KEY */
|
|
|
|
|
|
|
|
/* entryUUID is generated as "%08x-%04x-%04x-0000-eaddrXXX"
|
|
|
|
* with eid_oc_id as %08x and hi and lo eid_id as %04x-%04x */
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( bi != NULL );
|
|
|
|
assert( id != NULL );
|
|
|
|
assert( entryUUID != NULL );
|
2004-10-04 09:19:53 +08:00
|
|
|
|
|
|
|
#ifdef BACKSQL_ARBITRARY_KEY
|
|
|
|
snprintf( uuidbuf, sizeof( uuidbuf ),
|
|
|
|
"%08x-0000-0000-0000-000000000000",
|
|
|
|
( id->eid_oc_id & 0xFFFFFFFF ) );
|
|
|
|
lmax = id->eid_keyval.bv_len < 12 ? id->eid_keyval.bv_len : 12;
|
|
|
|
for ( l = 0, i = 9; l < lmax; l++, i += 2 ) {
|
|
|
|
switch ( i ) {
|
|
|
|
case STRLENOF( "00000000-0000" ):
|
|
|
|
case STRLENOF( "00000000-0000-0000" ):
|
|
|
|
case STRLENOF( "00000000-0000-0000-0000" ):
|
|
|
|
uuidbuf[ i++ ] = '-';
|
|
|
|
/* FALLTHRU */
|
|
|
|
|
|
|
|
default:
|
|
|
|
snprintf( &uuidbuf[ i ], 3, "%2x", id->eid_keyval.bv_val[ l ] );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* ! BACKSQL_ARBITRARY_KEY */
|
|
|
|
/* note: works only with 32 bit architectures... */
|
|
|
|
snprintf( uuidbuf, sizeof( uuidbuf ),
|
|
|
|
"%08x-%04x-%04x-0000-000000000000",
|
2005-09-09 04:18:43 +08:00
|
|
|
( (unsigned)id->eid_oc_id & 0xFFFFFFFF ),
|
|
|
|
( ( (unsigned)id->eid_keyval & 0xFFFF0000 ) >> 020 /* 16 */ ),
|
|
|
|
( (unsigned)id->eid_keyval & 0xFFFF ) );
|
2004-10-04 09:19:53 +08:00
|
|
|
#endif /* ! BACKSQL_ARBITRARY_KEY */
|
|
|
|
|
|
|
|
uuid.bv_val = uuidbuf;
|
|
|
|
uuid.bv_len = strlen( uuidbuf );
|
|
|
|
|
|
|
|
ber_dupbv_x( entryUUID, &uuid, memctx );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
backsql_entryUUID_decode(
|
|
|
|
struct berval *entryUUID,
|
|
|
|
unsigned long *oc_id,
|
|
|
|
#ifdef BACKSQL_ARBITRARY_KEY
|
|
|
|
struct berval *keyval
|
|
|
|
#else /* ! BACKSQL_ARBITRARY_KEY */
|
|
|
|
unsigned long *keyval
|
|
|
|
#endif /* ! BACKSQL_ARBITRARY_KEY */
|
|
|
|
)
|
|
|
|
{
|
2005-11-24 09:00:28 +08:00
|
|
|
#if 0
|
2004-10-04 09:19:53 +08:00
|
|
|
fprintf( stderr, "==> backsql_entryUUID_decode()\n" );
|
2005-11-24 09:00:28 +08:00
|
|
|
#endif
|
2004-10-04 09:19:53 +08:00
|
|
|
|
2005-01-08 17:59:16 +08:00
|
|
|
*oc_id = ( entryUUID->bv_val[0] << 030 /* 24 */ )
|
|
|
|
+ ( entryUUID->bv_val[1] << 020 /* 16 */ )
|
|
|
|
+ ( entryUUID->bv_val[2] << 010 /* 8 */ )
|
2004-10-04 09:19:53 +08:00
|
|
|
+ entryUUID->bv_val[3];
|
|
|
|
|
|
|
|
#ifdef BACKSQL_ARBITRARY_KEY
|
2005-01-08 17:59:16 +08:00
|
|
|
/* FIXME */
|
2004-10-04 09:19:53 +08:00
|
|
|
#else /* ! BACKSQL_ARBITRARY_KEY */
|
2005-01-08 17:59:16 +08:00
|
|
|
*keyval = ( entryUUID->bv_val[4] << 030 /* 24 */ )
|
|
|
|
+ ( entryUUID->bv_val[5] << 020 /* 16 */ )
|
|
|
|
+ ( entryUUID->bv_val[6] << 010 /* 8 */ )
|
2004-10-04 09:19:53 +08:00
|
|
|
+ entryUUID->bv_val[7];
|
|
|
|
#endif /* ! BACKSQL_ARBITRARY_KEY */
|
|
|
|
|
2005-11-24 09:00:28 +08:00
|
|
|
#if 0
|
2004-10-04 09:19:53 +08:00
|
|
|
fprintf( stderr, "<== backsql_entryUUID_decode(): oc=%lu id=%lu\n",
|
|
|
|
*oc_id, *keyval );
|
2005-11-24 09:00:28 +08:00
|
|
|
#endif
|
2004-10-04 09:19:53 +08:00
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|