KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > rowio > RowInputTextLog


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.rowio;
33
34 import java.io.IOException JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.sql.Date JavaDoc;
37 import java.sql.Time JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39
40 import org.hsqldb.Column;
41 import org.hsqldb.HsqlDateTime;
42 import org.hsqldb.HsqlException;
43 import org.hsqldb.Token;
44 import org.hsqldb.Tokenizer;
45 import org.hsqldb.Types;
46 import org.hsqldb.scriptio.ScriptReaderBase;
47 import org.hsqldb.store.ValuePool;
48 import org.hsqldb.types.Binary;
49 import org.hsqldb.types.JavaObject;
50 import org.hsqldb.lib.java.JavaSystem;
51
52 /**
53  * Class for reading the data for a database row from the script file.
54  *
55  * @author fredt@users
56  * @version 1.8.0
57  * @since 1.7.3
58  */

59 public class RowInputTextLog extends RowInputBase
60 implements RowInputInterface {
61
62     Tokenizer tokenizer;
63     String JavaDoc tableName = null;
64     String JavaDoc schemaName = null;
65     int statementType;
66
67     public RowInputTextLog() {
68
69         super(new byte[0]);
70
71         tokenizer = new Tokenizer();
72     }
73
74     public void setSource(String JavaDoc text) throws HsqlException {
75
76         tokenizer.reset(text);
77
78         statementType = ScriptReaderBase.ANY_STATEMENT;
79
80         String JavaDoc s = tokenizer.getString();
81
82         if (s.equals(Token.T_INSERT)) {
83             statementType = ScriptReaderBase.INSERT_STATEMENT;
84
85             tokenizer.getString();
86
87             tableName = tokenizer.getString();
88
89             tokenizer.getString();
90         } else if (s.equals(Token.T_DELETE)) {
91             statementType = ScriptReaderBase.DELETE_STATEMENT;
92
93             tokenizer.getString();
94
95             tableName = tokenizer.getString();
96         } else if (s.equals(Token.T_COMMIT)) {
97             statementType = ScriptReaderBase.COMMIT_STATEMENT;
98         } else if (s.equals(Token.T_SET)) {
99             if (tokenizer.isGetThis(Token.T_SCHEMA)) {
100                 schemaName = tokenizer.getSimpleName();
101                 statementType = ScriptReaderBase.SCHEMA_STATEMENT;
102             }
103         }
104     }
105
106     public int getStatementType() {
107         return statementType;
108     }
109
110     public String JavaDoc getTableName() {
111         return tableName;
112     }
113
114     public String JavaDoc getSchemaName() {
115         return schemaName;
116     }
117
118     protected String JavaDoc readField() throws IOException JavaDoc {
119
120         try {
121             tokenizer.getString();
122
123             if (statementType == ScriptReaderBase.DELETE_STATEMENT) {
124                 tokenizer.getString();
125                 tokenizer.getString();
126             }
127
128             String JavaDoc s = tokenizer.getString();
129
130             if (tokenizer.getType() == Types.NULL) {
131                 s = null;
132             }
133
134             return s;
135         } catch (HsqlException e) {
136             throw new IOException JavaDoc(e.getMessage());
137         }
138     }
139
140     protected String JavaDoc readNumberField() throws IOException JavaDoc {
141
142         try {
143             tokenizer.getString();
144
145             if (statementType == ScriptReaderBase.DELETE_STATEMENT) {
146                 tokenizer.getString();
147                 tokenizer.getString();
148             }
149
150             String JavaDoc s = tokenizer.getString();
151
152             if ("-".equals(s)) {
153                 s = s + tokenizer.getString();
154             } else if (tokenizer.getType() == Types.NULL) {
155                 s = null;
156             }
157
158             return s;
159         } catch (HsqlException e) {
160             throw new IOException JavaDoc(e.getMessage());
161         }
162     }
163
164     public String JavaDoc readString() throws IOException JavaDoc {
165
166         String JavaDoc s = readField();
167
168         return ValuePool.getString(s);
169     }
170
171     public short readShortData() throws IOException JavaDoc {
172
173         String JavaDoc s = readNumberField();
174
175         if (s == null) {
176             return 0;
177         }
178
179         return Short.parseShort(s);
180     }
181
182     public int readIntData() throws IOException JavaDoc {
183
184         String JavaDoc s = readNumberField();
185
186         if (s == null) {
187             return 0;
188         }
189
190         return Integer.parseInt(s);
191     }
192
193     public long readLongData() throws IOException JavaDoc {
194
195         String JavaDoc s = readNumberField();
196
197         if (s == null) {
198             return 0;
199         }
200
201         return Long.parseLong(s);
202     }
203
204     public int readType() throws IOException JavaDoc {
205         return 0;
206     }
207
208     protected boolean checkNull() {
209
210         // Return null on each column read instead.
211
return false;
212     }
213
214     protected String JavaDoc readChar(int type) throws IOException JavaDoc {
215         return readString();
216     }
217
218     protected Integer JavaDoc readSmallint() throws IOException JavaDoc, HsqlException {
219
220         String JavaDoc s = readNumberField();
221
222         if (s == null) {
223             return null;
224         }
225
226         int i = Integer.parseInt(s);
227
228         return ValuePool.getInt(i);
229     }
230
231     protected Integer JavaDoc readInteger() throws IOException JavaDoc, HsqlException {
232
233         String JavaDoc s = readNumberField();
234
235         if (s == null) {
236             return null;
237         }
238
239         int i = Integer.parseInt(s);
240
241         return ValuePool.getInt(i);
242     }
243
244     protected Long JavaDoc readBigint() throws IOException JavaDoc, HsqlException {
245
246         String JavaDoc s = readNumberField();
247
248         if (s == null) {
249             return null;
250         }
251
252         long i = Long.parseLong(s);
253
254         return ValuePool.getLong(i);
255     }
256
257     protected Double JavaDoc readReal(int type) throws IOException JavaDoc, HsqlException {
258
259         String JavaDoc s = readNumberField();
260
261         if (s == null) {
262             return null;
263         }
264
265         double i = JavaSystem.parseDouble(s);
266
267         if (tokenizer.isGetThis(Token.T_DIVIDE)) {
268             s = tokenizer.getString();
269
270             // parse simply to ensure it's a number
271
double ii = JavaSystem.parseDouble(s);
272
273             if (i == 0E0) {
274                 i = Double.NaN;
275             } else if (i == -1E0) {
276                 i = Double.NEGATIVE_INFINITY;
277             } else if (i == 1E0) {
278                 i = Double.POSITIVE_INFINITY;
279             }
280         }
281
282         return ValuePool.getDouble(Double.doubleToLongBits(i));
283     }
284
285     protected BigDecimal JavaDoc readDecimal() throws IOException JavaDoc, HsqlException {
286
287         String JavaDoc s = readNumberField();
288
289         if (s == null) {
290             return null;
291         }
292
293         BigDecimal JavaDoc i = new BigDecimal JavaDoc(s);
294
295         return ValuePool.getBigDecimal(i);
296     }
297
298     protected Time JavaDoc readTime() throws IOException JavaDoc, HsqlException {
299
300         String JavaDoc s = readField();
301
302         if (s == null) {
303             return null;
304         }
305
306         return HsqlDateTime.timeValue(s);
307     }
308
309     protected Date JavaDoc readDate() throws IOException JavaDoc, HsqlException {
310
311         String JavaDoc s = readField();
312
313         if (s == null) {
314             return null;
315         }
316
317         return HsqlDateTime.dateValue(s);
318     }
319
320     protected Timestamp JavaDoc readTimestamp() throws IOException JavaDoc, HsqlException {
321
322         String JavaDoc s = readField();
323
324         if (s == null) {
325             return null;
326         }
327
328         return HsqlDateTime.timestampValue(s);
329     }
330
331     protected Boolean JavaDoc readBit() throws IOException JavaDoc, HsqlException {
332
333         String JavaDoc s = readField();
334
335         if (s == null) {
336             return null;
337         }
338
339         return s.equalsIgnoreCase("TRUE") ? Boolean.TRUE
340                                           : Boolean.FALSE;
341     }
342
343     protected Object JavaDoc readOther() throws IOException JavaDoc, HsqlException {
344
345         byte[] data;
346         String JavaDoc s = readField();
347
348         if (s == null) {
349             return null;
350         }
351
352         data = Column.hexToByteArray(s);
353
354         return new JavaObject(data);
355     }
356
357     protected Binary readBinary(int type) throws IOException JavaDoc, HsqlException {
358
359         String JavaDoc s = readField();
360
361         if (s == null) {
362             return null;
363         }
364
365         return new Binary(Column.hexToByteArray(s), false);
366     }
367 }
368
Popular Tags