01: package net.sourceforge.squirrel_sql.client.session.parser;
02:
03: import java.util.Hashtable;
04: import java.util.List;
05: import java.util.Vector;
06:
07: import net.sourceforge.squirrel_sql.client.session.ISession;
08: import net.sourceforge.squirrel_sql.client.session.parser.kernel.SQLSchema;
09: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
10: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
11:
12: public class SQLSchemaImpl implements SQLSchema {
13: private ISession _session;
14: private Hashtable<String, SQLSchema.Table> _tableCache = new Hashtable<String, Table>();
15: private SQLDatabaseMetaData _dmd;
16:
17: SQLSchemaImpl(ISession session) {
18: _session = session;
19: if (_session != null) {
20: _session.getApplication().getThreadPool().addTask(
21: new Runnable() {
22: public void run() {
23: ISQLConnection con = _session
24: .getSQLConnection();
25: if (con != null) {
26: _dmd = con.getSQLMetaData();
27: }
28: }
29: });
30: }
31: }
32:
33: public SQLSchema.Table getTable(String catalog, String schema,
34: String name) {
35: if (_session.getSchemaInfo().isTable(name)) {
36: String key = getKey(catalog, schema, name);
37: SQLSchema.Table ret = _tableCache.get(key);
38: if (null == ret) {
39: ret = new SQLSchema.Table(catalog, schema, name, _dmd);
40: _tableCache.put(key, ret);
41: }
42: return ret;
43: }
44: return null;
45: }
46:
47: private String getKey(String catalog, String schema, String name) {
48: if (null == catalog) {
49: catalog = "null";
50: }
51: if (null == schema) {
52: schema = "null";
53: }
54:
55: StringBuffer ret = new StringBuffer();
56: ret.append(catalog).append(",").append(schema).append(",")
57: .append(name);
58:
59: return ret.toString();
60: }
61:
62: public List<Table> getTables(String catalog, String schema,
63: String name) {
64: Vector<Table> ret = new Vector<Table>();
65: String[] tableNames = _session.getSchemaInfo().getTables();
66:
67: for (int i = 0; i < tableNames.length; i++) {
68: String key = getKey(catalog, schema, name);
69: SQLSchema.Table buf = _tableCache.get(key);
70: if (null == buf) {
71: buf = new SQLSchema.Table(catalog, schema,
72: tableNames[i], _dmd);
73: _tableCache.put(key, buf);
74: }
75: ret.add(buf);
76: }
77: return ret;
78: }
79:
80: public SQLSchema.Table getTableForAlias(String alias) {
81: return null;
82: }
83: }
|