1 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 ; 31 32 42 public class InsertOperation extends AbstractBatchOperation 43 { 44 InsertOperation() 45 { 46 } 47 48 51 public OperationData getOperationData(ITableMetaData metaData, 52 BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException 53 { 54 Column[] columns = metaData.getColumns(); 55 56 StringBuffer sqlBuffer = new StringBuffer (128); 58 sqlBuffer.append("insert into "); 59 sqlBuffer.append(getQualifiedName(connection.getSchema(), 60 metaData.getTableName(), connection)); 61 62 sqlBuffer.append(" ("); 64 String columnSeparator = ""; 65 for (int i = 0; i < columns.length; i++) 66 { 67 if (!ignoreMapping.get(i)) 68 { 69 String columnName = getQualifiedName(null, 71 columns[i].getColumnName(), connection); 72 sqlBuffer.append(columnSeparator); 73 sqlBuffer.append(columnName); 74 columnSeparator = ", "; 75 } 76 } 77 78 sqlBuffer.append(") values ("); 80 String 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 getIgnoreMapping(ITable table, int row) throws DataSetException 96 { 97 Column[] columns = table.getTableMetaData().getColumns(); 98 99 BitSet ignoreMapping = new BitSet (); 100 for (int i = 0; i < columns.length; i++) 101 { 102 Object 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 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 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 |