Package craftsman.spy

Source Code of craftsman.spy.SpyStatement

/*
* Craftsman Spy.
* Copyright (C) 2005  S�bastien LECACHEUR
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package craftsman.spy;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;

/**
* This classe is used for executing a static SQL statement and
* returning the results it produces.
*
* @author S�bastien LECACHEUR
*/
public class SpyStatement extends SpyEntity implements Statement {
  /**
   * The real SQL statement instance.
   */
  private Statement real = null;


  /**
   * The list of all the SQL in the current batch.
   */
  protected ArrayList batch = null;


  /**
   * Constructs a new Spy JDBC statement.
   *
   * @param c Connection The used connection.
   * @param stm Statement The real JDBC statement.
   */
  protected SpyStatement ( Connection c, Statement stm) {
    super ( c);
    real = stm;
    batch = new ArrayList();
  }


  /**
   * @see Statement#getFetchDirection()
   */
  public int getFetchDirection() throws SQLException {
    return real.getFetchDirection();
  }


  /**
   * @see Statement#getFetchSize()
   */
  public int getFetchSize() throws SQLException {
    return real.getFetchSize();
  }


  /**
   * @see Statement#getMaxFieldSize()
   */
  public int getMaxFieldSize() throws SQLException {
    return real.getMaxFieldSize();
  }


  /**
   * @see Statement#getMaxRows()
   */
  public int getMaxRows() throws SQLException {
    return real.getMaxRows();
  }


  /**
   * @see Statement#getQueryTimeout()
   */
  public int getQueryTimeout() throws SQLException {
    return real.getQueryTimeout();
  }


  /**
   * @see Statement#getResultSetConcurrency()
   */
  public int getResultSetConcurrency() throws SQLException {
    return real.getResultSetConcurrency();
  }


  /**
   * @see Statement#getResultSetHoldability()
   */
  public int getResultSetHoldability() throws SQLException {
    return real.getResultSetHoldability();
  }


  /**
   * @see Statement#getResultSetType()
   */
  public int getResultSetType() throws SQLException {
    return real.getResultSetType();
  }


  /**
   * @see Statement#getUpdateCount()
   */
  public int getUpdateCount() throws SQLException {
    return real.getUpdateCount();
  }


  /**
   * @see Statement#cancel()
   */
  public void cancel() throws SQLException {
    real.cancel();
  }


  /**
   * @see Statement#clearBatch()
   */
  public void clearBatch() throws SQLException {
    batch.clear();
    real.clearBatch();
  }


  /**
   * @see Statement#clearWarnings()
   */
  public void clearWarnings() throws SQLException {
    real.clearWarnings();
  }


  /**
   * @see Statement#close()
   */
  public void close() throws SQLException {
    real.close();
  }


  /**
   * @see Statement#getMoreResults()
   */
  public boolean getMoreResults() throws SQLException {
    return real.getMoreResults();
  }


