From 634324d5b9fb22f3d6133ab44443194dd26cdc66 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sat, 10 Jan 2004 10:42:44 +0000 Subject: [PATCH] allow multiple attribute definitions; honor ldap_entry_objclasses in filter; major cleanup --- servers/slapd/back-sql/entry-id.c | 19 +- servers/slapd/back-sql/modify.c | 94 ++++----- servers/slapd/back-sql/schema-map.c | 305 +++++++++++++++++----------- servers/slapd/back-sql/schema-map.h | 52 +++-- servers/slapd/back-sql/search.c | 223 +++++++++++--------- 5 files changed, 405 insertions(+), 288 deletions(-) diff --git a/servers/slapd/back-sql/entry-id.c b/servers/slapd/back-sql/entry-id.c index bdb58b986c..19f3cdac77 100644 --- a/servers/slapd/back-sql/entry-id.c +++ b/servers/slapd/back-sql/entry-id.c @@ -290,13 +290,13 @@ backsql_get_attr_vals( void *v_at, void *v_bsi ) Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): " "oc='%s' attr='%s' keyval=%ld\n", - BACKSQL_OC_NAME( bsi->bsi_oc ), at->ad->ad_cname.bv_val, + BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val, bsi->bsi_c_eid->keyval ); - rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->query, 0 ); + rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): " - "error preparing query: %s\n", at->query, 0, 0 ); + "error preparing query: %s\n", at->bam_query, 0, 0 ); backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc ); return 1; } @@ -312,7 +312,7 @@ backsql_get_attr_vals( void *v_at, void *v_bsi ) if ( ! BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): " "error executing attribute query '%s'\n", - at->query, 0, 0 ); + at->bam_query, 0, 0 ); backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); return 1; @@ -356,6 +356,10 @@ backsql_get_attr_vals( void *v_at, void *v_bsi ) SQLFreeStmt( sth, SQL_DROP ); Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 ); + if ( at->bam_next ) { + return backsql_get_attr_vals( at->bam_next, v_bsi ); + } + return 1; } @@ -396,6 +400,7 @@ backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid ) at = backsql_ad2at( bsi->bsi_oc, attr->an_desc ); if ( at != NULL ) { backsql_get_attr_vals( at, bsi ); + } else { Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): " "attribute '%s' is not defined " @@ -408,12 +413,12 @@ backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid ) } else { Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): " "retrieving all attributes\n", 0, 0, 0 ); - avl_apply( bsi->bsi_oc->attrs, backsql_get_attr_vals, + avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals, bsi, 0, AVL_INORDER ); } if ( attr_merge_normalize_one( bsi->bsi_e, ad_oc, - &bsi->bsi_oc->oc->soc_cname, + &bsi->bsi_oc->bom_oc->soc_cname, bsi->bsi_op->o_tmpmemctx ) ) { entry_free( e ); return NULL; @@ -427,7 +432,7 @@ backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid ) struct berval soc; int rc; - bv[ 0 ] = bsi->bsi_oc->oc->soc_cname; + bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname; bv[ 1 ].bv_val = NULL; rc = structural_class( bv, &soc, NULL, diff --git a/servers/slapd/back-sql/modify.c b/servers/slapd/back-sql/modify.c index 2238d1a7ba..3901a7b907 100644 --- a/servers/slapd/back-sql/modify.c +++ b/servers/slapd/back-sql/modify.c @@ -124,15 +124,15 @@ backsql_modify_internal( Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "replacing values for attribute '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); - if ( at->add_proc == NULL ) { + if ( at->bam_add_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "add procedure is not defined " "for attribute '%s' " "- unable to perform replacements\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; @@ -144,13 +144,13 @@ backsql_modify_internal( break; } - if ( at->delete_proc == NULL ) { + if ( at->bam_delete_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "delete procedure is not defined " "for attribute '%s' " "- adding only\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; @@ -163,7 +163,7 @@ backsql_modify_internal( } del_all: - rc = backsql_Prepare( dbh, &asth, at->query, 0 ); + rc = backsql_Prepare( dbh, &asth, at->bam_query, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " @@ -222,7 +222,7 @@ del_all: rc = SQLFetch( asth ); for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( asth ) ) { for ( i = 0; i < row.ncols; i++ ) { - if ( BACKSQL_IS_DEL( at->expect_return ) ) { + if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) { pno = 1; SQLBindParameter(sth, 1, SQL_PARAM_OUTPUT, @@ -232,7 +232,7 @@ del_all: } else { pno = 0; } - po = ( BACKSQL_IS_DEL( at->param_order ) ) > 0; + po = ( BACKSQL_IS_DEL( at->bam_param_order ) ) > 0; SQLBindParameter( sth, pno + 1 + po, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, @@ -251,9 +251,9 @@ del_all: Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "executing '%s'\n", - at->delete_proc, 0, 0 ); + at->bam_delete_proc, 0, 0 ); rc = SQLExecDirect( sth, - at->delete_proc, SQL_NTS ); + at->bam_delete_proc, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " @@ -278,19 +278,19 @@ del_all: backsql_FreeRow( &row ); SQLFreeStmt( asth, SQL_DROP ); } - + /* * PASSTHROUGH - to add new attributes -- do NOT add break */ case LDAP_MOD_ADD: case SLAP_MOD_SOFTADD: add_only:; - if ( at->add_proc == NULL ) { + if ( at->bam_add_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "add procedure is not defined " "for attribute '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; @@ -304,11 +304,11 @@ add_only:; Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "adding new values for attribute '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); for ( i = 0, at_val = c_mod->sm_bvalues; at_val->bv_val != NULL; i++, at_val++ ) { - if ( BACKSQL_IS_ADD( at->expect_return ) ) { + if ( BACKSQL_IS_ADD( at->bam_expect_return ) ) { pno = 1; SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, @@ -317,7 +317,7 @@ add_only:; } else { pno = 0; } - po = ( BACKSQL_IS_ADD( at->param_order ) ) > 0; + po = ( BACKSQL_IS_ADD( at->bam_param_order ) ) > 0; SQLBindParameter( sth, pno + 1 + po, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, @@ -336,8 +336,8 @@ add_only:; Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "executing '%s'\n", - at->add_proc, 0, 0 ); - rc = SQLExecDirect( sth, at->add_proc, + at->bam_add_proc, 0, 0 ); + rc = SQLExecDirect( sth, at->bam_add_proc, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, @@ -361,12 +361,12 @@ add_only:; break; case LDAP_MOD_DELETE: - if ( at->delete_proc == NULL ) { + if ( at->bam_delete_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "delete procedure is not defined " "for attribute '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; @@ -384,18 +384,18 @@ add_only:; "no values given to delete " "for attribute '%s' " "-- deleting all values\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); goto del_all; } Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "deleting values for attribute '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); for ( i = 0, at_val = c_mod->sm_bvalues; at_val->bv_val != NULL; i++, at_val++ ) { - if ( BACKSQL_IS_DEL( at->expect_return ) ) { + if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) { pno = 1; SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, @@ -404,7 +404,7 @@ add_only:; } else { pno = 0; } - po = ( BACKSQL_IS_DEL( at->param_order ) ) > 0; + po = ( BACKSQL_IS_DEL( at->bam_param_order ) ) > 0; SQLBindParameter( sth, pno + 1 + po, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, @@ -422,8 +422,8 @@ add_only:; Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): " "executing '%s'\n", - at->delete_proc, 0, 0 ); - rc = SQLExecDirect( sth, at->delete_proc, + at->bam_delete_proc, 0, 0 ); + rc = SQLExecDirect( sth, at->bam_delete_proc, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, @@ -946,7 +946,7 @@ backsql_add( Operation *op, SlapReply *rs ) return 1; } - if ( oc->create_proc == NULL ) { + if ( oc->bom_create_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "create procedure is not defined for this objectclass " "- aborting\n", 0, 0, 0 ); @@ -956,7 +956,7 @@ backsql_add( Operation *op, SlapReply *rs ) return 1; } else if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) - && oc->create_keyval == NULL ) { + && oc->bom_create_keyval == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "create procedure needs select procedure, " "but none is defined - aborting\n", 0, 0, 0 ); @@ -1064,14 +1064,14 @@ backsql_add( Operation *op, SlapReply *rs ) return 1; } - if ( BACKSQL_IS_ADD( oc->expect_return ) ) { + if ( BACKSQL_IS_ADD( oc->bom_expect_return ) ) { SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &new_keyval, 0, 0 ); } Debug( LDAP_DEBUG_TRACE, "backsql_add(): executing '%s'\n", - oc->create_proc, 0, 0 ); - rc = SQLExecDirect( sth, oc->create_proc, SQL_NTS ); + oc->bom_create_proc, 0, 0 ); + rc = SQLExecDirect( sth, oc->bom_create_proc, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "create_proc execution failed\n", 0, 0, 0 ); @@ -1086,7 +1086,7 @@ backsql_add( Operation *op, SlapReply *rs ) SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK ); } - if ( !BACKSQL_IS_ADD( oc->expect_return ) ) { + if ( !BACKSQL_IS_ADD( oc->bom_expect_return ) ) { SWORD ncols; SQLINTEGER value_len; @@ -1104,7 +1104,7 @@ backsql_add( Operation *op, SlapReply *rs ) } #endif /* BACKSQL_REALLOC_STMT */ - rc = SQLExecDirect( sth, oc->create_keyval, SQL_NTS ); + rc = SQLExecDirect( sth, oc->bom_create_keyval, SQL_NTS ); if ( rc != SQL_SUCCESS ) { rs->sr_err = LDAP_OTHER; rs->sr_text = "SQL-backend error"; @@ -1226,7 +1226,7 @@ backsql_add( Operation *op, SlapReply *rs ) continue; } - if ( at_rec->add_proc == NULL ) { + if ( at_rec->bam_add_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "add procedure is not defined " "for attribute '%s'\n", @@ -1244,7 +1244,7 @@ backsql_add( Operation *op, SlapReply *rs ) } #ifdef BACKSQL_REALLOC_STMT - rc = backsql_Prepare( dbh, &sth, at_rec->add_proc, 0 ); + rc = backsql_Prepare( dbh, &sth, at_rec->bam_add_proc, 0 ); if ( rc != SQL_SUCCESS ) { if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { @@ -1258,7 +1258,7 @@ backsql_add( Operation *op, SlapReply *rs ) } #endif /* BACKSQL_REALLOC_STMT */ - if ( BACKSQL_IS_ADD( at_rec->expect_return ) ) { + if ( BACKSQL_IS_ADD( at_rec->bam_expect_return ) ) { pno = 1; SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG, SQL_INTEGER, @@ -1267,7 +1267,7 @@ backsql_add( Operation *op, SlapReply *rs ) pno = 0; } - po = ( BACKSQL_IS_ADD( at_rec->param_order ) ) > 0; + po = ( BACKSQL_IS_ADD( at_rec->bam_param_order ) ) > 0; currpos = pno + 1 + po; SQLBindParameter( sth, currpos, SQL_PARAM_INPUT, SQL_C_ULONG, @@ -1283,7 +1283,7 @@ backsql_add( Operation *op, SlapReply *rs ) * to build the entry */ if ( at->a_desc == slap_schema.si_ad_objectClass ) { - if ( bvmatch( at_val, &oc->oc->soc_cname ) ) { + if ( bvmatch( at_val, &oc->bom_oc->soc_cname ) ) { continue; } } @@ -1298,14 +1298,14 @@ backsql_add( Operation *op, SlapReply *rs ) #ifdef SECURITY_PARANOID Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "executing '%s', id=%ld\n", - at_rec->add_proc, new_keyval, 0 ); + at_rec->bam_add_proc, new_keyval, 0 ); #else Debug( LDAP_DEBUG_TRACE, "backsql_add(): " "executing '%s' with val='%s', id=%ld\n", - at_rec->add_proc, at_val->bv_val, new_keyval ); + at_rec->bam_add_proc, at_val->bv_val, new_keyval ); #endif #ifndef BACKSQL_REALLOC_STMT - rc = SQLExecDirect( sth, at_rec->add_proc, SQL_NTS ); + rc = SQLExecDirect( sth, at_rec->bam_add_proc, SQL_NTS ); #else /* BACKSQL_REALLOC_STMT */ rc = SQLExecute( sth ); #endif /* BACKSQL_REALLOC_STMT */ @@ -1343,7 +1343,7 @@ backsql_add( Operation *op, SlapReply *rs ) backsql_BindParamStr( sth, 1, op->oq_add.rs_e->e_name.bv_val, BACKSQL_MAX_DN_LEN ); SQLBindParameter( sth, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, - 0, 0, &oc->id, 0, 0 ); + 0, 0, &oc->bom_id, 0, 0 ); SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &parent_id.id, 0, 0 ); SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, @@ -1352,7 +1352,7 @@ backsql_add( Operation *op, SlapReply *rs ) Debug( LDAP_DEBUG_TRACE, "backsql_add(): executing '%s' for dn '%s'\n", bi->insentry_query, op->oq_add.rs_e->e_name.bv_val, 0 ); Debug( LDAP_DEBUG_TRACE, " for oc_map_id=%ld, parent_id=%ld, " - "keyval=%ld\n", oc->id, parent_id.id, new_keyval ); + "keyval=%ld\n", oc->bom_id, parent_id.id, new_keyval ); #ifndef BACKSQL_REALLOC_STMT rc = SQLExecDirect( sth, bi->insentry_query, SQL_NTS ); #else /* BACKSQL_REALLOC_STMT */ @@ -1472,7 +1472,7 @@ backsql_delete( Operation *op, SlapReply *rs ) return 1; } - if ( oc->delete_proc == NULL ) { + if ( oc->bom_delete_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_delete(): " "delete procedure is not defined " "for this objectclass - aborting\n", 0, 0, 0 ); @@ -1483,7 +1483,7 @@ backsql_delete( Operation *op, SlapReply *rs ) } SQLAllocStmt( dbh, &sth ); - if ( BACKSQL_IS_DEL( oc->expect_return ) ) { + if ( BACKSQL_IS_DEL( oc->bom_expect_return ) ) { pno = 1; SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &rc, 0, 0 ); @@ -1495,8 +1495,8 @@ backsql_delete( Operation *op, SlapReply *rs ) SQL_C_ULONG, SQL_INTEGER, 0, 0, &e_id.keyval, 0, 0 ); Debug( LDAP_DEBUG_TRACE, "backsql_delete(): executing '%s'\n", - oc->delete_proc, 0, 0 ); - rc = SQLExecDirect( sth, oc->delete_proc, SQL_NTS ); + oc->bom_delete_proc, 0, 0 ); + rc = SQLExecDirect( sth, oc->bom_delete_proc, SQL_NTS ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_delete(): " "delete_proc execution failed\n", 0, 0, 0 ); diff --git a/servers/slapd/back-sql/schema-map.c b/servers/slapd/back-sql/schema-map.c index 9c2d62280f..1d9c85c5e3 100644 --- a/servers/slapd/back-sql/schema-map.c +++ b/servers/slapd/back-sql/schema-map.c @@ -40,14 +40,16 @@ static int backsql_cmp_oc( const void *v_m1, const void *v_m2 ) { const backsql_oc_map_rec *m1 = v_m1, *m2 = v_m2; - return SLAP_PTRCMP( m1->oc, m2->oc ); + + return SLAP_PTRCMP( m1->bom_oc, m2->bom_oc ); } static int backsql_cmp_oc_id( const void *v_m1, const void *v_m2 ) { const backsql_oc_map_rec *m1 = v_m1, *m2 = v_m2; - return ( m1->id < m2->id ? -1 : ( m1->id > m2->id ? 1 : 0 ) ); + + return ( m1->bom_id < m2->bom_id ? -1 : ( m1->bom_id > m2->bom_id ? 1 : 0 ) ); } /* @@ -57,7 +59,32 @@ static int backsql_cmp_attr( const void *v_m1, const void *v_m2 ) { const backsql_at_map_rec *m1 = v_m1, *m2 = v_m2; - return SLAP_PTRCMP( m1->ad, m2->ad ); + + return SLAP_PTRCMP( m1->bam_ad, m2->bam_ad ); +} + +/* + * Dummy duplicate handling function (required by avl_insert) + */ +#define BACKSQL_DUPLICATE (-1) +static int +backsql_dup_dummy(void *p1, void *p2) +{ + return BACKSQL_DUPLICATE; +} + +int +backsql_dup_attr( void *v_m1, void *v_m2 ) +{ + backsql_at_map_rec *m1 = v_m1, *m2 = v_m2; + + assert( m1->bam_ad == m2->bam_ad ); + + for ( ; m1->bam_next ; m1 = m1->bam_next ); + m1->bam_next = m2; + m2->bam_next = NULL; + + return BACKSQL_DUPLICATE; } static int @@ -69,24 +96,24 @@ backsql_make_attr_query( backsql_strfcat( &bb, "lblblblbcbl", (ber_len_t)sizeof( "SELECT " ) - 1, "SELECT ", - &at_map->sel_expr, + &at_map->bam_sel_expr, (ber_len_t)sizeof( " AS " ) - 1, " AS ", - &at_map->ad->ad_cname, + &at_map->bam_ad->ad_cname, (ber_len_t)sizeof( " FROM " ) - 1, " FROM ", - &at_map->from_tbls, + &at_map->bam_from_tbls, (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ", - &oc_map->keytbl, + &oc_map->bom_keytbl, '.', - &oc_map->keycol, + &oc_map->bom_keycol, (ber_len_t)sizeof( "=?" ) - 1, "=?" ); - if ( at_map->join_where.bv_val != NULL ) { + if ( at_map->bam_join_where.bv_val != NULL ) { backsql_strfcat( &bb, "lb", (ber_len_t)sizeof( " AND ") - 1, " AND ", - &at_map->join_where ); + &at_map->bam_join_where ); } - at_map->query = bb.bb_val.bv_val; + at_map->bam_query = bb.bb_val.bv_val; return 0; } @@ -100,20 +127,21 @@ backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) struct berbuf bb; - snprintf( s, sizeof( s ), "%ld", oc_map->id ); + snprintf( s, sizeof( s ), "%ld", oc_map->bom_id ); slen = strlen( s ); at_map = (backsql_at_map_rec *)ch_calloc(1, sizeof( backsql_at_map_rec ) ); - at_map->ad = slap_schema.si_ad_objectClass; - ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1, &at_map->sel_expr ); + at_map->bam_ad = slap_schema.si_ad_objectClass; + ber_str2bv( "ldap_entry_objclasses.oc_name", 0, 1, + &at_map->bam_sel_expr ); ber_str2bv( "ldap_entry_objclasses,ldap_entries", 0, 1, - &at_map->from_tbls ); + &at_map->bam_from_tbls ); - bb.bb_len = at_map->from_tbls.bv_len + 1; - bb.bb_val = at_map->from_tbls; - backsql_merge_from_clause( &bb, &oc_map->keytbl ); - at_map->from_tbls = bb.bb_val; + bb.bb_len = at_map->bam_from_tbls.bv_len + 1; + bb.bb_val = at_map->bam_from_tbls; + backsql_merge_from_clause( &bb, &oc_map->bom_keytbl ); + at_map->bam_from_tbls = bb.bb_val; bb.bb_val.bv_val = NULL; bb.bb_val.bv_len = 0; @@ -121,33 +149,40 @@ backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) backsql_strfcat( &bb, "lbcbll", (ber_len_t)sizeof( "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=" ) - 1, "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=", - &oc_map->keytbl, + &oc_map->bom_keytbl, '.', - &oc_map->keycol, + &oc_map->bom_keycol, (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1, " and ldap_entries.oc_map_id=", slen, s ); - at_map->join_where = bb.bb_val; - at_map->add_proc = NULL; - at_map->delete_proc = NULL; - at_map->param_order = 0; - at_map->expect_return = 0; + at_map->bam_oc = oc_map->bom_oc; + at_map->bam_join_where = bb.bb_val; + at_map->bam_add_proc = NULL; + at_map->bam_delete_proc = NULL; + at_map->bam_param_order = 0; + at_map->bam_expect_return = 0; + at_map->bam_next = NULL; backsql_make_attr_query( oc_map, at_map ); - avl_insert( &oc_map->attrs, at_map, backsql_cmp_attr, NULL ); + if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) { + Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): " + "duplicate attribute \"%s\" in objectClass \"%s\" map\n", + at_map->bam_ad->ad_cname.bv_val, + oc_map->bom_oc->soc_cname.bv_val, 0 ); + } at_map = (backsql_at_map_rec *)ch_calloc( 1, sizeof( backsql_at_map_rec ) ); - at_map->ad = slap_schema.si_ad_ref; - ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->sel_expr ); - ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->from_tbls ); + at_map->bam_ad = slap_schema.si_ad_ref; + ber_str2bv( "ldap_referrals.url", 0, 1, &at_map->bam_sel_expr ); + ber_str2bv( "ldap_referrals,ldap_entries", 0, 1, &at_map->bam_from_tbls ); bb.bb_val.bv_val = NULL; bb.bb_val.bv_len = 0; - bb.bb_len = at_map->from_tbls.bv_len + 1; - backsql_merge_from_clause( &bb, &oc_map->keytbl ); - at_map->from_tbls = bb.bb_val; + bb.bb_len = at_map->bam_from_tbls.bv_len + 1; + backsql_merge_from_clause( &bb, &oc_map->bom_keytbl ); + at_map->bam_from_tbls = bb.bb_val; bb.bb_val.bv_val = NULL; bb.bb_val.bv_len = 0; @@ -155,21 +190,28 @@ backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) backsql_strfcat( &bb, "lbcbll", (ber_len_t)sizeof( "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=" ) - 1, "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=", - &oc_map->keytbl, + &oc_map->bom_keytbl, '.', - &oc_map->keycol, + &oc_map->bom_keycol, (ber_len_t)sizeof( " and ldap_entries.oc_map_id=" ) - 1, " and ldap_entries.oc_map_id=", slen, s ); - at_map->join_where = bb.bb_val; - at_map->add_proc = NULL; - at_map->delete_proc = NULL; - at_map->param_order = 0; - at_map->expect_return = 0; + at_map->bam_oc = NULL; + at_map->bam_join_where = bb.bb_val; + at_map->bam_add_proc = NULL; + at_map->bam_delete_proc = NULL; + at_map->bam_param_order = 0; + at_map->bam_expect_return = 0; + at_map->bam_next = NULL; backsql_make_attr_query( oc_map, at_map ); - avl_insert( &oc_map->attrs, at_map, backsql_cmp_attr, NULL ); + if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) { + Debug( LDAP_DEBUG_TRACE, "backsql_add_sysmaps(): " + "duplicate attribute \"%s\" in objectClass \"%s\" map\n", + at_map->bam_ad->ad_cname.bv_val, + oc_map->bom_oc->soc_cname.bv_val, 0 ); + } return 1; } @@ -250,30 +292,30 @@ backsql_load_schema_map( backsql_info *si, SQLHDBC dbh ) oc_map = (backsql_oc_map_rec *)ch_calloc( 1, sizeof( backsql_oc_map_rec ) ); - oc_map->id = strtol( oc_row.cols[ 0 ], NULL, 0 ); + oc_map->bom_id = strtol( oc_row.cols[ 0 ], NULL, 0 ); - oc_map->oc = oc_find( oc_row.cols[ 1 ] ); - if ( oc_map->oc == NULL ) { + oc_map->bom_oc = oc_find( oc_row.cols[ 1 ] ); + if ( oc_map->bom_oc == NULL ) { Debug( LDAP_DEBUG_TRACE, "load_schema_map(): " "objectClass '%s' is not defined in schema\n", oc_row.cols[ 1 ], 0, 0 ); return LDAP_OTHER; /* undefined objectClass ? */ } - ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->keytbl ); - ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->keycol ); - oc_map->create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL + ber_str2bv( oc_row.cols[ 2 ], 0, 1, &oc_map->bom_keytbl ); + ber_str2bv( oc_row.cols[ 3 ], 0, 1, &oc_map->bom_keycol ); + oc_map->bom_create_proc = ( oc_row.value_len[ 4 ] < 0 ) ? NULL : ch_strdup( oc_row.cols[ 4 ] ); colnum = 5; if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) { colnum = 6; - oc_map->create_keyval = ( oc_row.value_len[ 5 ] < 0 ) + oc_map->bom_create_keyval = ( oc_row.value_len[ 5 ] < 0 ) ? NULL : ch_strdup( oc_row.cols[ 5 ] ); } - oc_map->delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL + oc_map->bom_delete_proc = ( oc_row.value_len[ colnum ] < 0 ) ? NULL : ch_strdup( oc_row.cols[ colnum ] ); - oc_map->expect_return = strtol( oc_row.cols[ colnum + 1 ], + oc_map->bom_expect_return = strtol( oc_row.cols[ colnum + 1 ], NULL, 0 ); /* @@ -281,30 +323,40 @@ backsql_load_schema_map( backsql_info *si, SQLHDBC dbh ) * instructions in {create|delete}_proc */ - oc_map->attrs = NULL; - avl_insert( &si->oc_by_oc, oc_map, backsql_cmp_oc, NULL ); - avl_insert( &si->oc_by_id, oc_map, backsql_cmp_oc_id, NULL ); - oc_id = oc_map->id; + oc_map->bom_attrs = NULL; + if ( avl_insert( &si->oc_by_oc, oc_map, backsql_cmp_oc, backsql_dup_dummy ) == BACKSQL_DUPLICATE ) { + Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): " + "duplicate objectClass \"%s\" in objectClass map\n", + oc_map->bom_oc->soc_cname.bv_val, 0, 0 ); + return LDAP_OTHER; + } + if ( avl_insert( &si->oc_by_id, oc_map, backsql_cmp_oc_id, backsql_dup_dummy ) == BACKSQL_DUPLICATE ) { + Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): " + "duplicate objectClass \"%s\" in objectClass by ID map\n", + oc_map->bom_oc->soc_cname.bv_val, 0, 0 ); + return LDAP_OTHER; + } + oc_id = oc_map->bom_id; Debug( LDAP_DEBUG_TRACE, "load_schema_map(): " "objectClass '%s': keytbl='%s' keycol='%s'\n", BACKSQL_OC_NAME( oc_map ), - oc_map->keytbl.bv_val, oc_map->keycol.bv_val ); - if ( oc_map->create_proc ) { + oc_map->bom_keytbl.bv_val, oc_map->bom_keycol.bv_val ); + if ( oc_map->bom_create_proc ) { Debug( LDAP_DEBUG_TRACE, "create_proc='%s'\n", - oc_map->create_proc, 0, 0 ); + oc_map->bom_create_proc, 0, 0 ); } - if ( oc_map->create_keyval ) { + if ( oc_map->bom_create_keyval ) { Debug( LDAP_DEBUG_TRACE, "create_keyval='%s'\n", - oc_map->create_keyval, 0, 0 ); + oc_map->bom_create_keyval, 0, 0 ); } - if ( oc_map->delete_proc ) { + if ( oc_map->bom_delete_proc ) { Debug( LDAP_DEBUG_TRACE, "delete_proc='%s'\n", - oc_map->delete_proc, 0, 0 ); + oc_map->bom_delete_proc, 0, 0 ); } Debug( LDAP_DEBUG_TRACE, "expect_return: " "add=%d, del=%d; attributes:\n", - BACKSQL_IS_ADD( oc_map->expect_return ), - BACKSQL_IS_DEL( oc_map->expect_return ), 0 ); + BACKSQL_IS_ADD( oc_map->bom_expect_return ), + BACKSQL_IS_DEL( oc_map->bom_expect_return ), 0 ); Debug( LDAP_DEBUG_TRACE, "load_schema_map(): " "autoadding 'objectClass' and 'ref' mappings\n", @@ -342,7 +394,7 @@ backsql_load_schema_map( backsql_info *si, SQLHDBC dbh ) at_map = (backsql_at_map_rec *)ch_calloc( 1, sizeof( backsql_at_map_rec ) ); rc = slap_str2ad( at_row.cols[ 0 ], - &at_map->ad, &text ); + &at_map->bam_ad, &text ); if ( rc != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "load_schema_map(): " "attribute '%s' for objectClass '%s' " @@ -351,44 +403,50 @@ backsql_load_schema_map( backsql_info *si, SQLHDBC dbh ) BACKSQL_OC_NAME( oc_map ), text ); return LDAP_CONSTRAINT_VIOLATION; } - - ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->sel_expr ); + + ber_str2bv( at_row.cols[ 1 ], 0, 1, &at_map->bam_sel_expr ); if ( at_row.value_len[ 8 ] < 0 ) { - at_map->sel_expr_u.bv_val = NULL; - at_map->sel_expr_u.bv_len = 0; + at_map->bam_sel_expr_u.bv_val = NULL; + at_map->bam_sel_expr_u.bv_len = 0; } else { ber_str2bv( at_row.cols[ 8 ], 0, 1, - &at_map->sel_expr_u ); + &at_map->bam_sel_expr_u ); } ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv ); backsql_merge_from_clause( &bb, &bv ); - at_map->from_tbls = bb.bb_val; + at_map->bam_from_tbls = bb.bb_val; if ( at_row.value_len[ 3 ] < 0 ) { - at_map->join_where.bv_val = NULL; - at_map->join_where.bv_len = 0; + at_map->bam_join_where.bv_val = NULL; + at_map->bam_join_where.bv_len = 0; } else { ber_str2bv( at_row.cols[ 3 ], 0, 1, - &at_map->join_where ); + &at_map->bam_join_where ); } - at_map->add_proc = NULL; + at_map->bam_add_proc = NULL; if ( at_row.value_len[ 4 ] > 0 ) { - at_map->add_proc = ch_strdup( at_row.cols[4] ); + at_map->bam_add_proc = ch_strdup( at_row.cols[4] ); } - at_map->delete_proc = NULL; + at_map->bam_delete_proc = NULL; if ( at_row.value_len[ 5 ] > 0 ) { - at_map->delete_proc - = ch_strdup( at_row.cols[ 5 ] ); + at_map->bam_delete_proc = ch_strdup( at_row.cols[ 5 ] ); } - at_map->param_order = strtol( at_row.cols[ 6 ], + at_map->bam_param_order = strtol( at_row.cols[ 6 ], NULL, 0 ); - at_map->expect_return = strtol( at_row.cols[ 7 ], + at_map->bam_expect_return = strtol( at_row.cols[ 7 ], NULL, 0 ); backsql_make_attr_query( oc_map, at_map ); Debug( LDAP_DEBUG_TRACE, "load_schema_map(): " "preconstructed query '%s'\n", - at_map->query, 0, 0 ); - avl_insert( &oc_map->attrs, at_map, backsql_cmp_attr, NULL ); + at_map->bam_query, 0, 0 ); + at_map->bam_next = NULL; + if ( avl_insert( &oc_map->bom_attrs, at_map, backsql_cmp_attr, backsql_dup_attr ) == BACKSQL_DUPLICATE ) { + Debug( LDAP_DEBUG_TRACE, "backsql_load_schema_map(): " + "duplicate attribute \"%s\" " + "in objectClass \"%s\" map\n", + at_map->bam_ad->ad_cname.bv_val, + oc_map->bom_oc->soc_cname.bv_val, 0 ); + } } backsql_FreeRow( &at_row ); SQLFreeStmt( at_sth, SQL_CLOSE ); @@ -412,7 +470,7 @@ backsql_oc2oc( backsql_info *si, ObjectClass *oc ) objclass, 0, 0 ); #endif /* BACKSQL_TRACE */ - tmp.oc = oc; + tmp.bom_oc = oc; res = (backsql_oc_map_rec *)avl_find( si->oc_by_oc, &tmp, backsql_cmp_oc ); #ifdef BACKSQL_TRACE if ( res != NULL ) { @@ -439,8 +497,8 @@ backsql_name2oc( backsql_info *si, struct berval *oc_name ) objclass, 0, 0 ); #endif /* BACKSQL_TRACE */ - tmp.oc = oc_bvfind( oc_name ); - if ( tmp.oc == NULL ) { + tmp.bom_oc = oc_bvfind( oc_name ); + if ( tmp.bom_oc == NULL ) { return NULL; } @@ -449,7 +507,7 @@ backsql_name2oc( backsql_info *si, struct berval *oc_name ) if ( res != NULL ) { Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): " "found name='%s', id=%d\n", - BACKSQL_OC_NAME( res ), res->id, 0 ); + BACKSQL_OC_NAME( res ), res->bom_id, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): " "not found\n", 0, 0, 0 ); @@ -469,7 +527,7 @@ backsql_id2oc( backsql_info *si, unsigned long id ) "searching for objectclass with id='%d'\n", id, 0, 0 ); #endif /* BACKSQL_TRACE */ - tmp.id = id; + tmp.bom_id = id; res = (backsql_oc_map_rec *)avl_find( si->oc_by_id, &tmp, backsql_cmp_oc_id ); @@ -477,7 +535,7 @@ backsql_id2oc( backsql_info *si, unsigned long id ) if ( res != NULL ) { Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): " "found name='%s', id=%d\n", - BACKSQL_OC_NAME( res ), res->id, 0 ); + BACKSQL_OC_NAME( res ), res->bom_id, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<==oc_with_name(): " "not found\n", 0, 0, 0 ); @@ -498,15 +556,16 @@ backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad ) attr, BACKSQL_OC_NAME( objclass ), 0 ); #endif /* BACKSQL_TRACE */ - tmp.ad = ad; - res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp, + tmp.bam_ad = ad; + res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp, backsql_cmp_attr ); #ifdef BACKSQL_TRACE if ( res != NULL ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): " "found name='%s', sel_expr='%s'\n", - res->ad->ad_cname.bv_val, res->sel_expr.bv_val, 0 ); + res->bam_ad->ad_cname.bv_val, + res->bam_sel_expr.bv_val, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<==backsql_ad2at(): " "not found\n", 0, 0, 0 ); @@ -516,6 +575,7 @@ backsql_ad2at( backsql_oc_map_rec* objclass, AttributeDescription *ad ) return res; } +#if 0 /* * Deprecated */ @@ -531,18 +591,18 @@ backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr ) attr, BACKSQL_OC_NAME( objclass ), 0 ); #endif /* BACKSQL_TRACE */ - if ( slap_bv2ad( attr, &tmp.ad, &text ) != LDAP_SUCCESS ) { + if ( slap_bv2ad( attr, &tmp.bam_ad, &text ) != LDAP_SUCCESS ) { return NULL; } - res = (backsql_at_map_rec *)avl_find( objclass->attrs, &tmp, + res = (backsql_at_map_rec *)avl_find( objclass->bom_attrs, &tmp, backsql_cmp_attr ); #ifdef BACKSQL_TRACE if ( res != NULL ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): " "found name='%s', sel_expr='%s'\n", - res->name, res->sel_expr.bv_val, 0 ); + res->bam_name, res->bam_sel_expr.bv_val, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<==backsql_name2at(): " "not found\n", 0, 0, 0 ); @@ -551,33 +611,39 @@ backsql_name2at( backsql_oc_map_rec* objclass, struct berval *attr ) return res; } +#endif static void backsql_free_attr( void *v_at ) { - backsql_at_map_rec *at = v_at; + backsql_at_map_rec *at = v_at; + Debug( LDAP_DEBUG_TRACE, "==>free_attr(): '%s'\n", - at->ad->ad_cname.bv_val, 0, 0 ); - ch_free( at->sel_expr.bv_val ); - if ( at->from_tbls.bv_val != NULL ) { - ch_free( at->from_tbls.bv_val ); + at->bam_ad->ad_cname.bv_val, 0, 0 ); + ch_free( at->bam_sel_expr.bv_val ); + if ( at->bam_from_tbls.bv_val != NULL ) { + ch_free( at->bam_from_tbls.bv_val ); } - if ( at->join_where.bv_val != NULL ) { - ch_free( at->join_where.bv_val ); + if ( at->bam_join_where.bv_val != NULL ) { + ch_free( at->bam_join_where.bv_val ); } - if ( at->add_proc != NULL ) { - ch_free( at->add_proc ); + if ( at->bam_add_proc != NULL ) { + ch_free( at->bam_add_proc ); } - if ( at->delete_proc != NULL ) { - ch_free( at->delete_proc ); + if ( at->bam_delete_proc != NULL ) { + ch_free( at->bam_delete_proc ); } - if ( at->query ) { - ch_free( at->query ); + if ( at->bam_query ) { + ch_free( at->bam_query ); } /* TimesTen */ - if ( at->sel_expr_u.bv_val ) { - ch_free( at->sel_expr_u.bv_val ); + if ( at->bam_sel_expr_u.bv_val ) { + ch_free( at->bam_sel_expr_u.bv_val ); + } + + if ( at->bam_next ) { + backsql_free_attr( at->bam_next ); } ch_free( at ); @@ -588,20 +654,21 @@ backsql_free_attr( void *v_at ) static void backsql_free_oc( void *v_oc ) { - backsql_oc_map_rec *oc = v_oc; + backsql_oc_map_rec *oc = v_oc; + Debug( LDAP_DEBUG_TRACE, "==>free_oc(): '%s'\n", BACKSQL_OC_NAME( oc ), 0, 0 ); - avl_free( oc->attrs, backsql_free_attr ); - ch_free( oc->keytbl.bv_val ); - ch_free( oc->keycol.bv_val ); - if ( oc->create_proc != NULL ) { - ch_free( oc->create_proc ); + avl_free( oc->bom_attrs, backsql_free_attr ); + ch_free( oc->bom_keytbl.bv_val ); + ch_free( oc->bom_keycol.bv_val ); + if ( oc->bom_create_proc != NULL ) { + ch_free( oc->bom_create_proc ); } - if ( oc->create_keyval != NULL ) { - ch_free( oc->create_keyval ); + if ( oc->bom_create_keyval != NULL ) { + ch_free( oc->bom_create_keyval ); } - if ( oc->delete_proc != NULL ) { - ch_free( oc->delete_proc ); + if ( oc->bom_delete_proc != NULL ) { + ch_free( oc->bom_delete_proc ); } ch_free( oc ); diff --git a/servers/slapd/back-sql/schema-map.h b/servers/slapd/back-sql/schema-map.h index b9c8aa4625..f9add2d7c8 100644 --- a/servers/slapd/back-sql/schema-map.h +++ b/servers/slapd/back-sql/schema-map.h @@ -21,58 +21,66 @@ #ifndef __BACKSQL_SCHEMA_MAP_H__ #define __BACKSQL_SCHEMA_MAP_H__ -typedef struct { +typedef struct backsql_oc_map_rec { /* * Structure of corresponding LDAP objectClass definition */ - ObjectClass *oc; -#define BACKSQL_OC_NAME(ocmap) ((ocmap)->oc->soc_cname.bv_val) + ObjectClass *bom_oc; +#define BACKSQL_OC_NAME(ocmap) ((ocmap)->bom_oc->soc_cname.bv_val) - struct berval keytbl; - struct berval keycol; + struct berval bom_keytbl; + struct berval bom_keycol; /* expected to return keyval of newly created entry */ - char *create_proc; + char *bom_create_proc; /* in case create_proc does not return the keyval of the newly * created row */ - char *create_keyval; + char *bom_create_keyval; /* supposed to expect keyval as parameter and delete * all the attributes as well */ - char *delete_proc; + char *bom_delete_proc; /* flags whether delete_proc is a function (whether back-sql * should bind first parameter as output for return code) */ - int expect_return; - unsigned long id; - Avlnode *attrs; + int bom_expect_return; + unsigned long bom_id; + Avlnode *bom_attrs; } backsql_oc_map_rec; -typedef struct { +typedef struct backsql_at_map_rec { /* Description of corresponding LDAP attribute type */ - AttributeDescription *ad; - struct berval from_tbls; - struct berval join_where; - struct berval sel_expr; + AttributeDescription *bam_ad; + /* ObjectClass if bam_ad is objectClass */ + ObjectClass *bam_oc; + + struct berval bam_from_tbls; + struct berval bam_join_where; + struct berval bam_sel_expr; /* supposed to expect 2 binded values: entry keyval * and attr. value to add, like "add_name(?,?,?)" */ - char *add_proc; + char *bam_add_proc; /* supposed to expect 2 binded values: entry keyval * and attr. value to delete */ - char *delete_proc; + char *bam_delete_proc; /* for optimization purposes attribute load query * is preconstructed from parts on schemamap load time */ - char *query; + char *bam_query; /* following flags are bitmasks (first bit used for add_proc, * second - for delete_proc) */ /* order of parameters for procedures above; * 1 means "data then keyval", 0 means "keyval then data" */ - int param_order; + int bam_param_order; /* flags whether one or more of procedures is a function * (whether back-sql should bind first parameter as output * for return code) */ - int expect_return; + int bam_expect_return; /* TimesTen */ - struct berval sel_expr_u; + struct berval bam_sel_expr_u; + + /* next mapping for attribute */ + struct backsql_at_map_rec *bam_next; } backsql_at_map_rec; +#define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, NULL, NULL, NULL, 0, 0, BER_BVNULL, NULL } + /* defines to support bitmasks above */ #define BACKSQL_ADD 0x1 #define BACKSQL_DEL 0x2 diff --git a/servers/slapd/back-sql/search.c b/servers/slapd/back-sql/search.c index 964f7d26da..7266bd3dfb 100644 --- a/servers/slapd/back-sql/search.c +++ b/servers/slapd/back-sql/search.c @@ -39,6 +39,9 @@ static int backsql_process_filter( backsql_srch_info *bsi, Filter *f ); +static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, + backsql_at_map_rec *at ); + static int backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad ) { @@ -223,8 +226,11 @@ backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f ) return 0; } +#if 0 /* always uppercase strings by now */ if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr, - bi->bi_caseIgnoreMatch ) ) { + bi->bi_caseIgnoreMatch ) ) +#endif + { casefold = 1; } @@ -241,16 +247,16 @@ backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f ) backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */ ); /* TimesTen */ - Debug( LDAP_DEBUG_TRACE, "expr: '%s' '%s'\n", at->sel_expr.bv_val, - at->sel_expr_u.bv_val ? at->sel_expr_u.bv_val : "", 0 ); + Debug( LDAP_DEBUG_TRACE, "expr: '%s' '%s'\n", at->bam_sel_expr.bv_val, + at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "", 0 ); if ( casefold && bi->upper_func.bv_val ) { /* * If a pre-upper-cased version of the column exists, use it */ - if ( at->sel_expr_u.bv_val ) { + if ( at->bam_sel_expr_u.bv_val ) { backsql_strfcat( &bsi->bsi_flt_where, "bl", - &at->sel_expr_u, + &at->bam_sel_expr_u, (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" ); } else { @@ -258,14 +264,14 @@ backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f ) "bcbcl", &bi->upper_func, '(', - &at->sel_expr, + &at->bam_sel_expr, ')', (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" ); } } else { backsql_strfcat( &bsi->bsi_flt_where, "bl", - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" ); } @@ -327,18 +333,10 @@ backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f ) static int backsql_process_filter( backsql_srch_info *bsi, Filter *f ) { - backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; backsql_at_map_rec *at; - backsql_at_map_rec oc_attr = { - slap_schema.si_ad_objectClass, BER_BVC(""), BER_BVC(""), - BER_BVNULL, NULL, NULL, NULL }; AttributeDescription *ad = NULL; int done = 0; - int casefold = 0; int rc = 0; - struct berval *filter_value = NULL; - MatchingRule *matching_rule = NULL; - struct berval ordering = BER_BVC("<="); Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 ); if ( f == NULL || f->f_choice == SLAPD_FILTER_COMPUTED ) { @@ -393,14 +391,44 @@ backsql_process_filter( backsql_srch_info *bsi, Filter *f ) */ if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) { - struct berbuf bb = BB_NULL; + /* + * If the filter is LDAP_FILTER_PRESENT, then it's done; + * otherwise, let's see if we are lucky: filtering + * for "structural" objectclass or ancestor... + */ + switch ( f->f_choice ) { + case LDAP_FILTER_EQUALITY: + { + ObjectClass *oc = oc_bvfind( &f->f_av_value ); - at = &oc_attr; - backsql_strfcat( &bb, "cbc", - '\'', - &bsi->bsi_oc->oc->soc_cname, - '\'' ); - at->sel_expr = bb.bb_val; + if ( oc == NULL ) { + bsi->bsi_status = LDAP_OTHER; + goto impossible; + } + + /* + * objectClass inheritance: + * - a search for "person" will also return "inetOrgPerson" + * - a search for "top" will return everything + */ + if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) { + goto filter_oc_success; + } + + break; + } + + case LDAP_FILTER_PRESENT: +filter_oc_success:; + backsql_strfcat( &bsi->bsi_flt_where, "l", + (ber_len_t)sizeof( "1=1" ) - 1, "1=1" ); + bsi->bsi_status = LDAP_SUCCESS; + goto done; + + default: + bsi->bsi_status = LDAP_OTHER; + goto impossible; + } } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) { /* @@ -435,11 +463,11 @@ backsql_process_filter( backsql_srch_info *bsi, Filter *f ) backsql_attrlist_add( bsi, NULL ); } goto done; - - } else { - at = backsql_ad2at( bsi->bsi_oc, ad ); } + /* look for attribute (also if objectClass but not structural one) */ + at = backsql_ad2at( bsi->bsi_oc, ad ); + if ( at == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): " "attribute '%s' is not defined for objectclass '%s'\n", @@ -450,24 +478,61 @@ backsql_process_filter( backsql_srch_info *bsi, Filter *f ) goto impossible; } - backsql_merge_from_clause( &bsi->bsi_from, &at->from_tbls ); + backsql_strfcat( &bsi->bsi_flt_where, "c", '(' ); +next:; + if ( backsql_process_filter_attr( bsi, f, at ) == -1 ) { + return -1; + } + + if ( at->bam_next ) { + backsql_strfcat( &bsi->bsi_flt_where, "l", + sizeof( " OR " ) - 1, " OR " ); + at = at->bam_next; + goto next; + } + backsql_strfcat( &bsi->bsi_flt_where, "c", ')' ); + +done:; + Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 ); + return 1; + +impossible:; + Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n", + 0, 0, 0 ); + return -1; +} + +static int +backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at ) +{ + backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; + int casefold = 0; + struct berval *filter_value = NULL; + MatchingRule *matching_rule = NULL; + struct berval ordering = BER_BVC("<="); + + Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n", + at->bam_ad->ad_cname.bv_val, 0, 0 ); + + backsql_merge_from_clause( &bsi->bsi_from, &at->bam_from_tbls ); + /* * need to add this attribute to list of attrs to load, * so that we can do test_filter() later */ - backsql_attrlist_add( bsi, ad ); + backsql_attrlist_add( bsi, at->bam_ad ); - if ( at->join_where.bv_val != NULL - && strstr( bsi->bsi_join_where.bb_val.bv_val, at->join_where.bv_val ) == NULL ) { + if ( at->bam_join_where.bv_val != NULL + && strstr( bsi->bsi_join_where.bb_val.bv_val, at->bam_join_where.bv_val ) == NULL ) { backsql_strfcat( &bsi->bsi_join_where, "lb", (ber_len_t)sizeof( " AND " ) - 1, " AND ", - &at->join_where ); + &at->bam_join_where ); } switch ( f->f_choice ) { case LDAP_FILTER_EQUALITY: filter_value = &f->f_av_value; - matching_rule = ad->ad_type->sat_equality; + matching_rule = at->bam_ad->ad_type->sat_equality; goto equality_match; @@ -486,26 +551,6 @@ equality_match:; casefold = 1; } - /* - * objectClass inheritance: - * - a search for "person" will also return "inetOrgPerson" - * - a search for "top" will return everything - */ - if ( at == &oc_attr ) { - ObjectClass *oc = oc_bvfind( filter_value ); - - rc = is_object_subclass( oc, bsi->bsi_oc->oc ); - if ( rc ) { - backsql_strfcat( &bsi->bsi_flt_where, "l", - sizeof( "(1=1)" ) - 1, "(1=1)" ); - break; - - } else { - rc = -1; - goto impossible; - } - } - /* * maybe we should check type of at->sel_expr here somehow, * to know whether upper_func is applicable, but for now @@ -515,10 +560,10 @@ equality_match:; if ( casefold && bi->upper_func.bv_val ) { size_t start; - if ( at->sel_expr_u.bv_val ) { + if ( at->bam_sel_expr_u.bv_val ) { backsql_strfcat( &bsi->bsi_flt_where, "cbl", '(', /* ) */ - &at->sel_expr_u, + &at->bam_sel_expr_u, (ber_len_t)sizeof( "='" ) - 1, "='" ); } else { @@ -526,7 +571,7 @@ equality_match:; '(' /* ) */ , &bi->upper_func, '(' /* ) */ , - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( /* ( */ ")='" ) - 1, /* ( */ ")='" ); } @@ -543,7 +588,7 @@ equality_match:; } else { backsql_strfcat( &bsi->bsi_flt_where, "cblbl", '(', - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( "='" ) - 1, "='", filter_value, (ber_len_t)sizeof( /* (' */ "')" ) - 1, @@ -557,8 +602,11 @@ equality_match:; /* fall thru to next case */ case LDAP_FILTER_LE: - if ( SLAP_MR_ASSOCIATED( ad->ad_type->sat_ordering, - bi->bi_caseIgnoreMatch ) ) { +#if 0 /* always uppercase strings by now */ + if ( SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering, + bi->bi_caseIgnoreMatch ) ) +#endif + { casefold = 1; } @@ -568,10 +616,10 @@ equality_match:; if ( casefold && bi->upper_func.bv_val ) { size_t start; - if ( at->sel_expr_u.bv_val ) { + if ( at->bam_sel_expr_u.bv_val ) { backsql_strfcat( &bsi->bsi_flt_where, "cbbc", '(', - &at->sel_expr_u, + &at->bam_sel_expr_u, &ordering, '\'' ); } else { @@ -579,7 +627,7 @@ equality_match:; '(' /* ) */ , &bi->upper_func, '(' /* ) */ , - &at->sel_expr, + &at->bam_sel_expr, /* ( */ ')', &ordering, '\'' ); @@ -597,7 +645,7 @@ equality_match:; } else { backsql_strfcat( &bsi->bsi_flt_where, "cbbcbl", '(' /* ) */ , - &at->sel_expr, + &at->bam_sel_expr, &ordering, '\'', &f->f_av_value, @@ -610,7 +658,7 @@ equality_match:; backsql_strfcat( &bsi->bsi_flt_where, "lbl", (ber_len_t)sizeof( "NOT (" /* ) */) - 1, "NOT (", /* ) */ - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( /* ( */ " IS NULL)" ) - 1, /* ( */ " IS NULL)" ); break; @@ -631,10 +679,10 @@ equality_match:; if ( bi->upper_func.bv_val ) { size_t start; - if ( at->sel_expr_u.bv_val ) { + if ( at->bam_sel_expr_u.bv_val ) { backsql_strfcat( &bsi->bsi_flt_where, "cbl", '(', /* ) */ - &at->sel_expr_u, + &at->bam_sel_expr_u, (ber_len_t)sizeof( " LIKE '%" ) - 1, " LIKE '%" ); } else { @@ -642,7 +690,7 @@ equality_match:; '(' /* ) */ , &bi->upper_func, '(' /* ) */ , - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( /* ( */ ") LIKE '%" ) - 1, /* ( */ ") LIKE '%" ); } @@ -659,7 +707,7 @@ equality_match:; } else { backsql_strfcat( &bsi->bsi_flt_where, "cblbl", '(', /* ) */ - &at->sel_expr, + &at->bam_sel_expr, (ber_len_t)sizeof( " LIKE '%" ) - 1, " LIKE '%", &f->f_av_value, @@ -677,21 +725,10 @@ equality_match:; } -done: - if ( oc_attr.sel_expr.bv_val != NULL ) { - free( oc_attr.sel_expr.bv_val ); - } - - Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 ); - return 1; + Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n", + at->bam_ad->ad_cname.bv_val, 0, 0 ); -impossible: - if ( oc_attr.sel_expr.bv_val != NULL ) { - free( oc_attr.sel_expr.bv_val ); - } - Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n", - 0, 0, 0 ); - return -1; + return 1; } static int @@ -721,9 +758,9 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) backsql_strfcat( &bsi->bsi_sel, "lbcbc", (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1, "SELECT DISTINCT ldap_entries.id,", - &bsi->bsi_oc->keytbl, + &bsi->bsi_oc->bom_keytbl, '.', - &bsi->bsi_oc->keycol, + &bsi->bsi_oc->bom_keycol, ',' ); if ( bi->strcast_func.bv_val ) { @@ -731,13 +768,13 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) &bi->strcast_func, (ber_len_t)sizeof( "('" /* ') */ ) - 1, "('" /* ') */ , - &bsi->bsi_oc->oc->soc_cname, + &bsi->bsi_oc->bom_oc->soc_cname, (ber_len_t)sizeof( /* (' */ "')" ) - 1, /* (' */ "')" ); } else { backsql_strfcat( &bsi->bsi_sel, "cbc", '\'', - &bsi->bsi_oc->oc->soc_cname, + &bsi->bsi_oc->bom_oc->soc_cname, '\'' ); } backsql_strfcat( &bsi->bsi_sel, "l", @@ -747,13 +784,13 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) backsql_strfcat( &bsi->bsi_from, "lb", (ber_len_t)sizeof( " FROM ldap_entries," ) - 1, " FROM ldap_entries,", - &bsi->bsi_oc->keytbl ); + &bsi->bsi_oc->bom_keytbl ); backsql_strfcat( &bsi->bsi_join_where, "lbcbl", (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ", - &bsi->bsi_oc->keytbl, + &bsi->bsi_oc->bom_keytbl, '.', - &bsi->bsi_oc->keycol, + &bsi->bsi_oc->bom_keycol, (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1, "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ); @@ -878,7 +915,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi ) if ( res ) { Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " "error while constructing query for objectclass '%s'\n", - oc->oc->soc_cname.bv_val, 0, 0 ); + oc->bom_oc->soc_cname.bv_val, 0, 0 ); /* * FIXME: need to separate errors from legally * impossible filters @@ -903,7 +940,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi ) if ( query.bv_val == NULL ) { Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " "could not construct query for objectclass '%s'\n", - oc->oc->soc_cname.bv_val, 0, 0 ); + oc->bom_oc->soc_cname.bv_val, 0, 0 ); bsi->bsi_status = LDAP_SUCCESS; return BACKSQL_CONTINUE; } @@ -921,9 +958,9 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi ) return BACKSQL_CONTINUE; } - Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->id, 0, 0 ); + Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 ); - if ( backsql_BindParamID( sth, 1, &bsi->bsi_oc->id ) != SQL_SUCCESS ) { + if ( backsql_BindParamID( sth, 1, &bsi->bsi_oc->bom_id ) != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " "error binding objectclass id parameter\n", 0, 0, 0 ); bsi->bsi_status = LDAP_OTHER; @@ -1051,7 +1088,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi ) sizeof( backsql_entryID ) ); c_id->id = strtol( row.cols[ 0 ], NULL, 0 ); c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 ); - c_id->oc_id = bsi->bsi_oc->id; + c_id->oc_id = bsi->bsi_oc->bom_id; ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn ); c_id->next = bsi->bsi_id_list; bsi->bsi_id_list = c_id; @@ -1273,7 +1310,7 @@ backsql_search( Operation *op, SlapReply *rs ) ber_bvarray_free( refs ); } - if (!rs->sr_ref) { + if ( !rs->sr_ref ) { rs->sr_text = "bad_referral object"; } -- 2.39.5