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 StringMatchesMethod method getBooleanLikeExpression.
protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
if (stmt.getDatastoreAdapter().supportsOption(DatastoreAdapter.RAW_PREFIX_LIKE_STATEMENTS)) {
// for spanner escape of Like statements should be done by double backslash '\\' or we should use raw strings r''
// we choose raw strings approach otherwise we have to change the escape logic in datanucleus-core
// see https://cloud.google.com/spanner/docs/operators#comparison_operators
regExpr.toSQLText().prepend("r");
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
BooleanExpression likeExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
SQLText sql = likeExpr.toSQLText();
sql.clearStatement();
if (Expression.OP_LIKE.isHigherThanLeftSide(expr.getLowestOperator())) {
sql.append("(").append(expr).append(")");
} else {
sql.append(expr);
}
sql.append(" LIKE ");
if (Expression.OP_LIKE.isHigherThanRightSide(regExpr.getLowestOperator())) {
sql.append("(").append(regExpr).append(")");
} else {
sql.append(regExpr);
}
BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
if (escapeExpr != null) {
if (escapeExpr instanceof CharacterLiteral) {
String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
if (chr.equals(dba.getEscapeCharacter())) {
// If the escape character specified matches the Java character then apply the known working ESCAPE
// This is because some datastore JDBC drivers require additional "\" characters to allow
// for Java usage
sql.append(dba.getEscapePatternExpression());
} else {
sql.append(" ESCAPE " + escapeExpr);
}
} else {
sql.append(" ESCAPE " + escapeExpr);
}
} else {
sql.append(" " + dba.getEscapePatternExpression());
}
return likeExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class StringSimilarPostgresqlMethod method getBooleanLikeExpression.
protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
BooleanExpression similarToExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
SQLText sql = similarToExpr.toSQLText();
sql.clearStatement();
if (OP_SIMILAR_TO.isHigherThanLeftSide(expr.getLowestOperator())) {
sql.append("(").append(expr).append(")");
} else {
sql.append(expr);
}
sql.append(" SIMILAR TO ");
if (OP_SIMILAR_TO.isHigherThanRightSide(regExpr.getLowestOperator())) {
sql.append("(").append(regExpr).append(")");
} else {
sql.append(regExpr);
}
BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
if (escapeExpr != null) {
if (escapeExpr instanceof CharacterLiteral) {
String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
if (chr.equals(dba.getEscapeCharacter())) {
// If the escape character specified matches the Java character then apply the known working ESCAPE
// This is because some datastore JDBC drivers require additional "\" characters to allow
// for Java usage
sql.append(dba.getEscapePatternExpression());
} else {
sql.append(" ESCAPE " + escapeExpr);
}
} else {
sql.append(" ESCAPE " + escapeExpr);
}
} else {
sql.append(" " + dba.getEscapePatternExpression());
}
return similarToExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanExpression in project datanucleus-rdbms by datanucleus.
the class StringStartsWith3Method 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"));
}
// {stringExpr}.indexOf(strExpr1 [,numExpr2])
ArrayList funcArgs = new ArrayList();
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"));
}
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
if (args.size() > 1) {
SQLExpression numExpr = args.get(1);
funcArgs.add(substrExpr);
funcArgs.add(expr);
return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one.add(numExpr));
}
funcArgs.add(substrExpr);
funcArgs.add(expr);
return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one);
}
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));
}
Aggregations