001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.common.jdbc;
022:
023: import java.sql.Connection;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.Hashtable;
028:
029: public class ConnectionHandler {
030: private Connection connection;
031: private Hashtable metacache;
032:
033: public ConnectionHandler(Connection connection) {
034: this .connection = connection;
035:
036: metacache = new Hashtable();
037: try {
038: metacache.put("$maxColumnNameLength", new Integer(
039: connection.getMetaData().getMaxColumnNameLength()));
040: metacache.put("$identifierQuoteString", connection
041: .getMetaData().getIdentifierQuoteString());
042:
043: // String term = connection.getMetaData().getSchemaTerm();
044: // metacache.put("$supportsSchema", new Boolean(term!=null && term.length()>0) );
045:
046: metacache
047: .put("$supportsSchema", new Boolean(connection
048: .getMetaData()
049: .supportsSchemasInTableDefinitions()));
050: } catch (Exception e) { /* do_nothing */
051: }
052:
053: loadSchemas();
054: loadTableTypes();
055: loadConnectionInfos();
056: }
057:
058: private boolean supportsSchema() {
059: return ((Boolean) metacache.get("$supportsSchema"))
060: .booleanValue();
061: }
062:
063: private void loadSchemas() {
064: ArrayList schemas = new ArrayList();
065: metacache.put("$schema_names", schemas);
066:
067: try {
068: if (this .supportsSchema()) {
069: ResultSet rs = connection.getMetaData().getSchemas();
070: while (rs.next()) {
071: String name = rs.getString(1).trim();
072: if (!schemas.contains(name))
073: schemas.add(name);
074: }
075: rs.close();
076: }
077: } catch (Exception e) { /* do_nothing */
078: }
079: }
080:
081: private void loadTableTypes() {
082: ArrayList tableTypes = new ArrayList();
083: metacache.put("$table_types", tableTypes);
084:
085: try {
086: ResultSet rs = connection.getMetaData().getTableTypes();
087: while (rs.next()) {
088: String type = rs.getString(1).trim();
089: if (!tableTypes.contains(type))
090: tableTypes.add(type);
091: }
092: rs.close();
093: } catch (Exception e) { /* do_nothing */
094: }
095: }
096:
097: private void loadConnectionInfos() {
098: ArrayList infos = new ArrayList();
099: metacache.put("$connection_infos", infos);
100:
101: try {
102: String[] info = new String[2];
103: info[0] = "database product name";
104: info[1] = connection.getMetaData().getDatabaseProductName();
105: infos.add(info);
106:
107: info = new String[2];
108: info[0] = "database product version";
109: info[1] = connection.getMetaData()
110: .getDatabaseProductVersion();
111: infos.add(info);
112:
113: info = new String[2];
114: info[0] = "driver name";
115: info[1] = connection.getMetaData().getDriverName();
116: infos.add(info);
117:
118: info = new String[2];
119: info[0] = "driver version";
120: info[1] = connection.getMetaData().getDriverVersion();
121: infos.add(info);
122: } catch (Exception e) { /* do_nothing */
123: }
124: }
125:
126: /* bad */
127: public Connection get() {
128: return connection;
129: }
130:
131: public void close() throws SQLException {
132: connection.close();
133: }
134:
135: public ArrayList getArrayList(String key) {
136: return (ArrayList) metacache.get(key);
137: }
138:
139: public Object getObject(String key) {
140: return metacache.get(key);
141: }
142: }
|