public static Stream<? extends Document> traverseOver(ForeignKey foreignKey) { return Stream.concat(foreignKey.foreignKeyColumns(), foreignKey.foreignKeyColumns().flatMap(DocumentDbUtil::traverseOver)); }
ImmutableForeignKey(ImmutableTable parent, Map<String, Object> data) { super(parent, data); final ForeignKey prototype = new ForeignKeyImpl(parent, data); this.id = prototype.getId(); this.name = prototype.getName(); this.enabled = prototype.isEnabled(); this.foreignKeyColumns = unmodifiableList(super.children(FOREIGN_KEY_COLUMNS, ImmutableForeignKeyColumn::new).collect(toList())); }
/** * Returns {@code true} if the two specified documents represents the same * element in the database. Two documents are considered same if they have * the same name and type and their parents are considered same. * * @param first the first document * @param second the second document * @return {@code true} if same, else {@code false} */ public static boolean isSame(ForeignKey first, ForeignKey second) { if (first.getId().equals(second.getId())) { final Table firstParent = first.getParentOrThrow(); final Table secondParent = second.getParentOrThrow(); return isSame(firstParent, secondParent); } else { return false; } }
if (rs.getInt("KEY_SEQ") == 1) { final String uniqueName = UUID.randomUUID().toString(); foreignKey.mutator().setId(uniqueName); foreignKey.mutator().setName(uniqueName); fksThatNeedNewNames.add(uniqueName); foreignKey.mutator().setId(foreignKeyName); foreignKey.mutator().setName(foreignKeyName); final ForeignKeyColumn foreignKeyColumn = foreignKey.mutator().addNewForeignKeyColumn(); final ForeignKeyColumnMutator<?> fkcMutator = foreignKeyColumn.mutator(); final String fkColumnName = rs.getString("FKCOLUMN_NAME"); .filter(fk -> fksThatNeedNewNames.contains(fk.getId())) .forEach(fk -> { final Set<String> thisSet = fk.foreignKeyColumns() .map(ForeignKeyColumn::getId) .collect(toSet()); fk.mutator().setId(found.get().getId()); fk.mutator().setName(found.get().getName()); } else { final String randName = md5(thisSet.toString()); fk.mutator().setId(randName); fk.mutator().setName(randName); LOGGER.error(format( "Found a foreign key in table '%s' with no name. " +
private IllegalStateException noEnabledForeignKeyException() { return new IllegalStateException( "FK " + fk.getId() + " does not have an enabled ForeignKeyColumn" ); }
foreignKey.mutator().setId(foreignKeyName); foreignKey.mutator().setName(foreignKeyName); final ForeignKeyColumn foreignKeyColumn = foreignKey.mutator().addNewForeignKeyColumn(); final ForeignKeyColumnMutator<?> fkcMutator = foreignKeyColumn.mutator(); final String fkColumnName = rs.getString("FKCOLUMN_NAME");
if (rs.getInt("KEY_SEQ") == 1) { final String uniqueName = UUID.randomUUID().toString(); foreignKey.mutator().setId(uniqueName); foreignKey.mutator().setName(uniqueName); fksThatNeedNewNames.add(uniqueName); foreignKey.mutator().setId(foreignKeyName); foreignKey.mutator().setName(foreignKeyName); final ForeignKeyColumn foreignKeyColumn = foreignKey.mutator().addNewForeignKeyColumn(); final ForeignKeyColumnMutator<?> fkcMutator = foreignKeyColumn.mutator(); final String fkColumnName = rs.getString("FKCOLUMN_NAME"); .filter(fk -> fksThatNeedNewNames.contains(fk.getId())) .forEach(fk -> { final Set<String> thisSet = fk.foreignKeyColumns() .map(ForeignKeyColumn::getId) .collect(toSet()); fk.mutator().setId(found.get().getId()); fk.mutator().setName(found.get().getName()); } else { final String randName = md5(thisSet.toString()); fk.mutator().setId(randName); fk.mutator().setName(randName); LOGGER.error(format( "Found a foreign key in table '%s' with no name. " +
/** * Locate the {@link ForeignKey} child with the specified id if it exists, * else return an empty {@code Optional}. * * @param id the {@link HasId#getId()} of the foreign key * @return the child found or an empty {@code Optional} */ default Optional<? extends ForeignKey> findForeignKey(String id) { return foreignKeys().filter(child -> child.getId().equals(id)).findAny(); }
foreignKey.mutator().setId(foreignKeyName); foreignKey.mutator().setName(foreignKeyName); final ForeignKeyColumn foreignKeyColumn = foreignKey.mutator().addNewForeignKeyColumn(); final ForeignKeyColumnMutator<?> fkcMutator = foreignKeyColumn.mutator(); final String fkColumnName = rs.getString("FKCOLUMN_NAME");
public static <T> Stream<T> traverseOver(ForeignKey fk, Class<T> clazz) { if (ForeignKeyColumn.class.isAssignableFrom(clazz)) { return fk.foreignKeyColumns().map(clazz::cast); } else { return fk.foreignKeyColumns().flatMap(fcc -> traverseOver(fcc, clazz)); } }
private IllegalStateException noEnabledForeignKeyException() { return new IllegalStateException( "FK " + fk.getId() + " does not have an enabled ForeignKeyColumn" ); }
public static Optional<? extends ForeignKeyColumn> getForeignKey(Table table, Column column) { requireNonNulls(table, column); return table.foreignKeys() .filter(HasEnabled::test) .filter(fk -> fk.foreignKeyColumns().count() == 1) // We can only handle one column FKs... .flatMap(ForeignKey::foreignKeyColumns) .filter(fkc -> fkc.findForeignTable().map(Table::isEnabled).orElse(false)) // We can only handle FKs pointing to an enabled Table .filter(fkc -> fkc.findForeignColumn().map(Column::isEnabled).orElse(false)) // We can only handle FKs pointing to an enabled column .filter(fkc -> DocumentDbUtil.isSame(column, fkc.findColumn().orElse(null))) .findFirst(); }
public FkHolder(Injector injector, ForeignKey fk) { requireNonNull(fk); this.codeGenerationComponent = injector.getOrThrow(CodeGenerationComponent.class); this.fk = fk; this.fkc = fk.foreignKeyColumns().findFirst().orElseThrow(this::noEnabledForeignKeyException); this.column = fkc.findColumn().orElseThrow(this::couldNotFindLocalColumnException); this.table = ancestor(column, Table.class).orElseThrow(NoSuchElementException::new); this.foreignColumn = fkc.findForeignColumn().orElseThrow(this::foreignKeyWasNullException); this.foreignTable = fkc.findForeignTable().orElseThrow(this::foreignKeyWasNullException); }
public static Optional<? extends ForeignKeyColumn> getForeignKey(Table table, Column column) { requireNonNulls(table, column); return table.foreignKeys() .filter(HasEnabled::test) .filter(fk -> fk.foreignKeyColumns().count() == 1) // We can only handle one column FKs... .flatMap(ForeignKey::foreignKeyColumns) .filter(fkc -> fkc.findForeignTable().map(Table::isEnabled).orElse(false)) // We can only handle FKs pointing to an enabled Table .filter(fkc -> fkc.findForeignColumn().map(Column::isEnabled).orElse(false)) // We can only handle FKs pointing to an enabled column .filter(fkc -> DocumentDbUtil.isSame(column, fkc.findColumn().orElse(null))) .findFirst(); }
public FkHolder(Injector injector, ForeignKey fk) { requireNonNull(fk); this.codeGenerationComponent = injector.getOrThrow(CodeGenerationComponent.class); this.fk = fk; this.fkc = fk.foreignKeyColumns().findFirst().orElseThrow(this::noEnabledForeignKeyException); this.column = fkc.findColumn().orElseThrow(this::couldNotFindLocalColumnException); this.table = ancestor(column, Table.class).orElseThrow(NoSuchElementException::new); this.foreignColumn = fkc.findForeignColumn().orElseThrow(this::foreignKeyWasNullException); this.foreignTable = fkc.findForeignTable().orElseThrow(this::foreignKeyWasNullException); }