  /**
   * @see Statement#executeBatch()
   */
  public int[] executeBatch() throws SQLException {
    long end, start = System.currentTimeMillis();
    int[] result = null;


    try {
      result = real.executeBatch();
      end = System.currentTimeMillis();

      if ( batch.size() != result.length) {
        if ( log.isWarnEnabled()) log.warn(getId()+":"+"expecting:"+batch.size()+" but was:"+result.length);
      }
      if ( log.isInfoEnabled()) {
        log.info(getId()+":"+"executing batch...");
        for (int i = 0; i < result.length && i <batch.size(); i++) {
          switch(result[i]) {
            case SUCCESS_NO_INFO:  log.info(getId()+":"+batch.get(i)+" => SUCCESS_NO_INFO");
                        break;

            case EXECUTE_FAILED:  log.info(getId()+":"+batch.get(i)+" => EXECUTE_FAILED");
                        break;

            default:        log.info(getId()+":"+batch.get(i)+" => "+result[i]);
                        break;
          }
        }
        log.info(getId()+":"+"batch executed ("+(end-start)+" ms)");
      }
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      for (int i = 0; i < batch.size(); i++) {
        log.error(getId()+":"+batch.get(i)+" => ...");
      }
      if ( log.isWarnEnabled()) log.warn(getId()+":batch.size="+batch.size());
      if ( log.isErrorEnabled()) log.error(getId()+":"+batch+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#setFetchDirection(int)
   */
  public void setFetchDirection(int direction) throws SQLException {
    real.setFetchDirection(direction);
  }


  /**
   * @see Statement#setFetchSize(int)
   */
  public void setFetchSize(int rows) throws SQLException {
    real.setFetchSize(rows);
  }


  /**
   * @see Statement#setMaxFieldSize(int)
   */
  public void setMaxFieldSize(int max) throws SQLException {
    real.setMaxFieldSize(max);
  }


  /**
   * @see Statement#setMaxRows(int)
   */
  public void setMaxRows(int max) throws SQLException {
    real.setMaxRows(max);
  }


  /**
   * @see Statement#setQueryTimeout(int)
   */
  public void setQueryTimeout(int seconds) throws SQLException {
    real.setQueryTimeout(seconds);
  }


  /**
   * @see Statement#getMoreResults(int)
   */
  public boolean getMoreResults(int current) throws SQLException {
    return real.getMoreResults(current);
  }


  /**
   * @see Statement#setEscapeProcessing(boolean)
   */
  public void setEscapeProcessing(boolean enable) throws SQLException {
    real.setEscapeProcessing(enable);
  }


  /**
   * @see Statement#executeUpdate(String)
   */
  public int executeUpdate(String sql) throws SQLException {
    long end, start = System.currentTimeMillis();
    int result = 0;


    try {
      result = real.executeUpdate(sql);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#addBatch(String)
   */
  public void addBatch(String sql) throws SQLException {
    batch.add(sql);
    real.addBatch(sql);
  }


  /**
   * @see Statement#setCursorName(String)
   */
  public void setCursorName(String name) throws SQLException {
    real.setCursorName(name);
  }


  /**
   * @see Statement#execute(String)
   */
  public boolean execute(String sql) throws SQLException {
    long end, start = System.currentTimeMillis();
    boolean result = false;


    try {
      result = real.execute(sql);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#executeUpdate(String, int)
   */
  public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
    long end, start = System.currentTimeMillis();
    int result = 0;


    try {
      result = real.executeUpdate(sql,autoGeneratedKeys);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#execute(String, int)
   */
  public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
    long end, start = System.currentTimeMillis();
    boolean result = false;


    try {
      result = real.execute(sql,autoGeneratedKeys);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#executeUpdate(String, int[])
   */
  public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
    long end, start = System.currentTimeMillis();
    int result = 0;


    try {
      result = real.executeUpdate(sql,columnIndexes);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#execute(String, int[])
   */
  public boolean execute(String sql, int[] columnIndexes) throws SQLException {
    long end, start = System.currentTimeMillis();
    boolean result = false;


    try {
      result = real.execute(sql,columnIndexes);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#getGeneratedKeys()
   */
  public ResultSet getGeneratedKeys() throws SQLException {
    return new SpyResultSet ( getConnection(), this, real.getGeneratedKeys());
  }


  /**
   * @see Statement#getResultSet()
   */
  public ResultSet getResultSet() throws SQLException {
    return new SpyResultSet ( getConnection(), this, real.getResultSet());
  }


  /**
   * @see Statement#getWarnings()
   */
  public SQLWarning getWarnings() throws SQLException {
    SQLWarning current, sw = real.getWarnings();


    if ( (current = sw)!=null) {
      do {
        if ( log.isInfoEnabled()) log.info(getId()+":sql warning state="+current.getSQLState()+",code="+current.getErrorCode()+",message="+current.getMessage());
      } while ( (current = current.getNextWarning())!=null);
    }

    return sw;
  }


  /**
   * @see Statement#executeUpdate(String, String[])
   */
  public int executeUpdate(String sql, String[] columnNames) throws SQLException {
    long end, start = System.currentTimeMillis();
    int result = 0;


    try {
      result = real.executeUpdate(sql,columnNames);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#execute(String, String[])
   */
  public boolean execute(String sql, String[] columnNames) throws SQLException {
    long end, start = System.currentTimeMillis();
    boolean result = false;


    try {
      result = real.execute(sql,columnNames);
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }


  /**
   * @see Statement#executeQuery(String)
   */
  public ResultSet executeQuery(String sql) throws SQLException {
    long end, start = System.currentTimeMillis();
    ResultSet result = null;


    try {
      result = new SpyResultSet ( getConnection(), this, real.executeQuery(sql));
      end = System.currentTimeMillis();
      if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => ... ("+(end-start)+" ms)");
    } catch ( SQLException e) {
      end = System.currentTimeMillis();
      if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
      throw e;
    }

    return result;
  }
}
TOP

Related Classes of craftsman.spy.SpyStatement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.