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      				new Object[]{ metaData, ignoreMapping, connection });
67      	}
68  
69          Column[] columns = metaData.getColumns();
70  
71          // insert
72          StringBuffer sqlBuffer = new StringBuffer(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     	if(logger.isDebugEnabled())
113     		logger.debug("getIgnoreMapping(table={}, row={}) - start", table, String.valueOf(row));
114 
115         Column[] columns = table.getTableMetaData().getColumns();
116 
117         BitSet ignoreMapping = new BitSet();
118         for (int i = 0; i < columns.length; i++)
119         {
120             Column column = columns[i];
121             Object value = table.getValue(row, column.getColumnName());
122             if (value == ITable.NO_VALUE
123                 || (value == null && column.isNotNullable() && column.hasDefaultValue()))
124             {
125                 ignoreMapping.set(i);
126             }
127         }
128         return ignoreMapping;
129     }
130 
131     protected boolean equalsIgnoreMapping(BitSet ignoreMapping, ITable table,
132             int row) throws DataSetException
133     {
134     	if (logger.isDebugEnabled())
135     	{
136     		logger.debug("equalsIgnoreMapping(ignoreMapping={}, table={}, row={}) - start",
137     				new Object[]{ ignoreMapping, table, String.valueOf(row) });
138     	}
139 
140         Column[] columns = table.getTableMetaData().getColumns();
141 
142         for (int i = 0; i < columns.length; i++)
143         {
144             boolean bit = ignoreMapping.get(i);
145             Object value = table.getValue(row, columns[i].getColumnName());
146             if ((bit && value != ITable.NO_VALUE) || (!bit && value == ITable.NO_VALUE))
147             {
148                 return false;
149             }
150         }
151 
152         return true;
153     }
154 }