1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.database.IDatabaseConnection; 25 import org.dbunit.dataset.*; 26 27 import java.math.BigInteger ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.BitSet ; 31 32 40 public class UpdateOperation extends AbstractBatchOperation 41 { 42 UpdateOperation() 43 { 44 } 45 46 49 public OperationData getOperationData(ITableMetaData metaData, BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException 50 { 51 Column[] columns = metaData.getColumns(); 52 Column[] primaryKeys = metaData.getPrimaryKeys(); 53 54 if (primaryKeys.length == 0) 56 { 57 throw new NoPrimaryKeyException(metaData.getTableName()); 58 } 59 60 StringBuffer sqlBuffer = new StringBuffer (128); 62 sqlBuffer.append("update "); 63 sqlBuffer.append(getQualifiedName(connection.getSchema(), 64 metaData.getTableName(), connection)); 65 66 boolean firstSet = true; 68 List columnList = new ArrayList (columns.length); 69 sqlBuffer.append(" set "); 70 for (int i = 0; i < columns.length; i++) 71 { 72 Column column = columns[i]; 73 74 if (DataSetUtils.getColumn(column.getColumnName(), primaryKeys) == null) 76 { 77 if (!firstSet) 78 { 79 sqlBuffer.append(", "); 80 } 81 firstSet = false; 82 83 String columnName = getQualifiedName(null, 85 column.getColumnName(), connection); 86 sqlBuffer.append(columnName); 87 sqlBuffer.append(" = ?"); 88 columnList.add(column); 89 } 90 } 91 92 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 String 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 |