CompoundSqlOperation.

COMPONENT JAVA
Applying Design Patterns to JDBC: Building a Lightweight Object-Relational Mapping Framework
Frank Sauer
Listing 6. CompoundSqlOperation.


package com.trcinc.infrastructureservices.jdbc;
import java.sql.*;
import java.util.*;

/**
 * CompoundSqlOperation allows for multiple AbstractSqlOperations
 * to be executed on the same JDBC connection. It has three
 * convenience constructors taking a number of
 * AbstractSqlOperations to be combined and has an add method to
 * add more.
 * @author  Frank Sauer
 * @version 1.0
 */
public class CompoundSqlOperation extends AbstractSqlOperation {

    private List operations = new ArrayList();
    
    /**
     * Creates new CompoundSqlOperation containing the single
     * AbstractSqlOperation op1
     */
    public CompoundSqlOperation(AbstractSqlOperation op1) {
        add(op1);
    }
    
    /**
     * Creates new CompoundSqlOperation containing the two
     * AbstractSqlOperations op1 and op2
     */
    public CompoundSqlOperation(AbstractSqlOperation op1,
                                AbstractSqlOperation op2) {
        add(op1);
        add(op2);
    }
    
    /**
     * Creates new CompoundSqlOperation containing the three
     * AbstractSqlOperations op1, op2 and op3
     */
    public CompoundSqlOperation(AbstractSqlOperation op1,
                                AbstractSqlOperation op2,
                                AbstractSqlOperation op3) {
        add(op1);
        add(op2);
        add(op3);
    }

    /**
     * Add the AbstractSqlOperation op to my list of operations.
     */
    public void add(AbstractSqlOperation op) {
        operations.add(op);
    }
    
    /**
     * Execute all operations contained in my operations list on the
     * same Connection. This method returns null. Compounding
     * SQL operations only makes sense for update, insert and
     * remove operations, which have no result.
     */
    protected Object execute(Connection c) throws SQLException {
        if (debugSQL()) System.err.println("Start of
                        CompoundSqlOperation");
        int index = 1;
        for (Iterator i = operations.iterator();i.hasNext();index++) {
            AbstractSqlOperation op = (AbstractSqlOperation)i.next();
            op.execute(c);
        }
        if (debugSQL()) System.err.println("End of
                        CompoundSqlOperation");
        return null;
    }
 }