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

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getBestRowIdentifier ( String  catalog,
String  schema,
String  table,
int  scope,
boolean  nullable 
) throws SQLException [inline]

Get a description of a table's optimal set of columns that uniquely identifies a row. They are ordered by SCOPE.

Each column description has the following columns:

  1. SCOPE short => actual scope of result

    • bestRowTemporary - very temporary, while using row
    • bestRowTransaction - valid for remainder of current transaction
    • bestRowSession - valid for remainder of current session

  2. COLUMN_NAME String => column name
  3. DATA_TYPE short => SQL data type from java.sql.Types
  4. TYPE_NAME String => Data source dependent type name
  5. COLUMN_SIZE int => precision
  6. BUFFER_LENGTH int => not used
  7. DECIMAL_DIGITS short => scale
  8. PSEUDO_COLUMN short => is this a pseudo column like an Oracle ROWID

    • bestRowUnknown - may or may not be pseudo column
    • bestRowNotPseudo - is NOT a pseudo column
    • bestRowPseudo - is a pseudo column

Parameters:
catalog a catalog name; "" retrieves those without a catalog
schema a schema name; "" retrieves those without a schema
table a table name
scope the scope of interest; use same values as SCOPE
nullable include columns that are nullable?
Returns:
ResultSet each row is a column description
Exceptions:
SQLException DOCUMENT ME!

Definition at line 227 of file DatabaseMetaData.java.

References com::mysql::jdbc::Statement::close(), com::mysql::jdbc::ResultSet::close(), conn, com::mysql::jdbc::Connection::createStatement(), database, com::mysql::jdbc::Statement::executeQuery(), com::mysql::jdbc::ResultSet::getBytes(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ResultSet::next(), quotedId, s2b(), and com::mysql::jdbc::Statement::setEscapeProcessing().

                            {
        Field[] fields = new Field[8];
        fields[0] = new Field("", "SCOPE", Types.SMALLINT, 5);
        fields[1] = new Field("", "COLUMN_NAME", Types.CHAR, 32);
        fields[2] = new Field("", "DATA_TYPE", Types.SMALLINT, 32);
        fields[3] = new Field("", "TYPE_NAME", Types.CHAR, 32);
        fields[4] = new Field("", "COLUMN_SIZE", Types.INTEGER, 10);
        fields[5] = new Field("", "BUFFER_LENGTH", Types.INTEGER, 10);
        fields[6] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 10);
        fields[7] = new Field("", "PSEUDO_COLUMN", Types.SMALLINT, 5);

        String databasePart = "";

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

        if (table == null) {
            throw new SQLException("Table not specified.",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        ResultSet results = null;
        Statement stmt = null;

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

            StringBuffer queryBuf = new StringBuffer("SHOW COLUMNS FROM ");
            queryBuf.append(this.quotedId);
            queryBuf.append(table);
            queryBuf.append(this.quotedId);
            queryBuf.append(databasePart);

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

            ArrayList tuples = new ArrayList();

            while (results.next()) {
                String keyType = results.getString("Key");

                if (keyType != null) {
                    if (StringUtils.startsWithIgnoreCase(keyType, "PRI")) {
                        byte[][] rowVal = new byte[8][];
                        rowVal[0] = Integer.toString(java.sql.DatabaseMetaData.bestRowSession)
                                           .getBytes();
                        rowVal[1] = results.getBytes("Field");

                        String type = results.getString("Type");
                        int size = MysqlIO.getMaxBuf();
                        int decimals = 0;

                        /*
                         * Parse the Type column from MySQL
                         */
                        if (type.indexOf("enum") != -1) {
                            String temp = type.substring(type.indexOf("("),
                                    type.indexOf(")"));
                            java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(temp,
                                    ",");
                            int maxLength = 0;

                            while (tokenizer.hasMoreTokens()) {
                                maxLength = Math.max(maxLength,
                                        (tokenizer.nextToken().length() - 2));
                            }

                            size = maxLength;
                            decimals = 0;
                            type = "enum";
                        } else if (type.indexOf("(") != -1) {
                            if (type.indexOf(",") != -1) {
                                size = Integer.parseInt(type.substring(type.indexOf(
                                                "(") + 1, type.indexOf(",")));
                                decimals = Integer.parseInt(type.substring(type.indexOf(
                                                ",") + 1, type.indexOf(")")));
                            } else {
                                size = Integer.parseInt(type.substring(type.indexOf(
                                                "(") + 1, type.indexOf(")")));
                            }

                            type = type.substring(type.indexOf("("));
                        }

                        rowVal[2] = new byte[0]; // FIXME!
                        rowVal[3] = s2b(type);
                        rowVal[4] = Integer.toString(size + decimals).getBytes();
                        rowVal[5] = Integer.toString(size + decimals).getBytes();
                        rowVal[6] = Integer.toString(decimals).getBytes();
                        rowVal[7] = Integer.toString(java.sql.DatabaseMetaData.bestRowNotPseudo)
                                           .getBytes();
                        tuples.add(rowVal);
                    }
                }
            }

            return buildResultSet(fields, tuples);
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception ex) {
                    ;
                }

                results = null;
            }

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

                stmt = null;
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index