]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/modify.c
now I remember why I introduced the 'has_ldapinfo_dn_ru' flag
[openldap] / servers / slapd / back-sql / modify.c
index bff2f9f8ed8bf009cc40b39e3f5fde60058d6658..3901a7b9078fda3d17e493fe57c0074396569c2d 100644 (file)
@@ -1,10 +1,21 @@
-/*
- *      Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1999-2004 The OpenLDAP Foundation.
+ * Portions Copyright 1999 Dmitry Kovalev.
+ * All rights reserved.
  *
- *      Redistribution and use in source and binary forms are permitted only
- *      as authorized by the OpenLDAP Public License.  A copy of this
- *      license is available at http://www.OpenLDAP.org/license.html or
- *      in file LICENSE in the top-level directory of the distribution.
+ * 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.
  */
 
 #include "portable.h"
@@ -13,7 +24,7 @@
 
 #include <stdio.h>
 #include <sys/types.h>
-#include <string.h>
+#include "ac/string.h"
 #include "slap.h"
 #include "ldap_pvt.h"
 #include "back-sql.h"
@@ -30,7 +41,7 @@
 /*
  * Skip:
  * - the first occurrence of objectClass, which is used
- *   to determine how to bulid the SQL entry (FIXME ?!?)
+ *   to determine how to build the SQL entry (FIXME ?!?)
  * - operational attributes
  *   empty attributes (FIXME ?!?)
  */
 
 static int
 backsql_modify_internal(
-       backsql_info            *bi,
+       Operation               *op,
+       SlapReply               *rs,
        SQLHDBC                 dbh, 
        backsql_oc_map_rec      *oc,
        backsql_entryID         *e_id,
-       Modifications           *modlist,
-       const char              **text )
+       Modifications           *modlist )
 {
+       backsql_info    *bi = (backsql_info*)op->o_bd->be_private;
        RETCODE         rc;
        SQLHSTMT        sth;
        Modifications   *ml;
-       int             res = LDAP_SUCCESS;
 
        Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
                "traversing modifications list\n", 0, 0, 0 );
 
-       *text = NULL;
-
 #ifndef BACKSQL_REALLOC_STMT
        SQLAllocStmt( dbh, &sth );
 #endif /* BACKSQL_REALLOC_STMT */
@@ -99,8 +108,8 @@ backsql_modify_internal(
                                ad->ad_cname.bv_val, 0, 0 );
 
                        if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                               res = LDAP_UNWILLING_TO_PERFORM;
-                               *text = "operation not permitted "
+                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                               rs->sr_text = "operation not permitted "
                                        "within namingContext";
                                goto done;
                        }
@@ -115,19 +124,19 @@ 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 ) ) {
-                                       res = LDAP_UNWILLING_TO_PERFORM;
-                                       *text = "operation not permitted "
+                                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                                       rs->sr_text = "operation not permitted "
                                                "within namingContext";
                                        goto done;
                                }
@@ -135,17 +144,17 @@ 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 ) ) {
-                                       res = LDAP_UNWILLING_TO_PERFORM;
-                                       *text = "operation not permitted "
+                                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                                       rs->sr_text = "operation not permitted "
                                                "within namingContext";
                                        goto done;
                                }
@@ -154,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(): "
@@ -163,8 +172,8 @@ del_all:
                                                asth, rc );
 
                                if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                       res = LDAP_OTHER;
-                                       *text = "SQL-backend error";
+                                       rs->sr_err = LDAP_OTHER;
+                                       rs->sr_text = "SQL-backend error";
                                        goto done;
                                }
 
@@ -182,8 +191,8 @@ del_all:
                                SQLFreeStmt( asth, SQL_DROP );
 
                                if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                       res = LDAP_OTHER;
-                                       *text = "SQL-backend error";
+                                       rs->sr_err = LDAP_OTHER;
+                                       rs->sr_text = "SQL-backend error";
                                        goto done;
                                }
 
@@ -201,8 +210,8 @@ del_all:
                                SQLFreeStmt( asth, SQL_DROP );
 
                                if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                       res = LDAP_OTHER;
-                                       *text = "SQL-backend error";
+                                       rs->sr_err = LDAP_OTHER;
+                                       rs->sr_text = "SQL-backend error";
                                        goto done;
                                }
 
@@ -213,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,
@@ -223,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,
@@ -242,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(): "
@@ -255,8 +264,8 @@ del_all:
                                                                dbh, sth, rc );
 
                                                if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                                       res = LDAP_OTHER;
-                                                       *text = "SQL-backend error";
+                                                       rs->sr_err = LDAP_OTHER;
+                                                       rs->sr_text = "SQL-backend error";
                                                        goto done;
                                                }
                                        }
@@ -269,23 +278,23 @@ 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 ) ) {
-                                       res = LDAP_UNWILLING_TO_PERFORM;
-                                       *text = "operation not permitted "
+                                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                                       rs->sr_text = "operation not permitted "
                                                "within namingContext";
                                        goto done;
                                }
@@ -295,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,
@@ -308,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,
@@ -327,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,
@@ -339,8 +348,8 @@ add_only:;
                                                        dbh, sth, rc );
 
                                        if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                               res = LDAP_OTHER;
