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.operation;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.database.IDatabaseConnection;
028: import org.dbunit.dataset.Column;
029: import org.dbunit.dataset.DataSetException;
030: import org.dbunit.dataset.ITable;
031: import org.dbunit.dataset.ITableMetaData;
032:
033: import java.util.BitSet;
034:
035: /**
036: * Inserts the dataset contents into the database. This operation assumes that
037: * table data does not exist in the database and fails if this is not the case.
038: * To prevent problems with foreign keys, tables must be sequenced appropriately
039: * in dataset.
040: *
041: * @author Manuel Laflamme
042: * @version $Revision: 554 $
043: * @since Feb 18, 2002
044: */
045: public class InsertOperation extends AbstractBatchOperation {
046:
047: /**
048: * Logger for this class
049: */
050: private static final Logger logger = LoggerFactory
051: .getLogger(InsertOperation.class);
052:
053: InsertOperation() {
054: }
055:
056: ////////////////////////////////////////////////////////////////////////////
057: // AbstractBatchOperation class
058:
059: public OperationData getOperationData(ITableMetaData metaData,
060: BitSet ignoreMapping, IDatabaseConnection connection)
061: throws DataSetException {
062: logger.debug("getOperationData(metaData=" + metaData
063: + ", ignoreMapping=" + ignoreMapping + ", connection="
064: + connection + ") - start");
065:
066: Column[] columns = metaData.getColumns();
067:
068: // insert
069: StringBuffer sqlBuffer = new StringBuffer(128);
070: sqlBuffer.append("insert into ");
071: sqlBuffer.append(getQualifiedName(connection.getSchema(),
072: metaData.getTableName(), connection));
073:
074: // columns
075: sqlBuffer.append(" (");
076: String columnSeparator = "";
077: for (int i = 0; i < columns.length; i++) {
078: if (!ignoreMapping.get(i)) {
079: // escape column name
080: String columnName = getQualifiedName(null, columns[i]
081: .getColumnName(), connection);
082: sqlBuffer.append(columnSeparator);
083: sqlBuffer.append(columnName);
084: columnSeparator = ", ";
085: }
086: }
087:
088: // values
089: sqlBuffer.append(") values (");
090: String valueSeparator = "";
091: for (int i = 0; i < columns.length; i++) {
092: if (!ignoreMapping.get(i)) {
093: sqlBuffer.append(valueSeparator);
094: sqlBuffer.append("?");
095: valueSeparator = ", ";
096: }
097: }
098: sqlBuffer.append(")");
099:
100: return new OperationData(sqlBuffer.toString(), columns);
101: }
102:
103: protected BitSet getIgnoreMapping(ITable table, int row)
104: throws DataSetException {
105: logger.debug("getIgnoreMapping(table=" + table + ", row=" + row
106: + ") - start");
107:
108: Column[] columns = table.getTableMetaData().getColumns();
109:
110: BitSet ignoreMapping = new BitSet();
111: for (int i = 0; i < columns.length; i++) {
112: Object value = table.getValue(row, columns[i]
113: .getColumnName());
114: if (value == ITable.NO_VALUE) {
115: ignoreMapping.set(i);
116: }
117: }
118: return ignoreMapping;
119: }
120:
121: protected boolean equalsIgnoreMapping(BitSet ignoreMapping,
122: ITable table, int row) throws DataSetException {
123: logger.debug("equalsIgnoreMapping(ignoreMapping="
124: + ignoreMapping + ", table=" + table + ", row=" + row
125: + ") - start");
126:
127: Column[] columns = table.getTableMetaData().getColumns();
128:
129: for (int i = 0; i < columns.length; i++) {
130: boolean bit = ignoreMapping.get(i);
131: Object value = table.getValue(row, columns[i]
132: .getColumnName());
133: if ((bit && value != ITable.NO_VALUE)
134: || (!bit && value == ITable.NO_VALUE)) {
135: return false;
136: }
137: }
138:
139: return true;
140: }
141: }
|