Search in sources :

Example 1 with AggregateNumericExpression

use of org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression in project datanucleus-rdbms by datanucleus.

the class AvgWithCastFunction method getAggregateExpression.

protected SQLExpression getAggregateExpression(SQLStatement stmt, List args, JavaTypeMapping m) {
    Class argType = ((SQLExpression) args.get(0)).getJavaTypeMapping().getJavaType();
    List<SQLExpression> checkedArgs = null;
    // Only add the CAST if the argument is a non-floating point
    if (!argType.equals(Double.class) && !argType.equals(Float.class)) {
        checkedArgs = new ArrayList<>();
        checkedArgs.add(new StringExpression(stmt, m, "CAST", args, asList("double")));
    } else {
        checkedArgs = args;
    }
    return new AggregateNumericExpression(stmt, m, getFunctionName(), checkedArgs);
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression)

Example 2 with AggregateNumericExpression

use of org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression in project datanucleus-rdbms by datanucleus.

the class SimpleNumericAggregateMethod 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));
    }
    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();
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    // Handle as Subquery "SELECT AVG(expr) FROM tbl"
    SQLExpression argExpr = args.get(0);
    SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
    subStmt.setClassLoaderResolver(clr);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    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 = new NumericSubqueryExpression(stmt, subStmt);
    subqExpr.setJavaTypeMapping(subqMapping);
    return subqExpr;
}
Also used : SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NucleusException(org.datanucleus.exceptions.NucleusException) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression)

Example 3 with AggregateNumericExpression

use of org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression in project datanucleus-rdbms by datanucleus.

the class SumFunction 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) {
        if (args == null || args.size() != 1) {
            throw new NucleusException(getFunctionName() + " is only supported with a single argument");
        }
        // Use same java type as the argument
        SQLExpression argExpr = args.get(0);
        JavaTypeMapping m = null;
        Class cls = argExpr.getJavaTypeMapping().getJavaType();
        if (cls == Integer.class || cls == Short.class || cls == Long.class) {
            m = stmt.getSQLExpressionFactory().getMappingForType(Long.class, true);
        } else if (Number.class.isAssignableFrom(cls)) {
            m = stmt.getSQLExpressionFactory().getMappingForType(argExpr.getJavaTypeMapping().getJavaType(), true);
        } else {
            throw new NucleusUserException("Cannot perform static SUM with arg of type " + cls.getName());
        }
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 4 with AggregateNumericExpression

use of org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression 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;
}
Also used : AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

AggregateNumericExpression (org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression)4 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)2 NumericSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)2 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AggregateStringExpression (org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression)1 AggregateTemporalExpression (org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression)1 StringSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression)1 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)1 TemporalSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression)1