KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > CachedSqlTokenizer


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * This tokenizer uses target to tokenize statements and remembers
24  * parsed statements and injections, so it can be reset and iterated several times.
25  * Call {@link #close()} to reset tokenizer.
26  *
27  * @author Fyodor Kupolov
28  * @version 1.0
29  */

30 final class CachedSqlTokenizer implements SqlTokenizer {
31     private SqlTokenizer target;
32     private List JavaDoc<String JavaDoc> statements;
33     private String JavaDoc[] statementsArray; //For better performance
34
private List JavaDoc<int[]> injections;
35     private int[][] injectionsArray; //For better performance
36
private int index=-1;
37
38
39     /**
40      * Instantiates tokenizer based on the specified target.
41      * @param target target to use. The statements are cached lazily as they
42      * are obtained by calling {@link #nextStatement()}.
43      */

44     public CachedSqlTokenizer(SqlTokenizer target) {
45         this.target = target;
46     }
47
48     public String JavaDoc nextStatement() throws IOException JavaDoc {
49         if (target != null) {
50             String JavaDoc st = target.nextStatement();
51             if (st != null) {
52                 if (statements == null) {
53                     statements = new ArrayList JavaDoc<String JavaDoc>();
54                     injections = new ArrayList JavaDoc<int[]>();
55                 }
56                 statements.add(st);
57             }
58             return st;
59         } else {
60             index++;
61             if (statementsArray == null || index >= statementsArray.length) {
62                 return null;
63             }
64             return statementsArray[index];
65         }
66     }
67
68     /**
69      * This method returns list of injections for the last returned statement.
70      *
71      * @return injections for the last returned statement.
72      */

73     public int[] getInjections() {
74         if (target != null) {
75             int[] inj = target.getInjections();
76             injections.add(inj);
77             return inj;
78         } else {
79             if (injectionsArray != null && index<injectionsArray.length) {
80                 return injectionsArray[index];
81             }
82             return null;
83         }
84     }
85
86     /**
87      * Important notes:
88      * <ul>
89      * <li>calling close first time closes target tokenizer,
90      * subsequent close invocations reset index to start iteration.
91      * <li>After close is called for the first time, unread statements
92      * are skipped and not returned in subsequent iterations.
93      * </ul>
94      *
95      */

96     public void close() throws IOException JavaDoc {
97         if (target != null) {
98             if (statements!=null) {
99                 statementsArray=statements.toArray(new String JavaDoc[statements.size()]);
100                 injectionsArray=injections.toArray(new int[injections.size()][]);
101                 statements=null;
102                 injections=null;
103             }
104             try {
105                 target.close();
106             } finally {
107                 target = null; //We do not need target anymore
108
}
109         } else {
110             index = -1;
111         }
112
113     }
114
115 }
116
Popular Tags