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

📄 jtdsdatabasemetadata.java

📁 jtds的源码 是你学习java的好东西
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        CallableStatement s = connection.prepareCall(syscall(catalog, query));

        s.setString(1, table);
        s.setString(2, schema);
        s.setString(3, catalog);
        s.setString(4, processEscapes(columnNamePattern));

        JtdsResultSet rs = (JtdsResultSet)s.executeQuery();

        rs.setColLabel(1, "TABLE_CAT");
        rs.setColLabel(2, "TABLE_SCHEM");

        upperCaseColumnNames(rs);

        return rs;
    }

    /**
     * Get a description of table columns available in a catalog. <P>
     *
     * Only column descriptions matching the catalog, schema, table and column
     * name criteria are returned. They are ordered by TABLE_SCHEM, TABLE_NAME
     * and ORDINAL_POSITION. <P>
     *
     * Each column 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>DATA_TYPE</B> short =>SQL type from java.sql.Types
     *   <LI> <B>TYPE_NAME</B> String =>Data source dependent type name
     *   <LI> <B>COLUMN_SIZE</B> int =>column size. For char or date types this
     *   is the maximum number of characters, for numeric or decimal types this
     *   is precision.
     *   <LI> <B>BUFFER_LENGTH</B> is not used.
     *   <LI> <B>DECIMAL_DIGITS</B> int =>the number of fractional digits
     *   <LI> <B>NUM_PREC_RADIX</B> int =>Radix (typically either 10 or 2)
     *   <LI> <B>NULLABLE</B> int =>is NULL allowed?
     *   <UL>
     *     <LI> columnNoNulls - might not allow NULL values
     *     <LI> columnNullable - definitely allows NULL values
     *     <LI> columnNullableUnknown - nullability unknown
     *   </UL>
     *
     *   <LI> <B>REMARKS</B> String =>comment describing column (may be null)
     *
     *   <LI> <B>COLUMN_DEF</B> String =>default value (may be null)
     *   <LI> <B>SQL_DATA_TYPE</B> int =>unused
     *   <LI> <B>SQL_DATETIME_SUB</B> int =>unused
     *   <LI> <B>CHAR_OCTET_LENGTH</B> int =>for char types the maximum number
     *   of bytes in the column
     *   <LI> <B>ORDINAL_POSITION</B> int =>index of column in table (starting
     *   at 1)
     *   <LI> <B>IS_NULLABLE</B> String =>"NO" means column definitely does not
     *   allow NULL values; "YES" means the column might allow NULL values. An
     *   empty string means nobody knows.
     * </OL>
     *
     *
     * @param catalog a catalog name; "" retrieves those without a catalog;
     *        <code>null</code> means drop catalog name from the selection criteria
     * @param schemaPattern a schema name pattern; "" retrieves those without a schema
     * @param tableNamePattern a table name pattern
     * @param columnNamePattern a column name pattern
     * @return ResultSet - each row is a column description
     * @throws SQLException if a database-access error occurs.
     *
     * @see #getSearchStringEscape
     */
    public java.sql.ResultSet getColumns(String catalog,
                                         String schemaPattern,
                                         String tableNamePattern,
                                         String columnNamePattern)
    throws SQLException {
        String colNames[] = {"TABLE_CAT",           "TABLE_SCHEM",
                             "TABLE_NAME",          "COLUMN_NAME",
                             "DATA_TYPE",           "TYPE_NAME",
                             "COLUMN_SIZE",         "BUFFER_LENGTH",
                             "DECIMAL_DIGITS",      "NUM_PREC_RADIX",
                             "NULLABLE",            "REMARKS",
                             "COLUMN_DEF",          "SQL_DATA_TYPE",
                             "SQL_DATETIME_SUB",    "CHAR_OCTET_LENGTH",
                             "ORDINAL_POSITION",    "IS_NULLABLE",
                             "SCOPE_CATALOG",       "SCOPE_SCHEMA",
                             "SCOPE_TABLE",         "SOURCE_DATA_TYPE"};

       int colTypes[]     = {Types.VARCHAR,         Types.VARCHAR,
                             Types.VARCHAR,         Types.VARCHAR,
                             Types.INTEGER,         Types.VARCHAR,
                             Types.INTEGER,         Types.INTEGER,
                             Types.INTEGER,         Types.INTEGER,
                             Types.INTEGER,         Types.VARCHAR,
                             Types.VARCHAR,         Types.INTEGER,
                             Types.INTEGER,         Types.INTEGER,
                             Types.INTEGER,         Types.VARCHAR,
                             Types.VARCHAR,         Types.VARCHAR,
                             Types.VARCHAR,         Types.SMALLINT};
        String query = "sp_columns ?, ?, ?, ?, ?";

        CallableStatement s = connection.prepareCall(syscall(catalog, query));

        s.setString(1, processEscapes(tableNamePattern));
        s.setString(2, processEscapes(schemaPattern));
        s.setString(3, catalog);
        s.setString(4, processEscapes(columnNamePattern));
        s.setInt(5, 3); // ODBC version 3

        JtdsResultSet rs = (JtdsResultSet)s.executeQuery();

        CachedResultSet rsTmp = new CachedResultSet((JtdsStatement)s, colNames, colTypes);
        rsTmp.moveToInsertRow();
        int colCnt = rs.getMetaData().getColumnCount();
        //
        // Neither type of server returns exactly the data required by the JDBC3 standard.
        // The result data is copied to a cached result set and modified on the fly.
        //
        while (rs.next()) {
            if (serverType == Driver.SYBASE) {
                // Sybase servers (older versions only return 14 columns)
                for (int i = 1; i <= 4; i++) {
                    rsTmp.updateObject(i, rs.getObject(i));
                }
                rsTmp.updateInt(5, TypeInfo.normalizeDataType(rs.getInt(5), connection.getUseLOBs()));
                String typeName = rs.getString(6);
                rsTmp.updateString(6, typeName);
                for (int i = 8; i <= 12; i++) {
                    rsTmp.updateObject(i, rs.getObject(i));
                }
                if (colCnt >= 20) {
                    // SYBASE 11.92, 12.5
                    for (int i = 13; i <= 18; i++) {
                        rsTmp.updateObject(i, rs.getObject(i + 2));
                    }
                } else {
                    // SYBASE 11.03
                    rsTmp.updateObject(16, rs.getObject(8));
                    rsTmp.updateObject(17, rs.getObject(14));
                }
                if ("image".equals(typeName) || "text".equals(typeName)) {
                    rsTmp.updateInt(7, Integer.MAX_VALUE);
                    rsTmp.updateInt(16, Integer.MAX_VALUE);
                } else
                if ("univarchar".equals(typeName) || "unichar".equals(typeName)) {
                    rsTmp.updateInt(7, rs.getInt(7) / 2);
                    rsTmp.updateObject(16, rs.getObject(7));
                } else {
                    rsTmp.updateInt(7, rs.getInt(7));
                }
            } else {
                // MS SQL Server - Mainly OK but we need to fix some data types.
                for (int i = 1; i <= colCnt; i++) {
                    if (i == 5) {
                        int type = TypeInfo.normalizeDataType(rs.getInt(i), connection.getUseLOBs());
                        rsTmp.updateInt(i, type);
                    } else
                    if (i == 19) {
                        // This is the SS_DATA_TYPE column and contains the TDS
                        // data type constant. We can use this to distinguish
                        // varchar(max) from text on SQL2005.
                        rsTmp.updateString(6, TdsData.getMSTypeName(rs.getString(6), rs.getInt(19)));
                    } else {
                        rsTmp.updateObject(i, rs.getObject(i));
                    }
                }
            }
            rsTmp.insertRow();
        }
        rs.close();
        rsTmp.moveToCurrentRow();
        rsTmp.setConcurrency(ResultSet.CONCUR_READ_ONLY);

        return rsTmp;
    }

    /**
     * Get a description of the foreign key columns in the foreign key table
     * that reference the primary key columns of the primary key table
     * (describe how one table imports another's key). This should normally
     * return a single foreign key/primary key pair (most tables only import a
     * foreign key from a table once.) They are ordered by FKTABLE_CAT,
     * FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ. <P>
     *
     * Each foreign key column description has the following columns:
     * <OL>
     *   <LI> <B>PKTABLE_CAT</B> String =>primary key table catalog (may be
     *   null)
     *   <LI> <B>PKTABLE_SCHEM</B> String =>primary key table schema (may be
     *   null)
     *   <LI> <B>PKTABLE_NAME</B> String =>primary key table name
     *   <LI> <B>PKCOLUMN_NAME</B> String =>primary key column name
     *   <LI> <B>FKTABLE_CAT</B> String =>foreign key table catalog (may be
     *   null) being exported (may be null)
     *   <LI> <B>FKTABLE_SCHEM</B> String =>foreign key table schema (may be
     *   null) being exported (may be null)
     *   <LI> <B>FKTABLE_NAME</B> String =>foreign key table name being
     *   exported
     *   <LI> <B>FKCOLUMN_NAME</B> String =>foreign key column name being
     *   exported
     *   <LI> <B>KEY_SEQ</B> short =>sequence number within foreign key
     *   <LI> <B>UPDATE_RULE</B> short =>What happens to foreign key when
     *   primary is updated:
     *   <UL>
     *     <LI> importedNoAction - do not allow update of primary key if it has
     *     been imported
     *     <LI> importedKeyCascade - change imported key to agree with primary
     *     key update
     *     <LI> importedKeySetNull - change imported key to NULL if its primary
     *     key has been updated
     *     <LI> importedKeySetDefault - change imported key to default values
     *     if its primary key has been updated
     *     <LI> importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x
     *     compatibility)
     *   </UL>
     *
     *   <LI> <B>DELETE_RULE</B> short =>What happens to the foreign key when
     *   primary is deleted.
     *   <UL>
     *     <LI> importedKeyNoAction - do not allow delete of primary key if it
     *     has been imported
     *     <LI> importedKeyCascade - delete rows that import a deleted key
     *     <LI> importedKeySetNull - change imported key to NULL if its primary
     *     key has been deleted
     *     <LI> importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x
     *     compatibility)
     *     <LI> importedKeySetDefault - change imported key to default if its
     *     primary key has been deleted
     *   </UL>
     *
     *   <LI> <B>FK_NAME</B> String =>foreign key name (may be null)
     *   <LI> <B>PK_NAME</B> String =>primary key name (may be null)
     *   <LI> <B>DEFERRABILITY</B> short =>can the evaluation of foreign key
     *   constraints be deferred until commit
     *   <UL>
     *     <LI> importedKeyInitiallyDeferred - see SQL92 for definition
     *     <LI> importedKeyInitiallyImmediate - see SQL92 for definition
     *     <LI> importedKeyNotDeferrable - see SQL92 for definition
     *   </UL>
     *
     * </OL>
     *
     * @param primaryCatalog a catalog name; "" retrieves those without a
     *        <code>null</code> means drop catalog name from the selection criteria
     * @param primarySchema a schema name pattern; "" retrieves those without a schema
     * @param primaryTable the table name that exports the key
     * @param foreignCatalog a catalog name; "" retrieves those without a
     *        <code>null</code> means drop catalog name from the selection criteria
     * @param foreignSchema a schema name pattern; "" retrieves those without a schema
     * @param foreignTable the table name that imports the key
     * @return ResultSet - each row is a foreign key column description
     * @throws SQLException if a database-access error occurs.
     *
     * @see #getImportedKeys
     */
    public java.sql.ResultSet getCrossReference(String primaryCatalog,
                                                String primarySchema,
                                                String primaryTable,
                                                String foreignCatalog,
                                                String foreignSchema,
                                                String foreignTable)
    throws SQLException {
        String colNames[] = {"PKTABLE_CAT",  "PKTABLE_SCHEM",
                             "PKTABLE_NAME", "PKCOLUMN_NAME",
                             "FKTABLE_CAT",  "FKTABLE_SCHEM",
                             "FKTABLE_NAME", "FKCOLUMN_NAME",
                             "KEY_SEQ",      "UPDATE_RULE",
                             "DELETE_RULE",  "FK_NAME",
                             "PK_NAME",      "DEFERRABILITY"};
        int colTypes[]    = {Types.VARCHAR,  Types.VARCHAR,
                             Types.VARCHAR,  Types.VARCHAR,
                             Types.VARCHAR,  Types.VARCHAR,
                             Types.VARCHAR,  Types.VARCHAR,
                             Types.SMALLINT, Types.SMALLINT,
                             Types.SMALLINT, Types.VARCHAR,
                             Types.VARCHAR,  Types.SMALLINT};

        String query = "sp_fkeys ?, ?, ?, ?, ?, ?";

        if (primaryCatalog != null) {
            query = syscall(primaryCatalog, query);
        } else if (foreignCatalog != null) {
            query = syscall(foreignCatalog, query);
        } else {
            query = syscall(null, query);
        }

        CallableStatement s = connection.prepareCall(query);

        s.setString(1, primaryTable);
        s.setString(2, processEscapes(primarySchema));
        s.setString(3, primaryCatalog);
        s.setString(4, foreignTable);
        s.setString(5, processEscapes(foreignSchema));
        s.setString(6, foreignCatalog);

        JtdsResultSet rs = (JtdsResultSet)s.executeQuery();
        int colCnt = rs.getMetaData().getColumnCount();
        CachedResultSet rsTmp = new CachedResultSet((JtdsStatement)s, colNames, colTypes);
        rsTmp.moveToInsertRow();
        while (rs.next()) {
            for (int i = 1; i <= colCnt; i++) {
                rsTmp.updateObject(i, rs.getObject(i));
            }
            if (colCnt < 14) {
                rsTmp.updateShort(14, (short)DatabaseMetaData.importedKeyNotDeferrable);
            }
            rsTmp.insertRow();
        }
        rs.close();

⌨️ 快捷键说明

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