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.Trace;
043: import org.hsqldb.Types;
044: import org.hsqldb.types.Binary;
045: import org.hsqldb.types.JavaObject;
046:
047: /**
048: * Class for reading the data for a database row in text table format.
049: *
050: * @author sqlbob@users (RMP)
051: * @version 1.8.0
052: * @since 1.7.0
053: */
054: public class RowInputText extends RowInputBase implements
055: RowInputInterface {
056:
057: // text table specific
058: private String fieldSep;
059: private String varSep;
060: private String longvarSep;
061: private int fieldSepLen;
062: private int varSepLen;
063: private int longvarSepLen;
064: private boolean fieldSepEnd;
065: private boolean varSepEnd;
066: private boolean longvarSepEnd;
067: private int textLen;
068: protected String text;
069: protected int line;
070: protected int field;
071: protected int next = 0;
072: protected boolean allQuoted;
073:
074: /**
075: * fredt@users - comment - in future may use a custom subclasse of
076: * InputStream to read the data.
077: *
078: * author: sqlbob@users (RMP)
079: */
080: public RowInputText(String fieldSep, String varSep,
081: String longvarSep, boolean allQuoted) {
082:
083: super (new byte[0]);
084:
085: //-- Newline indicates that field should match to end of line.
086: if (fieldSep.endsWith("\n")) {
087: fieldSepEnd = true;
088: fieldSep = fieldSep.substring(0, fieldSep.length() - 1);
089: }
090:
091: if (varSep.endsWith("\n")) {
092: varSepEnd = true;
093: varSep = varSep.substring(0, varSep.length() - 1);
094: }
095:
096: if (longvarSep.endsWith("\n")) {
097: longvarSepEnd = true;
098: longvarSep = longvarSep.substring(0,
099: longvarSep.length() - 1);
100: }
101:
102: this .allQuoted = allQuoted;
103: this .fieldSep = fieldSep;
104: this .varSep = varSep;
105: this .longvarSep = longvarSep;
106: fieldSepLen = fieldSep.length();
107: varSepLen = varSep.length();
108: longvarSepLen = longvarSep.length();
109: }
110:
111: public void setSource(String text, int pos, int byteSize) {
112:
113: size = byteSize;
114: this .text = text;
115: textLen = text.length();
116: filePos = pos;
117: next = 0;
118:
119: line++;
120:
121: field = 0;
122: }
123:
124: protected String getField(String sep, int sepLen, boolean isEnd)
125: throws IOException {
126:
127: String s = null;
128:
129: try {
130: int start = next;
131:
132: field++;
133:
134: if (isEnd) {
135: if ((next >= textLen) && (sepLen > 0)) {
136: throw Trace
137: .error(Trace.TextDatabaseRowInput_getField);
138: } else if (text.endsWith(sep)) {
139: next = textLen - sepLen;
140: } else {
141: throw Trace
142: .error(Trace.TextDatabaseRowInput_getField2);
143: }
144: } else {
145: next = text.indexOf(sep, start);
146:
147: if (next == -1) {
148: next = textLen;
149: }
150: }
151:
152: if (start > next) {
153: start = next;
154: }
155:
156: s = text.substring(start, next);
157: next += sepLen;
158: s = s.trim();
159:
160: if (s.length() == 0) {
161: s = null;
162: }
163: } catch (Exception e) {
164: throw new IOException(Trace.getMessage(
165: Trace.TextDatabaseRowInput_getField3, true,
166: new Object[] { new Integer(field), e.toString() }));
167: }
168:
169: return s;
170: }
171:
172: public String readString() throws IOException {
173: return getField(fieldSep, fieldSepLen, fieldSepEnd);
174: }
175:
176: private String readVarString() throws IOException {
177: return getField(varSep, varSepLen, varSepEnd);
178: }
179:
180: private String readLongVarString() throws IOException {
181: return getField(longvarSep, longvarSepLen, longvarSepEnd);
182: }
183:
184: public short readShortData() throws IOException {
185: return (short) readIntData();
186: }
187:
188: public int readIntData() throws IOException {
189:
190: String s = readString();
191:
192: if (s == null) {
193: return 0;
194: }
195:
196: s = s.trim();
197:
198: if (s.length() == 0) {
199: return 0;
200: }
201:
202: return Integer.parseInt(s);
203: }
204:
205: public long readLongData() throws IOException {
206: throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
207: "RowInputText");
208: }
209:
210: public int readType() throws IOException {
211: return 0;
212: }
213:
214: protected boolean checkNull() {
215:
216: // Return null on each column read instead.
217: return false;
218: }
219:
220: protected String readChar(int type) throws IOException {
221:
222: switch (type) {
223:
224: case Types.CHAR:
225: return readString();
226:
227: case Types.VARCHAR:
228: case Types.VARCHAR_IGNORECASE:
229: return readVarString();
230:
231: case Types.LONGVARCHAR:
232: default:
233: return readLongVarString();
234: }
235: }
236:
237: protected Integer readSmallint() throws IOException, HsqlException {
238:
239: String s = readString();
240:
241: if (s == null) {
242: return null;
243: }
244:
245: s = s.trim();
246:
247: if (s.length() == 0) {
248: return null;
249: }
250:
251: return Integer.valueOf(s);
252: }
253:
254: protected Integer readInteger() throws IOException, HsqlException {
255:
256: String s = readString();
257:
258: if (s == null) {
259: return null;
260: }
261:
262: s = s.trim();
263:
264: if (s.length() == 0) {
265: return null;
266: }
267:
268: return Integer.valueOf(s);
269: }
270:
271: protected Long readBigint() throws IOException, HsqlException {
272:
273: String s = readString();
274:
275: if (s == null) {
276: return null;
277: }
278:
279: s = s.trim();
280:
281: if (s.length() == 0) {
282: return null;
283: }
284:
285: return Long.valueOf(s);
286: }
287:
288: protected Double readReal(int type) throws IOException,
289: HsqlException {
290:
291: String s = readString();
292:
293: if (s == null) {
294: return null;
295: }
296:
297: s = s.trim();
298:
299: if (s.length() == 0) {
300: return null;
301: }
302:
303: return Double.valueOf(s);
304: }
305:
306: protected BigDecimal readDecimal() throws IOException,
307: HsqlException {
308:
309: String s = readString();
310:
311: if (s == null) {
312: return null;
313: }
314:
315: s = s.trim();
316:
317: if (s.length() == 0) {
318: return null;
319: }
320:
321: return new BigDecimal(s);
322: }
323:
324: protected Time readTime() throws IOException, HsqlException {
325:
326: String s = readString();
327:
328: if (s == null) {
329: return null;
330: }
331:
332: s = s.trim();
333:
334: if (s.length() == 0) {
335: return null;
336: }
337:
338: return HsqlDateTime.timeValue(s);
339: }
340:
341: protected Date readDate() throws IOException, HsqlException {
342:
343: String s = readString();
344:
345: if (s == null) {
346: return null;
347: }
348:
349: s = s.trim();
350:
351: if (s.length() == 0) {
352: return null;
353: }
354:
355: return HsqlDateTime.dateValue(s);
356: }
357:
358: protected Timestamp readTimestamp() throws IOException,
359: HsqlException {
360:
361: String s = readString();
362:
363: if (s == null) {
364: return null;
365: }
366:
367: s = s.trim();
368:
369: if (s.length() == 0) {
370: return null;
371: }
372:
373: return HsqlDateTime.timestampValue(s);
374: }
375:
376: protected Boolean readBit() throws IOException, HsqlException {
377:
378: String s = readString();
379:
380: if (s == null) {
381: return null;
382: }
383:
384: s = s.trim();
385:
386: if (s.length() == 0) {
387: return null;
388: }
389:
390: return s.equalsIgnoreCase("TRUE") ? Boolean.TRUE
391: : Boolean.FALSE;
392: }
393:
394: protected Object readOther() throws IOException, HsqlException {
395:
396: byte[] data;
397: String s = readString();
398:
399: if (s == null) {
400: return null;
401: }
402:
403: s = s.trim();
404:
405: if (s.length() == 0) {
406: return null;
407: }
408:
409: data = Column.hexToByteArray(s);
410:
411: return new JavaObject(data);
412: }
413:
414: protected Binary readBinary(int type) throws IOException,
415: HsqlException {
416:
417: String s = readString();
418:
419: if (s == null) {
420: return null;
421: }
422:
423: s = s.trim();
424:
425: if (s.length() == 0) {
426: return null;
427: }
428:
429: return new Binary(Column.hexToByteArray(s), false);
430: }
431:
432: public int getLineNumber() {
433: return line;
434: }
435:
436: public void skippedLine() {
437: line++;
438: }
439:
440: public void reset() {
441:
442: text = "";
443: textLen = 0;
444: filePos = 0;
445: next = 0;
446: field = 0;
447: line = 0;
448: }
449: }
|