KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataInputStream JavaDoc;
36 import java.io.EOFException JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream 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.Table;
46 import org.hsqldb.Trace;
47 import org.hsqldb.lib.Iterator;
48 import org.hsqldb.lib.SimpleLog;
49 import org.hsqldb.rowio.RowInputBase;
50 import org.hsqldb.rowio.RowInputBinary;
51
52 /**
53  * Reader corresponding to BinaryDatabaseScritReader.
54  *
55  * @author fredt@users
56  * @version 1.7.2
57  * @since 1.7.2
58  */

59 class ScriptReaderBinary extends ScriptReaderBase {
60
61     private RowInputBinary rowIn;
62     protected DataInputStream JavaDoc dataStreamIn;
63
64     ScriptReaderBinary(Database db,
65                        String JavaDoc file) throws HsqlException, IOException JavaDoc {
66
67         super(db, file);
68
69         rowIn = new RowInputBinary();
70     }
71
72     protected void openFile() throws IOException JavaDoc {
73
74         InputStream JavaDoc d = db.isFilesInJar()
75                         ? getClass().getResourceAsStream(fileName)
76                         : db.getFileAccess().openInputStreamElement(fileName);
77
78         dataStreamIn = new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(d,
79                 1 << 13));
80     }
81
82     protected void readDDL(Session session)
83     throws IOException JavaDoc, HsqlException {
84
85         Result r = Result.read(rowIn, dataStreamIn);
86         Iterator it = r.iterator();
87
88         while (it.hasNext()) {
89             Object JavaDoc[] data = (Object JavaDoc[]) it.next();
90             String JavaDoc s = (String JavaDoc) data[0];
91             Result result = session.sqlExecuteDirectNoPreChecks(s);
92
93             if (result.isError()) {
94                 db.logger.appLog.logContext(SimpleLog.LOG_ERROR,
95                                             result.getMainString());
96
97                 /** @todo fredt - trap if unavaialble external functions are to be ignored */
98                 throw Trace.error(result);
99             }
100         }
101     }
102
103     protected void readExistingData(Session session)
104     throws IOException JavaDoc, HsqlException {
105
106         for (;;) {
107             String JavaDoc s = readTableInit();
108
109             if (s == null) {
110                 break;
111             }
112
113             String JavaDoc schema = session.getSchemaName(currentSchema);
114             Table t = db.schemaManager.getUserTable(session, s, schema);
115             int j = 0;
116
117             for (j = 0; ; j++) {
118                 if (readRow(t) == false) {
119                     break;
120                 }
121             }
122
123             int checkCount = readTableTerm();
124
125             if (j != checkCount) {
126                 throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
127                                   Trace.ERROR_IN_BINARY_SCRIPT_1,
128                                   new Object JavaDoc[] {
129                     s, new Integer JavaDoc(j), new Integer JavaDoc(checkCount)
130                 });
131             }
132         }
133     }
134
135     // int : row size (0 if no more rows) ,
136
// BinaryServerRowInput : row (column values)
137
protected boolean readRow(Table t) throws IOException JavaDoc, HsqlException {
138
139         boolean more = readRow(rowIn, 0);
140
141         if (!more) {
142             return false;
143         }
144
145         Object JavaDoc[] data = rowIn.readData(t.getColumnTypes());
146
147         t.insertFromScript(data);
148
149         return true;
150     }
151
152     // int : rowcount
153
protected int readTableTerm() throws IOException JavaDoc, HsqlException {
154         return dataStreamIn.readInt();
155     }
156
157     // int : headersize (0 if no more tables), String : tablename, int : operation,
158
// String : schemaname
159
protected String JavaDoc readTableInit() throws IOException JavaDoc, HsqlException {
160
161         boolean more = readRow(rowIn, 0);
162
163         if (!more) {
164             return null;
165         }
166
167         String JavaDoc s = rowIn.readString();
168
169         // operation is always INSERT
170
int checkOp = rowIn.readIntData();
171
172         if (checkOp == ScriptWriterBase.INSERT_WITH_SCHEMA) {
173             currentSchema = rowIn.readString();
174         } else {
175             currentSchema = null;
176         }
177
178         if (checkOp != ScriptWriterBase.INSERT
179                 && checkOp != ScriptWriterBase.INSERT_WITH_SCHEMA) {
180             throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE,
181                               Trace.ERROR_IN_BINARY_SCRIPT_2);
182         }
183
184         return s;
185     }
186
187     boolean readRow(RowInputBase rowin, int pos) throws IOException JavaDoc {
188
189         try {
190             int length = dataStreamIn.readInt();
191             int count = 4;
192
193             if (length == 0) {
194                 return false;
195             }
196
197             rowin.resetRow(pos, length);
198             dataStreamIn.readFully(rowin.getBuffer(), count, length - count);
199
200             return true;
201         } catch (EOFException JavaDoc e) {
202             return false;
203         }
204     }
205
206     public boolean readLoggedStatement(Session session) throws IOException JavaDoc {
207         return false;
208     }
209
210     public void close() {
211
212         try {
213             dataStreamIn.close();
214         } catch (IOException JavaDoc e) {}
215     }
216 }
217
Popular Tags