use of org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression in project datanucleus-rdbms by datanucleus.
the class SimpleOrderableAggregateMethod 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 (expr != null) {
throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
} else if (args == null || args.size() != 1) {
throw new NucleusException(getFunctionName() + " is only supported with a single argument");
}
if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
// FUNC(argExpr)
// Use same java type as the argument
SQLExpression argExpr = args.get(0);
JavaTypeMapping m = argExpr.getJavaTypeMapping();
if (argExpr instanceof TemporalExpression) {
return new AggregateTemporalExpression(stmt, m, getFunctionName(), args);
} else if (argExpr instanceof StringExpression) {
return new AggregateStringExpression(stmt, m, getFunctionName(), args);
}
return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
}
// Handle as Subquery "SELECT AVG(expr) FROM tbl"
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
SQLExpression argExpr = args.get(0);
SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping mapping = stmt.getRDBMSManager().getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
String aggregateString = getFunctionName() + "(" + argExpr.toSQLText() + ")";
SQLExpression aggExpr = exprFactory.newLiteral(subStmt, mapping, aggregateString);
((StringLiteral) aggExpr).generateStatementWithoutQuotes();
subStmt.select(aggExpr, null);
JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
SQLExpression subqExpr = null;
if (argExpr instanceof TemporalExpression) {
subqExpr = new TemporalSubqueryExpression(stmt, subStmt);
} else if (argExpr instanceof StringExpression) {
subqExpr = new StringSubqueryExpression(stmt, subStmt);
} else {
subqExpr = new NumericSubqueryExpression(stmt, subStmt);
}
subqExpr.setJavaTypeMapping(subqMapping);
return subqExpr;
}
Aggregations