KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > statement > BatchStatementDecorator


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @version $Revision: 1.6 $
36  * @since Mar 16, 2002
37  */

38 public class BatchStatementDecorator implements IPreparedBatchStatement
39 {
40     private final IBatchStatement _statement;
41     private final String JavaDoc[] _sqlTemplate;
42     private StringBuffer JavaDoc _sqlBuffer;
43     private int _index;
44
45     BatchStatementDecorator(String JavaDoc sql, IBatchStatement statement)
46     {
47         List JavaDoc list = new ArrayList JavaDoc();
48         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(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 JavaDoc[])list.toArray(new String JavaDoc[0]);
60         _statement = statement;
61
62         // reset sql buffer
63
_index = 0;
64         _sqlBuffer = new StringBuffer JavaDoc(_sqlTemplate[_index++]);
65     }
66
67     ////////////////////////////////////////////////////////////////////////////
68
// IPreparedBatchStatement interface
69

70     public void addValue(Object JavaDoc value, DataType dataType)
71             throws TypeCastException, SQLException JavaDoc
72     {
73         _sqlBuffer.append(DataSetUtils.getSqlValueString(value, dataType));
74         _sqlBuffer.append(_sqlTemplate[_index++]);
75     }
76
77     public void addBatch() throws SQLException JavaDoc
78     {
79         _statement.addBatch(_sqlBuffer.toString());
80
81         // reset sql buffer
82
_index = 0;
83         _sqlBuffer = new StringBuffer JavaDoc(_sqlTemplate[_index++]);
84     }
85
86     public int executeBatch() throws SQLException JavaDoc
87     {
88         return _statement.executeBatch();
89     }
90
91     public void clearBatch() throws SQLException JavaDoc
92     {
93         _statement.clearBatch();
94
95         // reset sql buffer
96
_index = 0;
97         _sqlBuffer = new StringBuffer JavaDoc(_sqlTemplate[_index++]);
98     }
99
100     public void close() throws SQLException JavaDoc
101     {
102         _statement.close();
103     }
104 }
105
106
107
108
109
Popular Tags