KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > scriptio > ScriptReaderText


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.scriptio;
33
34 import java.io.BufferedInputStream JavaDoc;
35 import java.io.BufferedReader JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39
40 import org.hsqldb.Database;
41 import org.hsqldb.HsqlException;
42 import org.hsqldb.Result;
43 import org.hsqldb.ResultConstants;
44 import org.hsqldb.Session;
45 import org.hsqldb.Trace;
46 import org.hsqldb.lib.SimpleLog;
47 import org.hsqldb.lib.StringConverter;
48 import org.hsqldb.rowio.RowInputTextLog;
49
50 /**
51  * Handles operations involving reading back a script or log file written
52  * out by ScriptWriterText. This implementation
53  * corresponds to ScriptWriterText.
54  *
55  * @author fredt@users
56  * @version 1.8.0
57  * @since 1.7.2
58  */

59 public class ScriptReaderText extends ScriptReaderBase {
60
61     // this is used only to enable reading one logged line at a time
62
BufferedReader JavaDoc dataStreamIn;
63     RowInputTextLog rowIn;
64     boolean isInsert;
65
66     ScriptReaderText(Database db,
67                      String JavaDoc file) throws HsqlException, IOException JavaDoc {
68
69         super(db, file);
70
71         rowIn = new RowInputTextLog();
72     }
73
74     protected void openFile() throws IOException JavaDoc {
75
76         InputStream JavaDoc d = db.isFilesInJar()
77                         ? getClass().getResourceAsStream(fileName)
78                         : db.getFileAccess().openInputStreamElement(fileName);
79
80         dataStreamIn = new BufferedReader JavaDoc(
81             new InputStreamReader JavaDoc(new BufferedInputStream JavaDoc(d)));
82     }
83
84     protected void readDDL(Session session)
85     throws IOException JavaDoc, HsqlException {
86
87         for (; readLoggedStatement(session); ) {
88             if (rowIn.getStatementType() == INSERT_STATEMENT) {
89                 isInsert = true;
90
91                 break;
92             }
93
94             Result result = session.sqlExecuteDirectNoPreChecks(statement);
95
96             if (result != null && result.isError()) {
97                 db.logger.appLog.logContext(SimpleLog.LOG_ERROR,
98                                             result.getMainString());
99
100                 /** @todo fredt - if unavaialble external functions are to be ignored */
101                 throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
102                                   Trace.DatabaseScriptReader_readDDL,
103                                   new Object JavaDoc[] {
104                     new Integer JavaDoc(lineCount), result.getMainString()
105                 });
106             }
107         }
108     }
109
110     protected void readExistingData(Session session)
111     throws IOException JavaDoc, HsqlException {
112
113         try {
114             String JavaDoc tablename = null;
115
116             // fredt - needed for forward referencing FK constraints
117
db.setReferentialIntegrity(false);
118
119             for (; isInsert || readLoggedStatement(session);
120                     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 JavaDoc schema = session.getSchemaName(currentSchema);
130
131                         currentTable = db.schemaManager.getUserTable(session,
132                                 tablename, schema);
133                     }
134
135                     currentTable.insertFromScript(rowData);
136                 }
137             }
138
139             db.setReferentialIntegrity(true);
140         } catch (Exception JavaDoc e) {
141             db.logger.appLog.logContext(e, null);
142
143             throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
144                               Trace.DatabaseScriptReader_readExistingData,
145                               new Object JavaDoc[] {
146                 new Integer JavaDoc(lineCount), e.toString()
147             });
148         }
149     }
150
151     public boolean readLoggedStatement(Session session) throws IOException JavaDoc {
152
153         //fredt temporary solution - should read bytes directly from buffer
154
String JavaDoc s = dataStreamIn.readLine();
155
156         lineCount++;
157
158 // System.out.println(lineCount);
159
statement = StringConverter.asciiToUnicode(s);
160
161         if (statement == null) {
162             return false;
163         }
164
165         processStatement(session);
166
167         return true;
168     }
169
170     private void processStatement(Session session) throws IOException JavaDoc {
171
172         try {
173             if (statement.startsWith("/*C")) {
174                 int endid = statement.indexOf('*', 4);
175
176                 sessionNumber = Integer.parseInt(statement.substring(3,
177                         endid));
178                 statement = statement.substring(endid + 2);
179             }
180
181             rowIn.setSource(statement);
182
183             statementType = rowIn.getStatementType();
184
185             if (statementType == ANY_STATEMENT) {
186                 rowData = null;
187                 currentTable = null;
188
189                 return;
190             } else if (statementType == COMMIT_STATEMENT) {
191                 rowData = null;
192                 currentTable = null;
193
194                 return;
195             } else if (statementType == SCHEMA_STATEMENT) {
196                 rowData = null;
197                 currentTable = null;
198                 currentSchema = rowIn.getSchemaName();
199
200                 return;
201             }
202
203             String JavaDoc name = rowIn.getTableName();
204             String JavaDoc schema = session.getSchemaName(null);
205
206             currentTable = db.schemaManager.getUserTable(session, name,
207                     schema);
208
209             int[] colTypes;
210
211             if (statementType == INSERT_STATEMENT) {
212                 colTypes = currentTable.getColumnTypes();
213             } else if (currentTable.hasPrimaryKey()) {
214                 colTypes = currentTable.getPrimaryKeyTypes();
215             } else {
216                 colTypes = currentTable.getColumnTypes();
217             }
218
219             rowData = rowIn.readData(colTypes);
220         } catch (Exception JavaDoc e) {
221             throw new IOException JavaDoc(e.toString());
222         }
223     }
224
225     public void close() {
226
227         try {
228             dataStreamIn.close();
229         } catch (Exception JavaDoc e) {}
230     }
231 }
232
Popular Tags