-                                               *text = "SQL-backend error";
+                                               rs->sr_err = LDAP_OTHER;
+                                               rs->sr_text = "SQL-backend error";
                                                goto done;
                                        }
                                }
@@ -352,16 +361,16 @@ 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 ) ) {
-                                       res = LDAP_UNWILLING_TO_PERFORM;
-                                       *text = "operation not permitted "
+                                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                                       rs->sr_text = "operation not permitted "
                                                "within namingContext";
                                        goto done;
                                }
@@ -375,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,
@@ -395,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,
@@ -413,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,
@@ -425,8 +434,8 @@ add_only:;
                                                        dbh, sth, rc );
 
                                        if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                               res = LDAP_OTHER;
-                                               *text = "SQL-backend error";
+                                               rs->sr_err = LDAP_OTHER;
+                                               rs->sr_text = "SQL-backend error";
                                                goto done;
                                        }
                                }
@@ -453,36 +462,28 @@ done:;
        /*
         * FIXME: should fail in case one change fails?
         */
-       return res;
+       return rs->sr_err;
 }
 
 int
-backsql_modify(
-       BackendDB       *be,
-       Connection      *conn,
-       Operation       *op,
-       struct berval   *dn,
-       struct berval   *ndn,
-       Modifications   *modlist )
+backsql_modify( Operation *op, SlapReply *rs )
 {
-       backsql_info            *bi = (backsql_info*)be->be_private;
+       backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        SQLHDBC                 dbh;
        backsql_oc_map_rec      *oc = NULL;
        backsql_entryID         e_id;
        Entry                   e;
-       int                     res;
-       const char              *text = NULL;
 
        /*
         * FIXME: in case part of the operation cannot be performed
         * (missing mapping, SQL write fails or so) the entire operation
         * should be rolled-back
         */
-
        Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): changing entry '%s'\n",
-               ndn->bv_val, 0, 0 );
-       res = backsql_get_db_conn( be, conn, &dbh );
-       if ( res != LDAP_SUCCESS ) {
+               op->o_req_ndn.bv_val, 0, 0 );
+
+       rs->sr_err = backsql_get_db_conn( op, &dbh );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
                        "could not get connection handle - exiting\n", 
                        0, 0, 0 );
