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.scriptio;
032:
033: import java.io.BufferedInputStream;
034: import java.io.DataInputStream;
035: import java.io.EOFException;
036: import java.io.IOException;
037: import java.io.InputStream;
038:
039: import org.hsqldb.Database;
040: import org.hsqldb.HsqlException;
041: import org.hsqldb.Result;
042: import org.hsqldb.ResultConstants;
043: import org.hsqldb.Session;
044: import org.hsqldb.Table;
045: import org.hsqldb.Trace;
046: import org.hsqldb.lib.Iterator;
047: import org.hsqldb.lib.SimpleLog;
048: import org.hsqldb.rowio.RowInputBase;
049: import org.hsqldb.rowio.RowInputBinary;
050:
051: /**
052: * Reader corresponding to BinaryDatabaseScritReader.
053: *
054: * @author fredt@users
055: * @version 1.7.2
056: * @since 1.7.2
057: */
058: class ScriptReaderBinary extends ScriptReaderBase {
059:
060: private RowInputBinary rowIn;
061: protected DataInputStream dataStreamIn;
062:
063: ScriptReaderBinary(Database db, String file) throws HsqlException,
064: IOException {
065:
066: super (db, file);
067:
068: rowIn = new RowInputBinary();
069: }
070:
071: protected void openFile() throws IOException {
072:
073: InputStream d = db.isFilesInJar() ? getClass()
074: .getResourceAsStream(fileName) : db.getFileAccess()
075: .openInputStreamElement(fileName);
076:
077: dataStreamIn = new DataInputStream(new BufferedInputStream(d,
078: 1 << 13));
079: }
080:
081: protected void readDDL(Session session) throws IOException,
082: HsqlException {
083:
084: Result r = Result.read(rowIn, dataStreamIn);
085: Iterator it = r.iterator();
086:
087: while (it.hasNext()) {
088: Object[] data = (Object[]) it.next();
089: String s = (String) data[0];
090: Result result = session.sqlExecuteDirectNoPreChecks(s);
091:
092: if (result.isError()) {
093: db.logger.appLog.logContext(SimpleLog.LOG_ERROR, result
094: .getMainString());
095:
096: /** @todo fredt - trap if unavaialble external functions are to be ignored */
097: throw Trace.error(result);
098: }
099: }
100: }
101:
102: protected void readExistingData(Session session)
103: throws IOException, HsqlException {
104:
105: for (;;) {
106: String s = readTableInit();
107:
108: if (s == null) {
109: break;
110: }
111:
112: String schema = session.getSchemaName(currentSchema);
113: Table t = db.schemaManager.getUserTable(session, s, schema);
114: int j = 0;
115:
116: for (j = 0;; j++) {
117: if (readRow(t) == false) {
118: break;
119: }
120: }
121:
122: int checkCount = readTableTerm();
123:
124: if (j != checkCount) {
125: throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
126: Trace.ERROR_IN_BINARY_SCRIPT_1, new Object[] {
127: s, new Integer(j),
128: new Integer(checkCount) });
129: }
130: }
131: }
132:
133: // int : row size (0 if no more rows) ,
134: // BinaryServerRowInput : row (column values)
135: protected boolean readRow(Table t) throws IOException,
136: HsqlException {
137:
138: boolean more = readRow(rowIn, 0);
139:
140: if (!more) {
141: return false;
142: }
143:
144: Object[] data = rowIn.readData(t.getColumnTypes());
145:
146: t.insertFromScript(data);
147:
148: return true;
149: }
150:
151: // int : rowcount
152: protected int readTableTerm() throws IOException, HsqlException {
153: return dataStreamIn.readInt();
154: }
155:
156: // int : headersize (0 if no more tables), String : tablename, int : operation,
157: // String : schemaname
158: protected String readTableInit() throws IOException, HsqlException {
159:
160: boolean more = readRow(rowIn, 0);
161:
162: if (!more) {
163: return null;
164: }
165:
166: String s = rowIn.readString();
167:
168: // operation is always INSERT
169: int checkOp = rowIn.readIntData();
170:
171: if (checkOp == ScriptWriterBase.INSERT_WITH_SCHEMA) {
172: currentSchema = rowIn.readString();
173: } else {
174: currentSchema = null;
175: }
176:
177: if (checkOp != ScriptWriterBase.INSERT
178: && checkOp != ScriptWriterBase.INSERT_WITH_SCHEMA) {
179: throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
180: Trace.ERROR_IN_BINARY_SCRIPT_2);
181: }
182:
183: return s;
184: }
185:
186: boolean readRow(RowInputBase rowin, int pos) throws IOException {
187:
188: try {
189: int length = dataStreamIn.readInt();
190: int count = 4;
191:
192: if (length == 0) {
193: return false;
194: }
195:
196: rowin.resetRow(pos, length);
197: dataStreamIn.readFully(rowin.getBuffer(), count, length
198: - count);
199:
200: return true;
201: } catch (EOFException e) {
202: return false;
203: }
204: }
205:
206: public boolean readLoggedStatement(Session session)
207: throws IOException {
208: return false;
209: }
210:
211: public void close() {
212:
213: try {
214: dataStreamIn.close();
215: } catch (IOException e) {
216: }
217: }
218: }
|