/** * Create a ForeignKeyRecord instance from the specified index record */ public ForeignKey createForeignKeyRecord(final char[] record) { final List<String> tokens = getStrings(record, IndexConstants.RECORD_STRING.RECORD_DELIMITER); final ForeignKey fkRecord = new ForeignKey(); // Extract the index version information from the record int indexVersion = getIndexVersion(record); // The tokens are the standard header values int tokenIndex = 0; setRecordHeaderValues(fkRecord, tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++)); // The next token are the UUIDs for the column references List<String> uuids = getStrings(tokens.get(tokenIndex++), getListDelimiter(indexVersion)); fkRecord.setColumns(createColumns(uuids)); // The next token is the UUID of the unique key fkRecord.setUniqueKeyID(getObjectValue(tokens.get(tokenIndex++))); // The next tokens are footer values setRecordFooterValues(fkRecord, tokens, tokenIndex); return fkRecord; }
/** * Create a ForeignKeyRecord instance from the specified index record */ public ForeignKey createForeignKeyRecord(final char[] record) { final List<String> tokens = getStrings(record, IndexConstants.RECORD_STRING.RECORD_DELIMITER); final ForeignKey fkRecord = new ForeignKey(); // Extract the index version information from the record int indexVersion = getIndexVersion(record); // The tokens are the standard header values int tokenIndex = 0; setRecordHeaderValues(fkRecord, tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++), tokens.get(tokenIndex++)); // The next token are the UUIDs for the column references List<String> uuids = getStrings(tokens.get(tokenIndex++), getListDelimiter(indexVersion)); fkRecord.setColumns(createColumns(uuids)); // The next token is the UUID of the unique key fkRecord.setUniqueKeyID(getObjectValue(tokens.get(tokenIndex++))); // The next tokens are footer values setRecordFooterValues(fkRecord, tokens, tokenIndex); return fkRecord; }
/** * Adds a foreign key to the given table. The referenced key may be automatically created if addUniqueConstraint is true. The column names should be in key order. * if reference table is is another schema, they will be resolved during validation. * @param name * @param columnNames * @param referencedColumnNames, may be null to indicate that the primary key should be used. * @param referenceTable - schema qualified reference table name, can be from another schema * @param table * @param addUniqueConstraint - if true, if the referenced table columns do not match with either PK, or FK then a UNIQUE index on reference table is created. * @return * @throws MetadataException */ public ForeignKey addForeignKey(String name, List<String> columnNames, List<String> referencedColumnNames, String referenceTable, Table table) { ForeignKey foreignKey = new ForeignKey(); foreignKey.setParent(table); foreignKey.setColumns(new ArrayList<Column>(columnNames.size())); assignColumns(columnNames, table, foreignKey); foreignKey.setReferenceTableName(referenceTable); foreignKey.setReferenceColumns(referencedColumnNames); foreignKey.setName(name); setUUID(foreignKey); table.getForeignKeys().add(foreignKey); return foreignKey; }
reorderedCols.put(keyIndex, fk.getColumns().get(i)); fk.setColumns(new ArrayList<Column>(reorderedCols.values()));
reorderedCols.put(keyIndex, fk.getColumns().get(i)); fk.setColumns(new ArrayList<Column>(reorderedCols.values()));