@@ -490,19 +491,19 @@ backsql_modify(
                 * FIXME: we don't want to send back 
                 * excessively detailed messages
                 */
-               send_ldap_result( conn, op, res, "", 
-                               res == LDAP_OTHER ?  "SQL-backend error" : "", 
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ? "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       res = backsql_dn2id( bi, &e_id, dbh, ndn );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
                        "could not lookup entry id\n", 0, 0, 0 );
-               send_ldap_result( conn, op, res , "", 
-                               res == LDAP_OTHER ? "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ? "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                return 1;
        }
 
@@ -523,113 +524,94 @@ backsql_modify(
                 * FIXME: we don't want to send back 
                 * excessively detailed messages
                 */
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
 
        e.e_attrs = NULL;
-       e.e_name = *dn;
-       e.e_nname = *ndn;
-       if ( !acl_check_modlist( be, conn, op, &e, modlist )) {
-               res = LDAP_INSUFFICIENT_ACCESS;
+       e.e_name = op->o_req_dn;
+       e.e_nname = op->o_req_ndn;
+       if ( !acl_check_modlist( op, &e, op->oq_modify.rs_modlist ) ) {
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
 
        } else {
-               res = backsql_modify_internal( bi, dbh, oc, &e_id, 
-                               modlist, &text );
+               rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id,
+                               op->oq_modify.rs_modlist );
        }
 
-       if ( res == LDAP_SUCCESS ) {
+       if ( rs->sr_err == LDAP_SUCCESS ) {
                /*
                 * Commit only if all operations succeed
-                *
-                * FIXME: backsql_modify_internal() does not fail 
-                * if add/delete operations are not available, or
-                * if a multiple value add actually results in a replace, 
-                * or if a single operation on an attribute fails 
-                * for any reason
                 */
                SQLTransact( SQL_NULL_HENV, dbh, 
                                op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
        }
-       send_ldap_result( conn, op, res, NULL, text, NULL, NULL );
+       send_ldap_result( op, rs );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
 
-       return 0;
+       return op->o_noop;
 }
 
 int
-backsql_modrdn(
-       BackendDB       *be,
-       Connection      *conn,
-       Operation       *op,
-       struct berval   *dn,
-       struct berval   *ndn,
-       struct berval   *newrdn,
-       struct berval   *nnewrdn,
-       int             deleteoldrdn,
-       struct berval   *newSuperior,
-       struct berval   *nnewSuperior )
+backsql_modrdn( Operation *op, SlapReply *rs )
 {
-       backsql_info            *bi = (backsql_info*)be->be_private;
+       backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        SQLHDBC                 dbh;
        SQLHSTMT                sth;
        RETCODE                 rc;
        backsql_entryID         e_id, pe_id, new_pid;
        backsql_oc_map_rec      *oc = NULL;
-       int                     res;
        struct berval           p_dn, p_ndn,
                                *new_pdn = NULL, *new_npdn = NULL,
                                new_dn, new_ndn;
-       const char              *text = NULL;
-       LDAPRDN                 *new_rdn = NULL;
-       LDAPRDN                 *old_rdn = NULL;
+       LDAPRDN                 new_rdn = NULL;
+       LDAPRDN                 old_rdn = NULL;
        Entry                   e;
        Modifications           *mod;
+       struct berval           *newSuperior = op->oq_modrdn.rs_newSup;
  
        Debug( LDAP_DEBUG_TRACE, "==>backsql_modrdn() renaming entry '%s', "
                        "newrdn='%s', newSuperior='%s'\n",
-                       dn->bv_val, newrdn->bv_val, 
+                       op->o_req_dn.bv_val, op->oq_modrdn.rs_newrdn.bv_val, 
                        newSuperior ? newSuperior->bv_val : "(NULL)" );
-       res = backsql_get_db_conn( be, conn, &dbh );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_get_db_conn( op, &dbh );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "could not get connection handle - exiting\n", 
                        0, 0, 0 );
-               send_ldap_result( conn, op, res, "",
-                               res == LDAP_OTHER ?  "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ?  "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       res = backsql_dn2id( bi, &e_id, dbh, ndn );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "could not lookup entry id\n", 0, 0, 0 );
-               send_ldap_result( conn, op, res, "",
-                               res == LDAP_OTHER ?  "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ?  "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       /*
-        * FIXME: check whether entry has children
-        */
-
        Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): entry id is %ld\n",
                e_id.id, 0, 0 );
 
-       if ( backsql_has_children( bi, dbh, ndn ) == LDAP_COMPARE_TRUE ) {
+       if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
-                       "entry \"%s\" has children\n", dn->bv_val, 0, 0 );
-               send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
-                               NULL, "subtree delete not supported",
-                               NULL, NULL );
+                       "entry \"%s\" has children\n",
+                       op->o_req_dn.bv_val, 0, 0 );
+               rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
+               rs->sr_text = "subtree rename not supported";
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       dnParent( dn, &p_dn );
-       dnParent( ndn, &p_ndn );
+       dnParent( &op->o_req_dn, &p_dn );
+       dnParent( &op->o_req_ndn, &p_ndn );
 
        /*
         * namingContext "" is not supported
@@ -637,9 +619,9 @@ backsql_modrdn(
        if ( p_dn.bv_len == 0 ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "parent is \"\" - aborting\n", 0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, 
-                               "", "not allowed within namingContext", 
-                               NULL, NULL );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "not allowed within namingContext";
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
 
@@ -649,10 +631,10 @@ backsql_modrdn(
        e.e_attrs = NULL;
        e.e_name = p_dn;
        e.e_nname = p_ndn;
-       if ( !access_allowed( be, conn, op, &e, slap_schema.si_ad_children, 
+       if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
                                NULL, ACL_WRITE, NULL ) ) {
                Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0, 0, 0 );
-               res = LDAP_INSUFFICIENT_ACCESS;
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
                goto modrdn_return;
        }
 
@@ -663,14 +645,14 @@ backsql_modrdn(
                if ( newSuperior->bv_len == 0 ) {
                        Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                                "newSuperior is \"\" - aborting\n", 0, 0, 0 );
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, 
-                                       "", "not allowed within namingContext", 
-                                       NULL, NULL );
+                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                       rs->sr_text = "not allowed within namingContext";
+                       send_ldap_result( op, rs );
                        goto modrdn_return;
                }
 
                new_pdn = newSuperior;
-               new_npdn = nnewSuperior;
+               new_npdn = op->oq_modrdn.rs_nnewSup;
 
                e.e_name = *new_pdn;
                e.e_nname = *new_npdn;
@@ -678,12 +660,11 @@ backsql_modrdn(
                /*
                 * Check for children access to new parent
                 */
-               if ( !access_allowed( be, conn, op, &e, 
-                                       slap_schema.si_ad_children, 
+               if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
                                        NULL, ACL_WRITE, NULL ) ) {
                        Debug( LDAP_DEBUG_TRACE, "no access to new parent\n", 
                                        0, 0, 0 );
-                       res = LDAP_INSUFFICIENT_ACCESS;
+                       rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
                        goto modrdn_return;
                }
 
@@ -699,48 +680,51 @@ backsql_modrdn(
                newSuperior = NULL;
        }
 
-       if ( newSuperior && dn_match( ndn, new_npdn ) ) {
+       if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "newSuperior is equal to entry being moved "
                        "- aborting\n", 0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", 
-                               NULL, NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "newSuperior is equal to old DN";
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
 
-       build_new_dn( &new_dn, new_pdn, newrdn ); 
-       if ( dnNormalize2( NULL, &new_dn, &new_ndn ) != LDAP_SUCCESS ) {
+       build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn, NULL );
+       rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
+               op->o_tmpmemctx );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "new dn is invalid ('%s') - aborting\n",
                        new_dn.bv_val, 0, 0 );
