1 package org.ashkelon.db; 2 3 import java.sql.Connection ; 4 import java.sql.DriverManager ; 5 import java.sql.SQLException ; 6 import java.sql.Statement ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import org.ashkelon.util.Logger; 12 13 16 public class DBProc 17 { 18 static Map scriptMap = null; 19 private Logger log; 20 21 public DBProc() 22 { 23 if (scriptMap == null) 24 { 25 scriptMap = new HashMap (); 26 scriptMap.put("reset", "org/ashkelon/db/ashkelon.sql"); 27 scriptMap.put("reset_seqs", "org/ashkelon/db/init.sql"); 28 scriptMap.put("add_idx", "org/ashkelon/db/ashkelon_addidx.sql"); 29 scriptMap.put("del_idx", "org/ashkelon/db/ashkelon_dropidx.sql"); 30 } 31 log = Logger.getInstance(); 32 } 35 36 public static void entryPoint(String action) 40 { 41 DBProc proc = new DBProc(); 42 String script = (String ) scriptMap.get(action); 43 proc.log.debug("Script is: "+script); 44 if (script == null) return; 45 boolean warn = (action.indexOf("idx") > -1) ? false : true; 46 List commands = ScriptParser.parse(script, warn); 47 proc.log.debug("Commands length: "+commands.size()); 48 49 try 50 { 51 Connection conn = DriverManager.getConnection("jdbc:default:connection:"); 53 54 Statement stmt = conn.createStatement(); 55 String command = ""; 56 for (int i=0; i<commands.size(); i++) 57 { 58 command = (String ) commands.get(i); 59 proc.log.debug("Cmd: "+command); 60 try { stmt.execute(command); 62 } catch (SQLException ex) 63 { 64 proc.log.error("Cmd Failed: "+command); 65 } 66 } 67 stmt.close(); 68 } catch (SQLException ex) 69 { 70 proc.log.error("Failed: "+ex.getMessage()); 71 DBUtils.logSQLException(ex); 72 } 73 } 74 75 public void doAction(Connection conn, String action) throws SQLException 76 { 77 String script = (String ) scriptMap.get(action); 78 if (script == null) 79 { 80 log.brief("cannot fulfill request to "+action); 81 return; 82 } 83 boolean warn = (action.indexOf("idx") > -1) ? false : true; 85 List commands = ScriptParser.parse(script, warn); 86 DBUtils.submitBatch(conn, commands); 87 } 88 89 public static void main(String args[]) throws SQLException 90 { 91 if (args.length == 0) 94 { 95 Logger.getInstance().traceln("Must provide an action"); 96 return; 97 } 98 entryPoint(args[0]); 100 } 102 103 } 104 | Popular Tags |