001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.rowio;
032:
033: import java.io.IOException;
034: import java.math.BigDecimal;
035: import java.sql.Date;
036: import java.sql.Time;
037: import java.sql.Timestamp;
038:
039: import org.hsqldb.Column;
040: import org.hsqldb.HsqlDateTime;
041: import org.hsqldb.HsqlException;
042: import org.hsqldb.Token;
043: import org.hsqldb.Tokenizer;
044: import org.hsqldb.Types;
045: import org.hsqldb.scriptio.ScriptReaderBase;
046: import org.hsqldb.store.ValuePool;
047: import org.hsqldb.types.Binary;
048: import org.hsqldb.types.JavaObject;
049: import org.hsqldb.lib.java.JavaSystem;
050:
051: /**
052: * Class for reading the data for a database row from the script file.
053: *
054: * @author fredt@users
055: * @version 1.8.0
056: * @since 1.7.3
057: */
058: public class RowInputTextLog extends RowInputBase implements
059: RowInputInterface {
060:
061: Tokenizer tokenizer;
062: String tableName = null;
063: String schemaName = null;
064: int statementType;
065:
066: public RowInputTextLog() {
067:
068: super (new byte[0]);
069:
070: tokenizer = new Tokenizer();
071: }
072:
073: public void setSource(String text) throws HsqlException {
074:
075: tokenizer.reset(text);
076:
077: statementType = ScriptReaderBase.ANY_STATEMENT;
078:
079: String s = tokenizer.getString();
080:
081: if (s.equals(Token.T_INSERT)) {
082: statementType = ScriptReaderBase.INSERT_STATEMENT;
083:
084: tokenizer.getString();
085:
086: tableName = tokenizer.getString();
087:
088: tokenizer.getString();
089: } else if (s.equals(Token.T_DELETE)) {
090: statementType = ScriptReaderBase.DELETE_STATEMENT;
091:
092: tokenizer.getString();
093:
094: tableName = tokenizer.getString();
095: } else if (s.equals(Token.T_COMMIT)) {
096: statementType = ScriptReaderBase.COMMIT_STATEMENT;
097: } else if (s.equals(Token.T_SET)) {
098: if (tokenizer.isGetThis(Token.T_SCHEMA)) {
099: schemaName = tokenizer.getSimpleName();
100: statementType = ScriptReaderBase.SCHEMA_STATEMENT;
101: }
102: }
103: }
104:
105: public int getStatementType() {
106: return statementType;
107: }
108:
109: public String getTableName() {
110: return tableName;
111: }
112:
113: public String getSchemaName() {
114: return schemaName;
115: }
116:
117: protected String readField() throws IOException {
118:
119: try {
120: tokenizer.getString();
121:
122: if (statementType == ScriptReaderBase.DELETE_STATEMENT) {
123: tokenizer.getString();
124: tokenizer.getString();
125: }
126:
127: String s = tokenizer.getString();
128:
129: if (tokenizer.getType() == Types.NULL) {
130: s = null;
131: }
132:
133: return s;
134: } catch (HsqlException e) {
135: throw new IOException(e.getMessage());
136: }
137: }
138:
139: protected String readNumberField() throws IOException {
140:
141: try {
142: tokenizer.getString();
143:
144: if (statementType == ScriptReaderBase.DELETE_STATEMENT) {
145: tokenizer.getString();
146: tokenizer.getString();
147: }
148:
149: String s = tokenizer.getString();
150:
151: if ("-".equals(s)) {
152: s = s + tokenizer.getString();
153: } else if (tokenizer.getType() == Types.NULL) {
154: s = null;
155: }
156:
157: return s;
158: } catch (HsqlException e) {
159: throw new IOException(e.getMessage());
160: }
161: }
162:
163: public String readString() throws IOException {
164:
165: String s = readField();
166:
167: return ValuePool.getString(s);
168: }
169:
170: public short readShortData() throws IOException {
171:
172: String s = readNumberField();
173:
174: if (s == null) {
175: return 0;
176: }
177:
178: return Short.parseShort(s);
179: }
180:
181: public int readIntData() throws IOException {
182:
183: String s = readNumberField();
184:
185: if (s == null) {
186: return 0;
187: }
188:
189: return Integer.parseInt(s);
190: }
191:
192: public long readLongData() throws IOException {
193:
194: String s = readNumberField();
195:
196: if (s == null) {
197: return 0;
198: }
199:
200: return Long.parseLong(s);
201: }
202:
203: public int readType() throws IOException {
204: return 0;
205: }
206:
207: protected boolean checkNull() {
208:
209: // Return null on each column read instead.
210: return false;
211: }
212:
213: protected String readChar(int type) throws IOException {
214: return readString();
215: }
216:
217: protected Integer readSmallint() throws IOException, HsqlException {
218:
219: String s = readNumberField();
220:
221: if (s == null) {
222: return null;
223: }
224:
225: int i = Integer.parseInt(s);
226:
227: return ValuePool.getInt(i);
228: }
229:
230: protected Integer readInteger() throws IOException, HsqlException {
231:
232: String s = readNumberField();
233:
234: if (s == null) {
235: return null;
236: }
237:
238: int i = Integer.parseInt(s);
239:
240: return ValuePool.getInt(i);
241: }
242:
243: protected Long readBigint() throws IOException, HsqlException {
244:
245: String s = readNumberField();
246:
247: if (s == null) {
248: return null;
249: }
250:
251: long i = Long.parseLong(s);
252:
253: return ValuePool.getLong(i);
254: }
255:
256: protected Double readReal(int type) throws IOException,
257: HsqlException {
258:
259: String 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 readDecimal() throws IOException,
286: HsqlException {
287:
288: String s = readNumberField();
289:
290: if (s == null) {
291: return null;
292: }
293:
294: BigDecimal i = new BigDecimal(s);
295:
296: return ValuePool.getBigDecimal(i);
297: }
298:
299: protected Time readTime() throws IOException, HsqlException {
300:
301: String s = readField();
302:
303: if (s == null) {
304: return null;
305: }
306:
307: return HsqlDateTime.timeValue(s);
308: }
309:
310: protected Date readDate() throws IOException, HsqlException {
311:
312: String s = readField();
313:
314: if (s == null) {
315: return null;
316: }
317:
318: return HsqlDateTime.dateValue(s);
319: }
320:
321: protected Timestamp readTimestamp() throws IOException,
322: HsqlException {
323:
324: String s = readField();
325:
326: if (s == null) {
327: return null;
328: }
329:
330: return HsqlDateTime.timestampValue(s);
331: }
332:
333: protected Boolean readBit() throws IOException, HsqlException {
334:
335: String s = readField();
336:
337: if (s == null) {
338: return null;
339: }
340:
341: return s.equalsIgnoreCase("TRUE") ? Boolean.TRUE
342: : Boolean.FALSE;
343: }
344:
345: protected Object readOther() throws IOException, HsqlException {
346:
347: byte[] data;
348: String s = readField();
349:
350: if (s == null) {
351: return null;
352: }
353:
354: data = Column.hexToByteArray(s);
355:
356: return new JavaObject(data);
357: }
358:
359: protected Binary readBinary(int type) throws IOException,
360: HsqlException {
361:
362: String s = readField();
363:
364: if (s == null) {
365: return null;
366: }
367:
368: return new Binary(Column.hexToByteArray(s), false);
369: }
370: }
|