-               send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, "", 
-                               NULL, NULL, NULL );
+               rs->sr_text = "unable to build new DN";
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
        
        Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): new entry dn is '%s'\n",
                        new_dn.bv_val, 0, 0 );
 
-       res = backsql_dn2id( bi, &pe_id, dbh, &p_ndn );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &pe_id, dbh, &p_ndn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "could not lookup old parent entry id\n", 0, 0, 0 );
-               send_ldap_result( conn, op, res, "", 
-                               res == LDAP_OTHER ? "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ? "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
 
        Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                "old parent entry id is %ld\n", pe_id.id, 0, 0 );
 
-       res = backsql_dn2id( bi, &new_pid, dbh, new_npdn );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &new_pid, dbh, new_npdn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "could not lookup new parent entry id\n", 0, 0, 0 );
-               send_ldap_result( conn, op, res, "",
-                               res == LDAP_OTHER ? "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ? "SQL-backend error" : NULL;
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
        
@@ -759,8 +743,9 @@ backsql_modrdn(
                        "failed to delete record from ldap_entries\n",
                        0, 0, 0 );
                backsql_PrintErrors( bi->db_env, dbh, sth, rc );
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
 
@@ -780,8 +765,9 @@ backsql_modrdn(
                Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
                        "could not insert ldap_entries record\n", 0, 0, 0 );
                backsql_PrintErrors( bi->db_env, dbh, sth, rc );
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                goto modrdn_return;
        }
 
@@ -789,7 +775,8 @@ backsql_modrdn(
         * Get attribute type and attribute value of our new rdn,
         * we will need to add that to our new entry
         */
-       if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text, 
+       if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn,
+                               (char **)&rs->sr_text, 
                                LDAP_DN_FORMAT_LDAP ) ) {
 #ifdef NEW_LOGGING
                LDAP_LOG ( OPERATION, ERR, 
@@ -802,7 +789,7 @@ backsql_modrdn(
                        "type(s)/values(s) of newrdn\n", 
                        0, 0, 0 );
 #endif
-               rc = LDAP_INVALID_DN_SYNTAX;
+               rs->sr_err = LDAP_INVALID_DN_SYNTAX;
                goto modrdn_return;
        }
 
@@ -810,19 +797,20 @@ backsql_modrdn(
        LDAP_LOG ( OPERATION, RESULTS, 
                "backsql_modrdn: new_rdn_type=\"%s\", "
                "new_rdn_val=\"%s\"\n",
-               new_rdn[ 0 ][ 0 ]->la_attr.bv_val, 
-               new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
+               new_rdn[ 0 ]->la_attr.bv_val, 
+               new_rdn[ 0 ]->la_value.bv_val, 0 );
 #else
        Debug( LDAP_DEBUG_TRACE,
                "backsql_modrdn: new_rdn_type=\"%s\", "
                "new_rdn_val=\"%s\"\n",
-               new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
-               new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
+               new_rdn[ 0 ]->la_attr.bv_val,
+               new_rdn[ 0 ]->la_value.bv_val, 0 );
 #endif
 
-       if ( deleteoldrdn ) {
-               if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
-                       LDAP_DN_FORMAT_LDAP ) ) {
+       if ( op->oq_modrdn.rs_deleteoldrdn ) {
+               if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn,
+                                       (char **)&rs->sr_text,
+                                       LDAP_DN_FORMAT_LDAP ) ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG ( OPERATION, ERR, 
                                "backsql_modrdn: can't figure out "
@@ -834,37 +822,30 @@ backsql_modrdn(
                                "the old_rdn type(s)/value(s)\n", 
                                0, 0, 0 );
 #endif
-                       rc = LDAP_OTHER;
+                       rs->sr_err = LDAP_OTHER;
                        goto modrdn_return;             
                }
        }
 
        e.e_name = new_dn;
        e.e_nname = new_ndn;
-       res = slap_modrdn2mods( be, conn, op, &e, old_rdn, new_rdn, 
-                       deleteoldrdn, &mod );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                goto modrdn_return;
        }
 
