[#6304] Improve internal TableAlias discovery

This commit is contained in:
lukaseder 2017-06-01 12:23:00 +02:00
parent 9560b9454b
commit ec3e4127d2
4 changed files with 13 additions and 40 deletions

View File

@ -487,19 +487,9 @@ abstract class AbstractTable<R extends Record> extends AbstractQueryPart impleme
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();
// [#1460] [#6304] In case the other table was aliased
else {
Table<O> aliased = Tools.aliased(other);
if (aliased != null && aliased.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);

View File

@ -108,13 +108,10 @@ final class DeleteQueryImpl<R extends Record> extends AbstractDMLQuery<R> implem
// DELETE t1 FROM my_table AS t1
if (asList(MARIADB, MYSQL).contains(ctx.configuration().dialect())) {
// [#2579] TODO: Improve Table API to discover aliased tables more
// reliably instead of resorting to instanceof:
if (table instanceof TableAlias ||
(table instanceof TableImpl && ((TableImpl<R>) table).getAliasedTable() != null)) {
// [#2579] [#6304] TableAlias discovery
if (Tools.alias(table) != null)
ctx.visit(table)
.sql(' ');
}
}
ctx.visit(K_FROM).sql(' ')

View File

@ -602,25 +602,10 @@ implements
private final Table<?> search(Table<?> tree, Table<?> search) {
// TODO: Another instanceof, and we should probably resort to
// implementing a new org.jooq.Context to traverse the AST
if (tree instanceof TableImpl) {
TableImpl<?> t = (TableImpl<?>) tree;
if (t.alias == null && search.equals(t))
return t;
else if (t.alias != null && search.equals(t.alias.wrapped()))
return t;
else
return null;
}
else if (tree instanceof TableAlias) {
TableAlias<?> t = (TableAlias<?>) tree;
if (search.equals(t.alias.wrapped()))
return t;
else
return null;
// [#6304] Improved alias discovery
Alias<? extends Table<?>> alias = Tools.alias(tree);
if (alias != null) {
return search(alias.wrapped(), search);
}
else if (tree instanceof JoinTable) {
JoinTable t = (JoinTable) tree;
@ -631,8 +616,9 @@ implements
return r;
}
return tree;
else {
return search.equals(tree) ? tree : null;
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -3735,7 +3735,7 @@ final class Tools {
return null;
}
static final Alias<?> alias(Table<?> table) {
static final Alias<? extends Table<?>> alias(Table<?> table) {
if (table instanceof TableImpl)
return ((TableImpl<?>) table).alias;
else if (table instanceof TableAlias)