1 package com.daffodilwoods.tools.shell; 2 3 import java.io.*; 4 5 import com.daffodilwoods.daffodildb.server.serversystem.*; 6 7 12 13 public class ScriptExecuter { 14 private _Connection connection; 15 static int initialBlockSize = 1500; 16 private QueryExecuter queryExecuter; 17 18 public ScriptExecuter(QueryExecuter queryExecuter0) { 19 queryExecuter = queryExecuter0; 20 } 21 25 public ScriptExecuter() { 26 } 27 28 public void setConnection(_Connection con) { 29 connection = con; 30 } 31 32 39 public void runScript(String filename) throws Exception { 40 if (isConnectionClosed()) { 41 throw new Exception ("\nNot connected with any database\n"); 42 } 43 RandomAccessFile scriptFile = null; 44 try { 45 scriptFile = new RandomAccessFile(filename, "r"); 46 runScript(scriptFile); 47 } 48 catch (FileNotFoundException ex) { 49 DisplayOutput.displayLn("File [" + filename + "] does not exists"); 50 } 51 finally { 52 if (scriptFile != null) { 53 scriptFile.close(); 54 } 55 } 56 } 57 58 69 public void runScript(RandomAccessFile file) throws Exception { 70 String line = ""; 71 StringBuffer finalStr = new StringBuffer (); 72 int count = 1; 73 while ( (line = file.readLine()) != null) { 74 if (line.startsWith("#") || line.startsWith("--")) { 75 continue; 76 } 77 int lineCommentIndex = line.indexOf("--"); 78 line = (lineCommentIndex > -1 ) ? line.substring(0,lineCommentIndex -1) : line; 79 if (line.trim().equals("/")) 80 finalStr.append("\n").append(line.trim()).append("\n"); 81 else 82 finalStr.append(" ").append(line.trim()); 83 if (count >= initialBlockSize) 84 { 85 line = runScript(finalStr.toString(), true); finalStr.setLength(0); 87 finalStr.append(line); 88 count = 0; 89 } 90 count++; 91 } 92 if (count < initialBlockSize) { 93 runScript(finalStr.toString(), false); 94 } 95 } 96 97 105 private String runScript(String partialScript, boolean partial) throws 106 Exception { 107 partialScript = removeComments(partialScript); 108 int halfCommentedIndex = partialScript.indexOf("/*"); 109 String executableBlock = halfCommentedIndex == -1 ? partialScript : 110 partialScript.substring(0, halfCommentedIndex); 111 String stub = halfCommentedIndex == -1 ? "" : 112 partialScript.substring(halfCommentedIndex, partialScript.length()); 113 114 if (partial) { 115 int lastSemicolonIndx = executableBlock.lastIndexOf(";"); 116 stub = executableBlock.substring(lastSemicolonIndx) + stub; 117 executableBlock = executableBlock.substring(0, lastSemicolonIndx); 118 } 119 return processAtomicQuery(executableBlock) + stub; 120 } 121 122 128 private String removeComments(String str) { 129 int startS = 0; 130 StringBuffer finalString = new StringBuffer (); 131 while (true) { 132 int s = str.indexOf("/*", startS); 133 if (s == -1) { 134 break; 135 } 136 finalString.append(str.substring(startS, s)); 137 int e = str.indexOf("*/", startS + 2); 138 str = str.substring(e + 2); 139 } 140 finalString.append(str); 141 return finalString.toString(); 142 } 143 144 150 private boolean isQuotesMatching(String str) { 151 int len = str.length(); 152 int s = 0, d = 0; 153 for (int i = 0; i < len; i++) { 154 char c = str.charAt(i); 155 if (c == '\'') { 156 s++; 157 } 158 if (c == '\"') { 159 d++; 160 } 161 } 162 return s % 2 == 0 && d % 2 == 0; 163 } 164 165 171 int n=0; 172 private String processAtomicQuery(String str) throws Exception { 173 String delim = ";", query = ""; 174 boolean completeQuery = true; 175 while (true) { 176 int start = 0; 177 if (completeQuery) { 178 if(isPSMQuery(str.trim())) { 179 delim = "\n/\n"; 180 } 181 else { 182 delim = ";"; 183 } 184 } 185 int end = str.indexOf(delim); 186 if (end == -1) { 187 break; 188 } 189 if (completeQuery) { 190 query = str.substring(start, end); 191 } 192 else { 193 query = query + delim + str.substring(0, end); 194 } 195 completeQuery = isQuotesMatching(query); 196 if (completeQuery) { 197 executeQuery(query); 198 } 199 try 200 { 201 str = str.substring(end + delim.length()); 202 } 203 catch (Exception ex) 204 { 205 DisplayOutput.printError("Error ....",ex); 206 } 207 } 208 return str; 209 } 210 211 private boolean isPSMQuery(String str) { if (!str.toUpperCase().startsWith("CREATE")) { 213 return false; 214 } 215 int secondWordStart = str.indexOf(" "); 216 str = str.substring(secondWordStart, str.length()).trim(); 217 String dbObject = str.substring(0,str.indexOf(" ")).trim(); 218 return (dbObject.toUpperCase().equals("PROCEDURE") || 219 dbObject.toUpperCase().equals("TRIGGER") || 220 dbObject.toUpperCase().equals("FUNCTION") || 221 dbObject.toUpperCase().equals("OR")); 222 } 223 224 228 protected void executeQuery(String query) { 229 try { 230 if (!"".equals(query.trim())) { 231 DisplayOutput.displayLn("\n" + query); 232 queryExecuter.execute(query, connection); 233 } 234 } 235 catch (Exception ex) { 236 DisplayOutput.printError("", ex); 237 } 238 } 239 240 protected boolean isConnectionClosed() throws Exception { 241 return connection==null || connection.isClosed(); 242 } 243 } 244 | Popular Tags |