1 5 package org.h2.command.dml; 6 7 import java.io.IOException ; 8 import java.io.InputStreamReader ; 9 import java.io.Reader ; 10 import java.sql.SQLException ; 11 12 import org.h2.command.Prepared; 13 import org.h2.engine.Session; 14 import org.h2.message.Message; 15 import org.h2.util.ScriptReader; 16 import org.h2.util.StringUtils; 17 18 public class RunScript extends ScriptBase { 19 20 private String charset = StringUtils.getDefaultCharset(); 21 22 public RunScript(Session session) { 23 super(session); 24 } 25 26 public int update() throws SQLException { 27 session.getUser().checkAdmin(); 28 int count = 0; 29 try { 30 openInput(); 31 Reader reader = new InputStreamReader (in, charset); 32 ScriptReader r = new ScriptReader(reader); 33 while(true) { 34 String sql = r.readStatement(); 35 if(sql == null) { 36 break; 37 } 38 execute(sql); 39 count++; 40 } 41 reader.close(); 42 } catch(IOException e) { 43 throw Message.convert(e); 44 } finally { 45 closeIO(); 46 } 47 return count; 48 } 49 50 private void execute(String sql) throws SQLException { 51 try { 52 Prepared command = session.prepare(sql); 53 if(command.isQuery()) { 54 command.query(0); 55 } else { 56 command.update(); 57 } 58 if(session.getAutoCommit()) { 59 session.commit(); 60 } 61 } catch(SQLException e) { 62 throw Message.addSQL(e, sql); 63 } 64 } 65 66 public void setCharset(String charset) { 67 this.charset = charset; 68 } 69 70 } 71 | Popular Tags |