📄 metadatavalues.java
字号:
/* * MetaDataValues.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.databasemediators;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import java.util.Vector;import org.executequery.EventMediator;import org.executequery.event.ConnectionEvent;import org.executequery.datasource.ConnectionDataSource;import org.executequery.datasource.ConnectionManager;import org.executequery.event.ConnectionListener;import org.underworldlabs.jdbc.DataSourceException;import org.executequery.gui.browser.ColumnConstraint;import org.executequery.gui.browser.ColumnData;import org.executequery.gui.browser.ColumnIndex;import org.executequery.gui.browser.DatabaseObject;import org.executequery.gui.browser.TablePrivilege;import org.executequery.util.Log;import org.underworldlabs.util.MiscUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * This class provides access to the current connection's * database meta data. Each method performs specific requests * as may be required by the calling object to display the * relevant data usually within a table or similar widget. * * Depending on the calling class and its requirements, * the connection to the database may be left open thereby * removing the overhead associated with connection retrieval - * as in the case of the Database Browser which makes frequent * database access requests. Other objects not requiring a * dedicated connection simply choose not to maintain one and * make their requests as required. * * @author Takis Diakoumis * @version $Revision: 1.15 $ * @date $Date: 2006/09/21 13:17:46 $ */public class MetaDataValues implements ConnectionListener { /** The open database connection. */ private Connection connection; /** Whether to keep the connection open. */ private boolean keepAlive; /** the database connection object associated with this instance */ private DatabaseConnection databaseConnection; /** the connection 'container' */ private Map<DatabaseConnection,Connection> connections; /** <p>Constructs a new instance where the conection * is returned following each request. */ public MetaDataValues() { this(false); } /** <p>Constructs a new instance where the conection * is returned following each request only if the * passed boolean value is 'false'. Otherwise the * connection is initialised and maintained following * the first request and reused for any subsequent requests. * * @param whether to keep the connection open */ public MetaDataValues(boolean keepAlive) { this(null, keepAlive); } public MetaDataValues(DatabaseConnection databaseConnection, boolean keepAlive) { this.databaseConnection = databaseConnection; this.keepAlive = keepAlive; connections = Collections.synchronizedMap(new HashMap()); // register for connection events EventMediator.registerListener(EventMediator.CONNECTION_EVENT, this); } /** * Sets the database connection object to that specified. */ public void setDatabaseConnection(DatabaseConnection dc) { if (!connections.containsKey(dc)) { connections.put(dc, null); } if (this.databaseConnection != dc) { connection = null; // null out for pending change this.databaseConnection = dc; } /* if (this.databaseConnection == dc) { Log.debug("same conn"); return; } Log.debug("new conn"); closeConnection(); this.databaseConnection = dc; */ } private void ensureConnection() throws DataSourceException { try { if (connection == null || connection.isClosed()) { if (Log.isDebugEnabled()) { if (connection != null) { Log.debug("Connection is closed."); } else { Log.debug("Connection is null - checking cache"); } } // try the cache first connection = connections.get(databaseConnection); if (connection == null) { if (Log.isDebugEnabled()) { Log.debug("ensureConnection: Connection is null in cache."); Log.debug("ensureConnection: Retrieving new connection."); } // retrieve and add to the cache connection = ConnectionManager.getConnection(databaseConnection); connections.put(databaseConnection, connection); } //connection = ConnectionManager.getConnection(databaseConnection); // if still null - something bad has happened, or maybe closed if (connection == null || connection.isClosed()) { throw new DataSourceException("No connection available", true); } } } catch (SQLException e) { throw new DataSourceException(e); } } /** * Retrieves the current connection's hosted * schema names. The names are stored within a * <code>Vector</code> object as single String objects. * * @return the schema names within a <code>Vector</code> */ public Vector<String> getHostedCatalogsVector() throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getCatalogs(); Vector<String> v = new Vector<String>(); while (rs.next()) { v.add(rs.getString(1)); } return v; } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } public String[] getExportedKeyTables(String catalog, String schema, String table) throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getExportedKeys(catalog, schema, table); List list = new ArrayList(); while (rs.next()) { list.add(rs.getString(7)); } return (String[])list.toArray(new String[list.size()]); } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } public String[] getImportedKeyTables(String catalog, String schema, String table) throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getImportedKeys(catalog, schema, table); List list = new ArrayList(); while (rs.next()) { list.add(rs.getString(3)); } return (String[])list.toArray(new String[list.size()]); } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } public List getHostedCatalogSchemas() throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getSchemas(); List list = new ArrayList(); while (rs.next()) { list.add(rs.getString(1)); } return list; } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } /** <p>Retrieves the current connection's hosted * schema names. The names are stored within a * <code>Vector</code> object as single String objects. * * @return the schema names within a <code>Vector</code> */ public Vector<String> getHostedSchemasVector() throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getSchemas(); Vector v = new Vector<String>(); while (rs.next()) { v.add(rs.getString(1)); } int size = v.size(); if (size == 1) { String value = (String)v.elementAt(0); if (MiscUtils.isNull(value)) { return new Vector<String>(0); } } /* else if (size == 0) { return getHostedCatalogsVector(); } */ return v; } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } public String[] getTableTypes() throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getTableTypes(); List list = new ArrayList(); while (rs.next()) { list.add(rs.getString(1)); } return (String[])list.toArray(new String[list.size()]); } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } public TablePrivilege[] getPrivileges(String catalog, String schema, String table) throws DataSourceException { ResultSet rs = null; try { ensureConnection(); DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getTablePrivileges(catalog, schema, table); List list = new ArrayList(); while (rs.next()) { list.add(new TablePrivilege(rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7))); } return (TablePrivilege[])list.toArray(new TablePrivilege[list.size()]); } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } /** <p>Retrieves the column names for the specified * database table and schema as an array. * * @param the database table name * @param the database schema name * @return the column name */ public Map getColumnProperties(String schema, String table, String column) throws DataSourceException { ResultSet rs = null; try { ensureConnection(); if(schema == null) { schema = getSchemaName().toUpperCase(); } DatabaseMetaData dmd = connection.getMetaData(); rs = dmd.getColumns(null, schema, table, column); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); String[] metaColumnNames = new String[columnCount]; for (int i = 1; i < columnCount; i++) { metaColumnNames[i - 1] = rsmd.getColumnName(i); } Map map = new HashMap(); if (rs.next()) { for (int i = 1; i < columnCount; i++) { map.put(metaColumnNames[i - 1], rs.getString(i)); } } return map; } catch (SQLException e) { throw new DataSourceException(e); } finally { releaseResources(rs); } } /** * Retrieves a <code>Vector</code> of <code>ColumnIndexData</code> * objects containing all relevant information on the table indexes * for the specified table.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -