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.ITable;
028: import org.dbunit.dataset.datatype.DataType;
029: import org.dbunit.dataset.datatype.TypeCastException;
030:
031: import java.sql.Connection;
032: import java.sql.SQLException;
033:
034: /**
035: * @author Manuel Laflamme
036: * @version $Revision: 554 $
037: * @since Mar 16, 2002
038: */
039: public class SimplePreparedStatement extends
040: AbstractPreparedBatchStatement {
041:
042: /**
043: * Logger for this class
044: */
045: private static final Logger logger = LoggerFactory
046: .getLogger(SimplePreparedStatement.class);
047:
048: private int _index;
049: private int _result;
050:
051: public SimplePreparedStatement(String sql, Connection connection)
052: throws SQLException {
053: super (sql, connection);
054: _index = 0;
055: _result = 0;
056: }
057:
058: ////////////////////////////////////////////////////////////////////////////
059: // IPreparedBatchStatement interface
060:
061: public void addValue(Object value, DataType dataType)
062: throws TypeCastException, SQLException {
063: logger.debug("addValue(value=" + value + ", dataType="
064: + dataType + ") - start");
065:
066: // Special NULL handling
067: if (value == null || value == ITable.NO_VALUE) {
068: _statement.setNull(++_index, dataType.getSqlType());
069: return;
070: }
071:
072: dataType.setSqlValue(value, ++_index, _statement);
073: }
074:
075: public void addBatch() throws SQLException {
076: logger.debug("addBatch() - start");
077:
078: boolean result = _statement.execute();
079: if (!result) {
080: _result += _statement.getUpdateCount();
081: }
082: _index = 0;
083: // _statement.clearParameters();
084: }
085:
086: public int executeBatch() throws SQLException {
087: logger.debug("executeBatch() - start");
088:
089: int result = _result;
090: clearBatch();
091: return result;
092: }
093:
094: public void clearBatch() throws SQLException {
095: logger.debug("clearBatch() - start");
096:
097: // _statement.clearParameters();
098: _index = 0;
099: _result = 0;
100: }
101:
102: }
|