use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class StringLength3Method 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 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), "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 StringMatchesMethod method getBooleanLikeExpression.
protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
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.SQLExpressionFactory 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.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class TemporalDayOfWeekMethod4 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, "dw"));
funcArgs.add(invokedExpr);
// Add one to the SQL "dw" (origin=0) to be compatible with Java Calendar day of week (origin=1)
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "datepart", funcArgs), Expression.OP_ADD, one);
numExpr.encloseInParentheses();
return numExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class TemporalDayOfWeekMethod5 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_OF_WEEK");
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, "%w"));
funcArgs.add(invokedExpr);
// Add one to the SQL (origin=0) to be compatible with Java Calendar day of week (origin=1)
SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs), Expression.OP_ADD, one);
numExpr.encloseInParentheses();
return numExpr;
}
Aggregations