use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class SQLFunctionMethod 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 ignore, List args) {
if (args == null || args.size() < 1) {
throw new NucleusUserException("Cannot invoke SQL_function() without first argument defining the function");
}
SQLExpression expr = (SQLExpression) args.get(0);
if (!(expr instanceof StringLiteral)) {
throw new NucleusUserException("Cannot use SQL_function() without first argument defining the function");
}
String sql = (String) ((StringLiteral) expr).getValue();
List<SQLExpression> funcArgs = new ArrayList<>();
if (args.size() > 1) {
funcArgs.addAll(args.subList(1, args.size()));
}
// Return as ObjectExpression with an underlying SQLFunctionMapping
JavaTypeMapping m = new SQLFunctionMapping();
m.initialize(stmt.getRDBMSManager(), null);
ObjectExpression retExpr = new ObjectExpression(stmt, m, sql, funcArgs);
return retExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression 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;
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class TemporalMinuteMethod 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) {
SQLExpression invokedExpr = getInvokedExpression(expr, args, "MINUTE");
ArrayList funcArgs = new ArrayList();
funcArgs.add(invokedExpr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "MINUTE", funcArgs);
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class TemporalMonthJavaMethod2 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) {
SQLExpression invokedExpr = getInvokedExpression(expr, args, "MONTH_JAVA");
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
JavaTypeMapping mapping2 = storeMgr.getMappingManager().getMapping(String.class);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
SQLExpression mm = exprFactory.newLiteral(stmt, mapping2, "MM");
ArrayList funcArgs = new ArrayList();
funcArgs.add(invokedExpr);
funcArgs.add(mm);
ArrayList funcArgs2 = new ArrayList();
funcArgs2.add(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "TO_CHAR", funcArgs));
// Delete one from the SQL "month" (origin=1) to be compatible with Java month (origin=0)
NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "TO_NUMBER", funcArgs2), Expression.OP_SUB, one);
numExpr.encloseInParentheses();
return numExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class TemporalMonthJavaMethod3 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) {
SQLExpression invokedExpr = getInvokedExpression(expr, args, "MONTH_JAVA");
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
JavaTypeMapping mapping2 = storeMgr.getMappingManager().getMapping(String.class);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
ArrayList funcArgs = new ArrayList();
funcArgs.add(exprFactory.newLiteral(stmt, mapping2, "month"));
funcArgs.add(invokedExpr);
// Delete one from the SQL "month" (origin=1) to be compatible with Java month (origin=0)
NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "date_part", funcArgs), Expression.OP_SUB, one);
numExpr.encloseInParentheses();
return numExpr;
}
Aggregations