void init()
throws QueryParseException
{
super.init();
CharBuffer cb = new CharBuffer();
cb.append("update ");
FromItem item = _fromList.get(0);
// Postgres 8.2.x accepts UPDATE statements with
// table alias name, but Postgres 8.0.x does not.
//
// Also, adds portability for MySql 3.23 vs MySql 4.x/5.x
// In MySql 3.23, UPDATE with joins are not supported.
//
// jpa/1201 vs jpa/1202 vs jpa/1203
if (getMetaData().supportsUpdateTableAlias()
&& (_fromList.size() > 1)) {
if (getMetaData().supportsUpdateTableList()) {
// MySql: jpa/1202
generateFromList(cb, false);
}
else { // Oracle: jpa/1203
cb.append(item.getTable().getName());
cb.append(" ");
cb.append(item.getName());
}
}
else {
// Postgres: jpa/1201
cb.append(item.getTable().getName());
}
cb.append(" set ");
for (int i = 0; i < _fieldList.size(); i++) {
if (i != 0)
cb.append(", ");
// cb.append(_fieldList.get(i).generateSelect(null));
AmberExpr expr = _fieldList.get(i);
// jpa/1202, jpa/0k15
if (getMetaData().supportsUpdateTableAlias()
&& (_fromList.size() > 1))
expr.generateWhere(cb);
else
expr.generateUpdateWhere(cb);
cb.append("=");
// jpa/1231
if (getMetaData().supportsUpdateTableAlias() || hasSubQuery())
_valueList.get(i).generateWhere(cb);
else
_valueList.get(i).generateUpdateWhere(cb);
}
String updateJoin = null;
if (_where != null) {
// jpa/1200 vs jpa/1201 and jpa/1231
if (_fromList.size() == 1 && ! hasSubQuery()) {
cb.append(" where ");
_where.generateUpdateWhere(cb);
}
else {
boolean isFirst = true;
// jpa/1201: postgres 8.0.x/8.2.x compatibility
if (getMetaData() instanceof PostgresMetaData) {
item = _fromList.get(0);
EntityType type = item.getEntityType();
String targetId = type.getId().generateSelect(item.getName());
cb.append(" FROM ");
String tableName = item.getTable().getName();
cb.append(tableName);
cb.append(' ');
cb.append(item.getName());
isFirst = false;
cb.append(" where ");
cb.append(targetId);
cb.append(" = ");
cb.append(type.getId().generateSelect(item.getTable().getName()));
}
// jpa/1231, jpa/1202 vs jpa/1201 and jpa/1203
if (_fromList.size() > 1
&& ! getMetaData().supportsUpdateTableList()) {
// Postgres: jpa/1201 and Oracle: jpa/1203
item = _fromList.get(1);
EntityType type = item.getEntityType();
String relatedId = type.getId().generateSelect(item.getName());
if (isFirst) {
isFirst = false;
cb.append(" where ");
}
else
cb.append(" and ");
cb.append("exists (select ");
cb.append(relatedId);
cb.append(" from ");
generateFromList(cb, true);
// jpa/1231
isFirst = true;
}
for (int i = 0; i < _fromList.size(); i++) {
item = _fromList.get(i);
AmberExpr expr = item.getJoinExpr();
if (expr != null && ! item.isOuterJoin()) {
// jpa/1231
if (isFirst) {
isFirst = false;
cb.append(" where ");
}
else
cb.append(" and ");
expr.generateJoin(cb);
}
EntityType entityType = item.getEntityType();
// jpa/0l44
if (entityType != null) {
AmberColumn discriminator = entityType.getDiscriminator();
// jpa/0l43
if (entityType instanceof SubEntityType &&
discriminator != null) {
// jpa/0l12, jpa/0l4b
if (item.getTable() == discriminator.getTable()) {
if (isFirst) {
isFirst = false;
cb.append(" where ");
}
else
cb.append(" and ");
cb.append("(" + item.getName() + "." + discriminator.getName() + " = ");
cb.append("'" + entityType.getDiscriminatorValue() + "')");
}
}
}
}
if (isFirst) {
isFirst = false;
cb.append(" where ");
}
else
cb.append(" and ");
_where.generateWhere(cb);
}
} // end if (_where != null)
// jpa/1201 vs jpa/1202
if (_fromList.size() > 1
&& ! getMetaData().supportsUpdateTableList()) {
cb.append(")");
}
_sql = cb.close();
}