protected String getUpdateWithJoins()
{
// Generate Merge expression
resetParamUsage();
StringBuilder buf = new StringBuilder("MERGE INTO ");
DBRowSet table = set.get(0).getTable();
table.addSQL(buf, CTX_FULLNAME|CTX_ALIAS);
// join (only one allowed yet)
DBJoinExpr updateJoin = null;
for (DBJoinExpr jex : joins)
{ // The join
if (jex.isJoinOn(table)==false)
continue;
// found the join
updateJoin = jex;
break;
}
if (updateJoin==null)
throw new ObjectNotValidException(this);
Set<DBColumn> joinColumns = new HashSet<DBColumn>();
updateJoin.addReferencedColumns(joinColumns);
// using
buf.append("\r\nUSING ");
DBCommand inner = this.clone();
inner.clearSelect();
inner.clearOrderBy();
for (DBColumn jcol : joinColumns)
{ // Select join columns
if (jcol.getRowSet()!=table)
inner.select(jcol);
}
for (DBSetExpr sex : set)
{ // Select set expressions
Object val = sex.getValue();
if (val instanceof DBColumnExpr)
inner.select(((DBColumnExpr)val));
}
// remove join (if not necessary)
if (inner.hasConstraintOn(table)==false)
inner.removeJoinsOn(table);
// add SQL for inner statement
inner.addSQL(buf, CTX_DEFAULT);
// find the source table
DBColumnExpr left = updateJoin.getLeft();
DBColumnExpr right = updateJoin.getRight();
DBRowSet source = right.getUpdateColumn().getRowSet();
if (source==table)
source = left.getUpdateColumn().getRowSet();
// add Alias
buf.append(" ");
buf.append(source.getAlias());
buf.append("\r\nON (");
left.addSQL(buf, CTX_DEFAULT);
buf.append(" = ");
right.addSQL(buf, CTX_DEFAULT);
// Compare Expression