}
private void buildFinder( JDOClassDescriptor clsDesc ) throws MappingException, QueryException {
Vector fields;
QueryExpression expr;
QueryExpression find;
fields = new Vector();
expr = _factory.getQueryExpression();
find = _factory.getQueryExpression();
//addLoadSql( expr, fields, false, true, true );
//_fields = new FieldInfo[ fields.size() ];
//fields.copyInto( _fields );
// get id columns' names
String[] idnames = _clsDesc.getIdentityColumnNames();
for ( int i=0; i<_ids.length; i++ ) {
expr.addParameter( _mapTo, _ids[i].name, QueryExpression.OpEquals );
}
// join all the extended table
JDOClassDescriptor curDesc = clsDesc;
JDOClassDescriptor baseDesc;
while ( curDesc.getExtends() != null ) {
baseDesc = (JDOClassDescriptor) curDesc.getExtends();
expr.addInnerJoin(curDesc.getTableName(), curDesc.getIdentityColumnNames(),
baseDesc.getTableName(), baseDesc.getIdentityColumnNames());
find.addInnerJoin(curDesc.getTableName(), curDesc.getIdentityColumnNames(),
baseDesc.getTableName(), baseDesc.getIdentityColumnNames());
curDesc = baseDesc;
}
for ( int i=0; i<_ids.length; i++ ) {
find.addColumn( _mapTo, idnames[i] );
}
// join all the related/depended table
Vector joinTables = new Vector();
for ( int i=0; i<_fields.length; i++ ) {
String alias = _fields[i].tableName;
if ( _fields[i].load ) {
if ( _fields[i].joined /*&& !joinTables.contains( _fields[i].tableName )*/ ) {
int offset = 0;
String[] rightCol = _fields[i].joinFields;
String[] leftCol = new String[_ids.length-offset];
for ( int j=0; j<leftCol.length; j++ ) {
leftCol[j] = _ids[j+offset].name;
}
if (joinTables.contains( _fields[i].tableName ) || clsDesc.getTableName().equals( _fields[i].tableName )) {
alias = alias.replace('.', '_') + "_f" + i; // should not mix with aliases in ParseTreeWalker
expr.addOuterJoin( _mapTo, leftCol, _fields[i].tableName, rightCol, alias );
find.addOuterJoin( _mapTo, leftCol, _fields[i].tableName, rightCol, alias );
} else {
expr.addOuterJoin( _mapTo, leftCol, _fields[i].tableName, rightCol );
find.addOuterJoin( _mapTo, leftCol, _fields[i].tableName, rightCol );
joinTables.add( _fields[i].tableName );
}
}
for ( int j=0; j<_fields[i].columns.length; j++ ) {
expr.addColumn( alias, _fields[i].columns[j].name );
find.addColumn( alias, _fields[i].columns[j].name );
}
expr.addTable(_fields[i].tableName, alias);
find.addTable(_fields[i].tableName, alias);
}
}
_sqlLoad = expr.getStatement( false );
_sqlLoadLock = expr.getStatement( true );