*
* @return the load SQL.
*/
public String generateLoadSQL(boolean fullSelect)
{
CharBuffer cb = CharBuffer.allocate();
cb.append("select ");
if (_isDistinct)
cb.append(" distinct ");
for (int i = 0; i < _resultList.size(); i++) {
if (i != 0)
cb.append(", ");
AmberExpr expr = _resultList.get(i);
if (_groupList == null && expr instanceof LoadEntityExpr)
((LoadEntityExpr) expr).generateSelect(cb, fullSelect);
else
expr.generateSelect(cb);
}
if (_hasFrom)
cb.append(" from ");
// jpa/114f: reorder from list for left outer join
for (int i = 1; i < _fromList.size(); i++) {
FromItem item = _fromList.get(i);
if (item.isOuterJoin()) {
JoinExpr join = item.getJoinExpr();
if (join == null)
continue;
FromItem parent = join.getJoinParent();
int index = _fromList.indexOf(parent);
if (index < 0)
continue;
_fromList.remove(i);
if (index < i)
index++;
_fromList.add(index, item);
}
}
boolean hasJoinExpr = false;
boolean isFirst = true;
for (int i = 0; i < _fromList.size(); i++) {
FromItem item = _fromList.get(i);
// jpa/1178
if (getParentQuery() != null) {
ArrayList<FromItem> fromList = getParentQuery().getFromList();
if (fromList != null) {
if (fromList.contains(item)) {
hasJoinExpr = true;
continue;
}
}
}
if (isFirst) {
isFirst = false;
}
else {
if (item.isOuterJoin())
cb.append(" left outer join ");
else {
cb.append(", ");
if (item.getJoinExpr() != null)
hasJoinExpr = true;
}
}
cb.append(item.getTable().getName());
cb.append(" ");
cb.append(item.getName());
if (item.getJoinExpr() != null && item.isOuterJoin()) {
cb.append(" on ");
item.getJoinExpr().generateJoin(cb);
}
EntityType entityType = item.getEntityType();
// jpa/0l44, jpa/0l12
/* XXX: jpa/0l47 move this to LoadExpr.generateSelect
if (entityType != null) {
AmberColumn discriminator = entityType.getDiscriminator();
if (entityType instanceof SubEntityType &&
discriminator != null) {
// jpa/0l4b
// XXX: needs to use parser.createTableName()
FromItem discriminatorItem
= new FromItem((EntityType) entityType,
discriminator.getTable(),
item.getName() + "_disc",
++i);
discriminatorItem.setQuery(this);
_fromList.add(i, discriminatorItem);
cb.append(", ");
cb.append(discriminator.getTable().getName());
cb.append(' ');
cb.append(discriminatorItem.getName());
}
}
*/
}
// jpa/0l12
// if (hasJoinExpr || _where != null) {
boolean hasExpr = false;
for (int i = 0; i < _fromList.size(); i++) {
FromItem item = _fromList.get(i);
AmberExpr expr = item.getJoinExpr();
if (expr != null && ! item.isOuterJoin()) {
if (hasExpr)
cb.append(" and ");
else {
cb.append(" where ");
hasExpr = true;
}
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 (hasExpr)
cb.append(" and ");
else {
cb.append(" where ");
hasExpr = true;
}
cb.append("(" + item.getName() + "." + discriminator.getName() + " = ");
cb.append("'" + entityType.getDiscriminatorValue() + "')");
}
}
}
}
if (_where != null) {
if (hasExpr)
cb.append(" and ");
else {
cb.append(" where ");
hasExpr = true;
}
_where.generateWhere(cb);
}
if (_groupList != null) {
cb.append(" group by ");
for (int i = 0; i < _groupList.size(); i++) {
if (i != 0)
cb.append(", ");
_groupList.get(i).generateSelect(cb);
}
}
if (_having != null) {
hasExpr = false;
cb.append(" having ");
/*
for (int i = 0; i < _fromList.size(); i++) {
FromItem item = _fromList.get(i);
AmberExpr expr = item.getJoinExpr();
if (expr != null && ! item.isOuterJoin()) {
if (hasExpr)
cb.append(" and ");
hasExpr = true;
expr.generateJoin(cb);
}
}
*/
if (_having != null) {
if (hasExpr)
cb.append(" and ");
hasExpr = true;
_having.generateHaving(cb);
}
}
if (_orderList != null) {
cb.append(" order by ");
for (int i = 0; i < _orderList.size(); i++) {
if (i != 0)
cb.append(", ");
_orderList.get(i).generateSelect(cb);
if (Boolean.FALSE.equals(_ascList.get(i)))
cb.append(" desc");
}
}
return cb.toString();
}