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 java.util.ArrayList;
028: import java.util.BitSet;
029: import java.util.List;
030:
031: import org.dbunit.database.IDatabaseConnection;
032: import org.dbunit.dataset.Column;
033: import org.dbunit.dataset.DataSetException;
034: import org.dbunit.dataset.DataSetUtils;
035: import org.dbunit.dataset.ITableMetaData;
036: import org.dbunit.dataset.NoPrimaryKeyException;
037:
038: /**
039: * Updates the database from the dataset contents. This operation assumes that
040: * table data already exists in the database and fails if this is not the case.
041:
042: * @author Manuel Laflamme
043: * @version $Revision: 554 $
044: * @since Feb 19, 2002
045: */
046: public class UpdateOperation extends AbstractBatchOperation {
047:
048: /**
049: * Logger for this class
050: */
051: private static final Logger logger = LoggerFactory
052: .getLogger(UpdateOperation.class);
053:
054: UpdateOperation() {
055: }
056:
057: ////////////////////////////////////////////////////////////////////////////
058: // AbstractBatchOperation class
059:
060: public OperationData getOperationData(ITableMetaData metaData,
061: BitSet ignoreMapping, IDatabaseConnection connection)
062: throws DataSetException {
063: logger.debug("getOperationData(metaData=" + metaData
064: + ", ignoreMapping=" + ignoreMapping + ", connection="
065: + connection + ") - start");
066:
067: Column[] columns = metaData.getColumns();
068: Column[] primaryKeys = metaData.getPrimaryKeys();
069:
070: // cannot construct where clause if no primary key
071: if (primaryKeys.length == 0) {
072: throw new NoPrimaryKeyException(metaData.getTableName());
073: }
074:
075: // update table
076: StringBuffer sqlBuffer = new StringBuffer(128);
077: sqlBuffer.append("update ");
078: sqlBuffer.append(getQualifiedName(connection.getSchema(),
079: metaData.getTableName(), connection));
080:
081: // set
082: boolean firstSet = true;
083: List columnList = new ArrayList(columns.length);
084: sqlBuffer.append(" set ");
085: for (int i = 0; i < columns.length; i++) {
086: Column column = columns[i];
087:
088: // set if not primary key
089: if (DataSetUtils.getColumn(column.getColumnName(),
090: primaryKeys) == null) {
091: if (!firstSet) {
092: sqlBuffer.append(", ");
093: }
094: firstSet = false;
095:
096: // escape column name
097: String columnName = getQualifiedName(null, column
098: .getColumnName(), connection);
099: sqlBuffer.append(columnName);
100: sqlBuffer.append(" = ?");
101: columnList.add(column);
102: }
103: }
104:
105: // where
106: sqlBuffer.append(" where ");
107: for (int i = 0; i < primaryKeys.length; i++) {
108: Column column = primaryKeys[i];
109:
110: if (i > 0) {
111: sqlBuffer.append(" and ");
112: }
113:
114: // escape column name
115: String columnName = getQualifiedName(null, column
116: .getColumnName(), connection);
117: sqlBuffer.append(columnName);
118: sqlBuffer.append(" = ?");
119: columnList.add(column);
120: }
121:
122: return new OperationData(sqlBuffer.toString(),
123: (Column[]) columnList.toArray(new Column[0]));
124: }
125:
126: }
|