From 3c3c8bcbe8b3551ee59660be42945000a2137a9c Mon Sep 17 00:00:00 2001 From: Claus Nagel Date: Fri, 7 Feb 2020 09:41:25 +0100 Subject: [PATCH] added possibility to query correlated outer tables in select statements --- .../org/citydb/sqlbuilder/select/Select.java | 23 +++++++++++++++++++ .../AbstractComparisonOperator.java | 3 +++ .../select/projection/ColumnExpression.java | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/citydb/sqlbuilder/select/Select.java b/src/main/java/org/citydb/sqlbuilder/select/Select.java index 661ca5e..d08db10 100644 --- a/src/main/java/org/citydb/sqlbuilder/select/Select.java +++ b/src/main/java/org/citydb/sqlbuilder/select/Select.java @@ -378,6 +378,29 @@ public Set getInvolvedTables() { return tables; } + public Set
getOuterTables() { + Set
tables = new LinkedHashSet<>(); + + for (PredicateToken token : predicateTokens) + token.getInvolvedTables(tables); + + for (HavingToken token : havingTokens) + token.getInvolvedTables(tables); + + for (OrderByToken token : orderByTokens) + token.getInvolvedTables(tables); + + if (offsetToken != null) + offsetToken.getInvolvedTables(tables); + + if (fetchToken != null) + fetchToken.getInvolvedTables(tables); + + tables.removeAll(getInvolvedTables()); + + return tables; + } + @Override public List> getInvolvedPlaceHolders() { List> statements = new ArrayList<>(); diff --git a/src/main/java/org/citydb/sqlbuilder/select/operator/comparison/AbstractComparisonOperator.java b/src/main/java/org/citydb/sqlbuilder/select/operator/comparison/AbstractComparisonOperator.java index d9b0daf..24babda 100644 --- a/src/main/java/org/citydb/sqlbuilder/select/operator/comparison/AbstractComparisonOperator.java +++ b/src/main/java/org/citydb/sqlbuilder/select/operator/comparison/AbstractComparisonOperator.java @@ -25,6 +25,7 @@ import org.citydb.sqlbuilder.schema.Table; import org.citydb.sqlbuilder.select.PredicateToken; import org.citydb.sqlbuilder.select.ProjectionToken; +import org.citydb.sqlbuilder.select.Select; import org.citydb.sqlbuilder.select.operator.Operator; import java.util.List; @@ -37,6 +38,8 @@ public void getInvolvedTables(Expression operand, Set
tables) { ((ProjectionToken) operand).getInvolvedTables(tables); else if (operand instanceof PredicateToken) ((PredicateToken) operand).getInvolvedTables(tables); + else if (operand instanceof Select) + tables.addAll(((Select) operand).getOuterTables()); } public void getInvolvedPlaceHolders(Expression operand, List> statements) { diff --git a/src/main/java/org/citydb/sqlbuilder/select/projection/ColumnExpression.java b/src/main/java/org/citydb/sqlbuilder/select/projection/ColumnExpression.java index 83c5a6b..53e9799 100644 --- a/src/main/java/org/citydb/sqlbuilder/select/projection/ColumnExpression.java +++ b/src/main/java/org/citydb/sqlbuilder/select/projection/ColumnExpression.java @@ -50,7 +50,7 @@ public String getAsName() { @Override public void getInvolvedTables(Set
tables) { - // nothing to do here + tables.addAll(select.getOuterTables()); } @Override