Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getColumns ( String  catalog,
String  schemaPattern,
String  tableName,
String  columnNamePattern 
) throws SQLException [inline]

Get a description of table columns available in a catalog.

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.

Each column description has the following columns:

  1. TABLE_CAT String => table catalog (may be null)
  2. TABLE_SCHEM String => table schema (may be null)
  3. TABLE_NAME String => table name
  4. COLUMN_NAME String => column name
  5. DATA_TYPE short => SQL type from java.sql.Types
  6. TYPE_NAME String => Data source dependent type name
  7. COLUMN_SIZE int => column size. For char or date types this is the maximum number of characters, for numeric or decimal types this is precision.
  8. BUFFER_LENGTH is not used.
  9. DECIMAL_DIGITS int => the number of fractional digits
  10. NUM_PREC_RADIX int => Radix (typically either 10 or 2)
  11. NULLABLE int => is NULL allowed?

    • columnNoNulls - might not allow NULL values
    • columnNullable - definitely allows NULL values
    • columnNullableUnknown - nullability unknown

  12. REMARKS String => comment describing column (may be null)
  13. COLUMN_DEF String => default value (may be null)
  14. SQL_DATA_TYPE int => unused
  15. SQL_DATETIME_SUB int => unused
  16. CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column
  17. ORDINAL_POSITION int => index of column in table (starting at 1)
  18. IS_NULLABLE String => "NO" means column definitely does not allow NULL values; "YES" means the column might allow NULL values. An empty string means nobody knows.

Parameters:
catalog a catalog name; "" retrieves those without a catalog
schemaPattern a schema name pattern; "" retrieves those without a schema
tableName a table name pattern
columnNamePattern a column name pattern
Returns:
ResultSet each row is a column description
Exceptions:
SQLException if a database access error occurs
See also:
getSearchStringEscape

Definition at line 720 of file DatabaseMetaData.java.

References com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::bufferLength, com::mysql::jdbc::Statement::close(), com::mysql::jdbc::ResultSet::close(), com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::columnSize, conn, com::mysql::jdbc::Connection::createStatement(), database, com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::dataType, com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::decimalDigits, com::mysql::jdbc::Statement::executeQuery(), com::mysql::jdbc::ResultSet::getBytes(), com::mysql::jdbc::ResultSet::getString(), getTables(), com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::isNullable, com::mysql::jdbc::ResultSet::next(), com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::nullability, com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::numPrecRadix, quotedId, s2b(), com::mysql::jdbc::Statement::setEscapeProcessing(), com::mysql::jdbc::DatabaseMetaData::TypeDescriptor::typeName, and com::mysql::jdbc::Connection::versionMeetsMinimum().

