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 );
58 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
62 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n",
67 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
68 Debug( LDAP_DEBUG_TRACE,
69 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
70 "exceeds max DN length (%d):\n",
71 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
76 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
77 assert( bi->id_query );
78 rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
79 if ( rc != SQL_SUCCESS ) {
80 Debug( LDAP_DEBUG_TRACE,
81 "backsql_dn2id(): error preparing SQL:\n%s",
83 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
84 SQLFreeStmt( sth, SQL_DROP );
88 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
90 * Prepare an upper cased, byte reversed version
91 * that can be searched using indexes
94 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
95 upperdn[ i ] = dn->bv_val[ j ];
98 ldap_pvt_str2upper( upperdn );
100 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
104 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
105 AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
106 ldap_pvt_str2upper( upperdn );
107 Debug( LDAP_DEBUG_TRACE,
108 "==>backsql_dn2id(): upperdn='%s'\n",
117 rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
118 if ( rc != SQL_SUCCESS) {
120 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
121 "error binding dn=\"%s\" parameter:\n",
123 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
124 SQLFreeStmt( sth, SQL_DROP );
128 rc = SQLExecute( sth );
129 if ( rc != SQL_SUCCESS ) {
130 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
131 "error executing query (\"%s\", \"%s\"):\n",
132 bi->id_query, toBind, 0 );
133 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
134 SQLFreeStmt( sth, SQL_DROP );
138 backsql_BindRowAsStrings( sth, &row );
139 rc = SQLFetch( sth );
140 if ( BACKSQL_SUCCESS( rc ) ) {
141 id->id = strtol( row.cols[ 0 ], NULL, 0 );
142 id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
143 id->oc_id = strtol( row.cols[ 2 ], NULL, 0 );
144 ber_dupbv( &id->dn, dn );
150 res = LDAP_NO_SUCH_OBJECT;
152 backsql_FreeRow( &row );
154 SQLFreeStmt( sth, SQL_DROP );
155 if ( res == LDAP_SUCCESS ) {
156 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
159 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
166 backsql_has_children(
176 Debug( LDAP_DEBUG_TRACE, "==>backsql_has_children(): dn='%s'\n",
179 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
180 Debug( LDAP_DEBUG_TRACE,
181 "backsql_has_children(): DN \"%s\" (%ld bytes) "
182 "exceeds max DN length (%d):\n",
183 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
188 Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n",
189 bi->has_children_query, 0, 0);
190 assert( bi->has_children_query );
191 rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
192 if ( rc != SQL_SUCCESS ) {
193 Debug( LDAP_DEBUG_TRACE,
194 "backsql_has_children(): error preparing SQL:\n%s",
195 bi->has_children_query, 0, 0);
196 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
197 SQLFreeStmt( sth, SQL_DROP );
201 rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
202 if ( rc != SQL_SUCCESS) {
204 Debug( LDAP_DEBUG_TRACE, "backsql_has_children(): "
205 "error binding dn=\"%s\" parameter:\n",
207 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
208 SQLFreeStmt( sth, SQL_DROP );
212 rc = SQLExecute( sth );
213 if ( rc != SQL_SUCCESS ) {
214 Debug( LDAP_DEBUG_TRACE, "backsql_has_children(): "
215 "error executing query (\"%s\", \"%s\"):\n",
216 bi->has_children_query, dn->bv_val, 0 );
217 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
218 SQLFreeStmt( sth, SQL_DROP );
222 backsql_BindRowAsStrings( sth, &row );
224 rc = SQLFetch( sth );
225 if ( BACKSQL_SUCCESS( rc ) ) {
226 if ( strtol( row.cols[ 0 ], NULL, 0 ) > 0 ) {
227 res = LDAP_COMPARE_TRUE;
229 res = LDAP_COMPARE_FALSE;
235 backsql_FreeRow( &row );
237 SQLFreeStmt( sth, SQL_DROP );
239 Debug( LDAP_DEBUG_TRACE, "<==backsql_has_children(): %s\n",
240 res == LDAP_COMPARE_TRUE ? "yes" : "no", 0, 0 );
246 backsql_get_attr_vals( backsql_at_map_rec *at, backsql_srch_info *bsi )
256 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
257 "oc='%s' attr='%s' keyval=%ld\n",
258 // bsi->oc->name.bv_val, at->name.bv_val,
259 bsi->oc->oc->soc_names[0], at->ad->ad_cname.bv_val,
260 bsi->c_eid->keyval );
262 rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
263 if ( rc != SQL_SUCCESS ) {
264 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
265 "error preparing query: %s\n", at->query, 0, 0 );
266 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
270 rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
271 if ( rc != SQL_SUCCESS ) {
272 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
273 "error binding key value parameter\n", 0, 0, 0 );
277 rc = SQLExecute( sth );
278 if ( ! BACKSQL_SUCCESS( rc ) ) {
279 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
280 "error executing attribute query '%s'\n",
282 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
283 SQLFreeStmt( sth, SQL_DROP );
287 backsql_BindRowAsStrings( sth, &row );
289 rc = SQLFetch( sth );
290 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
291 for ( i = 0; i < row.ncols; i++ ) {
292 if ( row.is_null[ i ] > 0 ) {
295 bv.bv_val = row.cols[ i ];
297 bv.bv_len = row.col_prec[ i ];
300 * FIXME: what if a binary
303 bv.bv_len = strlen( row.cols[ i ] );
305 backsql_entry_addattr( bsi->e,
306 &row.col_names[ i ], &bv );
309 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
310 (int)row.col_prec[ i ], 0, 0 );
312 Debug( LDAP_DEBUG_TRACE, "NULL value "
313 "in this row for attribute '%s'\n",
314 row.col_names[ i ].bv_val, 0, 0 );
315 #endif /* BACKSQL_TRACE */
320 backsql_FreeRow( &row );
321 SQLFreeStmt( sth, SQL_DROP );
322 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
328 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
331 backsql_at_map_rec *at;
333 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
335 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
337 rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
338 if ( rc != LDAP_SUCCESS ) {
342 bsi->oc = backsql_id2oc( bsi->bi, eid->oc_id );
348 /* if ( bsi->base_dn != NULL)??? */
352 if ( bsi->attrs != NULL ) {
353 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
354 "custom attribute list\n", 0, 0, 0 );
355 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
356 AttributeName *attr = &bsi->attrs[ i ];
358 if ( attr->an_desc == ad_oc
359 #if 0 /* FIXME: what is 0.10 ? */
360 || !BACKSQL_NCMP( &attr->an_name, &bv_n_0_10 )
364 backsql_entry_addattr( bsi->e,
371 at = backsql_ad2at( bsi->oc, attr->an_desc );
373 backsql_get_attr_vals( at, bsi );
375 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
376 "attribute '%s' is not defined "
377 "for objectlass '%s'\n",
378 attr->an_name.bv_val,
379 bsi->oc->name.bv_val, 0 );
384 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
385 "retrieving all attributes\n", 0, 0, 0 );
386 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
387 bsi, 0, AVL_INORDER );
390 if ( attr_merge_one( bsi->e, ad_oc, &bsi->oc->name ) ) {
395 if ( global_schemacheck ) {
396 const char *text = NULL;
397 char textbuf[ 1024 ];
398 size_t textlen = sizeof( textbuf );
399 struct berval bv[ 2 ] = { bsi->oc->name, { 0, NULL } };
401 AttributeDescription *ad_soc
402 = slap_schema.si_ad_structuralObjectClass;
404 int rc = structural_class( bv, &soc, NULL,
405 &text, textbuf, textlen );
406 if ( rc != LDAP_SUCCESS ) {
411 if ( bsi->attr_flags | BSQL_SF_ALL_OPER
412 || an_find( bsi->attrs, &AllOper ) ) {
413 if ( attr_merge_one( bsi->e, ad_soc, &soc ) ) {
420 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
425 #endif /* SLAPD_SQL */