command.setIfExists(ifExists);
return command;
}
private TableFilter readJoin(TableFilter top, Select command, boolean fromOuter) {
TableFilter last = top;
while (true) {
if (readIf("RIGHT")) {
readIf("OUTER");
read("JOIN");
// the right hand side is the 'inner' table usually
TableFilter newTop = readTableFilter(fromOuter);
newTop = readJoin(newTop, command, true);
Expression on = null;
if (readIf("ON")) {
on = readExpression();
}
if (SysProperties.NESTED_JOINS) {
String joinTable = Constants.PREFIX_JOIN + parseIndex;
TableFilter nt = new TableFilter(session, getDualTable(true), joinTable, rightsChecked, currentSelect);
nt.addJoin(top, false, true, null);
newTop.addJoin(nt, true, false, on);
} else {
newTop.addJoin(top, true, false, on);
}
top = newTop;
last = newTop;
} else if (readIf("LEFT")) {
readIf("OUTER");
read("JOIN");
TableFilter join = readTableFilter(true);
top = readJoin(top, command, true);
Expression on = null;
if (readIf("ON")) {
on = readExpression();
}
top.addJoin(join, true, false, on);
last = join;
} else if (readIf("FULL")) {
throw getSyntaxError();
} else if (readIf("INNER")) {
read("JOIN");
TableFilter join = readTableFilter(fromOuter);
top = readJoin(top, command, false);
Expression on = null;
if (readIf("ON")) {
on = readExpression();
}
if (SysProperties.NESTED_JOINS) {
top.addJoin(join, false, false, on);
} else {
top.addJoin(join, fromOuter, false, on);
}
last = join;
} else if (readIf("JOIN")) {
TableFilter join = readTableFilter(fromOuter);
top = readJoin(top, command, false);
Expression on = null;
if (readIf("ON")) {
on = readExpression();
}
if (SysProperties.NESTED_JOINS) {
top.addJoin(join, false, false, on);
} else {
top.addJoin(join, fromOuter, false, on);
}
last = join;
} else if (readIf("CROSS")) {
read("JOIN");
TableFilter join = readTableFilter(fromOuter);
if (SysProperties.NESTED_JOINS) {
top.addJoin(join, false, false, null);
} else {
top.addJoin(join, fromOuter, false, null);
}
last = join;
} else if (readIf("NATURAL")) {
read("JOIN");
TableFilter join = readTableFilter(fromOuter);
Column[] tableCols = last.getTable().getColumns();
Column[] joinCols = join.getTable().getColumns();
String tableSchema = last.getTable().getSchema().getName();
String joinSchema = join.getTable().getSchema().getName();
Expression on = null;
for (Column tc : tableCols) {
String tableColumnName = tc.getName();
for (Column c : joinCols) {
String joinColumnName = c.getName();
if (equalsToken(tableColumnName, joinColumnName)) {
join.addNaturalJoinColumn(c);
Expression tableExpr = new ExpressionColumn(database, tableSchema, last
.getTableAlias(), tableColumnName);
Expression joinExpr = new ExpressionColumn(database, joinSchema, join
.getTableAlias(), joinColumnName);
Expression equal = new Comparison(session, Comparison.EQUAL, tableExpr, joinExpr);
if (on == null) {
on = equal;
} else {