1 21 22 package org.dbunit.database.statement; 23 24 import org.dbunit.dataset.DataSetUtils; 25 import org.dbunit.dataset.datatype.DataType; 26 import org.dbunit.dataset.datatype.TypeCastException; 27 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 33 38 public class BatchStatementDecorator implements IPreparedBatchStatement 39 { 40 private final IBatchStatement _statement; 41 private final String [] _sqlTemplate; 42 private StringBuffer _sqlBuffer; 43 private int _index; 44 45 BatchStatementDecorator(String sql, IBatchStatement statement) 46 { 47 List list = new ArrayList (); 48 StringTokenizer tokenizer = new StringTokenizer (sql, "?"); 49 while (tokenizer.hasMoreTokens()) 50 { 51 list.add(tokenizer.nextToken()); 52 } 53 54 if (sql.endsWith("?")) 55 { 56 list.add(""); 57 } 58 59 _sqlTemplate = (String [])list.toArray(new String [0]); 60 _statement = statement; 61 62 _index = 0; 64 _sqlBuffer = new StringBuffer (_sqlTemplate[_index++]); 65 } 66 67 70 public void addValue(Object value, DataType dataType) 71 throws TypeCastException, SQLException 72 { 73 _sqlBuffer.append(DataSetUtils.getSqlValueString(value, dataType)); 74 _sqlBuffer.append(_sqlTemplate[_index++]); 75 } 76 77 public void addBatch() throws SQLException 78 { 79 _statement.addBatch(_sqlBuffer.toString()); 80 81 _index = 0; 83 _sqlBuffer = new StringBuffer (_sqlTemplate[_index++]); 84 } 85 86 public int executeBatch() throws SQLException 87 { 88 return _statement.executeBatch(); 89 } 90 91 public void clearBatch() throws SQLException 92 { 93 _statement.clearBatch(); 94 95 _index = 0; 97 _sqlBuffer = new StringBuffer (_sqlTemplate[_index++]); 98 } 99 100 public void close() throws SQLException 101 { 102 _statement.close(); 103 } 104 } 105 106 107 108 109 | Popular Tags |