InsertOperation.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 org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.database.IDatabaseConnection;
  25. import org.dbunit.dataset.Column;
  26. import org.dbunit.dataset.DataSetException;
  27. import org.dbunit.dataset.ITable;
  28. import org.dbunit.dataset.ITableMetaData;

  29. import java.util.BitSet;

  30. /**
  31.  * Inserts the dataset contents into the database. This operation assumes that
  32.  * table data does not exist in the database and fails if this is not the case.
  33.  * To prevent problems with foreign keys, tables must be sequenced appropriately
  34.  * in dataset.
  35.  *
  36.  * @author Manuel Laflamme
  37.  * @version $Revision$
  38.  * @since Feb 18, 2002
  39.  */
  40. public class InsertOperation extends AbstractBatchOperation
  41. {

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

  46.     InsertOperation()
  47.     {
  48.     }

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

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

  59.         Column[] columns = metaData.getColumns();

  60.         // insert
  61.         final StringBuilder sqlBuffer = new StringBuilder(128);
  62.         sqlBuffer.append("insert into ");
  63.         sqlBuffer.append(getQualifiedName(connection.getSchema(),
  64.                 metaData.getTableName(), connection));

  65.         // columns
  66.         sqlBuffer.append(" (");
  67.         String columnSeparator = "";
  68.         for (int i = 0; i < columns.length; i++)
  69.         {
  70.             if (!ignoreMapping.get(i))
  71.             {
  72.                 // escape column name
  73.                 String columnName = getQualifiedName(null,
  74.                         columns[i].getColumnName(), connection);
  75.                 sqlBuffer.append(columnSeparator);
  76.                 sqlBuffer.append(columnName);
  77.                 columnSeparator = ", ";
  78.             }
  79.         }

  80.         // values
  81.         sqlBuffer.append(") values (");
  82.         String valueSeparator = "";
  83.         for (int i = 0; i < columns.length; i++)
  84.         {
  85.             if (!ignoreMapping.get(i))
  86.             {
  87.                 sqlBuffer.append(valueSeparator);
  88.                 sqlBuffer.append("?");
  89.                 valueSeparator = ", ";
  90.             }
  91.         }
  92.         sqlBuffer.append(")");

  93.         return new OperationData(sqlBuffer.toString(), columns);
  94.     }

  95.     protected BitSet getIgnoreMapping(ITable table, int row) throws DataSetException
  96.     {
  97.         logger.debug("getIgnoreMapping(table={}, row={}) - start", table, row);

  98.         Column[] columns = table.getTableMetaData().getColumns();

  99.         BitSet ignoreMapping = new BitSet();
  100.         for (int i = 0; i < columns.length; i++)
  101.         {
  102.             Column column = columns[i];
  103.             Object value = table.getValue(row, column.getColumnName());
  104.             if (value == ITable.NO_VALUE
  105.                 || (value == null && column.isNotNullable() && column.hasDefaultValue()))
  106.             {
  107.                 ignoreMapping.set(i);
  108.             }
  109.         }
  110.         return ignoreMapping;
  111.     }

  112.     protected boolean equalsIgnoreMapping(BitSet ignoreMapping, ITable table,
  113.             int row) throws DataSetException
  114.     {
  115.         logger.debug("equalsIgnoreMapping(ignoreMapping={}, table={}, row={}) - start",
  116.                  ignoreMapping, table, row);

  117.         Column[] columns = table.getTableMetaData().getColumns();

  118.         for (int i = 0; i < columns.length; i++)
  119.         {
  120.             boolean bit = ignoreMapping.get(i);
  121.             Object value = table.getValue(row, columns[i].getColumnName());
  122.             if ((bit && value != ITable.NO_VALUE) || (!bit && value == ITable.NO_VALUE))
  123.             {
  124.                 return false;
  125.             }
  126.         }

  127.         return true;
  128.     }
  129. }