use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringLength4Method 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.isParameter() && expr instanceof StringLiteral) {
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
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), "LEN", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringLengthMethod 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.isParameter() && expr instanceof StringLiteral) {
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
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));
}
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringMatchesDerbyMethod method getExpressionForStringExpressionInput.
protected BooleanExpression getExpressionForStringExpressionInput(SQLStatement stmt, SQLExpression expr, SQLExpression argExpr, SQLExpression escapeExpr) {
// Use Derby "NUCLEUS_MATCHES" function
List funcArgs = new ArrayList();
funcArgs.add(expr);
funcArgs.add(argExpr);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
JavaTypeMapping m = exprFactory.getMappingForType(BigInteger.class, false);
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
return new NumericExpression(stmt, m, "NUCLEUS_MATCHES", funcArgs).eq(one);
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringMatchesMethod 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() > 2) {
throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
} else if (!(args.get(0) instanceof StringExpression) && !(args.get(0) instanceof ParameterLiteral)) {
throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
}
SQLExpression likeExpr = args.get(0);
if (!(likeExpr instanceof StringExpression) && !(likeExpr instanceof CharacterExpression) && !(likeExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "like/matches", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
SQLExpression escapeExpr = null;
if (args.size() > 1) {
escapeExpr = args.get(1);
}
if ((likeExpr instanceof StringLiteral || likeExpr instanceof ParameterLiteral) && likeExpr.isParameter()) {
// Argument as parameter needs translation to use SQL "LIKE" syntax, so has to be embedded as literal
stmt.getQueryGenerator().useParameterExpressionAsLiteral((SQLLiteral) likeExpr);
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof StringLiteral && likeExpr instanceof StringLiteral) {
// String.matches(String) so evaluate in-memory
String primary = (String) ((StringLiteral) expr).getValue();
String pattern = (String) ((StringLiteral) likeExpr).getValue();
return new BooleanLiteral(stmt, exprFactory.getMappingForType(boolean.class, false), primary.matches(pattern));
} else if (expr instanceof StringLiteral) {
return getBooleanLikeExpression(stmt, expr, likeExpr, escapeExpr);
} else if (expr instanceof StringExpression && likeExpr instanceof StringLiteral) {
// Convert the pattern to use the regex constructs suitable for the datastore
String pattern = (String) ((StringLiteral) likeExpr).getValue();
if (stmt.getQueryGenerator().getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JDOQL)) {
// JDOQL input is in java.lang.String regular expression format, so convert to SQL like
boolean caseSensitive = false;
if (pattern.startsWith("(?i)")) {
caseSensitive = true;
pattern = pattern.substring(4);
}
DatastoreAdapter dba = stmt.getDatastoreAdapter();
RegularExpressionConverter converter = new RegularExpressionConverter(dba.getPatternExpressionZeroMoreCharacters().charAt(0), dba.getPatternExpressionAnyCharacter().charAt(0), dba.getEscapeCharacter().charAt(0));
if (caseSensitive) {
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern).toLowerCase());
return getBooleanLikeExpression(stmt, expr.invoke("toLowerCase", null), patternExpr, escapeExpr);
}
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern));
return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
}
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), pattern);
return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
} else if (expr instanceof StringExpression) {
return getExpressionForStringExpressionInput(stmt, expr, likeExpr, escapeExpr);
} else {
throw new NucleusException(Localiser.msg("060001", "matches", expr));
}
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringReplaceAllMethod 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() != 2) {
throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 2, "StringExpression/CharacterExpression"));
}
// {strExpr}.translate(strExpr1, strExpr2)
SQLExpression strExpr1 = args.get(0);
SQLExpression strExpr2 = args.get(1);
if (!(strExpr1 instanceof StringExpression) && !(strExpr1 instanceof CharacterExpression)) {
throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 1, "StringExpression/CharacterExpression"));
}
if (!(strExpr2 instanceof StringExpression) && !(strExpr2 instanceof CharacterExpression)) {
throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 2, "StringExpression/CharacterExpression"));
}
// Invoke substring(startExpr, endExpr)
List<SQLExpression> newArgs = new ArrayList<>(3);
newArgs.add(expr);
newArgs.add(strExpr1);
newArgs.add(strExpr2);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
JavaTypeMapping mapping = exprFactory.getMappingForType(String.class, false);
return new StringExpression(stmt, mapping, "replace", newArgs);
}
Aggregations