001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.database.statement;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.DataSetUtils;
028: import org.dbunit.dataset.datatype.DataType;
029: import org.dbunit.dataset.datatype.TypeCastException;
030:
031: import java.sql.SQLException;
032: import java.util.ArrayList;
033: import java.util.List;
034: import java.util.StringTokenizer;
035:
036: /**
037: * @author Manuel Laflamme
038: * @version $Revision: 554 $
039: * @since Mar 16, 2002
040: */
041: public class BatchStatementDecorator implements IPreparedBatchStatement {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger logger = LoggerFactory
047: .getLogger(BatchStatementDecorator.class);
048:
049: private final IBatchStatement _statement;
050: private final String[] _sqlTemplate;
051: private StringBuffer _sqlBuffer;
052: private int _index;
053:
054: BatchStatementDecorator(String sql, IBatchStatement statement) {
055: List list = new ArrayList();
056: StringTokenizer tokenizer = new StringTokenizer(sql, "?");
057: while (tokenizer.hasMoreTokens()) {
058: list.add(tokenizer.nextToken());
059: }
060:
061: if (sql.endsWith("?")) {
062: list.add("");
063: }
064:
065: _sqlTemplate = (String[]) list.toArray(new String[0]);
066: _statement = statement;
067:
068: // reset sql buffer
069: _index = 0;
070: _sqlBuffer = new StringBuffer(_sqlTemplate[_index++]);
071: }
072:
073: ////////////////////////////////////////////////////////////////////////////
074: // IPreparedBatchStatement interface
075:
076: public void addValue(Object value, DataType dataType)
077: throws TypeCastException, SQLException {
078: logger.debug("addValue(value=" + value + ", dataType="
079: + dataType + ") - start");
080:
081: _sqlBuffer.append(DataSetUtils.getSqlValueString(value,
082: dataType));
083: _sqlBuffer.append(_sqlTemplate[_index++]);
084: }
085:
086: public void addBatch() throws SQLException {
087: logger.debug("addBatch() - start");
088:
089: _statement.addBatch(_sqlBuffer.toString());
090:
091: // reset sql buffer
092: _index = 0;
093: _sqlBuffer = new StringBuffer(_sqlTemplate[_index++]);
094: }
095:
096: public int executeBatch() throws SQLException {
097: logger.debug("executeBatch() - start");
098:
099: return _statement.executeBatch();
100: }
101:
102: public void clearBatch() throws SQLException {
103: logger.debug("clearBatch() - start");
104:
105: _statement.clearBatch();
106:
107: // reset sql buffer
108: _index = 0;
109: _sqlBuffer = new StringBuffer(_sqlTemplate[_index++]);
110: }
111:
112: public void close() throws SQLException {
113: logger.debug("close() - start");
114:
115: _statement.close();
116: }
117: }
|