KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26
27 import java.math.BigInteger JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.BitSet JavaDoc;
31
32 /**
33  * Updates the database from the dataset contents. This operation assumes that
34  * table data already exists in the database and fails if this is not the case.
35
36  * @author Manuel Laflamme
37  * @version $Revision: 1.14 $
38  * @since Feb 19, 2002
39  */

40 public class UpdateOperation extends AbstractBatchOperation
41 {
42     UpdateOperation()
43     {
44     }
45
46     ////////////////////////////////////////////////////////////////////////////
47
// AbstractBatchOperation class
48

49     public OperationData getOperationData(ITableMetaData metaData, BitSet JavaDoc ignoreMapping, IDatabaseConnection connection) throws DataSetException
50     {
51         Column[] columns = metaData.getColumns();
52         Column[] primaryKeys = metaData.getPrimaryKeys();
53
54         // cannot construct where clause if no primary key
55
if (primaryKeys.length == 0)
56         {
57             throw new NoPrimaryKeyException(metaData.getTableName());
58         }
59
60         // update table
61
StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(128);
62         sqlBuffer.append("update ");
63         sqlBuffer.append(getQualifiedName(connection.getSchema(),
64                 metaData.getTableName(), connection));
65
66         // set
67
boolean firstSet = true;
68         List JavaDoc columnList = new ArrayList JavaDoc(columns.length);
69         sqlBuffer.append(" set ");
70         for (int i = 0; i < columns.length; i++)
71         {
72             Column column = columns[i];
73
74             // set if not primary key
75
if (DataSetUtils.getColumn(column.getColumnName(), primaryKeys) == null)
76             {
77                 if (!firstSet)
78                 {
79                     sqlBuffer.append(", ");
80                 }
81                 firstSet = false;
82
83                 // escape column name
84
String JavaDoc columnName = getQualifiedName(null,
85                         column.getColumnName(), connection);
86                 sqlBuffer.append(columnName);
87                 sqlBuffer.append(" = ?");
88                 columnList.add(column);
89             }
90         }
91
92         // where
93
sqlBuffer.append(" where ");
94         for (int i = 0; i < primaryKeys.length; i++)
95         {
96             Column column = primaryKeys[i];
97
98             if (i > 0)
99             {
100                 sqlBuffer.append(" and ");
101             }
102
103             // escape column name
104
String JavaDoc columnName = getQualifiedName(null,
105                     column.getColumnName(), connection);
106             sqlBuffer.append(columnName);
107             sqlBuffer.append(" = ?");
108             columnList.add(column);
109         }
110
111         return new OperationData(sqlBuffer.toString(),
112                 (Column[])columnList.toArray(new Column[0]));
113     }
114
115 }
116
117
118
119
120
121
122
Popular Tags