Package net.sourceforge.jdbclogger.core

Source Code of net.sourceforge.jdbclogger.core.PreparedStatementWrapper

package net.sourceforge.jdbclogger.core;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import net.sourceforge.jdbclogger.core.formatters.ParameterFormatter;
import net.sourceforge.jdbclogger.core.util.Tokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
* @author Martin Marinschek (latest modification by $Author: catalean $)
* @version $Revision: 123 $ $Date: 2007-09-18 04:44:46 +0800 (周二, 2007-09-18) $
*/
public class PreparedStatementWrapper extends StatementWrapper implements PreparedStatement
{
    private static Log log = LogFactory.getLog(PreparedStatementWrapper.class);

    protected List _paramFormatterList;

    protected PreparedStatement _prep;

    protected List _batchList;
    protected int _currentIndex = 0;

    protected Map _paramIndexToListIndex = new HashMap();

    private List getSqlAsList()
    {
        if(_batchList == null)
        {
            _batchList = new ArrayList();
        }

        if(_batchList.size()==_currentIndex)
        {
            if(_batchList.size()==0)
            {
                _batchList.add(new ArrayList());
            }
            else
            {
                _batchList.add(new ArrayList((List) _batchList.get(0)));
            }
        }
        else if(_batchList.size()<_currentIndex)
        {
            throw new IllegalStateException("batchList size less than currentIndex");
        }

        return (List) _batchList.get(_currentIndex);
    }

    public PreparedStatementWrapper(
            PreparedStatement statement, String sql, List formatters)
    {
        super(statement);

        Tokenizer tokenizer = new Tokenizer(sql,new String[]{"?"},true,
                true,'\"','\"');

        int delimiterCount = 1;

        while(tokenizer.hasMoreTokens())
        {
            String token = tokenizer.nextToken();

            if(token.equals("?"))
            {
                _paramIndexToListIndex.put(new Integer(delimiterCount),
                        new Integer(getSqlAsList().size()));
                delimiterCount++;
            }

            getSqlAsList().add(token);
        }

        _paramFormatterList = formatters;

        _prep = statement;
    }

    protected void setParameter(int parameterIndex, Object parameter)
    {
        Integer listIndex = (Integer)
                _paramIndexToListIndex.get(new Integer(parameterIndex));

        if(listIndex != null)
        {
            String sql;
            ParameterFormatter pf;

            if (parameter == null)
                sql = "null";
            else if ((pf = getParamFormatterFor(parameter.getClass ())) != null)
                sql = pf.format (parameter);
            else
                sql = "'"+parameter+"'";

            getSqlAsList().set(listIndex.intValue(), sql);
        }
    }

    protected ParameterFormatter getParamFormatterFor (Class clazz)
    {
        for (int i = 0; i < _paramFormatterList.size (); i++)
        {
            ParameterFormatter pf = (ParameterFormatter) _paramFormatterList.get (i);

            if (pf.getFormattingClass().isAssignableFrom (clazz))
                return pf;
        }

        return null;
    }

    protected void logStatement()
    {
        if(_batchList!=null)
        {
            for(int i=0; i<_batchList.size();i++)
            {
                List sqlAsList = (List) _batchList.get(i);

                if(log.isDebugEnabled())
                {
                    StringBuffer buf = new StringBuffer();

                    for (int j = 0; j < sqlAsList.size(); j++)
                    {
                        String token = (String) sqlAsList.get(j);
                        buf.append(token);
                    }

                    log.debug("Prepared Statement : " + buf.toString());
                }
            }
        }
    }

    public int executeUpdate() throws SQLException
    {
        logStatement();
      long begin = System.currentTimeMillis ();

      int res = _prep.executeUpdate();

      logTime (begin);

      return res;
    }

    public void addBatch() throws SQLException
    {
        _currentIndex++;

        _prep.addBatch();
    }

    public int[] executeBatch() throws SQLException
    {
        logStatement();

        _currentIndex = 0;

        return super.executeBatch();
    }

    public void clearBatch() throws SQLException
    {
        _batchList = null;
        _currentIndex = 0;

        super.clearBatch();
    }

    public void clearParameters() throws SQLException
    {
        _prep.clearParameters();
    }

