Column otherIndex = otherTable.newColumn("MyIndex", Column.Type.INTEGER);
otherTable.newPrimaryKey().addColumn(otherIndex);
ForeignKey foreignKey = otherTable.newForeignKey(table);
SelectStatement selectStatement = sqlFactory.newSelectStatement();
selectStatement.setTable(otherTable);
DeleteStatement deleteStatement = sqlFactory.newDeleteStatement();
deleteStatement.setTable(table);
List columns = new ArrayList();
for (Iterator iter = table.getColumns(); iter.hasNext(); ) {
Column column = (Column) iter.next();
Column refColumn = otherTable.newColumn("Ref" + column.getName(), column.getType());
foreignKey.addColumnLink(refColumn, column);
if (column.isPrimaryKeyPart()) {
selectStatement.addResultColumn(selectStatement.getTableReference().newColumnReference(refColumn));
columns.add(deleteStatement.getTableReference().newColumnReference(column));
}
}
BooleanConstraint eq = selectStatement.getWhere().createEQ();
eq.addPart(selectStatement.getTableReference().newColumnReference("RefMyName"));
eq.addPlaceholder();
BooleanConstraint bc = deleteStatement.getWhere().createIN();
bc.addPart((ColumnReference[]) columns.toArray(new ColumnReference[columns.size()]));
bc.addPart(selectStatement);
String expect = "DELETE FROM MySchema.MyTable WHERE (MyTable.MyIndex, VerNum) IN ((SELECT RefMyIndex, RefVerNum FROM MySchema.OtherTable WHERE RefMyName=?))";
String got = gen.getQuery(deleteStatement);
assertEquals(expect, got);