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.bv_val, at->name.bv_val, 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 ) {
220 bv.bv_val = row.cols[ i ];
222 bv.bv_len = row.col_prec[ i ];
225 * FIXME: what if a binary
228 bv.bv_len = strlen( row.cols[ i ] );
230 backsql_entry_addattr( bsi->e,
231 &row.col_names[ i ], &bv );
234 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
235 (int)row.col_prec[ i ], 0, 0 );
237 Debug( LDAP_DEBUG_TRACE, "NULL value "
238 "in this row for attribute '%s'\n",
239 row.col_names[ i ].bv_val, 0, 0 );
245 backsql_FreeRow( &row );
246 SQLFreeStmt( sth, SQL_DROP );
247 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
253 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
256 backsql_at_map_rec *at;
259 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
261 rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
262 if ( rc != LDAP_SUCCESS ) {
266 bsi->oc = backsql_id2oc( bsi->bi, eid->oc_id );
272 /* if ( bsi->base_dn != NULL)??? */
276 if ( bsi->attrs != NULL ) {
277 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
278 "custom attribute list\n", 0, 0, 0 );
279 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
280 AttributeName *attr = &bsi->attrs[ i ];
282 if ( attr->an_desc == slap_schema.si_ad_objectClass
283 #if 0 /* FIXME: what is 0.10 ? */
284 || !BACKSQL_NCMP( &attr->an_name, &bv_n_0_10 )
288 backsql_entry_addattr( bsi->e,
295 at = backsql_ad2at( bsi->oc, attr->an_desc );
297 backsql_get_attr_vals( at, bsi );
299 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
300 "attribute '%s' is not defined "
301 "for objectlass '%s'\n",
302 attr->an_name.bv_val,
303 bsi->oc->name.bv_val, 0 );
307 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
308 "retrieving all attributes\n", 0, 0, 0 );
309 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
310 bsi, 0, AVL_INORDER );
313 backsql_entry_addattr( bsi->e, &bv_n_objectclass, &bsi->oc->name );
315 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
320 #endif /* SLAPD_SQL */