1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.database.statement;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import org.dbunit.dataset.ITable;
28 import org.dbunit.dataset.datatype.DataType;
29 import org.dbunit.dataset.datatype.TypeCastException;
30
31 import java.sql.Connection;
32 import java.sql.SQLException;
33
34
35
36
37
38
39 public class PreparedBatchStatement extends AbstractPreparedBatchStatement
40 {
41
42
43
44
45 private static final Logger logger = LoggerFactory.getLogger(PreparedBatchStatement.class);
46
47 private int _index;
48
49 PreparedBatchStatement(String sql, Connection connection)
50 throws SQLException
51 {
52 super(sql, connection);
53 _index = 0;
54 }
55
56
57
58
59 public void addValue(Object value, DataType dataType)
60 throws TypeCastException, SQLException
61 {
62 logger.debug("addValue(value={}, dataType={}) - start", value, dataType);
63
64
65 if (value == null || value == ITable.NO_VALUE)
66 {
67 String sqlTypeName = dataType.getSqlTypeName();
68 if (sqlTypeName == null) {
69 _statement.setNull(++_index, dataType.getSqlType());
70 } else {
71 _statement.setNull(++_index, dataType.getSqlType(), sqlTypeName);
72 }
73 return;
74 }
75
76 dataType.setSqlValue(value, ++_index, _statement);
77 }
78
79 public void addBatch() throws SQLException
80 {
81 logger.debug("addBatch() - start");
82
83 _statement.addBatch();
84 _index = 0;
85 }
86
87 public int executeBatch() throws SQLException
88 {
89 logger.debug("executeBatch() - start");
90
91 int[] results = _statement.executeBatch();
92 int result = 0;
93 for (int i = 0; i < results.length; i++)
94 {
95 result += results[i];
96 }
97 return result;
98 }
99
100 public void clearBatch() throws SQLException
101 {
102 logger.debug("clearBatch() - start");
103 _statement.clearBatch();
104 }
105 }
106
107
108
109
110
111
112