BatchStatementDecorator.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.database.statement;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.dataset.DataSetUtils;
  25. import org.dbunit.dataset.datatype.DataType;
  26. import org.dbunit.dataset.datatype.TypeCastException;

  27. import java.sql.SQLException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import java.util.StringTokenizer;

  31. /**
  32.  * @author Manuel Laflamme
  33.  * @version $Revision$
  34.  * @since Mar 16, 2002
  35.  */
  36. public class BatchStatementDecorator implements IPreparedBatchStatement
  37. {

  38.     /**
  39.      * Logger for this class
  40.      */
  41.     private static final Logger logger = LoggerFactory.getLogger(BatchStatementDecorator.class);

  42.     private final IBatchStatement _statement;
  43.     private final String[] _sqlTemplate;
  44.     private StringBuilder _sqlBuffer;
  45.     private int _index;

  46.     BatchStatementDecorator(String sql, IBatchStatement statement)
  47.     {
  48.         List list = new ArrayList();
  49.         StringTokenizer tokenizer = new StringTokenizer(sql, "?");
  50.         while (tokenizer.hasMoreTokens())
  51.         {
  52.             list.add(tokenizer.nextToken());
  53.         }

  54.         if (sql.endsWith("?"))
  55.         {
  56.             list.add("");
  57.         }

  58.         _sqlTemplate = (String[])list.toArray(new String[0]);
  59.         _statement = statement;

  60.         // reset sql buffer
  61.         _index = 0;
  62.         _sqlBuffer = new StringBuilder(_sqlTemplate[_index++]);
  63.     }

  64.     ////////////////////////////////////////////////////////////////////////////
  65.     // IPreparedBatchStatement interface

  66.     public void addValue(Object value, DataType dataType)
  67.             throws TypeCastException, SQLException
  68.     {
  69.         logger.debug("addValue(value={}, dataType={}) - start", value, dataType);

  70.         _sqlBuffer.append(DataSetUtils.getSqlValueString(value, dataType));
  71.         _sqlBuffer.append(_sqlTemplate[_index++]);
  72.     }

  73.     public void addBatch() throws SQLException
  74.     {
  75.         logger.debug("addBatch() - start");

  76.         _statement.addBatch(_sqlBuffer.toString());

  77.         // reset sql buffer
  78.         _index = 0;
  79.         _sqlBuffer = new StringBuilder(_sqlTemplate[_index++]);
  80.     }

  81.     public int executeBatch() throws SQLException
  82.     {
  83.         logger.debug("executeBatch() - start");

  84.         return _statement.executeBatch();
  85.     }

  86.     public void clearBatch() throws SQLException
  87.     {
  88.         logger.debug("clearBatch() - start");

  89.         _statement.clearBatch();

  90.         // reset sql buffer
  91.         _index = 0;
  92.         _sqlBuffer = new StringBuilder(_sqlTemplate[_index++]);
  93.     }

  94.     public void close() throws SQLException
  95.     {
  96.         logger.debug("close() - start");

  97.         _statement.close();
  98.     }
  99. }