KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > InsertOperation


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.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
30 import java.util.BitSet JavaDoc;
31
32 /**
33  * Inserts the dataset contents into the database. This operation assumes that
34  * table data does not exist in the database and fails if this is not the case.
35  * To prevent problems with foreign keys, tables must be sequenced appropriately
36  * in dataset.
37  *
38  * @author Manuel Laflamme
39  * @version $Revision: 1.17 $
40  * @since Feb 18, 2002
41  */

42 public class InsertOperation extends AbstractBatchOperation
43 {
44     InsertOperation()
45     {
46     }
47
48     ////////////////////////////////////////////////////////////////////////////
49
// AbstractBatchOperation class
50

51     public OperationData getOperationData(ITableMetaData metaData,
52             BitSet JavaDoc ignoreMapping, IDatabaseConnection connection) throws DataSetException
53     {
54         Column[] columns = metaData.getColumns();
55
56         // insert
57
StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(128);
58         sqlBuffer.append("insert into ");
59         sqlBuffer.append(getQualifiedName(connection.getSchema(),
60                 metaData.getTableName(), connection));
61
62         // columns
63
sqlBuffer.append(" (");
64         String JavaDoc columnSeparator = "";
65         for (int i = 0; i < columns.length; i++)
66         {
67             if (!ignoreMapping.get(i))
68             {
69                 // escape column name
70
String JavaDoc columnName = getQualifiedName(null,
71                         columns[i].getColumnName(), connection);
72                 sqlBuffer.append(columnSeparator);
73                 sqlBuffer.append(columnName);
74                 columnSeparator = ", ";
75             }
76         }
77
78         // values
79
sqlBuffer.append(") values (");
80         String JavaDoc valueSeparator = "";
81         for (int i = 0; i < columns.length; i++)
82         {
83             if (!ignoreMapping.get(i))
84             {
85                 sqlBuffer.append(valueSeparator);
86                 sqlBuffer.append("?");
87                 valueSeparator = ", ";
88             }
89         }
90         sqlBuffer.append(")");
91
92         return new OperationData(sqlBuffer.toString(), columns);
93     }
94
95     protected BitSet JavaDoc getIgnoreMapping(ITable table, int row) throws DataSetException
96     {
97         Column[] columns = table.getTableMetaData().getColumns();
98
99         BitSet JavaDoc ignoreMapping = new BitSet JavaDoc();
100         for (int i = 0; i < columns.length; i++)
101         {
102             Object JavaDoc value = table.getValue(row, columns[i].getColumnName());
103             if (value == ITable.NO_VALUE)
104             {
105                 ignoreMapping.set(i);
106             }
107         }
108         return ignoreMapping;
109     }
110
111     protected boolean equalsIgnoreMapping(BitSet JavaDoc ignoreMapping, ITable table,
112             int row) throws DataSetException
113     {
114         Column[] columns = table.getTableMetaData().getColumns();
115
116         for (int i = 0; i < columns.length; i++)
117         {
118             boolean bit = ignoreMapping.get(i);
119             Object JavaDoc value = table.getValue(row, columns[i].getColumnName());
120             if ((bit && value != ITable.NO_VALUE) || (!bit && value == ITable.NO_VALUE))
121             {
122                 return false;
123             }
124         }
125
126         return true;
127     }
128 }
129
130
131
132
133
134
135
136
137
Popular Tags