use of org.datanucleus.store.rdbms.sql.expression.NumericExpression in project datanucleus-rdbms by datanucleus.
the class StringIndexOf3Method 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 || args.size() > 2) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
// {stringExpr}.indexOf(strExpr1 [,numExpr2])
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
List funcArgs2 = new ArrayList();
SQLExpression substrExpr = args.get(0);
if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
funcArgs2.add(substrExpr);
List types = new ArrayList();
// max 4000 according DB2 docs
types.add("VARCHAR(4000)");
funcArgs.add(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class, true), "CAST", funcArgs2, types));
if (args.size() == 2) {
SQLExpression fromExpr = args.get(1);
if (!(fromExpr instanceof NumericExpression)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
}
types = new ArrayList();
types.add("BIGINT");
List funcArgs3 = new ArrayList();
funcArgs3.add(new NumericExpression(fromExpr, Expression.OP_ADD, one));
// Add 1 to the passed in value so that it is of origin 1 to be compatible with LOCATE
// Make sure argument is typed as BIGINT
funcArgs.add(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "CAST", funcArgs3, types));
}
NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "LOCATE", funcArgs);
// Subtract 1 from the result of LOCATE to be consistent with Java strings
return new NumericExpression(locateExpr, Expression.OP_SUB, one).encloseInParentheses();
}
use of org.datanucleus.store.rdbms.sql.expression.NumericExpression in project datanucleus-rdbms by datanucleus.
the class StringIndexOf5Method 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 || args.size() > 2) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
// {stringExpr}.indexOf(strExpr1 [,numExpr2])
SQLExpression substrExpr = args.get(0);
if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
ArrayList funcArgs = new ArrayList();
if (args.size() == 1) {
// strExpr.indexOf(str1)
funcArgs.add(expr);
funcArgs.add(substrExpr);
SQLExpression oneExpr = ExpressionUtils.getLiteralForOne(stmt);
NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "STRPOS", funcArgs);
return new NumericExpression(locateExpr, Expression.OP_SUB, oneExpr);
}
// strExpr.indexOf(str1, pos)
SQLExpression fromExpr = args.get(1);
if (!(fromExpr instanceof NumericExpression)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
}
// Find the substring starting at this position
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
ArrayList substrArgs = new ArrayList(1);
substrArgs.add(fromExpr);
SQLExpression strExpr = exprFactory.invokeMethod(stmt, "java.lang.String", "substring", expr, substrArgs);
funcArgs.add(strExpr);
funcArgs.add(substrExpr);
NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "STRPOS", funcArgs);
SQLExpression[] whenExprs = new SQLExpression[1];
NumericExpression zeroExpr = new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(0), null);
whenExprs[0] = locateExpr.gt(zeroExpr);
SQLExpression[] actionExprs = new SQLExpression[1];
SQLExpression oneExpr = ExpressionUtils.getLiteralForOne(stmt);
NumericExpression posExpr1 = new NumericExpression(locateExpr, Expression.OP_SUB, oneExpr);
actionExprs[0] = new NumericExpression(posExpr1, Expression.OP_ADD, fromExpr);
SQLExpression elseExpr = new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(-1), null);
return new CaseNumericExpression(whenExprs, actionExprs, elseExpr);
}
use of org.datanucleus.store.rdbms.sql.expression.NumericExpression in project datanucleus-rdbms by datanucleus.
the class StringLength2Method 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) {
DatastoreAdapter dba = stmt.getDatastoreAdapter();
if (!(dba instanceof FirebirdAdapter)) {
throw new NucleusException("StringLength2Method being used for evaluation of String.length yet this is for Firebird ONLY. Please report this");
}
FirebirdAdapter fba = (FirebirdAdapter) dba;
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (fba.supportsCharLengthFunction()) {
// Firebird v2+ support CHAR_LENGTH
if (!expr.isParameter() && expr instanceof StringLiteral) {
JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
String val = (String) ((StringLiteral) expr).getValue();
return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
} else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHAR_LENGTH", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
if (expr instanceof StringLiteral) {
// Firebird v1 requires STRLEN
JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
String val = (String) ((StringLiteral) expr).getValue();
return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
} else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "STRLEN", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
use of org.datanucleus.store.rdbms.sql.expression.NumericExpression in project datanucleus-rdbms by datanucleus.
the class TemporalDayMethod2 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, "DAY");
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
ArrayList funcArgs = new ArrayList();
funcArgs.add(invokedExpr);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
funcArgs.add(exprFactory.newLiteral(stmt, mapping, "DD"));
ArrayList funcArgs2 = new ArrayList();
funcArgs2.add(new StringExpression(stmt, mapping, "TO_CHAR", funcArgs));
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "TO_NUMBER", funcArgs2);
}
use of org.datanucleus.store.rdbms.sql.expression.NumericExpression in project datanucleus-rdbms by datanucleus.
the class TemporalDayMethod4 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, "DAY");
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
ArrayList funcArgs = new ArrayList();
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
funcArgs.add(exprFactory.newLiteral(stmt, mapping, "%d"));
funcArgs.add(invokedExpr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs);
}
Aggregations