buildStatement();
}
private void buildStatement() throws MappingException {
try {
QueryExpression expr = _factory.getQueryExpression();
Map identitiesUsedForTable = new HashMap();
Vector joinTables = new Vector();
// join all the extended table
ClassDescriptor curDesc = _engine.getDescriptor();
ClassDescriptor baseDesc;
while (curDesc.getExtends() != null) {
baseDesc = curDesc.getExtends();
String[] curDescIdNames = SQLHelper.getIdentitySQLNames(curDesc);
String[] baseDescIdNames = SQLHelper.getIdentitySQLNames(baseDesc);
expr.addInnerJoin(
new ClassDescriptorJDONature(curDesc).getTableName(), curDescIdNames,
new ClassDescriptorJDONature(baseDesc).getTableName(), baseDescIdNames);
joinTables.add(new ClassDescriptorJDONature(baseDesc).getTableName());
curDesc = baseDesc;
}
SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities();
SQLFieldInfo[] fields = _engine.getInfo();
// join all the related/depended table
String aliasOld = null;
String alias = null;
for (int i = 0; i < fields.length; i++) {
SQLFieldInfo field = fields[i];
if (i > 0) { aliasOld = alias; }
alias = field.getTableName();
// add id fields for root table if first field points to a separate table
if ((i == 0) && field.isJoined()) {
String[] identities = SQLHelper.getIdentitySQLNames(_engine.getDescriptor());
for (int j = 0; j < identities.length; j++) {
expr.addColumn(
new ClassDescriptorJDONature(curDesc).getTableName(),
identities[j]);
}
identitiesUsedForTable.put(
new ClassDescriptorJDONature(curDesc).getTableName(),
Boolean.TRUE);
}
// add id columns to select statement
if (!alias.equals(aliasOld) && !field.isJoined()) {
ClassDescriptor classDescriptor =
field.getFieldDescriptor().getContainingClassDescriptor();
boolean isTableNameAlreadyAdded = identitiesUsedForTable.containsKey(
new ClassDescriptorJDONature(classDescriptor).getTableName());
if (!isTableNameAlreadyAdded) {
String[] identities = SQLHelper.getIdentitySQLNames(classDescriptor);
for (int j = 0; j < identities.length; j++) {
expr.addColumn(alias, identities[j]);
}
identitiesUsedForTable.put(
new ClassDescriptorJDONature(classDescriptor).getTableName(),
Boolean.TRUE);
}
}
if (field.isJoined()) {
int offset = 0;
String[] rightCol = field.getJoinFields();
String[] leftCol = new String[ids.length - offset];
for (int j = 0; j < leftCol.length; j++) {
leftCol[j] = ids[j + offset].getName();
}
ClassDescriptor clsDescriptor = _engine.getDescriptor();
ClassDescriptorJDONature nature = new ClassDescriptorJDONature(clsDescriptor);
if (joinTables.contains(field.getTableName())
|| nature.getTableName().equals(field.getTableName())) {
// should not mix with aliases in ParseTreeWalker
alias = alias.replace('.', '_') + "_f" + i;
expr.addOuterJoin(_mapTo, leftCol, field.getTableName(), rightCol, alias);
} else {
expr.addOuterJoin(_mapTo, leftCol, field.getTableName(), rightCol);
joinTables.add(field.getTableName());
}
}
for (int j = 0; j < field.getColumnInfo().length; j++) {
expr.addColumn(alias, field.getColumnInfo()[j].getName());
}
expr.addTable(field.getTableName(), alias);
}
// 'join' all the extending tables
List classDescriptorsToAdd = new LinkedList();
ClassDescriptor classDescriptor = null;
SQLHelper.addExtendingClassDescriptors(classDescriptorsToAdd,
new ClassDescriptorJDONature(_engine.getDescriptor()).getExtended());
if (classDescriptorsToAdd.size() > 0) {
for (Iterator iter = classDescriptorsToAdd.iterator(); iter.hasNext(); ) {
classDescriptor = (ClassDescriptor) iter.next();
ClassDescriptorJDONature clsDescNature =
new ClassDescriptorJDONature(classDescriptor);
if (LOG.isTraceEnabled()) {
LOG.trace("Adding outer left join for "
+ classDescriptor.getJavaClass().getName() + " on table "
+ clsDescNature.getTableName());
}
String[] engDescIdNames = SQLHelper.getIdentitySQLNames(
_engine.getDescriptor());
String[] clsDescIdNames = SQLHelper.getIdentitySQLNames(classDescriptor);
expr.addOuterJoin(_mapTo, engDescIdNames,
clsDescNature.getTableName(), clsDescIdNames);
Persistence persistenceEngine;
try {
persistenceEngine = _factory.getPersistence(classDescriptor);
} catch (MappingException e) {
throw new QueryException(
"Problem obtaining persistence engine for ClassDescriptor "
+ classDescriptor.getJavaClass().getName(), e);
}
SQLEngine engine = (SQLEngine) persistenceEngine;
SQLColumnInfo[] idInfos = engine.getColumnInfoForIdentities();
for (int i = 0; i < idInfos.length; i++) {
expr.addColumn(clsDescNature.getTableName(), idInfos[i].getName());
}
SQLFieldInfo[] fieldInfos = ((SQLEngine) persistenceEngine).getInfo();
for (int i = 0; i < fieldInfos.length; i++) {
boolean hasFieldToAdd = false;
SQLColumnInfo[] columnInfos = fieldInfos[i].getColumnInfo();
if (clsDescNature.getTableName().equals(fieldInfos[i].getTableName())) {
for (int j = 0; j < columnInfos.length; j++) {
expr.addColumn(clsDescNature.getTableName(),
fieldInfos[i].getColumnInfo()[j].getName());
}
hasFieldToAdd = true;
}
if (hasFieldToAdd) {
expr.addTable(clsDescNature.getTableName());
}
}
}
}
QueryExpression find = (QueryExpression) expr.clone();
// get id columns' names
for (int i = 0; i < ids.length; i++) {
expr.addParameter(_mapTo, ids[i].getName(), QueryExpression.OP_EQUALS);
}
_statementNoLock = expr.getStatement(false);
_statementLock = expr.getStatement(true);
// add table information if the class in question does not have any non-identity
// fields
if (fields.length == 0) {
for (int i = 0; i < ids.length; i++) {
find.addColumn(_mapTo, ids[i].getName());
}
}
_queryExpression = find;