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"
21 #include "schema-map.h"
26 backsql_free_entryID( backsql_entryID *id, int freeit )
28 backsql_entryID *next;
34 if ( id->dn.bv_val != NULL ) {
35 free( id->dn.bv_val );
46 * FIXME: need to change API to pass backsql_entryID **id
47 * and return an error code, to distinguish LDAP_OTHER from
66 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
70 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n",
75 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
76 Debug( LDAP_DEBUG_TRACE,
77 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
78 "exceeds max DN length (%d):\n",
79 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
84 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
85 assert( bi->id_query );
86 rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
87 if ( rc != SQL_SUCCESS ) {
88 Debug( LDAP_DEBUG_TRACE,
89 "backsql_dn2id(): error preparing SQL:\n%s",
91 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
92 SQLFreeStmt( sth, SQL_DROP );
96 if ( bi->has_ldapinfo_dn_ru ) {
98 * Prepare an upper cased, byte reversed version
99 * that can be searched using indexes
102 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
103 upperdn[ i ] = dn->bv_val[ j ];
106 ldap_pvt_str2upper( upperdn );
108 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
112 if ( bi->isTimesTen ) {
113 AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
114 ldap_pvt_str2upper( upperdn );
115 Debug( LDAP_DEBUG_TRACE,
116 "==>backsql_dn2id(): upperdn='%s'\n",
125 rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
126 if ( rc != SQL_SUCCESS) {
128 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
129 "error binding dn=\"%s\" parameter:\n", toBind, 0, 0 );
130 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
131 SQLFreeStmt( sth, SQL_DROP );
135 rc = SQLExecute( sth );
136 if ( rc != SQL_SUCCESS ) {
137 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
138 "error executing query (\"%s\", \"%s\"):\n",
139 bi->id_query, toBind, 0 );
140 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
141 SQLFreeStmt( sth, SQL_DROP );
145 backsql_BindRowAsStrings( sth, &row );
146 rc = SQLFetch( sth );
147 if ( BACKSQL_SUCCESS( rc ) ) {
148 id->id = atoi( row.cols[ 0 ] );
149 id->keyval = atoi( row.cols[ 1 ] );
150 id->oc_id = atoi( row.cols[ 2 ] );
151 ber_dupbv( &id->dn, dn );
157 res = LDAP_NO_SUCH_OBJECT;
159 backsql_FreeRow( &row );
161 SQLFreeStmt( sth, SQL_DROP );
162 if ( res == LDAP_SUCCESS ) {
163 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
166 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
173 backsql_get_attr_vals( backsql_at_map_rec *at, backsql_srch_info *bsi )
183 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
184 "oc='%s' attr='%s' keyval=%ld\n",
185 bsi->oc->name, at->name, bsi->c_eid->keyval );
187 rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
188 if ( rc != SQL_SUCCESS ) {
189 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
190 "error preparing query: %s\n", at->query, 0, 0 );
191 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
195 rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
196 if ( rc != SQL_SUCCESS ) {
197 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
198 "error binding key value parameter\n", 0, 0, 0 );
202 rc = SQLExecute( sth );
203 if ( ! BACKSQL_SUCCESS( rc ) ) {
204 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
205 "error executing attribute query '%s'\n",
207 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
208 SQLFreeStmt( sth, SQL_DROP );
212 backsql_BindRowAsStrings( sth, &row );
214 rc = SQLFetch( sth );
215 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
216 for ( i = 0; i < row.ncols; i++ ) {
217 if ( row.is_null[ i ] > 0 ) {
218 backsql_entry_addattr( bsi->e,
225 * FIXME: what if a binary
228 strlen( row.cols[ i ] )
232 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
233 (int)row.col_prec[ i ], 0, 0 );
235 Debug( LDAP_DEBUG_TRACE, "NULL value "
236 "in this row for attribute '%s'\n",
237 row.col_names[ i ], 0, 0 );
243 backsql_FreeRow( &row );
244 SQLFreeStmt( sth, SQL_DROP );
245 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
251 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
254 backsql_at_map_rec *at;
257 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
259 rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
260 if ( rc != LDAP_SUCCESS ) {
264 bsi->oc = backsql_oc_with_id( bsi->bi, eid->oc_id );
270 /* if ( bsi->base_dn != NULL)??? */
274 if ( bsi->attrs != NULL ) {
275 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
276 "custom attribute list\n", 0, 0, 0 );
277 for ( c_at_name = bsi->attrs; *c_at_name != NULL; c_at_name++ ) {
278 if ( !strcasecmp( *c_at_name, "objectclass" )
279 || !strcasecmp( *c_at_name, "0.10" ) ) {
281 backsql_entry_addattr( bsi->e, "objectclass",
283 strlen( bsi->oc->name ) );
287 at = backsql_at_with_name( bsi->oc, *c_at_name );
289 backsql_get_attr_vals( at, bsi );
291 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
292 "attribute '%s' is not defined "
293 "for objectlass '%s'\n",
294 *c_at_name, bsi->oc->name, 0 );
298 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
299 "retrieving all attributes\n", 0, 0, 0 );
300 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
301 bsi, 0, AVL_INORDER );
304 backsql_entry_addattr( bsi->e, "objectclass", bsi->oc->name,
305 strlen( bsi->oc->name ) );
307 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
312 #endif /* SLAPD_SQL */