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 SimplePreparedStatement extends AbstractPreparedBatchStatement
40 {
41
42
43
44
45 private static final Logger logger = LoggerFactory.getLogger(SimplePreparedStatement.class);
46
47 private int _index;
48 private int _result;
49
50 public SimplePreparedStatement(String sql, Connection connection)
51 throws SQLException
52 {
53 super(sql, connection);
54 _index = 0;
55 _result = 0;
56 }
57
58
59
60
61 public void addValue(Object value, DataType dataType)
62 throws TypeCastException, SQLException
63 {
64 logger.debug("addValue(value={}, dataType={}) - start", value, dataType);
65
66
67 if (value == null || value == ITable.NO_VALUE)
68 {
69 String sqlTypeName = dataType .getSqlTypeName();
70 if (sqlTypeName == null) {
71 _statement.setNull(++_index, dataType.getSqlType());
72 } else {
73 _statement.setNull(++_index, dataType.getSqlType(), sqlTypeName);
74 }
75 return;
76 }
77
78 dataType.setSqlValue(value, ++_index, _statement);
79 }
80
81 public void addBatch() throws SQLException
82 {
83 logger.debug("addBatch() - start");
84
85 boolean result = _statement.execute();
86 if (!result)
87 {
88 _result += _statement.getUpdateCount();
89 }
90 _index = 0;
91 }
92
93 public int executeBatch() throws SQLException
94 {
95 logger.debug("executeBatch() - start");
96
97 int result = _result;
98 clearBatch();
99 return result;
100 }
101
102 public void clearBatch() throws SQLException
103 {
104 logger.debug("clearBatch() - start");
105
106 _index = 0;
107 _result = 0;
108 }
109
110 }
111
112
113
114
115
116