static final <R extends Record> Table<R> aliased(Table<R> table) { if (table instanceof TableImpl) return ((TableImpl<R>) table).getAliasedTable(); else if (table instanceof TableAlias) return ((TableAlias<R>) table).getAliasedTable(); else return null; }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public final <O extends Record> List<ForeignKey<R, O>> getReferencesTo(Table<O> other) { List<ForeignKey<R, O>> result = new ArrayList<ForeignKey<R, O>>(); for (ForeignKey<R, ?> reference : getReferences()) { if (other.equals(reference.getKey().getTable())) { result.add((ForeignKey<R, O>) reference); } // TODO: Refactor the following two blocks and make things more OO // [#1460] In case the other table was aliased using else if (other instanceof TableImpl) { Table<O> aliased = ((TableImpl<O>) other).getAliasedTable(); if (aliased != null && aliased.equals(reference.getKey().getTable())) { result.add((ForeignKey<R, O>) reference); } } // [#1460] In case the other table was aliased using else if (other instanceof TableAlias) { Table<O> aliased = ((TableAlias<O>) other).getAliasedTable(); if (aliased != null && aliased.equals(reference.getKey().getTable())) { result.add((ForeignKey<R, O>) reference); } } } return Collections.unmodifiableList(result); }
@Override public final void toSQL(RenderContext context) { boolean declare = context.declareTables(); context.start(DELETE_DELETE) .keyword("delete").sql(" "); // [#2464] MySQL supports a peculiar multi-table DELETE syntax for aliased tables: // DELETE t1 FROM my_table AS t1 if (asList(MARIADB, MYSQL).contains(context.configuration().dialect())) { // [#2579] TODO: Improve Table API to discover aliased tables more // reliably instead of resorting to instanceof: if (getFrom() instanceof TableAlias || (getFrom() instanceof TableImpl && ((TableImpl<R>)getFrom()).getAliasedTable() != null)) { context.visit(getFrom()) .sql(" "); } } context.keyword("from").sql(" ") .declareTables(true) .visit(getFrom()) .declareTables(declare) .end(DELETE_DELETE) .start(DELETE_WHERE); if (!(getWhere() instanceof TrueCondition)) { context.formatSeparator() .keyword("where").sql(" ") .visit(getWhere()); } context.end(DELETE_WHERE); }