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"
22 #include "schema-map.h"
27 backsql_free_entryID( backsql_entryID *id, int freeit )
29 backsql_entryID *next;
35 if ( id->dn.bv_val != NULL ) {
36 free( id->dn.bv_val );
59 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
63 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n",
68 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
69 Debug( LDAP_DEBUG_TRACE,
70 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
71 "exceeds max DN length (%d):\n",
72 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
77 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
78 assert( bi->id_query );
79 rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
80 if ( rc != SQL_SUCCESS ) {
81 Debug( LDAP_DEBUG_TRACE,
82 "backsql_dn2id(): error preparing SQL:\n%s",
84 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
85 SQLFreeStmt( sth, SQL_DROP );
89 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
91 * Prepare an upper cased, byte reversed version
92 * that can be searched using indexes
95 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
96 upperdn[ i ] = dn->bv_val[ j ];
99 ldap_pvt_str2upper( upperdn );
101 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
105 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
106 AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
107 ldap_pvt_str2upper( upperdn );
108 Debug( LDAP_DEBUG_TRACE,
109 "==>backsql_dn2id(): upperdn='%s'\n",
118 rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
119 if ( rc != SQL_SUCCESS) {
121 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
122 "error binding dn=\"%s\" parameter:\n",
124 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
125 SQLFreeStmt( sth, SQL_DROP );
129 rc = SQLExecute( sth );
130 if ( rc != SQL_SUCCESS ) {
131 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
132 "error executing query (\"%s\", \"%s\"):\n",
133 bi->id_query, toBind, 0 );
134 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
135 SQLFreeStmt( sth, SQL_DROP );
139 backsql_BindRowAsStrings( sth, &row );
140 rc = SQLFetch( sth );
141 if ( BACKSQL_SUCCESS( rc ) ) {
142 id->id = strtol( row.cols[ 0 ], NULL, 0 );
143 id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
144 id->oc_id = strtol( row.cols[ 2 ], NULL, 0 );
145 ber_dupbv( &id->dn, dn );
151 res = LDAP_NO_SUCH_OBJECT;
153 backsql_FreeRow( &row );
155 SQLFreeStmt( sth, SQL_DROP );
156 if ( res == LDAP_SUCCESS ) {
157 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
160 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
167 backsql_count_children(
171 unsigned long *nchildren )
176 int res = LDAP_SUCCESS;
178 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n",
181 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
182 Debug( LDAP_DEBUG_TRACE,
183 "backsql_count_children(): DN \"%s\" (%ld bytes) "
184 "exceeds max DN length (%d):\n",
185 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
190 Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n",
191 bi->has_children_query, 0, 0);
192 assert( bi->has_children_query );
193 rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
194 if ( rc != SQL_SUCCESS ) {
195 Debug( LDAP_DEBUG_TRACE,
196 "backsql_count_children(): error preparing SQL:\n%s",
197 bi->has_children_query, 0, 0);
198 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
199 SQLFreeStmt( sth, SQL_DROP );
203 rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
204 if ( rc != SQL_SUCCESS) {
206 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
207 "error binding dn=\"%s\" parameter:\n",
209 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
210 SQLFreeStmt( sth, SQL_DROP );
214 rc = SQLExecute( sth );
215 if ( rc != SQL_SUCCESS ) {
216 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
217 "error executing query (\"%s\", \"%s\"):\n",
218 bi->has_children_query, dn->bv_val, 0 );
219 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
220 SQLFreeStmt( sth, SQL_DROP );
224 backsql_BindRowAsStrings( sth, &row );
226 rc = SQLFetch( sth );
227 if ( BACKSQL_SUCCESS( rc ) ) {
230 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
231 if ( end[ 0 ] != '\0' ) {
238 backsql_FreeRow( &row );
240 SQLFreeStmt( sth, SQL_DROP );
242 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
249 backsql_has_children(
254 unsigned long nchildren;
257 rc = backsql_count_children( bi, dbh, dn, &nchildren );
259 if ( rc == LDAP_SUCCESS ) {
260 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
267 backsql_get_attr_vals( void *v_at, void *v_bsi )
269 backsql_at_map_rec *at = v_at;
270 backsql_srch_info *bsi = v_bsi;
271 backsql_info *bi = (backsql_info *)bsi->op->o_bd->be_private;
280 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
281 "oc='%s' attr='%s' keyval=%ld\n",
282 BACKSQL_OC_NAME( bsi->oc ), at->ad->ad_cname.bv_val,
283 bsi->c_eid->keyval );
285 rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
286 if ( rc != SQL_SUCCESS ) {
287 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
288 "error preparing query: %s\n", at->query, 0, 0 );
289 backsql_PrintErrors( bi->db_env, bsi->dbh, sth, rc );
293 rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
294 if ( rc != SQL_SUCCESS ) {
295 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
296 "error binding key value parameter\n", 0, 0, 0 );
300 rc = SQLExecute( sth );
301 if ( ! BACKSQL_SUCCESS( rc ) ) {
302 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
303 "error executing attribute query '%s'\n",
305 backsql_PrintErrors( bi->db_env, bsi->dbh, sth, rc );
306 SQLFreeStmt( sth, SQL_DROP );
310 backsql_BindRowAsStrings( sth, &row );
312 rc = SQLFetch( sth );
313 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
314 for ( i = 0; i < row.ncols; i++ ) {
315 if ( row.value_len[ i ] > 0 ) {
318 bv.bv_val = row.cols[ i ];
320 bv.bv_len = row.col_prec[ i ];
323 * FIXME: what if a binary
326 bv.bv_len = strlen( row.cols[ i ] );
328 backsql_entry_addattr( bsi->e,
329 &row.col_names[ i ], &bv,
330 bsi->op->o_tmpmemctx );
333 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
334 (int)row.col_prec[ i ], 0, 0 );
336 Debug( LDAP_DEBUG_TRACE, "NULL value "
337 "in this row for attribute '%s'\n",
338 row.col_names[ i ].bv_val, 0, 0 );
339 #endif /* BACKSQL_TRACE */
344 backsql_FreeRow( &row );
345 SQLFreeStmt( sth, SQL_DROP );
346 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
352 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
355 backsql_at_map_rec *at;
357 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
359 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
361 rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname,
362 bsi->op->o_tmpmemctx );
363 if ( rc != LDAP_SUCCESS ) {
367 bsi->oc = backsql_id2oc( bsi->op->o_bd->be_private, eid->oc_id );
375 if ( bsi->attrs != NULL ) {
376 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
377 "custom attribute list\n", 0, 0, 0 );
378 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
379 AttributeName *attr = &bsi->attrs[ i ];
381 if ( attr->an_desc == ad_oc ) {
385 at = backsql_ad2at( bsi->oc, attr->an_desc );
387 backsql_get_attr_vals( at, bsi );
389 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
390 "attribute '%s' is not defined "
391 "for objectlass '%s'\n",
392 attr->an_name.bv_val,
393 BACKSQL_OC_NAME( bsi->oc ), 0 );
398 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
399 "retrieving all attributes\n", 0, 0, 0 );
400 avl_apply( bsi->oc->attrs, backsql_get_attr_vals,
401 bsi, 0, AVL_INORDER );
404 if ( attr_merge_normalize_one( bsi->e, ad_oc, &bsi->oc->oc->soc_cname,
405 bsi->op->o_tmpmemctx ) ) {
410 if ( global_schemacheck ) {
411 const char *text = NULL;
412 char textbuf[ 1024 ];
413 size_t textlen = sizeof( textbuf );
414 struct berval bv[ 2 ];
418 bv[ 0 ] = bsi->oc->oc->soc_cname;
419 bv[ 1 ].bv_val = NULL;
421 rc = structural_class( bv, &soc, NULL,
422 &text, textbuf, textlen );
423 if ( rc != LDAP_SUCCESS ) {
428 if ( ( bsi->bsi_flags | BSQL_SF_ALL_OPER )
429 || an_find( bsi->attrs, &AllOper ) ) {
430 rc = attr_merge_normalize_one( bsi->e,
431 slap_schema.si_ad_structuralObjectClass,
432 &soc, bsi->op->o_tmpmemctx );
433 if ( rc != LDAP_SUCCESS ) {
440 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
445 #endif /* SLAPD_SQL */