⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databasemetadata.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
//
// Copyright 1998 CDS Networks, Inc., Medford Oregon
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
//    must display the following acknowledgement:
//      This product includes software developed by CDS Networks, Inc.
// 4. The name of CDS Networks, Inc.  may not be used to endorse or promote
//    products derived from this software without specific prior
//    written permission.
//
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//

package net.sourceforge.jtds.jdbc;

import java.sql.*;

/**
 * @author   Craig Spannring
 * @author   The FreeTDS project
 * @author   Alin Sinpalean
 * @created  17 March 2001
 * @version  $Id: DatabaseMetaData.java,v 1.4 2002/10/26 12:20:47 alin_sinpalean Exp $
 */
public class DatabaseMetaData implements java.sql.DatabaseMetaData
{
    /**
     * CVS version of the file.
     */
    public final static String cvsVersion = "$Id: DatabaseMetaData.java,v 1.4 2002/10/26 12:20:47 alin_sinpalean Exp $";

    // internal data needed by this implemention.
    Tds tds;
    TdsConnection connection;

    /**
     * Length of a sysname object (table name, catalog name etc.) -- 128 for
     * TDS 7.0, 30 for earlier versions.
     */
    int sysnameLength = 30;

    /**
     * <code>Boolean.TRUE</code> if identifiers are case sensitive (the server
     * was installed that way). Initially <code>null</code>, set the first time
     * any of the methods that check this are called.
     */
    Boolean caseSensitive = null;

    public DatabaseMetaData(TdsConnection connection_, Tds tds_) throws SQLException
    {
        connection = connection_;
        tds = tds_;
    }

    public static DatabaseMetaData getInstance(TdsConnection connection_, Tds tds_)
        throws SQLException
    {
        // Return a Microsoft7MetaData only if we're using TDS 7.0 or later.
        // (The limitations of the TDS version not the server version apply.)
        if( tds_.getTdsVer() >= Tds.TDS70 )
            return new net.sourceforge.jtds.jdbc.Microsoft7MetaData(connection_, tds_);

        return new net.sourceforge.jtds.jdbc.DatabaseMetaData(connection_, tds_);
    }

   //----------------------------------------------------------------------
   // First, a variety of minor information about the target database.

    /**
     * Can all the procedures returned by getProcedures be called by the
     * current user?
     *
     * @return true if so
     * @exception SQLException if a database-access error occurs.
     */
    public boolean allProceduresAreCallable() throws SQLException
    {
        // XXX Need to check for Sybase
        return true; // per "Programming ODBC for SQLServer" Appendix A
    }

    /**
     * Can all the tables returned by getTable be SELECTed by the
     * current user?
     *
     * @return true if so
     * @exception SQLException if a database-access error occurs.
     */
    public boolean allTablesAreSelectable() throws SQLException
    {
        // XXX Need to check for Sybase
        return true;
    }

    /**
     * Does a data definition statement within a transaction force the
     * transaction to commit?
     *
     * @return true if so
     * @exception SQLException if a database-access error occurs.
     */
    public boolean dataDefinitionCausesTransactionCommit() throws SQLException
    {
        // XXX needs to be checked for Sybase
        return false;
    }

    /**
     * Is a data definition statement within a transaction ignored?
     *
     * @return true if so
     * @exception SQLException if a database-access error occurs.
     */
    public boolean dataDefinitionIgnoredInTransactions() throws SQLException
    {
        // XXX needs to be checked for Sybase
        return false;
    }

    /**
     * Did getMaxRowSize() include LONGVARCHAR and LONGVARBINARY blobs?
     *
     * @return true if so
     * @exception SQLException if a database-access error occurs.
     */
    public boolean doesMaxRowSizeIncludeBlobs() throws SQLException
    {
        return false;
    }

    /**
     * Get a description of a table's optimal set of columns that
     * uniquely identifies a row. They are ordered by SCOPE.
     *
     * <P>Each column description has the following columns:
     *  <OL>
     *    <LI> <B>SCOPE</B> short =>actual scope of result
     *    <UL>
     *      <LI> bestRowTemporary - very temporary, while using row
     *      <LI> bestRowTransaction - valid for remainder of current transaction
     *
     *      <LI> bestRowSession - valid for remainder of current session
     *    </UL>
     *
     *    <LI> <B>COLUMN_NAME</B> String =>column name
     *    <LI> <B>DATA_TYPE</B> short =>SQL data type from java.sql.Types
     *    <LI> <B>TYPE_NAME</B> String =>Data source dependent type name
     *    <LI> <B>COLUMN_SIZE</B> int =>precision
     *    <LI> <B>BUFFER_LENGTH</B> int =>not used
     *    <LI> <B>DECIMAL_DIGITS</B> short =>scale
     *    <LI> <B>PSEUDO_COLUMN</B> short =>is this a pseudo column like an
     *    Oracle ROWID
     *    <UL>
     *      <LI> bestRowUnknown - may or may not be pseudo column
     *      <LI> bestRowNotPseudo - is NOT a pseudo column
     *      <LI> bestRowPseudo - is a pseudo column
     *    </UL>
     *
     *  </OL>
     *
     *
     *@param  catalog           a catalog name; "" retrieves those without a
     *      catalog; null means drop catalog name from the selection criteria
     *@param  schema            a schema name; "" retrieves those without a
     *      schema
     *@param  table             a table name
     *@param  scope             the scope of interest; use same values as SCOPE
     *@param  nullable          include columns that are nullable?
     *@return                   ResultSet - each row is a column description
     *@exception  SQLException  if a database-access error occurs.
     */
    public java.sql.ResultSet getBestRowIdentifier(
            String catalog,
            String schema,
            String table,
            int scope,
            boolean nullable )
             throws SQLException
    {
        String query = "exec sp_special_columns ?, ?, ?, ?, ?, ?";
        if( catalog != null )
            query = "exec "+catalog+"..sp_special_columns ?, ?, ?, ?, ?, ?";

        CallableStatement s = connection.prepareCall(query);
        s.setString(1, table);
        s.setString(2, schema);
        s.setString(3, catalog);
        s.setString(4, "R");
        s.setString(5, "T");
        s.setString(6, "U");

        TdsResultSet rs = (TdsResultSet)s.executeQuery();
        Columns col = rs.getContext().getColumnInfo();

        col.setName(5, "COLUMN_SIZE");
        col.setLabel(5, "COLUMN_SIZE");
        col.setName(6, "BUFFER_LENGTH");
        col.setLabel(6, "BUFFER_LENGTH");
        col.setName(7, "DECIMAL_DIGITS");
        col.setLabel(7, "DECIMAL_DIGITS");

        return rs;
    }

