1 5 package org.h2.tools; 6 7 import java.io.BufferedInputStream ; 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.io.Reader ; 14 import java.sql.Connection ; 15 import java.sql.DriverManager ; 16 import java.sql.ResultSet ; 17 import java.sql.SQLException ; 18 import java.sql.Statement ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 22 import org.h2.engine.Constants; 23 import org.h2.message.Message; 24 import org.h2.util.JdbcUtils; 25 import org.h2.util.ScriptReader; 26 import org.h2.util.StringUtils; 27 28 34 public class RunScript { 35 36 private static final boolean MULTI_THREAD = false; 37 38 private void showUsage() { 39 System.out.println("java " + getClass().getName() + " -url <url> -user <user> [-password <pwd>] [-script <file>] [-driver <driver] [-options <option> ...]"); 40 } 41 42 64 public static void main(String [] args) throws SQLException { 65 new RunScript().run(args); 66 } 67 68 private void run(String [] args) throws SQLException { 69 String url = null; 70 String user = null; 71 String password = ""; 72 String script = "backup.sql"; 73 String options = null; 74 boolean continueOnError = false; 75 for (int i = 0; args != null && i < args.length; i++) { 76 if (args[i].equals("-url")) { 77 url = args[++i]; 78 } else if (args[i].equals("-user")) { 79 user = args[++i]; 80 } else if (args[i].equals("-password")) { 81 password = args[++i]; 82 } else if (args[i].equals("-continueOnError")) { 83 continueOnError = true; 84 } else if (args[i].equals("-script")) { 85 script = args[++i]; 86 } else if (args[i].equals("-driver")) { 87 String driver = args[++i]; 88 try { 89 Class.forName(driver); 90 } catch (ClassNotFoundException e) { 91 throw Message.convert(e); 92 } 93 } else if (args[i].equals("-options")) { 94 StringBuffer buff = new StringBuffer (); 95 i++; 96 for (; i < args.length; i++) { 97 buff.append(' '); 98 buff.append(args[i]); 99 } 100 options = buff.toString(); 101 } else { 102 showUsage(); 103 return; 104 } 105 } 106 if (url == null || user == null || password == null || script == null) { 107 showUsage(); 108 return; 109 } 110 if (options != null) { 114 executeRunscript(url, user, password, script, options); 115 } else { 116 execute(url, user, password, script, null, continueOnError); 117 } 118 } 122 123 130 public static ResultSet execute(Connection conn, Reader reader) throws SQLException { 131 Statement stat = conn.createStatement(); 132 ResultSet rs = null; 133 ScriptReader r = new ScriptReader(reader); 134 while (true) { 135 String sql = r.readStatement(); 136 if (sql == null) { 137 break; 138 } 139 boolean resultset = stat.execute(sql); 140 if (resultset) { 141 if (rs != null) { 142 rs.close(); 143 rs = null; 144 } 145 rs = stat.getResultSet(); 146 } 147 } 148 return rs; 149 } 150 151 private static void execute(Connection conn, HashMap threadMap, String fileName, boolean continueOnError, String charsetName) throws SQLException , IOException { 152 InputStream in = new FileInputStream (fileName); 153 String path = new File (fileName).getAbsoluteFile().getParent(); 154 try { 155 BufferedInputStream bin = new BufferedInputStream (in, Constants.IO_BUFFER_SIZE); 156 InputStreamReader reader = new InputStreamReader (bin, charsetName); 157 execute(conn, threadMap, continueOnError, path, reader, charsetName); 158 } finally { 159 in.close(); 160 } 161 } 162 163 private static void execute(Connection conn, HashMap threadMap, boolean continueOnError, String path, Reader reader, String charsetName) throws SQLException , IOException { 164 Statement stat = conn.createStatement(); 165 ScriptReader r = new ScriptReader(reader); 166 while (true) { 167 String sql = r.readStatement(); 168 if (sql == null) { 169 break; 170 } 171 sql = sql.trim(); 172 if (sql.startsWith("@") && StringUtils.toUpperEnglish(sql).startsWith("@INCLUDE")) { 173 sql = sql.substring("@INCLUDE".length()).trim(); 174 if(!new File (sql).isAbsolute()) { 175 sql = path + File.separator + sql; 176 } 177 execute(conn, threadMap, sql, continueOnError, charsetName); 178 } else if (MULTI_THREAD && sql.startsWith("/*")) { 179 int idx = sql.indexOf(']'); 180 Integer id = new Integer (Integer.parseInt(sql.substring("/*".length(), idx))); 181 RunScriptThread thread = (RunScriptThread) threadMap.get(id); 182 if (thread == null) { 183 Connection c = DriverManager.getConnection(conn.getMetaData().getURL()); 184 thread = new RunScriptThread(id.intValue(), c); 185 threadMap.put(id, thread); 186 thread.start(); 187 } 188 sql = sql.substring(sql.indexOf("*/") + 2).trim(); 189 String up = StringUtils.toUpperEnglish(sql); 190 thread.addStatement(sql); 191 if (up.startsWith("CREATE") || up.startsWith("DROP") || up.startsWith("ALTER")) { 192 thread.executeAll(); 193 } else { 194 } 195 } else { 196 try { 197 stat.execute(sql); 198 } catch (SQLException e) { 199 if (continueOnError) { 200 e.printStackTrace(); 201 } else { 202 throw e; 203 } 204 } 205 } 206 } 207 Iterator it = threadMap.values().iterator(); 208 while (it.hasNext()) { 209 RunScriptThread thread = (RunScriptThread) it.next(); 210 try { 211 thread.join(); 212 } catch (InterruptedException e) { 213 e.printStackTrace(); 214 } 215 } 216 } 217 218 private static void executeRunscript(String url, String user, String password, String fileName, String options) throws SQLException { 219 Connection conn = null; 220 Statement stat = null; 221 try { 222 org.h2.Driver.load(); 223 conn = DriverManager.getConnection(url, user, password); 224 stat = conn.createStatement(); 225 String sql = "RUNSCRIPT FROM '" + fileName + "' " + options; 226 stat.execute(sql); 227 } catch (Exception e) { 228 throw Message.convert(e); 229 } finally { 230 JdbcUtils.closeSilently(stat); 231 JdbcUtils.closeSilently(conn); 232 } 233 } 234 235 246 public static void execute(String url, String user, String password, String fileName, String charsetName, boolean continueOnError) throws SQLException { 247 try { 248 org.h2.Driver.load(); 249 Connection conn = DriverManager.getConnection(url, user, password); 250 if (charsetName == null) { 251 charsetName = Constants.UTF8; 252 } 253 HashMap threadMap = new HashMap (); 254 try { 255 execute(conn, threadMap, fileName, continueOnError, charsetName); 256 } finally { 257 conn.close(); 258 } 259 } catch (Exception e) { 260 throw Message.convert(e); 261 } 262 } 263 264 } 265 | Popular Tags |