-       if ( !acl_check_modlist( be, conn, op, &e, mod )) {
-               res = LDAP_INSUFFICIENT_ACCESS;
+       if ( !acl_check_modlist( op, &e, mod )) {
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
                goto modrdn_return;
        }
 
        oc = backsql_id2oc( bi, e_id.oc_id );
-       res = backsql_modify_internal( bi, dbh, oc, &e_id, mod, &text );
+       rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
 
-       if ( res == LDAP_SUCCESS ) {
+       if ( rs->sr_err == LDAP_SUCCESS ) {
 
                /*
                 * Commit only if all operations succeed
-                *
-                * FIXME: backsql_modify_internal() does not fail 
-                * if add/delete operations are not available, or
-                * if a multiple value add actually results in a replace, 
-                * or if a single operation on an attribute fails for any
-                * reason
                 */
                SQLTransact( SQL_NULL_HENV, dbh,
                                op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
@@ -896,20 +877,16 @@ modrdn_return:
                }
        }
 
-       send_ldap_result( conn, op, res, "", text, NULL, NULL );
+       send_ldap_result( op, rs );
 
        Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
-       return 0;
+       return op->o_noop;
 }
 
 int
-backsql_add(
-       BackendDB       *be,
-       Connection      *conn,
-       Operation       *op,
-       Entry           *e )
+backsql_add( Operation *op, SlapReply *rs )
 {
-       backsql_info            *bi = (backsql_info*)be->be_private;
+       backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        SQLHDBC                 dbh;
        SQLHSTMT                sth;
        unsigned long           new_keyval = 0;
@@ -919,7 +896,6 @@ backsql_add(
        backsql_at_map_rec      *at_rec = NULL;
        backsql_entryID         e_id, parent_id;
        Entry                   p;
-       int                     res;
        Attribute               *at;
        struct berval           *at_val;
        struct berval           pdn;
@@ -929,105 +905,103 @@ backsql_add(
        int                     prc;
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_add(): adding entry '%s'\n",
-                       e->e_name.bv_val, 0, 0 );
-
-       for ( at = e->e_attrs; at != NULL; at = at->a_next ) {
-               if ( at->a_desc == slap_schema.si_ad_objectClass ) {
-                       if ( global_schemacheck ) {
-                               const char      *text = NULL;
-                               char            textbuf[ 1024 ];
-                               size_t          textlen = sizeof( textbuf );
-                               struct berval   soc;
-
-                               int rc = structural_class( at->a_vals, &soc, 
-                                               NULL, &text, textbuf, textlen );
-                               if ( rc != LDAP_SUCCESS ) {
-                                       break;
-                               }
-                               oc = backsql_name2oc( bi, &soc );
+                       op->oq_add.rs_e->e_name.bv_val, 0, 0 );
 
-                       } else {
+       /* check schema */
+       if ( global_schemacheck ) {
+               char            textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
 
-                               /*
-                                * FIXME: only the objectClass provided first
-                                * is considered when creating a new entry
-                                */
-                               oc = backsql_name2oc( bi, &at->a_vals[ 0 ] );
-                       }
+               rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
+                               NULL,
+                               &rs->sr_text, textbuf, sizeof( textbuf ) );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
+                       Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
+                               "entry failed schema check -- aborting\n",
+                               0, 0, 0 );
+                       send_ldap_result( op, rs );
+                       return 1;
+               }
+       }
+
+       /* search structural objectClass */
+       for ( at = op->oq_add.rs_e->e_attrs; at != NULL; at = at->a_next ) {
+               if ( at->a_desc == slap_schema.si_ad_structuralObjectClass ) {
                        break;
                }
        }
 
+       /* there must exist */
+       assert( at != NULL );
+
+       /* I guess we should play with sub/supertypes to find a suitable oc */
+       oc = backsql_name2oc( bi, &at->a_vals[0] );
+
        if ( oc == NULL ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
                        "cannot determine objectclass of entry -- aborting\n",
                        0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, "",
-                               "operation not permitted within namingContext",
-                               NULL, NULL );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "operation not permitted within namingContext";
+               send_ldap_result( op, 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 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, "",
-                               "operation not permitted within namingContext",
-                               NULL, NULL );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "operation not permitted within namingContext";
+               send_ldap_result( op, 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, but none is defined"
-                       "- aborting\n", 0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, "",
-                               "operation not permitted within namingContext",
-                               NULL, NULL );
+                       "create procedure needs select procedure, "
+                       "but none is defined - aborting\n", 0, 0, 0 );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "operation not permitted within namingContext";
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       prc = backsql_get_db_conn( be, conn, &dbh );
-       if ( prc != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_get_db_conn( op, &dbh );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
                        "could not get connection handle - exiting\n", 
                        0, 0, 0 );
-               send_ldap_result( conn, op, prc, "",
-                               prc == LDAP_OTHER ?  "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ?  "SQL-backend error" : NULL,
+               send_ldap_result( op, rs );
                return 1;
        }
 
        /*
         * Check if entry exists
         */
-       res = backsql_dn2id( bi, &e_id, dbh, &e->e_name );
-       if ( res == LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->oq_add.rs_e->e_name );
+       if ( rs->sr_err == LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
                        "entry '%s' exists\n",
-                       e->e_name.bv_val, 0, 0 );
-               send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", 
-                               NULL, NULL, NULL );
+                       op->oq_add.rs_e->e_name.bv_val, 0, 0 );
+               rs->sr_err = LDAP_ALREADY_EXISTS;
+               send_ldap_result( op, rs );
                return 1;
        }
 
        /*
         * Check if parent exists
         */
