KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > server > web > AppSession


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.server.web;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DatabaseMetaData JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Statement JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashMap JavaDoc;
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 JavaDoc commandHistory = new ArrayList JavaDoc();
21     
22     private Connection JavaDoc conn;
23     private DatabaseMetaData JavaDoc 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 JavaDoc executingStatement;
32     ResultSet JavaDoc 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 // bnf.updateTopic("newTableName", new String[]{"TEST"});
51
// String[] schemas;
52
// if(contents.isMySQL) {
53
// schemas = new String[0];
54
// } else {
55
// schemas = new String[contents.schemas.length];
56
// for(int i=0; i<contents.schemas.length; i++) {
57
// schemas[i] = contents.schemas[i].quotedName + ".";
58
// }
59
// }
60
// bnf.updateTopic("schemaName", schemas);
61
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             // bnf.updateTopic("name", new String[]{""});
67
newBnf.linkStatements();
68             bnf = newBnf;
69         } catch(Exception JavaDoc e) {
70             // ok we don't have the bnf
71
e.printStackTrace();
72         }
73     }
74     
75     String JavaDoc getCommand(int id) {
76         return (String JavaDoc) commandHistory.get(id);
77     }
78     
79     void addCommand(String JavaDoc 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 JavaDoc getCommands() {
98         return commandHistory;
99     }
100     
101     public HashMap JavaDoc getInfo() {
102         HashMap JavaDoc 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 JavaDoc e) {
109             TraceSystem.traceThrowable(e);
110         }
111         return m;
112     }
113
114     void setConnection(Connection JavaDoc conn) throws SQLException JavaDoc {
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 JavaDoc getMetaData() {
125         return meta;
126     }
127
128     Connection JavaDoc getConnection() {
129         return conn;
130     }
131
132     public DbContents getContents() {
133         return contents;
134     }
135     
136 }
137
Popular Tags