Referenced by getTablePrivileges().

                                                                        {
        String databasePart = "";

        if (columnNamePattern == null) {
            columnNamePattern = "%";
        }

        if (catalog != null) {
            if (!catalog.equals("")) {
                databasePart = " FROM " + this.quotedId + catalog +
                    this.quotedId;
            }
        } else {
            databasePart = " FROM " + this.quotedId + this.database +
                this.quotedId;
        }

        ArrayList tableNameList = new ArrayList();
        int tablenameLength = 0;

        if (tableName == null) {
            // Select from all tables
            java.sql.ResultSet tables = null;

            try {
                tables = getTables(catalog, schemaPattern, "%", new String[0]);

                while (tables.next()) {
                    String tableNameFromList = tables.getString("TABLE_NAME");
                    tableNameList.add(tableNameFromList);

                    if (tableNameFromList.length() > tablenameLength) {
                        tablenameLength = tableNameFromList.length();
                    }
                }
            } finally {
                if (tables != null) {
                    try {
                        tables.close();
                    } catch (Exception sqlEx) {
                        AssertionFailedException.shouldNotHappen(sqlEx);
                    }

                    tables = null;
                }
            }
        } else {
            java.sql.ResultSet tables = null;

            try {
                tables = getTables(catalog, schemaPattern, tableName,
                        new String[0]);

                while (tables.next()) {
                    String tableNameFromList = tables.getString("TABLE_NAME");
                    tableNameList.add(tableNameFromList);

                    if (tableNameFromList.length() > tablenameLength) {
                        tablenameLength = tableNameFromList.length();
                    }
                }
            } finally {
                if (tables != null) {
                    try {
                        tables.close();
                    } catch (SQLException sqlEx) {
                        AssertionFailedException.shouldNotHappen(sqlEx);
                    }

                    tables = null;
                }
            }
        }

        int catalogLength = 0;

        if (catalog != null) {
            catalogLength = catalog.length();
        } else {
            catalog = "";
            catalogLength = 0;
        }

        java.util.Iterator tableNames = tableNameList.iterator();
        Field[] fields = new Field[18];
        fields[0] = new Field("", "TABLE_CAT", Types.CHAR, catalogLength);
        fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0);
        fields[2] = new Field("", "TABLE_NAME", Types.CHAR, tablenameLength);
        fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 32);
        fields[4] = new Field("", "DATA_TYPE", Types.SMALLINT, 5);
        fields[5] = new Field("", "TYPE_NAME", Types.CHAR, 16);
        fields[6] = new Field("", "COLUMN_SIZE", Types.INTEGER,
                Integer.toString(Integer.MAX_VALUE).length());
        fields[7] = new Field("", "BUFFER_LENGTH", Types.INTEGER, 10);
        fields[8] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 10);
        fields[9] = new Field("", "NUM_PREC_RADIX", Types.INTEGER, 10);
        fields[10] = new Field("", "NULLABLE", Types.INTEGER, 10);
        fields[11] = new Field("", "REMARKS", Types.CHAR, 0);
        fields[12] = new Field("", "COLUMN_DEF", Types.CHAR, 0);
        fields[13] = new Field("", "SQL_DATA_TYPE", Types.INTEGER, 10);
        fields[14] = new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 10);
        fields[15] = new Field("", "CHAR_OCTET_LENGTH", Types.INTEGER,
                Integer.toString(Integer.MAX_VALUE).length());
        fields[16] = new Field("", "ORDINAL_POSITION", Types.INTEGER, 10);
        fields[17] = new Field("", "IS_NULLABLE", Types.CHAR, 3);

        ArrayList tuples = new ArrayList();

        while (tableNames.hasNext()) {
            String tableNamePattern = (String) tableNames.next();
            Statement stmt = null;
            ResultSet results = null;

            try {
                stmt = this.conn.createStatement();
                stmt.setEscapeProcessing(false);

                StringBuffer queryBuf = new StringBuffer("SHOW ");

                if (this.conn.versionMeetsMinimum(4, 1, 0)) {
                    queryBuf.append("FULL ");
                }

                queryBuf.append("COLUMNS FROM ");
                queryBuf.append(this.quotedId);
                queryBuf.append(tableNamePattern);
                queryBuf.append(this.quotedId);
                queryBuf.append(databasePart);
                queryBuf.append(" LIKE '");
                queryBuf.append(columnNamePattern);
                queryBuf.append("'");

                // Return correct ordinals if column name pattern is
                // not '%'
                // Currently, MySQL doesn't show enough data to do
                // this, so we do it the 'hard' way...Once _SYSTEM
                // tables are in, this should be much easier
                boolean fixUpOrdinalsRequired = false;
                Map ordinalFixUpMap = null;

                if (!columnNamePattern.equals("%")) {
                    fixUpOrdinalsRequired = true;

                    StringBuffer fullColumnQueryBuf = new StringBuffer("SHOW ");

                    if (this.conn.versionMeetsMinimum(4, 1, 0)) {
                        fullColumnQueryBuf.append("FULL ");
                    }

                    fullColumnQueryBuf.append("COLUMNS FROM ");
                    fullColumnQueryBuf.append(this.quotedId);
                    fullColumnQueryBuf.append(tableNamePattern);
                    fullColumnQueryBuf.append(this.quotedId);
                    fullColumnQueryBuf.append(databasePart);

                    results = stmt.executeQuery(fullColumnQueryBuf.toString());

                    ordinalFixUpMap = new HashMap();

                    int fullOrdinalPos = 1;

                    while (results.next()) {
                        String fullOrdColName = results.getString("Field");

                        ordinalFixUpMap.put(fullOrdColName,
                            new Integer(fullOrdinalPos++));
                    }
                }

                results = stmt.executeQuery(queryBuf.toString());

                int ordPos = 1;

                while (results.next()) {
                    byte[][] rowVal = new byte[18][];
                    rowVal[0] = s2b(catalog); // TABLE_CAT
                    rowVal[1] = null; // TABLE_SCHEM (No schemas in MySQL)

                    rowVal[2] = s2b(tableNamePattern); // TABLE_NAME
                    rowVal[3] = results.getBytes("Field");

                    TypeDescriptor typeDesc = new TypeDescriptor(results.getString(
                                "Type"), results.getString("Null"));

                    rowVal[4] = Short.toString(typeDesc.dataType).getBytes();

                    // DATA_TYPE (jdbc)
                    rowVal[5] = s2b(typeDesc.typeName); // TYPE_NAME (native)
                    rowVal[6] = s2b(Integer.toString(typeDesc.columnSize));
                    rowVal[7] = s2b(Integer.toString(typeDesc.bufferLength));
                    rowVal[8] = s2b(Integer.toString(typeDesc.decimalDigits));
                    rowVal[9] = s2b(Integer.toString(typeDesc.numPrecRadix));
                    rowVal[10] = s2b(Integer.toString(typeDesc.nullability));

                    //
                    // Doesn't always have this field, depending on version
                    //
                    //
                    // REMARK column
                    //
                    try {
                        if (this.conn.versionMeetsMinimum(4, 1, 0)) {
                            rowVal[11] = results.getBytes("Comment");
                        } else {
                            rowVal[11] = results.getBytes("Extra");
                        }
                    } catch (Exception E) {
                        rowVal[11] = new byte[0];
                    }

                    // COLUMN_DEF
                    rowVal[12] = results.getBytes("Default");

                    rowVal[13] = new byte[] { (byte) '0' }; // SQL_DATA_TYPE
                    rowVal[14] = new byte[] { (byte) '0' }; // SQL_DATE_TIME_SUB
                    rowVal[15] = rowVal[6]; // CHAR_OCTET_LENGTH

                    // ORDINAL_POSITION
                    if (!fixUpOrdinalsRequired) {
                        rowVal[16] = Integer.toString(ordPos++).getBytes();
                    } else {
                        String origColName = results.getString("Field");
                        Integer realOrdinal = (Integer) ordinalFixUpMap.get(origColName);

                        if (realOrdinal != null) {
                            rowVal[16] = realOrdinal.toString().getBytes();
                        } else {
                            throw new SQLException("Can not find column in full column list to determine true ordinal position.",
                                SQLError.SQL_STATE_GENERAL_ERROR);
                        }
                    }

                    rowVal[17] = s2b(typeDesc.isNullable);

                    tuples.add(rowVal);
                }
            } finally {
                if (results != null) {
                    try {
                        results.close();
                    } catch (Exception ex) {
                        ;
                    }

                    results = null;
                }

                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception ex) {
                        ;
                    }

                    stmt = null;
                }
            }
        }

        java.sql.ResultSet results = buildResultSet(fields, tuples);

        return results;
    }


Generated by  Doxygen 1.6.0   Back to index