use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringStartsWithMethod 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", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
}
SQLExpression substrExpr = args.get(0);
if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
}
if (args.size() > 1) {
// TODO Use numExpr in a SUBSTRING
if (substrExpr.isParameter()) {
// Any pattern expression cannot be a parameter here
SQLLiteral substrLit = (SQLLiteral) substrExpr;
stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
if (substrLit.getValue() == null) {
return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
}
}
SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
}
// Create a new StringExpression and manually update its SQL
if (substrExpr.isParameter()) {
// Any pattern expression cannot be a parameter here
SQLLiteral substrLit = (SQLLiteral) substrExpr;
stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
if (substrLit.getValue() == null) {
return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
}
}
SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
}
use of org.datanucleus.exceptions.NucleusException 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));
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringTrim2Method 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() > 1) {
throw new NucleusException("TRIM has incorrect number of args");
}
if (args != null && args.size() == 1) {
NucleusLogger.QUERY.warn("This database does not support use of the trimSpec argument to trim(). Ignoring.");
}
if (expr instanceof StringLiteral) {
String val = (String) ((StringLiteral) expr).getValue();
return new StringLiteral(stmt, expr.getJavaTypeMapping(), val.trim(), null);
}
ArrayList rtrimArgs = new ArrayList();
rtrimArgs.add(expr);
StringExpression strExpr = new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class), "RTRIM", rtrimArgs);
List<SQLExpression> ltrimArgs = new ArrayList<>();
ltrimArgs.add(strExpr);
return new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class), "LTRIM", ltrimArgs);
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringIndexOf2Method 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"));
}
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
funcArgs.add(substrExpr);
if (args.size() == 2) {
SQLExpression fromExpr = args.get(1);
if (!(fromExpr instanceof NumericExpression)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
}
funcArgs.add(fromExpr.add(one));
}
NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "INSTR", funcArgs);
return new NumericExpression(locateExpr, Expression.OP_SUB, one).encloseInParentheses();
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringIndexOfMethod 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"));
}
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
ArrayList funcArgs = new ArrayList();
funcArgs.add(substrExpr);
funcArgs.add(expr);
if (args.size() == 2) {
SQLExpression fromExpr = args.get(1);
if (!(fromExpr instanceof NumericExpression)) {
throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
}
funcArgs.add(fromExpr.add(one));
}
NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "LOCATE", funcArgs);
return new NumericExpression(locateExpr, Expression.OP_SUB, one).encloseInParentheses();
}
Aggregations