    public boolean execute() throws SQLException
    {
        logStatement();
      long begin = System.currentTimeMillis ();

      boolean res = _prep.execute();

      logTime (begin);      

        return res;
    }

    public void setByte(int parameterIndex, byte x) throws SQLException
    {
        setParameter(parameterIndex, new Byte(x));

        _prep.setByte(parameterIndex, x);
    }

    public void setDouble(int parameterIndex, double x) throws SQLException
    {
        setParameter(parameterIndex, new Double(x));

        _prep.setDouble(parameterIndex, x);
    }

    public void setFloat(int parameterIndex, float x) throws SQLException
    {
        setParameter(parameterIndex, new Float(x));

        _prep.setFloat(parameterIndex, x);
    }

    public void setInt(int parameterIndex, int x) throws SQLException
    {
        setParameter(parameterIndex, new Integer(x));

        _prep.setInt(parameterIndex, x);
    }

    public void setNull(int parameterIndex, int sqlType) throws SQLException
    {
        setParameter(parameterIndex, null);

        _prep.setNull(parameterIndex, sqlType);
    }

    public void setLong(int parameterIndex, long x) throws SQLException
    {
        setParameter(parameterIndex, new Long(x));

        _prep.setLong(parameterIndex, x);
    }

    public void setShort(int parameterIndex, short x) throws SQLException
    {
        setParameter(parameterIndex, new Short(x));

        _prep.setShort(parameterIndex, x);
    }

    public void setBoolean(int parameterIndex, boolean x) throws SQLException
    {
        setParameter(parameterIndex, Boolean.valueOf(x));

        _prep.setBoolean(parameterIndex, x);
    }

    public void setBytes(int parameterIndex, byte x[]) throws SQLException
    {
        //how could this be done ?
        setParameter(parameterIndex, x);

        _prep.setBytes(parameterIndex, x);
    }

    public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setAsciiStream(parameterIndex, x, length);
    }

    public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setBinaryStream(parameterIndex, x, length);
    }

    /**
     * @deprecated
     */
    public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setUnicodeStream(parameterIndex, x, length);
    }

    public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException
    {
        setParameter(parameterIndex, reader);

        _prep.setCharacterStream(parameterIndex, reader, length);
    }

    public void setObject(int parameterIndex, Object x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setObject(parameterIndex, x);
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setObject(parameterIndex, x, targetSqlType);
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setObject(parameterIndex, x, targetSqlType, scale);
    }

    public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException
    {
        _prep.setNull(paramIndex, sqlType, typeName);
    }

    public void setString(int parameterIndex, String x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setString(parameterIndex, x);
    }

    public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setBigDecimal(parameterIndex, x);
    }

    public void setURL(int parameterIndex, URL x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setURL(parameterIndex, x);
    }

    public void setArray(int parameterIndex, Array x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setArray(parameterIndex, x);
    }

    public void setBlob(int parameterIndex, Blob x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setBlob(parameterIndex, x);
    }

    public void setClob(int parameterIndex, Clob x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setClob(parameterIndex, x);
    }

    public void setDate(int parameterIndex, Date x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setDate(parameterIndex, x);
    }

    public ParameterMetaData getParameterMetaData() throws SQLException
    {
        return _prep.getParameterMetaData();
    }

    public void setRef(int parameterIndex, Ref x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setRef(parameterIndex, x);
    }

    public ResultSet executeQuery() throws SQLException
    {
        logStatement();
      long begin = System.currentTimeMillis ();

      ResultSet res = _prep.executeQuery();

      logTime (begin);

        return res;
    }

    public ResultSetMetaData getMetaData() throws SQLException
    {
        return _prep.getMetaData();
    }

    public void setTime(int parameterIndex, Time x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setTime(parameterIndex, x);
    }

    public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setTimestamp(parameterIndex, x);
    }

    public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setDate(parameterIndex, x, cal);
    }

    public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setTime(parameterIndex, x, cal);
    }

    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException
    {
        setParameter(parameterIndex, x);

        _prep.setTimestamp(parameterIndex, x, cal);
    }
}
TOP

Related Classes of net.sourceforge.jdbclogger.core.PreparedStatementWrapper

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.