Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/jdbc/Expectations.java
package org.hibernate.jdbc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.StaleStateException;
import org.hibernate.HibernateException;
import org.hibernate.engine.ExecuteUpdateResultCheckStyle;
import org.hibernate.util.JDBCExceptionReporter;
import org.hibernate.exception.GenericJDBCException;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Types;

/**
 * Holds various often used {@link Expectation} definitions.
 *
 @author Steve Ebersole
 */
public class Expectations {
  private static final Log log = LogFactory.getLogExpectations.class );

  public static final int USUAL_EXPECTED_COUNT = 1;
  public static final int USUAL_PARAM_POSITION = 1;


  // Base Expectation impls ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static class BasicExpectation implements Expectation {
    private final int expectedRowCount;

    protected BasicExpectation(int expectedRowCount) {
      this.expectedRowCount = expectedRowCount;
      if expectedRowCount < ) {
        throw new IllegalArgumentException"Expected row count must be greater than zero" );
      }
    }

    public final void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition) {
      rowCount = determineRowCountrowCount, statement );
      if batchPosition < ) {
        checkNonBatchedrowCount );
      }
      else {
        checkBatchedrowCount, batchPosition );
      }
    }

    private void checkBatched(int rowCount, int batchPosition) {
      if rowCount == -) {
        if log.isDebugEnabled() ) {
          log.debug"success of batch update unknown: " + batchPosition );
        }
      }
      else if rowCount == -) {
        throw new BatchFailedException"Batch update failed: " + batchPosition );
      }
      else {
        if expectedRowCount > rowCount ) {
          throw new StaleStateException(
              "Batch update returned unexpected row count from update [" + batchPosition +
              "]; actual row count: " + rowCount +
              "; expected: " + expectedRowCount
          );
        }
        if expectedRowCount < rowCount ) {
          String msg = "Batch update returned unexpected row count from update [" +
                       batchPosition + "]; actual row count: " + rowCount +
                       "; expected: " + expectedRowCount;
          throw new BatchedTooManyRowsAffectedExceptionmsg, expectedRowCount, rowCount, batchPosition );
        }
      }
    }

    private void checkNonBatched(int rowCount) {
      if expectedRowCount > rowCount ) {
        throw new StaleStateException(
            "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount
        );
      }
      if expectedRowCount < rowCount ) {
        String msg = "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount;
        throw new TooManyRowsAffectedExceptionmsg, expectedRowCount, rowCount );
      }
    }

    public int prepare(PreparedStatement statementthrows SQLException, HibernateException {
      return 0;
    }

    public boolean canBeBatched() {
      return true;
    }

    protected int determineRowCount(int reportedRowCount, PreparedStatement statement) {
      return reportedRowCount;
    }
  }

  public static class BasicParamExpectation extends BasicExpectation {
    private final int parameterPosition;
    protected BasicParamExpectation(int expectedRowCount, int parameterPosition) {
      superexpectedRowCount );
      this.parameterPosition = parameterPosition;
    }

    public int prepare(PreparedStatement statementthrows SQLException, HibernateException {
      toCallableStatementstatement ).registerOutParameterparameterPosition, Types.NUMERIC );
      return 1;
    }

    public boolean canBeBatched() {
      return false;
    }

    protected int determineRowCount(int reportedRowCount, PreparedStatement statement) {
      try {
        return toCallableStatementstatement ).getIntparameterPosition );
      }
      catchSQLException sqle ) {
        JDBCExceptionReporter.logExceptionssqle, "could not extract row counts from CallableStatement" );
        throw new GenericJDBCException"could not extract row counts from CallableStatement", sqle );
      }
    }

    private CallableStatement toCallableStatement(PreparedStatement statement) {
      if ! CallableStatement.class.isInstancestatement ) ) {
        throw new HibernateException"BasicParamExpectation operates exclusively on CallableStatements : " + statement.getClass() );
      }
      return CallableStatement statement;
    }
  }


  // Various Expectation instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static final Expectation NONE = new Expectation() {
    public void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition) {
      // explicitly perform no checking...
    }

    public int prepare(PreparedStatement statement) {
      return 0;
    }

    public boolean canBeBatched() {
      return true;
    }
  };

  public static final Expectation BASIC = new BasicExpectationUSUAL_EXPECTED_COUNT );

  public static final Expectation PARAM = new BasicParamExpectationUSUAL_EXPECTED_COUNT, USUAL_PARAM_POSITION );


  public static Expectation appropriateExpectation(ExecuteUpdateResultCheckStyle style) {
    if style == ExecuteUpdateResultCheckStyle.NONE ) {
      return NONE;
    }
    else if style == ExecuteUpdateResultCheckStyle.COUNT ) {
      return BASIC;
    }
    else if style == ExecuteUpdateResultCheckStyle.PARAM ) {
      return PARAM;
    }
    else {
      throw new HibernateException"unknown check style : " + style );
    }
  }

  private Expectations() {
  }
}