    /**
     *  Get the catalog names available in this database. The results are
     *  ordered by catalog name. <P>
     *
     *  The catalog column is:
     *  <OL>
     *    <LI> <B>TABLE_CAT</B> String =>catalog name
     *  </OL>
     *
     *
     *@return                   ResultSet - each row has a single String column
     *      that is a catalog name
     *@exception  SQLException  if a database-access error occurs.
     */
    public java.sql.ResultSet getCatalogs()
             throws SQLException
    {
        String query = "exec sp_tables '', '', '%', NULL";
        Statement s = connection.createStatement();
        TdsResultSet rs = (TdsResultSet)s.executeQuery(query);
        Columns col = rs.getContext().getColumnInfo();

        col.setFakeColumnCount(1);
        col.setName(1, "TABLE_CAT");
        col.setLabel(1, "TABLE_CAT");

        return rs;
    }

    /**
     *  What's the separator between catalog and table name?
     *
     *@return                   the separator string
     *@exception  SQLException  if a database-access error occurs.
     */
    public String getCatalogSeparator() throws SQLException
    {
        return ".";
    }

    /**
     *  What's the database vendor's preferred term for "catalog"?
     *
     *@return                   the vendor term
     *@exception  SQLException  if a database-access error occurs.
     */
    public String getCatalogTerm() throws SQLException
    {
        return "database";
    }

    /**
     *  Get a description of the access rights for a table's columns. <P>
     *
     *  Only privileges matching the column name criteria are returned. They are
     *  ordered by COLUMN_NAME and PRIVILEGE. <P>
     *
     *  Each privilige description has the following columns:
     *  <OL>
     *    <LI> <B>TABLE_CAT</B> String =>table catalog (may be null)
     *    <LI> <B>TABLE_SCHEM</B> String =>table schema (may be null)
     *    <LI> <B>TABLE_NAME</B> String =>table name
     *    <LI> <B>COLUMN_NAME</B> String =>column name
     *    <LI> <B>GRANTOR</B> =>grantor of access (may be null)
     *    <LI> <B>GRANTEE</B> String =>grantee of access
     *    <LI> <B>PRIVILEGE</B> String =>name of access (SELECT, INSERT, UPDATE,
     *    REFRENCES, ...)
     *    <LI> <B>IS_GRANTABLE</B> String =>"YES" if grantee is permitted to
     *    grant to others; "NO" if not; null if unknown
     *  </OL>
     *
     *
     *@param  catalog            a catalog name; "" retrieves those without a
     *      catalog; null means drop catalog name from the selection criteria
     *@param  schema             a schema name; "" retrieves those without a
     *      schema
     *@param  table              a table name
     *@param  columnNamePattern  a column name pattern
     *@return                    ResultSet - each row is a column privilege
     *      description
     *@exception  SQLException   if a database-access error occurs.
     *@see                       #getSearchStringEscape
     */
    public java.sql.ResultSet getColumnPrivileges( String catalog, String schema,
            String table, String columnNamePattern )
             throws SQLException
    {
        String query = "exec sp_column_privileges ?, ?, ?, ?";
        if( catalog != null )
            query = "exec "+catalog+"..sp_column_privileges ?, ?, ?, ?";

        CallableStatement s = connection.prepareCall(query);
        s.setString(1, table);
        s.setString(2, schema);
        s.setString(3, catalog);
        s.setString(4, columnNamePattern);

        TdsResultSet rs = (TdsResultSet)s.executeQuery();
        Columns col = rs.getContext().getColumnInfo();

        col.setName(1, "TABLE_CAT");
        col.setLabel(1, "TABLE_CAT");
        col.setName(2, "TABLE_SCHEM");
        col.setLabel(2, "TABLE_SCHEM");

        return rs;
    }

    /**
     *  Get a description of table columns available in a catalog. <P>
     *
     *  Only column descriptions matching the catalog, schema, table and column

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -