📄 add.c
字号:
/* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/add.c,v 1.20.2.13 2007/01/02 21:44:07 kurt Exp $ *//* This work is part of OpenLDAP Software <http://www.openldap.org/>. * * Copyright 1999-2007 The OpenLDAP Foundation. * Portions Copyright 1999 Dmitry Kovalev. * Portions Copyright 2002 Pierangelo Masarati. * Portions Copyright 2004 Mark Adamson. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted only as authorized by the OpenLDAP * Public License. * * A copy of this license is available in the file LICENSE in the * top-level directory of the distribution or, alternatively, at * <http://www.OpenLDAP.org/license.html>. *//* ACKNOWLEDGEMENTS: * This work was initially developed by Dmitry Kovalev for inclusion * by OpenLDAP Software. Additional significant contributors include * Pierangelo Masarati and Mark Adamson. */#include "portable.h"#include <stdio.h>#include <sys/types.h>#include "ac/string.h"#include "slap.h"#include "proto-sql.h"#ifdef BACKSQL_SYNCPROV#include <lutil.h>#endif /* BACKSQL_SYNCPROV *//* * Skip: * - null values (e.g. delete modification) * - single occurrence of objectClass, because it is already used * to determine how to build the SQL entry * - operational attributes * - empty attributes */#define backsql_opattr_skip(ad) \ (is_at_operational( (ad)->ad_type ) && (ad) != slap_schema.si_ad_ref )#define backsql_attr_skip(ad, vals) \ ( \ ( (ad) == slap_schema.si_ad_objectClass \ && (vals) && BER_BVISNULL( &((vals)[ 1 ]) ) ) \ || backsql_opattr_skip( (ad) ) \ || ( (vals) && BER_BVISNULL( &((vals)[ 0 ]) ) ) \ )intbacksql_modify_delete_all_values( Operation *op, SlapReply *rs, SQLHDBC dbh, backsql_entryID *e_id, backsql_at_map_rec *at ){ backsql_info *bi = (backsql_info *)op->o_bd->be_private; RETCODE rc; SQLHSTMT asth = SQL_NULL_HSTMT; BACKSQL_ROW_NTS row; assert( at != NULL ); if ( at->bam_delete_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "missing attribute value delete procedure " "for attr \"%s\"\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_text = "SQL-backend error"; return rs->sr_err = LDAP_OTHER; } return LDAP_SUCCESS; } rc = backsql_Prepare( dbh, &asth, at->bam_query, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error preparing attribute value select query " "\"%s\"\n", at->bam_query, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, asth, rc ); rs->sr_text = "SQL-backend error"; return rs->sr_err = LDAP_OTHER; } rc = backsql_BindParamID( asth, 1, SQL_PARAM_INPUT, &e_id->eid_keyval ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error binding key value parameter " "to attribute value select query\n", 0, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, asth, rc ); SQLFreeStmt( asth, SQL_DROP ); rs->sr_text = "SQL-backend error"; return rs->sr_err = LDAP_OTHER; } rc = SQLExecute( asth ); if ( !BACKSQL_SUCCESS( rc ) ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error executing attribute value select query\n", 0, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, asth, rc ); SQLFreeStmt( asth, SQL_DROP ); rs->sr_text = "SQL-backend error"; return rs->sr_err = LDAP_OTHER; } backsql_BindRowAsStrings_x( asth, &row, op->o_tmpmemctx ); for ( rc = SQLFetch( asth ); BACKSQL_SUCCESS( rc ); rc = SQLFetch( asth ) ) { int i; /* first parameter no, parameter order */ SQLUSMALLINT pno = 0, po = 0; /* procedure return code */ int prc = LDAP_SUCCESS; for ( i = 0; i < row.ncols; i++ ) { SQLHSTMT sth = SQL_NULL_HSTMT; ber_len_t col_len; rc = backsql_Prepare( dbh, &sth, at->bam_delete_proc, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error preparing attribute value " "delete procedure " "\"%s\"\n", at->bam_delete_proc, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; } if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) { pno = 1; rc = backsql_BindParamInt( sth, 1, SQL_PARAM_OUTPUT, &prc ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error binding output parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; } } po = ( BACKSQL_IS_DEL( at->bam_param_order ) ) > 0; rc = backsql_BindParamID( sth, pno + 1 + po, SQL_PARAM_INPUT, &e_id->eid_keyval ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error binding keyval parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; }#ifdef BACKSQL_ARBITRARY_KEY Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values() " "arg(%d)=%s\n", pno + 1 + po, e_id->eid_keyval.bv_val, 0 );#else /* ! BACKSQL_ARBITRARY_KEY */ Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values() " "arg(%d)=%lu\n", pno + 1 + po, e_id->eid_keyval, 0 );#endif /* ! BACKSQL_ARBITRARY_KEY */ /* * check for syntax needed here * maybe need binary bind? */ col_len = strlen( row.cols[ i ] ); rc = backsql_BindParamStr( sth, pno + 2 - po, SQL_PARAM_INPUT, row.cols[ i ], col_len ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "error binding value parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; } Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "arg(%d)=%s; executing \"%s\"\n", pno + 2 - po, row.cols[ i ], at->bam_delete_proc ); rc = SQLExecute( sth ); if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS ) { rs->sr_err = LDAP_SUCCESS; } else { Debug( LDAP_DEBUG_TRACE, " backsql_modify_delete_all_values(): " "delete_proc " "execution failed (rc=%d, prc=%d)\n", rc, prc, 0 ); if ( prc != LDAP_SUCCESS ) { /* SQL procedure executed fine * but returned an error */ rs->sr_err = BACKSQL_SANITIZE_ERROR( prc ); } else { backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); rs->sr_err = LDAP_OTHER; } rs->sr_text = op->o_req_dn.bv_val; SQLFreeStmt( sth, SQL_DROP ); goto done; } SQLFreeStmt( sth, SQL_DROP ); } } rs->sr_err = LDAP_SUCCESS;done:; backsql_FreeRow_x( &row, op->o_tmpmemctx ); SQLFreeStmt( asth, SQL_DROP ); return rs->sr_err;}intbacksql_modify_internal( Operation *op, SlapReply *rs, SQLHDBC dbh, backsql_oc_map_rec *oc, backsql_entryID *e_id, Modifications *modlist ){ backsql_info *bi = (backsql_info *)op->o_bd->be_private; RETCODE rc; Modifications *ml; Debug( LDAP_DEBUG_TRACE, "==>backsql_modify_internal(): " "traversing modifications list\n", 0, 0, 0 ); for ( ml = modlist; ml != NULL; ml = ml->sml_next ) { AttributeDescription *ad; int sm_op; static char *sm_ops[] = { "add", "delete", "replace", "increment", NULL }; BerVarray sm_values;#if 0 /* NOTE: some day we'll have to pass * the normalized values as well */ BerVarray sm_nvalues;#endif backsql_at_map_rec *at = NULL; struct berval *at_val; int i; ad = ml->sml_mod.sm_desc; sm_op = ( ml->sml_mod.sm_op & LDAP_MOD_OP ); sm_values = ml->sml_mod.sm_values;#if 0 sm_nvalues = ml->sml_mod.sm_nvalues;#endif Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "modifying attribute \"%s\" (%s) according to " "mappings for objectClass \"%s\"\n", ad->ad_cname.bv_val, sm_ops[ sm_op ], BACKSQL_OC_NAME( oc ) ); if ( backsql_attr_skip( ad, sm_values ) ) { continue; } at = backsql_ad2at( oc, ad ); if ( at == NULL ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "attribute \"%s\" is not registered " "in objectClass \"%s\"\n", ad->ad_cname.bv_val, BACKSQL_OC_NAME( oc ), 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; rs->sr_text = "operation not permitted " "within namingContext"; goto done; } continue; } switch ( sm_op ) { case LDAP_MOD_REPLACE: { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "replacing values for attribute \"%s\"\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); 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->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; rs->sr_text = "operation not permitted " "within namingContext"; goto done; } break; } if ( at->bam_delete_proc == NULL ) { if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "delete procedure is not defined " "for attribute \"%s\"\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); rs->sr_err = LDAP_UNWILLING_TO_PERFORM; rs->sr_text = "operation not permitted " "within namingContext"; goto done; } Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "delete procedure is not defined " "for attribute \"%s\" " "- adding only\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); goto add_only; }del_all: rs->sr_err = backsql_modify_delete_all_values( op, rs, dbh, e_id, at ); if ( rs->sr_err != LDAP_SUCCESS ) { goto done; } /* LDAP_MOD_DELETE gets here if all values must be deleted */ if ( sm_op == LDAP_MOD_DELETE ) { break; } } /* * PASSTHROUGH - to add new attributes -- do NOT add break */ case LDAP_MOD_ADD: /* case SLAP_MOD_SOFTADD: */add_only:; if ( at->bam_add_proc == NULL ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "add procedure is not defined " "for attribute \"%s\"\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; rs->sr_text = "operation not permitted " "within namingContext"; goto done; } break; } Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "adding new values for attribute \"%s\"\n", at->bam_ad->ad_cname.bv_val, 0, 0 ); /* can't add a NULL val array */ assert( sm_values != NULL ); for ( i = 0, at_val = sm_values; !BER_BVISNULL( at_val ); i++, at_val++ ) { SQLHSTMT sth = SQL_NULL_HSTMT; /* first parameter position, parameter order */ SQLUSMALLINT pno = 0, po; /* procedure return code */ int prc = LDAP_SUCCESS; rc = backsql_Prepare( dbh, &sth, at->bam_add_proc, 0 ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "error preparing add query\n", 0, 0, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); rs->sr_err = LDAP_OTHER; rs->sr_text = "SQL-backend error"; goto done; } if ( BACKSQL_IS_ADD( at->bam_expect_return ) ) { pno = 1; rc = backsql_BindParamInt( sth, 1, SQL_PARAM_OUTPUT, &prc ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "error binding output parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; } } po = ( BACKSQL_IS_ADD( at->bam_param_order ) ) > 0; rc = backsql_BindParamID( sth, pno + 1 + po, SQL_PARAM_INPUT, &e_id->eid_keyval ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "error binding keyval parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; }#ifdef BACKSQL_ARBITRARY_KEY Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "arg(%d)=\"%s\"\n", pno + 1 + po, e_id->eid_keyval.bv_val, 0 );#else /* ! BACKSQL_ARBITRARY_KEY */ Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "arg(%d)=\"%lu\"\n", pno + 1 + po, e_id->eid_keyval, 0 );#endif /* ! BACKSQL_ARBITRARY_KEY */ /* * check for syntax needed here * maybe need binary bind? */ rc = backsql_BindParamBerVal( sth, pno + 2 - po, SQL_PARAM_INPUT, at_val ); if ( rc != SQL_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "error binding value parameter for %s[%d]\n", at->bam_ad->ad_cname.bv_val, i, 0 ); backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc ); SQLFreeStmt( sth, SQL_DROP ); rs->sr_text = "SQL-backend error"; rs->sr_err = LDAP_OTHER; goto done; } Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): " "arg(%d)=\"%s\"; executing \"%s\"\n", pno + 2 - po, at_val->bv_val, at->bam_add_proc );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -