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.BufferedReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.InputStreamReader;
038:
039: import org.hsqldb.Database;
040: import org.hsqldb.HsqlException;
041: import org.hsqldb.Result;
042: import org.hsqldb.Session;
043: import org.hsqldb.Trace;
044: import org.hsqldb.lib.SimpleLog;
045: import org.hsqldb.lib.StringConverter;
046: import org.hsqldb.rowio.RowInputTextLog;
047:
048: /**
049: * Handles operations involving reading back a script or log file written
050: * out by ScriptWriterText. This implementation
051: * corresponds to ScriptWriterText.
052: *
053: * @author fredt@users
054: * @version 1.8.0
055: * @since 1.7.2
056: */
057: public class ScriptReaderText extends ScriptReaderBase {
058:
059: // this is used only to enable reading one logged line at a time
060: BufferedReader dataStreamIn;
061: RowInputTextLog rowIn;
062: boolean isInsert;
063:
064: ScriptReaderText(Database db, String file) throws HsqlException,
065: IOException {
066:
067: super (db, file);
068:
069: rowIn = new RowInputTextLog();
070: }
071:
072: protected void openFile() throws IOException {
073:
074: InputStream d = db.isFilesInJar() ? getClass()
075: .getResourceAsStream(fileName) : db.getFileAccess()
076: .openInputStreamElement(fileName);
077:
078: dataStreamIn = new BufferedReader(new InputStreamReader(
079: new BufferedInputStream(d)));
080: }
081:
082: protected void readDDL(Session session) throws IOException,
083: HsqlException {
084:
085: for (; readLoggedStatement(session);) {
086: if (rowIn.getStatementType() == INSERT_STATEMENT) {
087: isInsert = true;
088:
089: break;
090: }
091:
092: Result result = session
093: .sqlExecuteDirectNoPreChecks(statement);
094:
095: if (result != null && result.isError()) {
096: db.logger.appLog.logContext(SimpleLog.LOG_ERROR, result
097: .getMainString());
098:
099: HsqlException error = Trace.error(
100: Trace.ERROR_IN_SCRIPT_FILE,
101: Trace.DatabaseScriptReader_readDDL,
102: new Object[] { new Integer(lineCount),
103: result.getMainString() });
104:
105: /** @todo fredt - if unavaialble external functions are to be ignored */
106: throw error;
107: }
108: }
109: }
110:
111: protected void readExistingData(Session session)
112: throws IOException, HsqlException {
113:
114: try {
115: String tablename = null;
116:
117: // fredt - needed for forward referencing FK constraints
118: db.setReferentialIntegrity(false);
119:
120: for (; isInsert || readLoggedStatement(session); isInsert = false) {
121: if (statementType == SCHEMA_STATEMENT) {
122: session.setSchema(currentSchema);
123:
124: continue;
125: } else if (statementType == INSERT_STATEMENT) {
126: if (!rowIn.getTableName().equals(tablename)) {
127: tablename = rowIn.getTableName();
128:
129: String schema = session
130: .getSchemaName(currentSchema);
131:
132: currentTable = db.schemaManager.getUserTable(
133: session, tablename, schema);
134: }
135:
136: currentTable.insertFromScript(rowData);
137: }
138: }
139:
140: db.setReferentialIntegrity(true);
141: } catch (Exception e) {
142: db.logger.appLog.logContext(e, null);
143:
144: throw Trace
145: .error(
146: Trace.ERROR_IN_SCRIPT_FILE,
147: Trace.DatabaseScriptReader_readExistingData,
148: new Object[] { new Integer(lineCount),
149: e.toString() });
150: }
151: }
152:
153: public boolean readLoggedStatement(Session session)
154: throws IOException {
155:
156: //fredt temporary solution - should read bytes directly from buffer
157: String s = dataStreamIn.readLine();
158:
159: lineCount++;
160:
161: // System.out.println(lineCount);
162: statement = StringConverter.asciiToUnicode(s);
163:
164: if (statement == null) {
165: return false;
166: }
167:
168: processStatement(session);
169:
170: return true;
171: }
172:
173: private void processStatement(Session session) throws IOException {
174:
175: try {
176: if (statement.startsWith("/*C")) {
177: int endid = statement.indexOf('*', 4);
178:
179: sessionNumber = Integer.parseInt(statement.substring(3,
180: endid));
181: statement = statement.substring(endid + 2);
182: }
183:
184: rowIn.setSource(statement);
185:
186: statementType = rowIn.getStatementType();
187:
188: if (statementType == ANY_STATEMENT) {
189: rowData = null;
190: currentTable = null;
191:
192: return;
193: } else if (statementType == COMMIT_STATEMENT) {
194: rowData = null;
195: currentTable = null;
196:
197: return;
198: } else if (statementType == SCHEMA_STATEMENT) {
199: rowData = null;
200: currentTable = null;
201: currentSchema = rowIn.getSchemaName();
202:
203: return;
204: }
205:
206: String name = rowIn.getTableName();
207: String schema = session.getSchemaName(null);
208:
209: currentTable = db.schemaManager.getUserTable(session, name,
210: schema);
211:
212: int[] colTypes;
213:
214: if (statementType == INSERT_STATEMENT) {
215: colTypes = currentTable.getColumnTypes();
216: } else if (currentTable.hasPrimaryKey()) {
217: colTypes = currentTable.getPrimaryKeyTypes();
218: } else {
219: colTypes = currentTable.getColumnTypes();
220: }
221:
222: rowData = rowIn.readData(colTypes);
223: } catch (Exception e) {
224: throw new IOException(e.toString());
225: }
226: }
227:
228: public void close() {
229:
230: try {
231: dataStreamIn.close();
232: } catch (Exception e) {
233: }
234: }
235: }
|