1 30 31 32 package org.hsqldb.util; 33 34 import java.io.BufferedReader ; 35 import java.io.FileReader ; 36 import java.sql.Connection ; 37 import java.sql.DriverManager ; 38 import java.sql.ResultSet ; 39 import java.sql.ResultSetMetaData ; 40 import java.sql.SQLException ; 41 import java.sql.Statement ; 42 import java.util.Properties ; 43 44 import org.hsqldb.lib.java.JavaSystem; 45 46 49 55 public class ScriptTool { 56 57 private static Properties pProperties = new Properties (); 58 private Connection cConn; 59 private Statement sStatement; 60 private boolean BATCH = true; 61 private String EKW = new String ("go"); 62 private boolean EOF = false; 63 private int ln = 0; 64 65 71 public static void main(String [] arg) { 72 73 for (int i = 0; i < arg.length; i++) { 74 String p = arg[i]; 75 76 if (p.equals("-?")) { 77 printHelp(); 78 System.exit(0); 79 } 80 } 81 82 ScriptTool tool = new ScriptTool(); 83 84 tool.execute(arg); 85 System.exit(0); 86 } 88 public void execute(String [] arg) { 89 90 for (int i = 0; i < arg.length; i++) { 91 String p = arg[i]; 92 93 if (p.charAt(0) == '-') { 94 pProperties.put(p.substring(1), arg[i + 1]); 95 96 i++; 97 } 98 } 99 100 ln = 0; 101 EOF = false; 102 103 BufferedReader in = null; 104 Properties p = pProperties; 105 String driver = p.getProperty("driver", "org.hsqldb.jdbcDriver"); 106 String url = p.getProperty("url", "jdbc:hsqldb:"); 107 String database = p.getProperty("database", "test"); 108 String user = p.getProperty("user", "sa"); 109 String password = p.getProperty("password", ""); 110 String script = p.getProperty("script", "st.sql"); 111 boolean log = p.getProperty("log", "false").equalsIgnoreCase("true"); 112 113 BATCH = p.getProperty("batch", "true").equalsIgnoreCase("true"); 114 115 try { 116 if (log) { 117 trace("driver = " + driver); 118 trace("url = " + url); 119 trace("database = " + database); 120 trace("user = " + user); 121 trace("password = " + password); 122 trace("script = " + script); 123 trace("log = " + log); 124 trace("batch = " + BATCH); 125 JavaSystem.setLogToSystem(true); 126 } 127 128 Class.forName(driver).newInstance(); 134 135 cConn = DriverManager.getConnection(url + database, user, 136 password); 137 in = new BufferedReader (new FileReader (script)); 138 } catch (Exception e) { 139 System.out.println("ScriptTool.init error: " + e.getMessage()); 140 e.printStackTrace(); 141 } 142 143 try { 144 sStatement = cConn.createStatement(); 145 146 String sql; 147 148 while ((sql = fileToString(in)) != null) { 149 if (sql.length() == 1) { 150 continue; 151 } 152 153 if (log) { 154 trace("SQL (" + ln + ") : " 155 + sql.substring(0, sql.length() - 2)); 156 } 157 158 sStatement.execute(sql); 159 160 ResultSet results = sStatement.getResultSet(); 161 int updateCount = sStatement.getUpdateCount(); 162 163 if (updateCount == -1) { 164 trace(toString(results)); 165 } else { 166 trace("update count " + updateCount); 167 } 168 } 169 } catch (SQLException e) { 170 System.out.println("SQL Error at line " + ln + ": " + e); 171 } 172 173 try { 174 cConn.close(); 175 in.close(); 176 } catch (Exception ce) {} 177 } 178 179 183 private String toString(ResultSet r) { 184 185 try { 186 if (r == null) { 187 return "No Result"; 188 } 189 190 ResultSetMetaData m = r.getMetaData(); 191 int col = m.getColumnCount(); 192 StringBuffer strbuf = new StringBuffer (); 193 194 for (int i = 1; i <= col; i++) { 195 strbuf = strbuf.append(m.getColumnLabel(i) + "\t"); 196 } 197 198 strbuf = strbuf.append("\n"); 199 200 while (r.next()) { 201 for (int i = 1; i <= col; i++) { 202 strbuf = strbuf.append(r.getString(i) + "\t"); 203 204 if (r.wasNull()) { 205 strbuf = strbuf.append("(null)\t"); 206 } 207 } 208 209 strbuf = strbuf.append("\n"); 210 } 211 212 return strbuf.toString(); 213 } catch (SQLException e) { 214 return null; 215 } 216 } 217 218 221 private String fileToString(BufferedReader in) { 222 223 if (EOF) { 224 return null; 225 } 226 227 EOF = true; 228 229 StringBuffer a = new StringBuffer (); 230 231 try { 232 String line; 233 234 while ((line = in.readLine()) != null) { 235 ln = ln + 1; 236 237 if (BATCH) { 238 if (line.startsWith("print ")) { 239 trace("\n" + line.substring(5)); 240 241 continue; 242 } 243 244 if (line.equalsIgnoreCase(EKW)) { 245 EOF = false; 246 247 break; 248 } 249 } 250 251 a.append(line); 252 a.append('\n'); 253 } 254 255 a.append('\n'); 256 257 return a.toString(); 258 } catch (Exception e) { 259 e.printStackTrace(); 260 261 throw new RuntimeException (e.getMessage()); 262 } 263 } 264 265 271 private void trace(String s) { 272 System.out.println(s); 273 } 274 275 279 private static void printHelp() { 280 281 System.out.println( 282 "Usage: java ScriptTool [-options]\n" 283 + "where options include:\n" 284 + " -driver <classname> name of the driver class\n" 285 + " -url <name> first part of the jdbc url\n" 286 + " -database <name> second part of the jdbc url\n" 287 + " -user <name> username used for connection\n" 288 + " -password <name> password for this user\n" 289 + " -log <true/false> write log to system out\n" 290 + " -batch <true/false> allow go/print pseudo statements\n" 291 + " -script <script file> reads from script file\n"); 292 } 293 } 294 | Popular Tags |