1 30 31 32 package org.hsqldb.scriptio; 33 34 import java.io.BufferedInputStream ; 35 import java.io.BufferedReader ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.InputStreamReader ; 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 59 public class ScriptReaderText extends ScriptReaderBase { 60 61 BufferedReader dataStreamIn; 63 RowInputTextLog rowIn; 64 boolean isInsert; 65 66 ScriptReaderText(Database db, 67 String file) throws HsqlException, IOException { 68 69 super(db, file); 70 71 rowIn = new RowInputTextLog(); 72 } 73 74 protected void openFile() throws IOException { 75 76 InputStream d = db.isFilesInJar() 77 ? getClass().getResourceAsStream(fileName) 78 : db.getFileAccess().openInputStreamElement(fileName); 79 80 dataStreamIn = new BufferedReader ( 81 new InputStreamReader (new BufferedInputStream (d))); 82 } 83 84 protected void readDDL(Session session) 85 throws IOException , 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 101 throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE, 102 Trace.DatabaseScriptReader_readDDL, 103 new Object [] { 104 new Integer (lineCount), result.getMainString() 105 }); 106 } 107 } 108 } 109 110 protected void readExistingData(Session session) 111 throws IOException , HsqlException { 112 113 try { 114 String tablename = null; 115 116 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 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 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 [] { 146 new Integer (lineCount), e.toString() 147 }); 148 } 149 } 150 151 public boolean readLoggedStatement(Session session) throws IOException { 152 153 String s = dataStreamIn.readLine(); 155 156 lineCount++; 157 158 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 { 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 name = rowIn.getTableName(); 204 String 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 e) { 221 throw new IOException (e.toString()); 222 } 223 } 224 225 public void close() { 226 227 try { 228 dataStreamIn.close(); 229 } catch (Exception e) {} 230 } 231 } 232 | Popular Tags |