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

ResultSet com::mysql::jdbc::DatabaseMetaData::extractForeignKeyFromCreateTable ( java.sql.Connection  connToUse,
java.sql.DatabaseMetaData  metadata,
String  catalog,
String  tableName 
) throws SQLException [inline]

Creates a result set similar enough to 'SHOW TABLE STATUS' to allow the same code to work on extracting the foreign key data

Parameters:
connToUse the database connection to use
metadata the DatabaseMetaData instance calling this method
catalog the database name to extract foreign key info for
tableName the table to extract foreign key info for
Returns:
A result set that has the structure of 'show table status'
Exceptions:
SQLException if a database access error occurs.

Definition at line 5321 of file DatabaseMetaData.java.

References com::mysql::jdbc::Statement::close(), com::mysql::jdbc::Statement::executeQuery(), extractForeignKeyForTable(), getIdentifierQuoteString(), and com::mysql::jdbc::Statement::setEscapeProcessing().

Referenced by getCrossReference(), getExportedKeys(), and getImportedKeys().

                                                              {
        ArrayList tableList = new ArrayList();
        java.sql.ResultSet rs = null;
        java.sql.Statement stmt = null;

        if (tableName != null) {
            tableList.add(tableName);
        } else {
            try {
                rs = metadata.getTables(catalog, "", "%",
                        new String[] { "TABLE" });

                while (rs.next()) {
                    tableList.add(rs.getString("TABLE_NAME"));
                }
            } finally {
                if (rs != null) {
                    rs.close();
                }

                rs = null;
            }
        }

        ArrayList rows = new ArrayList();
        Field[] fields = new Field[3];
        fields[0] = new Field("", "Name", Types.CHAR, Integer.MAX_VALUE);
        fields[1] = new Field("", "Type", Types.CHAR, 255);
        fields[2] = new Field("", "Comment", Types.CHAR, Integer.MAX_VALUE);

        int numTables = tableList.size();
        stmt = connToUse.createStatement();
        stmt.setEscapeProcessing(false);

        String quoteChar = getIdentifierQuoteString();
        
        if (quoteChar == null) {
            quoteChar = "`";
        }
        
        try {
            for (int i = 0; i < numTables; i++) {
                String tableToExtract = (String) tableList.get(i);

                String query = new StringBuffer("SHOW CREATE TABLE ").append(
                        quoteChar).append(catalog).append(quoteChar).append(".").append(quoteChar).append(tableToExtract)
                                                                     .append(quoteChar)
                                                                     .toString();
                rs = stmt.executeQuery(query);

                while (rs.next()) {
                    extractForeignKeyForTable(rows, rs, catalog);
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            rs = null;

            if (stmt != null) {
                stmt.close();
            }

            stmt = null;
        }

        return buildResultSet(fields, rows);
    }


Generated by  Doxygen 1.6.0   Back to index