Search in sources :

Example 81 with JavaTypeMapping

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);
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Calendar(java.util.Calendar)

Example 82 with JavaTypeMapping

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));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) FirebirdAdapter(org.datanucleus.store.rdbms.adapter.FirebirdAdapter) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 83 with JavaTypeMapping

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));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 84 with JavaTypeMapping

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;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 85 with JavaTypeMapping

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;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)230 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)96 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)82 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)81 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)76 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)69 ArrayList (java.util.ArrayList)66 NucleusException (org.datanucleus.exceptions.NucleusException)57 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)50 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)47 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)43 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)37 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)30 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)30 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)27 List (java.util.List)25 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)22 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)21 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)21