View Javadoc
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  
22  package org.dbunit.operation;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import org.dbunit.database.IDatabaseConnection;
28  import org.dbunit.dataset.Column;
29  import org.dbunit.dataset.DataSetException;
30  import org.dbunit.dataset.ITable;
31  import org.dbunit.dataset.ITableMetaData;
32  
33  import java.util.BitSet;
34  
35  /**
36   * Inserts the dataset contents into the database. This operation assumes that
37   * table data does not exist in the database and fails if this is not the case.
38   * To prevent problems with foreign keys, tables must be sequenced appropriately
39   * in dataset.
40   *
41   * @author Manuel Laflamme
42   * @version $Revision$
43   * @since Feb 18, 2002
44   */
45  public class InsertOperation extends AbstractBatchOperation
46  {
47  
48      /**
49       * Logger for this class
50       */
51      private static final Logger logger = LoggerFactory.getLogger(InsertOperation.class);
52  
53      InsertOperation()
54      {
55      }
56  
57      ////////////////////////////////////////////////////////////////////////////
58      // AbstractBatchOperation class
59  
60      public OperationData getOperationData(ITableMetaData metaData,
61              BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException
62      {
63      	if (logger.isDebugEnabled())
64      	{
65      		logger.debug("getOperationData(metaData={}, ignoreMapping={}, connection={}) - start",
66      				metaData, ignoreMapping, connection);
67      	}
68  
69          Column[] columns = metaData.getColumns();
70  
71          // insert
72          final StringBuilder sqlBuffer = new StringBuilder(128);
73          sqlBuffer.append("insert into ");
74          sqlBuffer.append(getQualifiedName(connection.getSchema(),
75                  metaData.getTableName(), connection));
76  
77          // columns
78          sqlBuffer.append(" (");
79          String columnSeparator = "";
80          for (int i = 0; i < columns.length; i++)
81          {
82              if (!ignoreMapping.get(i))
83              {
84                  // escape column name
85                  String columnName = getQualifiedName(null,
86                          columns[i].getColumnName(), connection);
87                  sqlBuffer.append(columnSeparator);
88                  sqlBuffer.append(columnName);
89                  columnSeparator = ", ";
90              }
91          }
92  
93          // values
94          sqlBuffer.append(") values (");
95          String valueSeparator = "";
96          for (int i = 0; i < columns.length; i++)
97          {
98              if (!ignoreMapping.get(i))
99              {
100                 sqlBuffer.append(valueSeparator);
101                 sqlBuffer.append("?");
102                 valueSeparator = ", ";
103             }
104         }
105         sqlBuffer.append(")");
106 
107         return new OperationData(sqlBuffer.toString(), columns);
108     }
109 
110     protected BitSet getIgnoreMapping(ITable table, int row) throws DataSetException
111     {
112     	logger.debug("getIgnoreMapping(table={}, row={}) - start", table, row);
113 
114         Column[] columns = table.getTableMetaData().getColumns();
115 
116         BitSet ignoreMapping = new BitSet();
117         for (int i = 0; i < columns.length; i++)
118         {
119             Column column = columns[i];
120             Object value = table.getValue(row, column.getColumnName());
121             if (value == ITable.NO_VALUE
122                 || (value == null && column.isNotNullable() && column.hasDefaultValue()))
123             {
124                 ignoreMapping.set(i);
125             }
126         }
127         return ignoreMapping;
128     }
129 
130     protected boolean equalsIgnoreMapping(BitSet ignoreMapping, ITable table,
131             int row) throws DataSetException
132     {
133     	logger.debug("equalsIgnoreMapping(ignoreMapping={}, table={}, row={}) - start",
134     			 ignoreMapping, table, row);
135 
136         Column[] columns = table.getTableMetaData().getColumns();
137 
138         for (int i = 0; i < columns.length; i++)
139         {
140             boolean bit = ignoreMapping.get(i);
141             Object value = table.getValue(row, columns[i].getColumnName());
142             if ((bit && value != ITable.NO_VALUE) || (!bit && value == ITable.NO_VALUE))
143             {
144                 return false;
145             }
146         }
147 
148         return true;
149     }
150 }