1 5 package org.h2.server.web; 6 7 import java.sql.Connection ; 8 import java.sql.DatabaseMetaData ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Statement ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 15 import org.h2.bnf.Bnf; 16 import org.h2.message.TraceSystem; 17 18 public class AppSession extends WebServerSession { 19 private static final int MAX_HISTORY = 1000; 20 private ArrayList commandHistory = new ArrayList (); 21 22 private Connection conn; 23 private DatabaseMetaData meta; 24 private DbContents contents = new DbContents(); 25 private DbContextRule columnRule; 26 private DbContextRule newAliasRule; 27 private DbContextRule tableRule; 28 private DbContextRule aliasRule; 29 private DbContextRule columnAliasRule; 30 private Bnf bnf; 31 Statement executingStatement; 32 ResultSet result; 33 34 AppSession(WebServer server) { 35 super(server); 36 } 37 38 public Bnf getBnf() { 39 return bnf; 40 } 41 42 void loadBnf() { 43 try { 44 Bnf newBnf = Bnf.getInstance(null); 45 columnRule = new DbContextRule(contents, DbContextRule.COLUMN); 46 newAliasRule = new DbContextRule(contents, DbContextRule.NEW_TABLE_ALIAS); 47 aliasRule = new DbContextRule(contents, DbContextRule.TABLE_ALIAS); 48 tableRule = new DbContextRule(contents, DbContextRule.TABLE); 49 columnAliasRule = new DbContextRule(contents, DbContextRule.COLUMN_ALIAS); 50 newBnf.updateTopic("columnName", columnRule); 62 newBnf.updateTopic("newTableAlias", newAliasRule); 63 newBnf.updateTopic("tableAlias", aliasRule); 64 newBnf.updateTopic("columnAlias", columnAliasRule); 65 newBnf.updateTopic("tableName", tableRule); 66 newBnf.linkStatements(); 68 bnf = newBnf; 69 } catch(Exception e) { 70 e.printStackTrace(); 72 } 73 } 74 75 String getCommand(int id) { 76 return (String ) commandHistory.get(id); 77 } 78 79 void addCommand(String sql) { 80 if(sql == null) { 81 return; 82 } 83 sql = sql.trim(); 84 if(sql.length() == 0) { 85 return; 86 } 87 if(commandHistory.size() > MAX_HISTORY) { 88 commandHistory.remove(0); 89 } 90 int idx = commandHistory.indexOf(sql); 91 if(idx >= 0) { 92 commandHistory.remove(idx); 93 } 94 commandHistory.add(sql); 95 } 96 97 ArrayList getCommands() { 98 return commandHistory; 99 } 100 101 public HashMap getInfo() { 102 HashMap m = super.getInfo(); 103 try { 104 m.put("url", conn == null ? "not connected" : conn.getMetaData().getURL()); 105 m.put("user", conn == null ? "-" : conn.getMetaData().getUserName()); 106 m.put("lastQuery", commandHistory.size()==0 ? "" : commandHistory.get(0)); 107 m.put("executing", executingStatement==null ? "no" : "yes"); 108 } catch (SQLException e) { 109 TraceSystem.traceThrowable(e); 110 } 111 return m; 112 } 113 114 void setConnection(Connection conn) throws SQLException { 115 this.conn = conn; 116 if(conn == null) { 117 meta = null; 118 } else { 119 meta = conn.getMetaData(); 120 } 121 contents = new DbContents(); 122 } 123 124 DatabaseMetaData getMetaData() { 125 return meta; 126 } 127 128 Connection getConnection() { 129 return conn; 130 } 131 132 public DbContents getContents() { 133 return contents; 134 } 135 136 } 137 | Popular Tags |