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 java.util.ArrayList;
25  import java.util.BitSet;
26  import java.util.List;
27  
28  import org.dbunit.database.IDatabaseConnection;
29  import org.dbunit.dataset.Column;
30  import org.dbunit.dataset.Columns;
31  import org.dbunit.dataset.DataSetException;
32  import org.dbunit.dataset.ITableMetaData;
33  import org.dbunit.dataset.NoPrimaryKeyException;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * Updates the database from the dataset contents. This operation assumes that
39   * table data already exists in the database and fails if this is not the case.
40  
41   * @author Manuel Laflamme
42   * @version $Revision$
43   * @since Feb 19, 2002
44   */
45  public class UpdateOperation extends AbstractBatchOperation
46  {
47  
48      /**
49       * Logger for this class
50       */
51      private static final Logger logger = LoggerFactory.getLogger(UpdateOperation.class);
52  
53      UpdateOperation()
54      {
55      }
56  
57      ////////////////////////////////////////////////////////////////////////////
58      // AbstractBatchOperation class
59  
60      public OperationData getOperationData(ITableMetaData metaData, BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException
61      {
62          if(logger.isDebugEnabled())
63              logger.debug("getOperationData(metaData={}, ignoreMapping={}, connection={}) - start", 
64                  new Object[]{metaData, ignoreMapping, connection});
65  
66          Column[] columns = metaData.getColumns();
67          Column[] primaryKeys = metaData.getPrimaryKeys();
68  
69          // cannot construct where clause if no primary key
70          if (primaryKeys.length == 0)
71          {
72              throw new NoPrimaryKeyException(metaData.getTableName());
73          }
74  
75          // update table
76          StringBuffer sqlBuffer = new StringBuffer(128);
77          sqlBuffer.append("update ");
78          sqlBuffer.append(getQualifiedName(connection.getSchema(),
79                  metaData.getTableName(), connection));
80  
81          // set
82          boolean firstSet = true;
83          List columnList = new ArrayList(columns.length);
84          sqlBuffer.append(" set ");
85          for (int i = 0; i < columns.length; i++)
86          {
87              Column column = columns[i];
88  
89              // set if not primary key
90              if (Columns.getColumn(column.getColumnName(), primaryKeys) == null)
91              {
92                  if (!firstSet)
93                  {
94                      sqlBuffer.append(", ");
95                  }
96                  firstSet = false;
97  
98                  // escape column name
99                  String columnName = getQualifiedName(null,
100                         column.getColumnName(), connection);
101                 sqlBuffer.append(columnName);
102                 sqlBuffer.append(" = ?");
103                 columnList.add(column);
104             }
105         }
106 
107         // where
108         sqlBuffer.append(" where ");
109         for (int i = 0; i < primaryKeys.length; i++)
110         {
111             Column column = primaryKeys[i];
112 
113             if (i > 0)
114             {
115                 sqlBuffer.append(" and ");
116             }
117 
118             // escape column name
119             String columnName = getQualifiedName(null,
120                     column.getColumnName(), connection);
121             sqlBuffer.append(columnName);
122             sqlBuffer.append(" = ?");
123             columnList.add(column);
124         }
125 
126         return new OperationData(sqlBuffer.toString(),
127                 (Column[])columnList.toArray(new Column[0]));
128     }
129 
130 }