001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.lang.reflect.Method;
022: import java.sql.DatabaseMetaData;
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
031: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetDefinition;
032: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSet;
033: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
034: import net.sourceforge.squirrel_sql.fw.util.NullMessageHandler;
035: import net.sourceforge.squirrel_sql.fw.util.StringManager;
036: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
037:
038: public class MetaDataDataSet implements IDataSet {
039: /** Internationalized strings for this class. */
040: private static final StringManager s_stringMgr = StringManagerFactory
041: .getStringManager(MetaDataDataSet.class);
042:
043: private final static Map<String, Object> s_ignoreMethods = new HashMap<String, Object>();
044: static {
045: s_ignoreMethods.put("getCatalogs", null);
046: s_ignoreMethods.put("getConnection", null);
047: s_ignoreMethods.put("getSchemas", null);
048: s_ignoreMethods.put("getTableTypes", null);
049: s_ignoreMethods.put("getTypeInfo", null);
050: s_ignoreMethods.put("fail", null);
051: s_ignoreMethods.put("hashCode", null);
052: s_ignoreMethods.put("toString", null);
053: s_ignoreMethods.put("getNumericFunctions", null);
054: s_ignoreMethods.put("getStringFunctions", null);
055: s_ignoreMethods.put("getSystemFunctions", null);
056: s_ignoreMethods.put("getTimeDateFunctions", null);
057: s_ignoreMethods.put("getSQLKeywords", null);
058: }
059:
060: private static interface IStrings {
061: String UNSUPPORTED = s_stringMgr
062: .getString("MetaDataDataSet.unsupported");
063: String NAME_COLUMN = s_stringMgr
064: .getString("MetaDataDataSet.propname");
065: String VALUE_COLUMN = s_stringMgr
066: .getString("MetaDataDataSet.value");
067: }
068:
069: private final static String[] s_hdgs = new String[] {
070: IStrings.NAME_COLUMN, IStrings.VALUE_COLUMN };
071: private DataSetDefinition _dsDef;
072:
073: private Iterator<Object[]> _rowsIter;
074: private Object[] _row;
075:
076: private IMessageHandler _msgHandler;
077:
078: /**
079: * Data. Each element represents a row of the table and is made up of
080: * an array of strings. Each string is an element in the row.
081: */
082: private List<Object[]> _data = new ArrayList<Object[]>();
083:
084: public MetaDataDataSet(DatabaseMetaData md) {
085: this (md, null);
086: }
087:
088: public MetaDataDataSet(DatabaseMetaData md,
089: IMessageHandler msgHandler) {
090: super ();
091: _msgHandler = msgHandler != null ? msgHandler
092: : NullMessageHandler.getInstance();
093: _dsDef = new DataSetDefinition(createColumnDefinitions());
094: load(md);
095: }
096:
097: public final int getColumnCount() {
098: return s_hdgs.length;
099: }
100:
101: public DataSetDefinition getDataSetDefinition() {
102: return _dsDef;
103: }
104:
105: public synchronized boolean next(IMessageHandler msgHandler) {
106: if (_rowsIter.hasNext()) {
107: _row = _rowsIter.next();
108: } else {
109: _row = null;
110: }
111: return _row != null;
112: }
113:
114: public synchronized Object get(int columnIndex) {
115: return _row[columnIndex];
116: }
117:
118: private ColumnDisplayDefinition[] createColumnDefinitions() {
119: final int columnCount = getColumnCount();
120: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[columnCount];
121: for (int i = 0; i < columnCount; ++i) {
122: columnDefs[i] = new ColumnDisplayDefinition(200, s_hdgs[i]);
123: }
124: return columnDefs;
125: }
126:
127: private void load(DatabaseMetaData md) {
128: Method[] methods = DatabaseMetaData.class.getMethods();
129: for (int i = 0; i < methods.length; ++i) {
130: final Method method = methods[i];
131: if (method.getParameterTypes().length == 0
132: && method.getReturnType() != Void.TYPE
133: && !s_ignoreMethods.containsKey(method.getName())) {
134: _data.add(generateLine(md, method));
135: }
136: }
137:
138: // Sort the rows by the property name.
139: // Collections.sort(_data, new DataSorter());
140:
141: _rowsIter = _data.iterator();
142: }
143:
144: /**
145: * Generate a line for the result of calling the passed method.
146: *
147: * @param getter The "getter" function to retrieve the
148: * properties value.
149: *
150: * @return An <TT>Object[]</CODE> containing the cells for the line in
151: * the table. Element zero the first cell etc. Return
152: * <CODE>null</CODE> if this property is <B>not</B> to be added
153: * to the table.
154: */
155: private Object[] generateLine(DatabaseMetaData md, Method getter) {
156: final Object[] line = new Object[2];
157: line[0] = getter.getName();
158: if (line[0].equals("getDefaultTransactionIsolation")) {
159: try {
160: line[1] = IStrings.UNSUPPORTED;
161: final int isol = md.getDefaultTransactionIsolation();
162: switch (isol) {
163: case java.sql.Connection.TRANSACTION_NONE: {
164: line[1] = "TRANSACTION_NONE";
165: break;
166: }
167: case java.sql.Connection.TRANSACTION_READ_COMMITTED: {
168: line[1] = "TRANSACTION_READ_COMMITTED";
169: break;
170: }
171: case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: {
172: line[1] = "TRANSACTION_READ_UNCOMMITTED";
173: break;
174: }
175: case java.sql.Connection.TRANSACTION_REPEATABLE_READ: {
176: line[1] = "TRANSACTION_REPEATABLE_READ";
177: break;
178: }
179: case java.sql.Connection.TRANSACTION_SERIALIZABLE: {
180: line[1] = "TRANSACTION_SERIALIZABLE";
181: break;
182: }
183: default: {
184: line[1] = "" + isol + "?";
185: break;
186: }
187: }
188: } catch (SQLException ex) {
189: _msgHandler.showMessage(ex, null);
190: }
191:
192: } else {
193: Object obj = executeGetter(md, getter);
194: line[1] = obj;
195: }
196: return line;
197: }
198:
199: protected Object executeGetter(Object bean, Method getter) {
200: try {
201: return getter.invoke(bean, (Object[]) null);
202: } catch (Throwable th) {
203: return IStrings.UNSUPPORTED;
204: }
205: }
206: }
|