-       dnParent( &e->e_name, &pdn );
-       res = backsql_dn2id( bi, &parent_id, dbh, &pdn );
-       if ( res != LDAP_SUCCESS ) {
-               
-               /*
-                * NO SUCH OBJECT seems more appropriate
-                */
+       dnParent( &op->oq_add.rs_e->e_name, &pdn );
+       rs->sr_err = backsql_dn2id( bi, &parent_id, dbh, &pdn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
                        "could not lookup parent entry for new record '%s'\n",
                        pdn.bv_val, 0, 0 );
 
-               if ( res != LDAP_NO_SUCH_OBJECT ) {
-                       send_ldap_result( conn, op, res, "", NULL, NULL, NULL );
+               if ( rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
+                       send_ldap_result( op, rs );
                        return 1;
                }
 
@@ -1036,7 +1010,7 @@ backsql_add(
                 */
                while ( 1 ) {
                        struct berval   dn;
-                       char            *matched = "";
+                       char            *matched = NULL;
 
                        dn = pdn;
                        dnParent( &dn, &pdn );
@@ -1044,8 +1018,8 @@ backsql_add(
                        /*
                         * Empty DN ("") defaults to LDAP_SUCCESS
                         */
-                       res = backsql_dn2id( bi, &parent_id, dbh, &pdn );
-                       switch ( res ) {
+                       rs->sr_err = backsql_dn2id( bi, &parent_id, dbh, &pdn );
+                       switch ( rs->sr_err ) {
                        case LDAP_NO_SUCH_OBJECT:
                                if ( pdn.bv_len > 0 ) {
                                        break;
@@ -1057,8 +1031,9 @@ backsql_add(
                                /* fail over to next case */
 
                        default:
-                               send_ldap_result( conn, op, res, matched, 
-                                               NULL, NULL, NULL );
+                               rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                               rs->sr_matched = matched;
+                               send_ldap_result( op, rs );
                                return 1;
                        } 
                }
@@ -1073,50 +1048,47 @@ backsql_add(
 
        p.e_attrs = NULL;
        p.e_name = pdn;
-       dnParent( &e->e_nname, &p.e_nname );
-       if ( !access_allowed( be, conn, op, &p, slap_schema.si_ad_children,
+       dnParent( &op->oq_add.rs_e->e_nname, &p.e_nname );
+       if ( !access_allowed( op, &p, slap_schema.si_ad_children,
                                NULL, ACL_WRITE, NULL ) ) {
-               send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, 
-                               NULL, NULL, NULL, NULL );
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
+               send_ldap_result( op, rs );
                return 1;
        }
 
-#ifndef BACKSQL_REALLOC_STMT
        rc = SQLAllocStmt( dbh, &sth );
-#else /* BACKSQL_REALLOC_STMT */
-       rc = backsql_Prepare( dbh, &sth, oc->create_proc, 0 );
-#endif /* BACKSQL_REALLOC_STMT */
        if ( rc != SQL_SUCCESS ) {
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, 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 );
-#ifndef BACKSQL_REALLOC_STMT
-       rc = SQLExecDirect( sth, oc->create_proc, SQL_NTS );
-#else /* BACKSQL_REALLOC_STMT */
-       rc = SQLExecute( sth );
-#endif /* BACKSQL_REALLOC_STMT */
+               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 );
                backsql_PrintErrors( bi->db_env, dbh, sth, rc);
                SQLFreeStmt( sth, SQL_DROP );
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
+       if ( op->o_noop ) {
+               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      is_null;
+               SQLINTEGER      value_len;
 
                if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
 #ifndef BACKSQL_REALLOC_STMT
@@ -1125,16 +1097,18 @@ backsql_add(
                        SQLFreeStmt( sth, SQL_DROP );
                        rc = SQLAllocStmt( dbh, &sth );
                        if ( rc != SQL_SUCCESS ) {
-                               send_ldap_result( conn, op, LDAP_OTHER, "",
-                                       "SQL-backend error", NULL, NULL );
+                               rs->sr_err = LDAP_OTHER;
+                               rs->sr_text = "SQL-backend error";
+                               send_ldap_result( op, rs );
                                return 1;
                        }
 #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 ) {
-                               send_ldap_result( conn, op, LDAP_OTHER, "",
-                                       "SQL-backend error", NULL, NULL );
+                               rs->sr_err = LDAP_OTHER;
+                               rs->sr_text = "SQL-backend error";
+                               send_ldap_result( op, rs );
                                return 1;
                        }
                }
@@ -1150,8 +1124,9 @@ backsql_add(
                                0, 0, 0 );
                        backsql_PrintErrors( bi->db_env, dbh, sth, rc);
                        SQLFreeStmt( sth, SQL_DROP );
-                       send_ldap_result( conn, op, LDAP_OTHER, "",
-                                       "SQL-backend error", NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "SQL-backend error";
+                       send_ldap_result( op, rs );
                        return 1;
 
                } else if ( ncols != 1 ) {
@@ -1160,8 +1135,9 @@ backsql_add(
                                ncols, 0, 0 );
                        backsql_PrintErrors( bi->db_env, dbh, sth, rc);
                        SQLFreeStmt( sth, SQL_DROP );
-                       send_ldap_result( conn, op, LDAP_OTHER, "",
-                                       "SQL-backend error", NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "SQL-backend error";
+                       send_ldap_result( op, rs );
                        return 1;
                }
 
@@ -1186,25 +1162,21 @@ backsql_add(
                rc = SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_ULONG,
                                (SQLPOINTER)&new_keyval, 
                                (SQLINTEGER)sizeof( new_keyval ), 
-                               &is_null );
+                               &value_len );
 
                rc = SQLFetch( sth );
 
-#if 0
-               /*
-                * FIXME: what does is_null mean?
-                */
-               if ( is_null ) {
+               if ( value_len <= 0 ) {
                        Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
-                               "create_proc result is null\n",
+                               "create_proc result is empty?\n",
                                0, 0, 0 );
                        backsql_PrintErrors( bi->db_env, dbh, sth, rc);
                        SQLFreeStmt( sth, SQL_DROP );
-                       send_ldap_result( conn, op, LDAP_OTHER, "",
-                                       "SQL-backend error", NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "SQL-backend error";
+                       send_ldap_result( op, rs );
                        return 1;
                }
-#endif
        }
 
 #ifndef BACKSQL_REALLOC_STMT
@@ -1216,7 +1188,7 @@ backsql_add(
        Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
                "create_proc returned keyval=%ld\n", new_keyval, 0, 0 );
 
-       for ( at = e->e_attrs; at != NULL; at = at->a_next ) {
+       for ( at = op->oq_add.rs_e->e_attrs; at != NULL; at = at->a_next ) {
                SQLUSMALLINT    currpos;
 
                Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
@@ -1244,29 +1216,27 @@ backsql_add(
                                BACKSQL_OC_NAME( oc ), 0 );
 
                        if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                               send_ldap_result( conn, op, 
-                                               LDAP_UNWILLING_TO_PERFORM, "",
-                                               "operation not permitted "
-                                               "within namingContext",
-                                               NULL, NULL );
+                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                               rs->sr_text = "operation not permitted "
+                                       "within namingContext";
+                               send_ldap_result( op, rs );
                                return 1;
                        }
 
                        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",
                                at->a_desc->ad_cname.bv_val, 0, 0 );
 
                        if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                               send_ldap_result( conn, op, 
-                                               LDAP_UNWILLING_TO_PERFORM, "",
-                                               "operation not permitted "
-                                               "within namingContext",
-                                               NULL, NULL );
+                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                               rs->sr_text = "operation not permitted "
+                                       "within namingContext";
+                               send_ldap_result( op, rs );
                                return 1;
                        }
 
@@ -1274,14 +1244,13 @@ backsql_add(
                }
 
 #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 ) ) {
-                               send_ldap_result( conn, op, 
-                                               LDAP_OTHER, "",
-                                               "SQL-backend error",
-                                               NULL, NULL );
+                               rs->sr_err = LDAP_OTHER;
+                               rs->sr_text = "SQL-backend error";
+                               send_ldap_result( op, rs );
                                return 1;
                        }
 
@@ -1289,7 +1258,7 @@ backsql_add(
                }
 #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,
@@ -1298,7 +1267,7 @@ backsql_add(
                        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,
@@ -1314,7 +1283,7 @@ backsql_add(
                         * 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;
                                }
                        }
@@ -1329,14 +1298,14 @@ backsql_add(
 #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 */
@@ -1347,10 +1316,9 @@ backsql_add(
                                backsql_PrintErrors( bi->db_env, dbh, sth, rc );
 
                                if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
-                                       send_ldap_result( conn, op, 
-                                                       LDAP_OTHER, "",
-                                                       "SQL-backend error",
-                                                       NULL, NULL );
+                                       rs->sr_err = LDAP_OTHER;
+                                       rs->sr_text = "SQL-backend error";
+                                       send_ldap_result( op, rs );
                                        return 1;
                                }
                        }
@@ -1365,24 +1333,26 @@ backsql_add(
 #ifdef BACKSQL_REALLOC_STMT
        rc = backsql_Prepare( dbh, &sth, bi->insentry_query, 0 );
        if ( rc != SQL_SUCCESS ) {
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
 #endif /* BACKSQL_REALLOC_STMT */
        
-       backsql_BindParamStr( sth, 1, e->e_name.bv_val, BACKSQL_MAX_DN_LEN );
+       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,
                        0, 0, &new_keyval, 0, 0 );
 
        Debug( LDAP_DEBUG_TRACE, "backsql_add(): executing '%s' for dn '%s'\n",
-                       bi->insentry_query, e->e_name.bv_val, 0 );
+                       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 */
@@ -1397,8 +1367,9 @@ backsql_add(
                 * execute delete_proc to delete data added !!!
                 */
                SQLFreeStmt( sth, SQL_DROP );
-               send_ldap_result( conn, op, LDAP_OTHER, "", 
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
        
@@ -1406,119 +1377,113 @@ backsql_add(
 
        /*
         * Commit only if all operations succeed
-        *
-        * FIXME: backsql_add() does not fail if add operations 
-        * are not available for some attributes, or if
-        * a multiple value add actually results in a replace, 
-        * or if a single operation on an attribute fails 
-        * for any reason
         */
        SQLTransact( SQL_NULL_HENV, dbh, 
-                               op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
+                       op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
+
+       /*
+        * FIXME: NOOP does not work for add -- it works for all 
+        * the other operations, and I don't get the reason :(
+        */
 
-       send_ldap_result( conn, op, LDAP_SUCCESS, "",
-                       NULL, NULL, NULL );
-       return 0;
+       send_ldap_result( op, rs );
+
+       return op->o_noop;
 }
 
 int
-backsql_delete(
-       BackendDB       *be,
-       Connection      *conn,
-       Operation       *op,
-       struct berval   *dn,
-       struct berval   *ndn )
+backsql_delete( Operation *op, SlapReply *rs )
 {
-       backsql_info            *bi = (backsql_info*)be->be_private;
+       backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        SQLHDBC                 dbh;
        SQLHSTMT                sth;
        RETCODE                 rc;
        backsql_oc_map_rec      *oc = NULL;
        backsql_entryID         e_id;
        Entry                   e;
-       int                     res;
        /* first parameter no */
        SQLUSMALLINT            pno;
 
        Debug( LDAP_DEBUG_TRACE, "==>backsql_delete(): deleting entry '%s'\n",
-                       ndn->bv_val, 0, 0 );
+                       op->o_req_ndn.bv_val, 0, 0 );
 
-       dnParent( dn, &e.e_name );
-       dnParent( ndn, &e.e_nname );
+       dnParent( &op->o_req_dn, &e.e_name );
+       dnParent( &op->o_req_ndn, &e.e_nname );
        e.e_attrs = NULL;
 
        /* check parent for "children" acl */
-       if ( !access_allowed( be, conn, op, &e, slap_schema.si_ad_children, 
+       if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
                        NULL, ACL_WRITE, NULL ) ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
                        "no write access to parent\n", 
                        0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, 
-                               "", NULL, NULL, NULL );
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
+               send_ldap_result( op, rs );
                return 1;
 
        }
        
-       res = backsql_get_db_conn( be, conn, &dbh );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_get_db_conn( op, &dbh );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
                        "could not get connection handle - exiting\n", 
                        0, 0, 0 );
-               send_ldap_result( conn, op, res, "", 
-                               res == LDAP_OTHER ? "SQL-backend error" : "",
-                               NULL, NULL );
+               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+                       ? "SQL-backend error" : NULL,
+               send_ldap_result( op, rs );
                return 1;
        }
        
-       res = backsql_dn2id( bi, &e_id, dbh, ndn );
-       if ( res != LDAP_SUCCESS ) {
+       rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
                        "could not lookup entry id\n", 0, 0, 0 );
-               send_ldap_result( conn, op, res, "", NULL, NULL, NULL );
+               send_ldap_result( op, rs );
                return 1;
        }
 
-       res = backsql_has_children( bi, dbh, ndn );
-       switch ( res ) {
+       rs->sr_err = backsql_has_children( bi, dbh, &op->o_req_ndn );
+       switch ( rs->sr_err ) {
        case LDAP_COMPARE_TRUE:
                Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
-                       "entry \"%s\" has children\n", dn->bv_val, 0, 0 );
-               send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
-                               NULL, "subtree delete not supported",
-                               NULL, NULL );
+                       "entry \"%s\" has children\n",
+                       op->o_req_dn.bv_val, 0, 0 );
+               rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
+               rs->sr_text = "subtree delete not supported";
+               send_ldap_result( op, rs );
                return 1;
 
        case LDAP_COMPARE_FALSE:
                break;
 
        default:
-               send_ldap_result( conn, op, res, NULL, NULL, NULL, NULL );
+               send_ldap_result( op, rs );
                return 1;
        }
 
        oc = backsql_id2oc( bi, e_id.oc_id );
        if ( oc == NULL ) {
                Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
-                       "cannot determine objectclass of entry "
-                       "-- aborting\n", 0, 0, 0 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, "",
-                               "operation not permitted within namingContext",
-                               NULL, NULL );
+                       "cannot determine objectclass of entry -- aborting\n",
+                       0, 0, 0 );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "operation not permitted within namingContext";
+               send_ldap_result( op, 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 );
-               send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, "",
-                               "operation not permitted within namingContext",
-                               NULL, NULL );
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_text = "operation not permitted within namingContext";
+               send_ldap_result( op, rs );
                return 1;
        }
 
        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 );
