use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class TemporalLiteral method invoke.
public SQLExpression invoke(String methodName, List args) {
if (jdbcEscapeValue != null) {
throw new NucleusUserException("Cannot invoke methods on TemporalLiteral using JDBC escape syntax - not supported");
}
if (parameterName == null) {
if (methodName.equals("getDay")) {
// Date.getDay()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.DAY_OF_MONTH)), null);
} else if (methodName.equals("getMonth")) {
// Date.getMonth()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.MONTH)), null);
} else if (methodName.equals("getYear")) {
// Date.getMonth()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.YEAR)), null);
} else if (methodName.equals("getHour")) {
// Date.getHour()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.HOUR_OF_DAY)), null);
} else if (methodName.equals("getMinutes")) {
// Date.getMinutes()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.MINUTE)), null);
} else if (methodName.equals("getSeconds")) {
// Date.getMinutes()
Calendar cal = Calendar.getInstance();
cal.setTime(value);
JavaTypeMapping m = stmt.getRDBMSManager().getMappingManager().getMapping(Integer.class);
return new IntegerLiteral(stmt, m, Integer.valueOf(cal.get(Calendar.SECOND)), null);
}
}
return super.invoke(methodName, args);
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class StringLength2Method 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) {
DatastoreAdapter dba = stmt.getDatastoreAdapter();
if (!(dba instanceof FirebirdAdapter)) {
throw new NucleusException("StringLength2Method being used for evaluation of String.length yet this is for Firebird ONLY. Please report this");
}
FirebirdAdapter fba = (FirebirdAdapter) dba;
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (fba.supportsCharLengthFunction()) {
// Firebird v2+ support CHAR_LENGTH
if (!expr.isParameter() && expr instanceof StringLiteral) {
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));
}
}
if (expr instanceof StringLiteral) {
// Firebird v1 requires STRLEN
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), "STRLEN", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping 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.mapping.java.JavaTypeMapping 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.mapping.java.JavaTypeMapping 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