From e52d5cb812a93ebfc9f58205f41d6d9c2939ce92 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Thu, 9 Jun 2005 09:43:20 +0000 Subject: [PATCH] make aliasing and aliasing quotes configurable --- servers/slapd/back-sql/back-sql.h | 4 +++ servers/slapd/back-sql/config.c | 53 ++++++++++++++++++++++++----- servers/slapd/back-sql/init.c | 43 +++++++++++++++++++---- servers/slapd/back-sql/proto-sql.h | 2 +- servers/slapd/back-sql/schema-map.c | 33 +++++++++--------- servers/slapd/back-sql/search.c | 12 ++----- servers/slapd/back-sql/util.c | 12 +++++-- 7 files changed, 114 insertions(+), 45 deletions(-) diff --git a/servers/slapd/back-sql/back-sql.h b/servers/slapd/back-sql/back-sql.h index 9945a615db..21c152e2b1 100644 --- a/servers/slapd/back-sql/back-sql.h +++ b/servers/slapd/back-sql/back-sql.h @@ -493,6 +493,10 @@ typedef struct backsql_info { BerVarray sql_concat_func; struct berval sql_strcast_func; + struct berval sql_aliasing; + struct berval sql_aliasing_quote; + struct berval sql_dn_oc_aliasing; + AttributeName *sql_anlist; unsigned int sql_flags; diff --git a/servers/slapd/back-sql/config.c b/servers/slapd/back-sql/config.c index a333ae0419..700f8cc1ee 100644 --- a/servers/slapd/back-sql/config.c +++ b/servers/slapd/back-sql/config.c @@ -321,7 +321,7 @@ backsql_db_config( Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): " "delobjclasses_stmt=%s\n", bi->sql_delobjclasses_stmt, 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "has_ldapinfo_dn_ru") ) { + } else if ( !strcasecmp( argv[ 0 ], "has_ldapinfo_dn_ru" ) ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " @@ -352,7 +352,7 @@ backsql_db_config( "has_ldapinfo_dn_ru=%s\n", BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ? "yes" : "no", 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "fail_if_no_mapping") ) { + } else if ( !strcasecmp( argv[ 0 ], "fail_if_no_mapping" ) ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " @@ -381,7 +381,7 @@ backsql_db_config( "fail_if_no_mapping=%s\n", BACKSQL_FAIL_IF_NO_MAPPING( bi ) ? "yes" : "no", 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "allow_orphans") ) { + } else if ( !strcasecmp( argv[ 0 ], "allow_orphans" ) ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " @@ -443,7 +443,7 @@ backsql_db_config( return 1; } - } else if ( !strcasecmp( argv[ 0 ], "sqllayer") ) { + } else if ( !strcasecmp( argv[ 0 ], "sqllayer" ) ) { if ( backsql_api_config( bi, argv[ 1 ], argc - 2, &argv[ 2 ] ) ) { Debug( LDAP_DEBUG_TRACE, @@ -466,7 +466,7 @@ backsql_db_config( Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): " "id_query=%s\n", bi->sql_id_query, 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "use_subtree_shortcut") ) { + } else if ( !strcasecmp( argv[ 0 ], "use_subtree_shortcut" ) ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " @@ -496,7 +496,7 @@ backsql_db_config( BACKSQL_USE_SUBTREE_SHORTCUT( bi ) ? "yes" : "no", 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "fetch_all_attrs") ) { + } else if ( !strcasecmp( argv[ 0 ], "fetch_all_attrs" ) ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " @@ -526,7 +526,7 @@ backsql_db_config( BACKSQL_FETCH_ALL_ATTRS( bi ) ? "yes" : "no", 0, 0 ); - } else if ( !strcasecmp( argv[ 0 ], "fetch_attrs") ) { + } else if ( !strcasecmp( argv[ 0 ], "fetch_attrs" ) ) { char *str, *s, *next; char delimstr[] = ","; @@ -561,8 +561,8 @@ backsql_db_config( return -1; } - } else if ( !strcasecmp( argv[ 0 ], "check_schema") ) { - if ( argc < 2 ) { + } else if ( !strcasecmp( argv[ 0 ], "check_schema" ) ) { + if ( argc != 2 ) { Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config (%s line %d): " "missing { yes | no }" @@ -591,6 +591,41 @@ backsql_db_config( BACKSQL_CHECK_SCHEMA( bi ) ? "yes" : "no", 0, 0 ); + } else if ( !strcasecmp( argv[ 0 ], "aliasing" ) ) { + if ( argc != 2 ) { + Debug( LDAP_DEBUG_TRACE, + "<==backsql_db_config (%s line %d): " + "missing arg " + "in \"aliasing \" directive\n", + fname, lineno, 0 ); + return 1; + } + + if ( ! BER_BVISNULL( &bi->sql_aliasing ) ) { + ch_free( bi->sql_aliasing.bv_val ); + } + + ber_str2bv( argv[ 1 ], strlen( argv[ 1 ] ) + 1, 1, + &bi->sql_aliasing ); + /* add a trailing space... */ + bi->sql_aliasing.bv_val[ bi->sql_aliasing.bv_len - 1] = ' '; + + } else if ( !strcasecmp( argv[ 0 ], "aliasing_quote" ) ) { + if ( argc != 2 ) { + Debug( LDAP_DEBUG_TRACE, + "<==backsql_db_config (%s line %d): " + "missing arg " + "in \"aliasing_quote \" directive\n", + fname, lineno, 0 ); + return 1; + } + + if ( ! BER_BVISNULL( &bi->sql_aliasing_quote ) ) { + ch_free( bi->sql_aliasing_quote.bv_val ); + } + + ber_str2bv( argv[ 1 ], 0, 1, &bi->sql_aliasing_quote ); + } else { return SLAP_CONF_UNKNOWN; } diff --git a/servers/slapd/back-sql/init.c b/servers/slapd/back-sql/init.c index cf0bebf73b..e31d3b85eb 100644 --- a/servers/slapd/back-sql/init.c +++ b/servers/slapd/back-sql/init.c @@ -147,6 +147,9 @@ backsql_db_destroy( free( bi->sql_renentry_stmt ); free( bi->sql_delobjclasses_stmt ); + free( bi->sql_aliasing.bv_val ); + free( bi->sql_aliasing_quote.bv_val ); + if ( bi->sql_anlist ) { int i; @@ -201,6 +204,21 @@ backsql_db_open( } } + /* + * see back-sql.h for default values + */ + if ( BER_BVISNULL( &bi->sql_aliasing ) ) { + ber_str2bv( BACKSQL_ALIASING, + STRLENOF( BACKSQL_ALIASING ), + 1, &bi->sql_aliasing ); + } + + if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) { + ber_str2bv( BACKSQL_ALIASING_QUOTE, + STRLENOF( BACKSQL_ALIASING_QUOTE ), + 1, &bi->sql_aliasing_quote ); + } + /* * Prepare cast string as required */ @@ -445,21 +463,31 @@ backsql_db_open( bi->sql_id_query = bb.bb_val.bv_val; } - /* + /* * Prepare children ID selection query */ - bi->sql_has_children_query = NULL; - - bb.bb_val.bv_val = NULL; - bb.bb_val.bv_len = 0; + BER_BVZERO( &bb.bb_val ); bb.bb_len = 0; - backsql_strfcat( &bb, "sb", + backsql_strfcat( &bb, "sbsb", "SELECT COUNT(distinct subordinates.id) " - "FROM ldap_entries,ldap_entries " BACKSQL_ALIASING "subordinates " + "FROM ldap_entries,ldap_entries ", + &bi->sql_aliasing, "subordinates " "WHERE subordinates.parent=ldap_entries.id AND ", &bi->sql_children_cond ); bi->sql_has_children_query = bb.bb_val.bv_val; + /* + * Prepare DN and objectClass aliasing bit of query + */ + BER_BVZERO( &bb.bb_val ); + bb.bb_len = 0; + backsql_strfcat( &bb, "sbbsbsbbsb", + " ", &bi->sql_aliasing, &bi->sql_aliasing_quote, + "objectClass", &bi->sql_aliasing_quote, + ",ldap_entries.dn ", &bi->sql_aliasing, + &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote ); + bi->sql_dn_oc_aliasing = bb.bb_val; + backsql_free_db_conn( op ); if ( !BACKSQL_SCHEMA_LOADED( bi ) ) { Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " @@ -475,6 +503,7 @@ backsql_db_open( /* enable if only one suffix is defined */ bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT; } + bi->sql_flags |= BSQLF_CHECK_SCHEMA; Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): " diff --git a/servers/slapd/back-sql/proto-sql.h b/servers/slapd/back-sql/proto-sql.h index 4cdc28d202..3a5bd3e2d9 100644 --- a/servers/slapd/back-sql/proto-sql.h +++ b/servers/slapd/back-sql/proto-sql.h @@ -242,7 +242,7 @@ struct berbuf * backsql_strfcat( struct berbuf *dest, const char *fmt, ... ); int backsql_entry_addattr( Entry *e, AttributeDescription *ad, struct berval *at_val, void *memctx ); -int backsql_merge_from_clause( struct berbuf *dest_from, +int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from, struct berval *src_from ); int backsql_split_pattern( const char *pattern, BerVarray *split_pattern, diff --git a/servers/slapd/back-sql/schema-map.c b/servers/slapd/back-sql/schema-map.c index bf87251401..44698952f5 100644 --- a/servers/slapd/back-sql/schema-map.c +++ b/servers/slapd/back-sql/schema-map.c @@ -100,19 +100,21 @@ backsql_dup_attr( void *v_m1, void *v_m2 ) static int backsql_make_attr_query( + backsql_info *bi, backsql_oc_map_rec *oc_map, backsql_at_map_rec *at_map ) { struct berbuf bb = BB_NULL; - backsql_strfcat( &bb, "lblblblbcbl", + backsql_strfcat( &bb, "lblbbbblblbcbl", (ber_len_t)STRLENOF( "SELECT " ), "SELECT ", &at_map->bam_sel_expr, - (ber_len_t)STRLENOF( " " BACKSQL_ALIASING BACKSQL_ALIASING_QUOTE ), - " " BACKSQL_ALIASING BACKSQL_ALIASING_QUOTE, + (ber_len_t)STRLENOF( " " ), " ", + &bi->sql_aliasing, + &bi->sql_aliasing_quote, &at_map->bam_ad->ad_cname, - (ber_len_t)STRLENOF( BACKSQL_ALIASING_QUOTE " FROM " ), - BACKSQL_ALIASING_QUOTE " FROM ", + &bi->sql_aliasing_quote, + (ber_len_t)STRLENOF( " FROM " ), " FROM ", &at_map->bam_from_tbls, (ber_len_t)STRLENOF( " WHERE " ), " WHERE ", &oc_map->bom_keytbl, @@ -126,12 +128,11 @@ backsql_make_attr_query( &at_map->bam_join_where ); } - backsql_strfcat( &bb, "lbl", - (ber_len_t)STRLENOF( " ORDER BY " BACKSQL_ALIASING_QUOTE ), - " ORDER BY " BACKSQL_ALIASING_QUOTE, + backsql_strfcat( &bb, "lbbb", + (ber_len_t)STRLENOF( " ORDER BY " ), " ORDER BY ", + &bi->sql_aliasing_quote, &at_map->bam_ad->ad_cname, - (ber_len_t)STRLENOF( BACKSQL_ALIASING_QUOTE ), - BACKSQL_ALIASING_QUOTE ); + &bi->sql_aliasing_quote ); at_map->bam_query = bb.bb_val.bv_val; @@ -162,7 +163,7 @@ backsql_make_attr_query( } static int -backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) +backsql_add_sysmaps( backsql_info *bi, backsql_oc_map_rec *oc_map ) { backsql_at_map_rec *at_map; char s[] = "+9223372036854775807L"; @@ -183,7 +184,7 @@ backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) 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 ); + backsql_merge_from_clause( bi, &bb, &oc_map->bom_keytbl ); at_map->bam_from_tbls = bb.bb_val; BER_BVZERO( &bb.bb_val ); @@ -240,7 +241,7 @@ backsql_add_sysmaps( backsql_oc_map_rec *oc_map ) at_map->bam_expect_return = 0; at_map->bam_next = NULL; - backsql_make_attr_query( oc_map, at_map ); + backsql_make_attr_query( bi, oc_map, at_map ); 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", @@ -359,7 +360,7 @@ backsql_oc_get_attr_mapping( void *v_oc, void *v_bas ) } ber_str2bv( at_row.cols[ 2 ], 0, 0, &bv ); - backsql_merge_from_clause( &bb, &bv ); + backsql_merge_from_clause( bas->bas_bi, &bb, &bv ); at_map->bam_from_tbls = bb.bb_val; if ( at_row.value_len[ 3 ] < 0 ) { BER_BVZERO( &at_map->bam_join_where ); @@ -386,7 +387,7 @@ backsql_oc_get_attr_mapping( void *v_oc, void *v_bas ) if ( next == at_row.cols[ 7 ] || next[0] != '\0' ) { /* error */ } - backsql_make_attr_query( oc_map, at_map ); + backsql_make_attr_query( bas->bas_bi, oc_map, at_map ); Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_attr_mapping(): " "preconstructed query \"%s\"\n", at_map->bam_query, 0, 0 ); @@ -419,7 +420,7 @@ backsql_oc_get_attr_mapping( void *v_oc, void *v_bas ) "autoadding 'objectClass' and 'ref' mappings\n", BACKSQL_OC_NAME( oc_map ), 0, 0 ); - (void)backsql_add_sysmaps( oc_map ); + (void)backsql_add_sysmaps( bas->bas_bi, oc_map ); return BACKSQL_AVL_CONTINUE; } diff --git a/servers/slapd/back-sql/search.c b/servers/slapd/back-sql/search.c index 98f0041501..aa08445385 100644 --- a/servers/slapd/back-sql/search.c +++ b/servers/slapd/back-sql/search.c @@ -1247,8 +1247,8 @@ equality_match:; static int backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) { - backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; - int rc; + backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; + int rc; assert( query ); BER_BVZERO( query ); @@ -1288,14 +1288,8 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) &bsi->bsi_oc->bom_oc->soc_cname, '\'' ); } - backsql_strfcat( &bsi->bsi_sel, "l", - (ber_len_t)STRLENOF( " " BACKSQL_ALIASING - BACKSQL_ALIASING_QUOTE "objectClass" BACKSQL_ALIASING_QUOTE - ",ldap_entries.dn " BACKSQL_ALIASING BACKSQL_ALIASING_QUOTE "dn" BACKSQL_ALIASING_QUOTE ), - " " BACKSQL_ALIASING - BACKSQL_ALIASING_QUOTE "objectClass" BACKSQL_ALIASING_QUOTE - ",ldap_entries.dn " BACKSQL_ALIASING BACKSQL_ALIASING_QUOTE "dn" BACKSQL_ALIASING_QUOTE ); + backsql_strfcat( &bsi->bsi_sel, "b", &bi->sql_dn_oc_aliasing ); backsql_strfcat( &bsi->bsi_from, "lb", (ber_len_t)STRLENOF( " FROM ldap_entries," ), " FROM ldap_entries,", diff --git a/servers/slapd/back-sql/util.c b/servers/slapd/back-sql/util.c index ab4042e926..c90823b73e 100644 --- a/servers/slapd/back-sql/util.c +++ b/servers/slapd/back-sql/util.c @@ -275,7 +275,7 @@ backsql_entry_addattr( } static char * -backsql_get_table_spec( char **p ) +backsql_get_table_spec( backsql_info *bi, char **p ) { char *s, *q; struct berbuf res = BB_NULL; @@ -312,13 +312,19 @@ backsql_get_table_spec( char **p ) } /* oracle doesn't understand "AS" :( and other RDBMSes don't need it */ - backsql_strcat( &res, " " BACKSQL_ALIASING BACKSQL_ALIASING_QUOTE, s, BACKSQL_ALIASING_QUOTE, NULL ); + backsql_strfcat( &res, "lbbsb", + STRLENOF( " " ), " ", + &bi->sql_aliasing, + &bi->sql_aliasing_quote, + s, + &bi->sql_aliasing_quote ); return res.bb_val.bv_val; } int backsql_merge_from_clause( + backsql_info *bi, struct berbuf *dest_from, struct berval *src_from ) { @@ -340,7 +346,7 @@ backsql_merge_from_clause( } while ( *p ) { - s = backsql_get_table_spec( &p ); + s = backsql_get_table_spec( bi, &p ); #ifdef BACKSQL_TRACE Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): " -- 2.39.5