1 30 31 32 package org.hsqldb.persist; 33 34 import java.io.EOFException ; 35 36 import org.hsqldb.Database; 37 import org.hsqldb.HsqlException; 38 import org.hsqldb.Result; 39 import org.hsqldb.ResultConstants; 40 import org.hsqldb.Session; 41 import org.hsqldb.Trace; 42 import org.hsqldb.lib.IntKeyHashMap; 43 import org.hsqldb.lib.SimpleLog; 44 import org.hsqldb.lib.StopWatch; 45 import org.hsqldb.scriptio.ScriptReaderBase; 46 47 57 public class ScriptRunner { 58 59 65 public static void runScript(Database database, String logFilename, 66 int logType) throws HsqlException { 67 68 IntKeyHashMap sessionMap = new IntKeyHashMap(); 69 Session sysSession = database.getSessionManager().getSysSession(); 70 Session current = sysSession; 71 int currentId = 0; 72 73 database.setReferentialIntegrity(false); 74 75 ScriptReaderBase scr = null; 76 77 try { 78 StopWatch sw = new StopWatch(); 79 80 scr = ScriptReaderBase.newScriptReader(database, logFilename, 81 logType); 82 83 while (scr.readLoggedStatement(current)) { 84 int sessionId = scr.getSessionNumber(); 85 86 if (currentId != sessionId) { 87 currentId = sessionId; 88 current = (Session) sessionMap.get(currentId); 89 90 if (current == null) { 91 current = 92 database.getSessionManager().newSession(database, 93 sysSession.getUser(), false, true); 94 95 sessionMap.put(currentId, current); 96 } 97 } 98 99 if (current.isClosed()) { 100 sessionMap.remove(currentId); 101 102 continue; 103 } 104 105 Result result = null; 106 107 switch (scr.getStatementType()) { 108 109 case ScriptReaderBase.ANY_STATEMENT : 110 result = current.sqlExecuteDirectNoPreChecks( 111 scr.getLoggedStatement()); 112 113 if (result != null && result.isError()) { 114 if (result.getException() != null) { 115 throw result.getException(); 116 } 117 118 throw Trace.error(result); 119 } 120 break; 121 122 case ScriptReaderBase.SEQUENCE_STATEMENT : 123 scr.getCurrentSequence().reset( 124 scr.getSequenceValue()); 125 break; 126 127 case ScriptReaderBase.COMMIT_STATEMENT : 128 current.commit(); 129 break; 130 131 case ScriptReaderBase.INSERT_STATEMENT : { 132 Object [] data = scr.getData(); 133 134 scr.getCurrentTable().insertNoCheckFromLog(current, 135 data); 136 137 break; 138 } 139 case ScriptReaderBase.DELETE_STATEMENT : { 140 Object [] data = scr.getData(); 141 142 scr.getCurrentTable().deleteNoCheckFromLog(current, 143 data); 144 145 break; 146 } 147 case ScriptReaderBase.SCHEMA_STATEMENT : { 148 current.setSchema(scr.getCurrentSchema()); 149 } 150 } 151 152 if (current.isClosed()) { 153 sessionMap.remove(currentId); 154 } 155 } 156 } catch (Throwable e) { 157 String message; 158 159 if (e instanceof EOFException ) { 161 162 } else if (e instanceof OutOfMemoryError ) { 164 message = "out of memory processing " + logFilename 165 + " line: " + scr.getLineNumber(); 166 167 database.logger.appLog.logContext(SimpleLog.LOG_ERROR, 168 message); 169 170 throw Trace.error(Trace.OUT_OF_MEMORY); 171 } else { 172 173 message = logFilename + " line: " + scr.getLineNumber() + " " 175 + e.toString(); 176 177 database.logger.appLog.logContext(SimpleLog.LOG_ERROR, 178 message); 179 } 180 } finally { 181 if (scr != null) { 182 scr.close(); 183 } 184 185 database.getSessionManager().closeAllSessions(); 186 database.setReferentialIntegrity(true); 187 } 188 } 189 } 190 | Popular Tags |