use of org.datanucleus.store.rdbms.table.JoinTable in project datanucleus-rdbms by datanucleus.
the class CollectionSizeMethod method getExpression.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
*/
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
if (args != null && args.size() > 0) {
throw new NucleusException(Localiser.msg("060015", "size", "CollectionExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof CollectionLiteral) {
// Just return the collection size since we have the value
Collection coll = (Collection) ((CollectionLiteral) expr).getValue();
return exprFactory.newLiteral(stmt, exprFactory.getMappingForType(int.class, false), Integer.valueOf(coll.size()));
}
AbstractMemberMetaData mmd = expr.getJavaTypeMapping().getMemberMetaData();
if (mmd.isSerialized()) {
throw new NucleusUserException("Cannot perform Collection.size when the collection is being serialised");
}
ApiAdapter api = stmt.getRDBMSManager().getApiAdapter();
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
Class elementCls = clr.classForName(mmd.getCollection().getElementType());
if (!api.isPersistable(elementCls) && mmd.getJoinMetaData() == null) {
throw new NucleusUserException("Cannot perform Collection.size when the collection<Non-Persistable> is not in a join table");
}
String elementType = mmd.getCollection().getElementType();
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
// TODO Allow for interface elements, etc
JavaTypeMapping ownerMapping = null;
Table collectionTbl = null;
if (mmd.getMappedBy() != null) {
// Bidirectional
AbstractMemberMetaData elementMmd = mmd.getRelatedMemberMetaData(clr)[0];
if (mmd.getJoinMetaData() != null || elementMmd.getJoinMetaData() != null) {
// JoinTable
collectionTbl = storeMgr.getTable(mmd);
ownerMapping = ((JoinTable) collectionTbl).getOwnerMapping();
} else {
// ForeignKey
collectionTbl = storeMgr.getDatastoreClass(elementType, clr);
ownerMapping = collectionTbl.getMemberMapping(elementMmd);
}
} else {
// Unidirectional
if (mmd.getJoinMetaData() != null) {
// JoinTable
collectionTbl = storeMgr.getTable(mmd);
ownerMapping = ((JoinTable) collectionTbl).getOwnerMapping();
} else {
// ForeignKey
collectionTbl = storeMgr.getDatastoreClass(elementType, clr);
ownerMapping = ((DatastoreClass) collectionTbl).getExternalMapping(mmd, MappingType.EXTERNAL_FK);
}
}
SelectStatement subStmt = new SelectStatement(stmt, storeMgr, collectionTbl, null, null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
((StringLiteral) countExpr).generateStatementWithoutQuotes();
subStmt.select(countExpr, null);
SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), expr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);
JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
subqExpr.setJavaTypeMapping(subqMapping);
return subqExpr;
}
Aggregations