use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression 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.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class StringEndsWithMethod 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", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
SQLExpression substrExpr = args.get(0);
if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
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, likeSubstrExpr.add(ExpressionUtils.getEscapedPatternExpression(substrExpr)));
}
// 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, likeSubstrExpr.add(ExpressionUtils.getEscapedPatternExpression(substrExpr)));
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processInExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processInExpression(org.datanucleus.query.expression.Expression)
*/
@Override
protected Object processInExpression(Expression expr) {
SQLExpression right = stack.pop();
SQLExpression left = stack.pop();
if (right instanceof CollectionExpression || right instanceof org.datanucleus.store.rdbms.sql.expression.ArrayExpression) {
// myElement IN myCollection
if (right.getParameterName() != null) {
setNotPrecompilable();
}
// Use Collection.contains(element)/Array.contains(element)
List<SQLExpression> sqlExprArgs = new ArrayList();
sqlExprArgs.add(left);
SQLExpression sqlExpr = right.invoke("contains", sqlExprArgs);
stack.push(sqlExpr);
return sqlExpr;
} else if (right.getParameterName() != null || left.getParameterName() != null) {
// "expr IN (:param)" or ":param IN (expr)" or ":param1 IN (:param2)"
setNotPrecompilable();
// Replace parameter(s) with equivalent literal of correct type
if (right instanceof ParameterLiteral) {
right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
}
if (left instanceof ParameterLiteral && !Collection.class.isAssignableFrom(right.getJavaTypeMapping().getJavaType())) {
left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
}
// Single valued parameter, so use equality
SQLExpression inExpr = new BooleanExpression(left, Expression.OP_EQ, right);
stack.push(inExpr);
return inExpr;
}
SQLExpression inExpr = left.in(right, false);
stack.push(inExpr);
return inExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processAndExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processAndExpression(org.datanucleus.query.expression.Expression)
*/
protected Object processAndExpression(Expression expr) {
SQLExpression rightExpr = stack.pop();
SQLExpression leftExpr = stack.pop();
if (!(rightExpr instanceof BooleanExpression)) {
throw new NucleusUserException("Query has clause " + rightExpr + " used with AND. This is illegal, and should be a boolean expression");
}
if (!(leftExpr instanceof BooleanExpression)) {
throw new NucleusUserException("Query has clause " + leftExpr + " used with AND. This is illegal, and should be a boolean expression");
}
BooleanExpression right = (BooleanExpression) rightExpr;
BooleanExpression left = (BooleanExpression) leftExpr;
if (left.getSQLStatement() != null && right.getSQLStatement() != null && left.getSQLStatement() != right.getSQLStatement()) {
if (left.getSQLStatement() == stmt && right.getSQLStatement().isChildStatementOf(stmt)) {
// Apply right to its sub-statement now and return left
right.getSQLStatement().whereAnd(right, true);
stack.push(left);
return left;
} else if (right.getSQLStatement() == stmt && left.getSQLStatement().isChildStatementOf(stmt)) {
// Apply left to its sub-statement now and return right
left.getSQLStatement().whereAnd(left, true);
stack.push(right);
return right;
}
// TODO Cater for more situations
}
if (compileComponent == CompilationComponent.FILTER) {
// Make sure any simple boolean field clauses are suitable
left = getBooleanExpressionForUseInFilter(left);
right = getBooleanExpressionForUseInFilter(right);
}
BooleanExpression opExpr = left.and(right);
stack.push(opExpr);
return opExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processGteqExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processGteqExpression(org.datanucleus.query.expression.Expression)
*/
@Override
protected Object processGteqExpression(Expression expr) {
SQLExpression right = stack.pop();
SQLExpression left = stack.pop();
if (left instanceof ParameterLiteral && !(right instanceof ParameterLiteral)) {
left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
} else if (right instanceof ParameterLiteral && !(left instanceof ParameterLiteral)) {
right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
}
ExpressionUtils.checkAndCorrectExpressionMappingsForBooleanComparison(left, right);
if (left instanceof UnboundExpression) {
processUnboundExpression((UnboundExpression) left);
left = stack.pop();
}
if (right instanceof UnboundExpression) {
processUnboundExpression((UnboundExpression) right);
right = stack.pop();
}
BooleanExpression opExpr = left.ge(right);
stack.push(opExpr);
return opExpr;
}
Aggregations