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

List com::mysql::jdbc::DatabaseMetaData::extractForeignKeyForTable ( ArrayList  rows,
java.sql.ResultSet  rs,
String  catalog 
) throws SQLException [inline]

Extracts foreign key info for one table.

Parameters:
rows the list of rows to add to
rs the result set from 'SHOW CREATE TABLE'
catalog the database name
Returns:
the list of rows with new rows added
Exceptions:
SQLException if a database access error occurs

Definition at line 5177 of file DatabaseMetaData.java.

References getIdentifierQuoteString(), s2b(), and SUPPORTS_FK.

Referenced by extractForeignKeyFromCreateTable().

                                                                   {
        byte[][] row = new byte[3][];
        row[0] = rs.getBytes(1);
        row[1] = s2b(SUPPORTS_FK);

        String createTableString = rs.getString(2);
        StringTokenizer lineTokenizer = new StringTokenizer(createTableString,
                "\n");
        StringBuffer commentBuf = new StringBuffer("comment; ");
        boolean firstTime = true;

        String quoteChar = getIdentifierQuoteString();
        
        if (quoteChar == null) {
            quoteChar = "`";
        }
        
        while (lineTokenizer.hasMoreTokens()) {
            String line = lineTokenizer.nextToken().trim();

            String constraintName = null;

            if (StringUtils.startsWithIgnoreCase(line, "CONSTRAINT")) {
                boolean usingBackTicks = true;
                int beginPos = line.indexOf(quoteChar);

                if (beginPos == -1) {
                    beginPos = line.indexOf("\"");
                    usingBackTicks = false;
                }

                if (beginPos != -1) {
                    int endPos = -1;

                    if (usingBackTicks) {
                        endPos = line.indexOf(quoteChar, beginPos + 1);
                    } else {
                        endPos = line.indexOf("\"", beginPos + 1);
                    }

                    if (endPos != -1) {
                        constraintName = line.substring(beginPos + 1, endPos);
                        line = line.substring(endPos + 1, line.length()).trim();
                    }
                }
            }

            if (line.startsWith("FOREIGN KEY")) {
                if (line.endsWith(",")) {
                    line = line.substring(0, line.length() - 1);
                }

                // Remove all back-ticks
                int lineLength = line.length();
                StringBuffer lineBuf = new StringBuffer(lineLength);

                for (int i = 0; i < lineLength; i++) {
                    char c = line.charAt(i);

                    if (c != quoteChar.charAt(0)) {
                        lineBuf.append(c);
                    }
                }

                line = lineBuf.toString();

                StringTokenizer keyTokens = new StringTokenizer(line, "()",
                        false);
                keyTokens.nextToken(); // eat 'FOREIGN KEY'

                String localColumnNamesString = keyTokens.nextToken();
                String referCatalogTableString = keyTokens.nextToken();
                StringTokenizer referSchemaTable = new StringTokenizer(referCatalogTableString.trim(),
                        " .");
                String referColumnNamesString = keyTokens.nextToken();
                referSchemaTable.nextToken(); //discard the REFERENCES token

                int numTokensLeft = referSchemaTable.countTokens();

                String referCatalog = null;
                String referTable = null;

                if (numTokensLeft == 2) {
                    // some versions of MySQL don't report the database name
                    referCatalog = referSchemaTable.nextToken();
                    referTable = referSchemaTable.nextToken();
                } else {
                    referTable = referSchemaTable.nextToken();
                    referCatalog = catalog;
                }

                if (!firstTime) {
                    commentBuf.append("; ");
                } else {
                    firstTime = false;
                }

                if (constraintName != null) {
                    commentBuf.append(constraintName);
                } else {
                    commentBuf.append("not_available");
                }

                commentBuf.append("(");
                commentBuf.append(localColumnNamesString);
                commentBuf.append(") REFER ");
                commentBuf.append(referCatalog);
                commentBuf.append("/");
                commentBuf.append(referTable);
                commentBuf.append("(");
                commentBuf.append(referColumnNamesString);
                commentBuf.append(")");

                int lastParenIndex = line.lastIndexOf(")");

                if (lastParenIndex != (line.length() - 1)) {
                    String cascadeOptions = cascadeOptions = line.substring(lastParenIndex +
                                1);
                    commentBuf.append(" ");
                    commentBuf.append(cascadeOptions);
                }
            }
        }

        row[2] = s2b(commentBuf.toString());
        rows.add(row);

        return rows;
    }


Generated by  Doxygen 1.6.0   Back to index