UpdateOperation.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.operation;

  22. import java.util.ArrayList;
  23. import java.util.BitSet;
  24. import java.util.List;

  25. import org.dbunit.database.IDatabaseConnection;
  26. import org.dbunit.dataset.Column;
  27. import org.dbunit.dataset.Columns;
  28. import org.dbunit.dataset.DataSetException;
  29. import org.dbunit.dataset.ITableMetaData;
  30. import org.dbunit.dataset.NoPrimaryKeyException;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;

  33. /**
  34.  * Updates the database from the dataset contents. This operation assumes that
  35.  * table data already exists in the database and fails if this is not the case.

  36.  * @author Manuel Laflamme
  37.  * @version $Revision$
  38.  * @since Feb 19, 2002
  39.  */
  40. public class UpdateOperation extends AbstractBatchOperation
  41. {

  42.     /**
  43.      * Logger for this class
  44.      */
  45.     private static final Logger logger = LoggerFactory.getLogger(UpdateOperation.class);

  46.     UpdateOperation()
  47.     {
  48.     }

  49.     ////////////////////////////////////////////////////////////////////////////
  50.     // AbstractBatchOperation class

  51.     public OperationData getOperationData(ITableMetaData metaData, BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException
  52.     {
  53.         if(logger.isDebugEnabled())
  54.             logger.debug("getOperationData(metaData={}, ignoreMapping={}, connection={}) - start",
  55.                 new Object[]{metaData, ignoreMapping, connection});

  56.         Column[] columns = metaData.getColumns();
  57.         Column[] primaryKeys = metaData.getPrimaryKeys();

  58.         // cannot construct where clause if no primary key
  59.         if (primaryKeys.length == 0)
  60.         {
  61.             throw new NoPrimaryKeyException(metaData.getTableName());
  62.         }

  63.         // update table
  64.         final StringBuilder sqlBuffer = new StringBuilder(128);
  65.         sqlBuffer.append("update ");
  66.         sqlBuffer.append(getQualifiedName(connection.getSchema(),
  67.                 metaData.getTableName(), connection));

  68.         // set
  69.         boolean firstSet = true;
  70.         List columnList = new ArrayList(columns.length);
  71.         sqlBuffer.append(" set ");
  72.         for (int i = 0; i < columns.length; i++)
  73.         {
  74.             Column column = columns[i];

  75.             // set if not primary key
  76.             if (Columns.getColumn(column.getColumnName(), primaryKeys) == null)
  77.             {
  78.                 if (!firstSet)
  79.                 {
  80.                     sqlBuffer.append(", ");
  81.                 }
  82.                 firstSet = false;

  83.                 // escape column name
  84.                 String columnName = getQualifiedName(null,
  85.                         column.getColumnName(), connection);
  86.                 sqlBuffer.append(columnName);
  87.                 sqlBuffer.append(" = ?");
  88.                 columnList.add(column);
  89.             }
  90.         }

  91.         // where
  92.         sqlBuffer.append(" where ");
  93.         for (int i = 0; i < primaryKeys.length; i++)
  94.         {
  95.             Column column = primaryKeys[i];

  96.             if (i > 0)
  97.             {
  98.                 sqlBuffer.append(" and ");
  99.             }

  100.             // escape column name
  101.             String columnName = getQualifiedName(null,
  102.                     column.getColumnName(), connection);
  103.             sqlBuffer.append(columnName);
  104.             sqlBuffer.append(" = ?");
  105.             columnList.add(column);
  106.         }

  107.         return new OperationData(sqlBuffer.toString(),
  108.                 (Column[])columnList.toArray(new Column[0]));
  109.     }

  110. }