@@ -1530,15 +1495,16 @@ backsql_delete(
                        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 );
                backsql_PrintErrors( bi->db_env, dbh, sth, rc );
                SQLFreeStmt( sth, SQL_DROP );
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
 #ifndef BACKSQL_REALLOC_STMT
@@ -1557,8 +1523,9 @@ backsql_delete(
                        0, 0, 0 );
                backsql_PrintErrors( bi->db_env, dbh, sth, rc );
                SQLFreeStmt( sth, SQL_DROP );
-               send_ldap_result( conn, op, LDAP_OTHER, "",
-                               "SQL-backend error", NULL, NULL );
+               rs->sr_err = LDAP_OTHER;
+               rs->sr_text = "SQL-backend error";
+               send_ldap_result( op, rs );
                return 1;
        }
        
@@ -1574,11 +1541,13 @@ backsql_delete(
         * for any reason
         */
        SQLTransact( SQL_NULL_HENV, dbh, 
-                               op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
+                       op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
 
-       send_ldap_result( conn, op, LDAP_SUCCESS, "", NULL, NULL, NULL );
+       rs->sr_err = LDAP_SUCCESS;
+       send_ldap_result( op, rs );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
-       return 0;
+
+       return op->o_noop;
 }
 
 #endif /* SLAPD_SQL */