Search in sources :

Example 46 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class SQLExpressionFactory method invokeOperation.

/**
 * Accessor for the result of an SQLOperation call on the supplied expression with the supplied args.
 * Throws a NucleusException is the method is not supported.
 * @param name Operation to be invoked
 * @param expr The first expression to perform the operation on
 * @param expr2 The second expression to perform the operation on
 * @return The result
 * @throws UnsupportedOperationException if the operation is not specified
 */
public SQLExpression invokeOperation(String name, SQLExpression expr, SQLExpression expr2) {
    // Check for instantiated plugin SQLOperation
    DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
    SQLOperation operation = sqlOperationsByName.get(name);
    if (operation != null) {
        return operation.getExpression(expr, expr2);
    }
    // Check for built-in SQLOperation class definition
    Class sqlOpClass = dba.getSQLOperationClass(name);
    if (sqlOpClass != null) {
        try {
            // Instantiate it
            operation = (SQLOperation) sqlOpClass.newInstance();
            sqlOperationsByName.put(name, operation);
            return operation.getExpression(expr, expr2);
        } catch (Exception e) {
            throw new NucleusException("Error creating SQLOperation of type " + sqlOpClass.getName() + " for operation " + name);
        }
    }
    // Check for plugin definition of this operation for this datastore
    // 1). Try datastore-dependent key
    String datastoreId = dba.getVendorID();
    String key = getSQLOperationKey(datastoreId, name);
    boolean datastoreDependent = true;
    if (!pluginSqlOperationKeysSupported.contains(key)) {
        // 2). No datastore-dependent method, so try a datastore-independent key
        key = getSQLOperationKey(null, name);
        datastoreDependent = false;
        if (!pluginSqlOperationKeysSupported.contains(key)) {
            throw new UnsupportedOperationException("Operation " + name + " on datastore=" + datastoreId + " not supported");
        }
    }
    PluginManager pluginMgr = storeMgr.getNucleusContext().getPluginManager();
    String[] attrNames = (datastoreDependent ? new String[] { "name", "datastore" } : new String[] { "name" });
    String[] attrValues = (datastoreDependent ? new String[] { name, datastoreId } : new String[] { name });
    try {
        operation = (SQLOperation) pluginMgr.createExecutableExtension("org.datanucleus.store.rdbms.sql_operation", attrNames, attrValues, "evaluator", null, null);
        synchronized (operation) {
            sqlOperationsByName.put(key, operation);
            return operation.getExpression(expr, expr2);
        }
    } catch (Exception e) {
        throw new NucleusUserException(Localiser.msg("060011", "operation=" + name), e);
    }
}
Also used : PluginManager(org.datanucleus.plugin.PluginManager) SQLOperation(org.datanucleus.store.rdbms.sql.operation.SQLOperation) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 47 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class SQLExpressionFactory method getMethod.

/**
 * Accessor for the method defined by the class/method names and supplied args.
 * Throws a NucleusException is the method is not supported.
 * Note that if the class name passed in is not for a listed class with that method defined then will check all remaining defined methods for a superclass.
 * @param className Class we are invoking the method on
 * @param methodName Name of the method
 * @param args Any arguments to the method call (ignored currently) TODO Check the arguments
 * @return The method
 */
protected SQLMethod getMethod(String className, String methodName, List args) {
    String datastoreId = storeMgr.getDatastoreAdapter().getVendorID();
    // Try to find datastore-dependent evaluator for class+method
    MethodKey methodKey1 = getSQLMethodKey(datastoreId, className, methodName);
    MethodKey methodKey2 = null;
    SQLMethod method = sqlMethodsByKey.get(methodKey1);
    if (method == null) {
        // Try to find datastore-independent evaluator for class+method
        methodKey2 = getSQLMethodKey(null, className, methodName);
        method = sqlMethodsByKey.get(methodKey2);
    }
    if (method != null) {
        return method;
    }
    // No existing instance, so check the built-in SQLMethods from DatastoreAdapter
    Class sqlMethodCls = storeMgr.getDatastoreAdapter().getSQLMethodClass(className, methodName, clr);
    if (sqlMethodCls != null) {
        // Built-in SQLMethod found, so instantiate it, cache it and return it
        try {
            method = (SQLMethod) sqlMethodCls.newInstance();
            MethodKey key = getSQLMethodKey(datastoreId, className, methodName);
            sqlMethodsByKey.put(key, method);
            return method;
        } catch (Exception e) {
            throw new NucleusException("Error creating SQLMethod of type " + sqlMethodCls.getName() + " for class=" + className + " method=" + methodName);
        }
    }
    // Check the plugin mechanism
    // 1). Try datastore-dependent key
    boolean datastoreDependent = true;
    if (!pluginSqlMethodsKeysSupported.contains(methodKey1)) {
        // 2). No datastore-dependent method, so try a datastore-independent key
        datastoreDependent = false;
        if (!pluginSqlMethodsKeysSupported.contains(methodKey2)) {
            // Not listed as supported for this particular class+method, so maybe is for a superclass
            boolean unsupported = true;
            if (!StringUtils.isWhitespace(className)) {
                Class cls = clr.classForName(className);
                // Try datastore-dependent
                for (MethodKey methodKey : pluginSqlMethodsKeysSupported) {
                    if (methodKey.methodName.equals(methodName) && methodKey.datastoreName.equals(datastoreId)) {
                        Class methodCls = null;
                        try {
                            methodCls = clr.classForName(methodKey.clsName);
                        } catch (ClassNotResolvedException cnre) {
                        // Maybe generic array support?
                        }
                        if (methodCls != null && methodCls.isAssignableFrom(cls)) {
                            // This one is usable here, for superclass
                            method = sqlMethodsByKey.get(methodKey);
                            if (method != null) {
                                MethodKey superMethodKey = new MethodKey();
                                superMethodKey.clsName = className;
                                superMethodKey.methodName = methodKey.methodName;
                                superMethodKey.datastoreName = methodKey.datastoreName;
                                // Cache the same method under this class also
                                sqlMethodsByKey.put(superMethodKey, method);
                                return method;
                            }
                            className = methodKey.clsName;
                            datastoreId = methodKey.datastoreName;
                            datastoreDependent = true;
                            unsupported = false;
                            break;
                        }
                    }
                }
                if (unsupported) {
                    // Try datastore-independent
                    for (MethodKey methodKey : pluginSqlMethodsKeysSupported) {
                        if (methodKey.methodName.equals(methodName) && methodKey.datastoreName.equals("ALL")) {
                            Class methodCls = null;
                            try {
                                methodCls = clr.classForName(methodKey.clsName);
                            } catch (ClassNotResolvedException cnre) {
                            // Maybe generic array support?
                            }
                            if (methodCls != null && methodCls.isAssignableFrom(cls)) {
                                // This one is usable here, for superclass
                                method = sqlMethodsByKey.get(methodKey);
                                if (method != null) {
                                    MethodKey superMethodKey = new MethodKey();
                                    superMethodKey.clsName = className;
                                    superMethodKey.methodName = methodKey.methodName;
                                    superMethodKey.datastoreName = methodKey.datastoreName;
                                    // Cache the same method under this class also
                                    sqlMethodsByKey.put(superMethodKey, method);
                                    return method;
                                }
                                className = methodKey.clsName;
                                datastoreId = methodKey.datastoreName;
                                datastoreDependent = false;
                                unsupported = false;
                                break;
                            }
                        }
                    }
                }
            }
            if (unsupported) {
                if (className != null) {
                    throw new NucleusUserException(Localiser.msg("060008", methodName, className));
                }
                throw new NucleusUserException(Localiser.msg("060009", methodName));
            }
        }
    }
    // Fallback to plugin lookup of class+method[+datastore]
    PluginManager pluginMgr = storeMgr.getNucleusContext().getPluginManager();
    String[] attrNames = (datastoreDependent ? new String[] { "class", "method", "datastore" } : new String[] { "class", "method" });
    String[] attrValues = (datastoreDependent ? new String[] { className, methodName, datastoreId } : new String[] { className, methodName });
    try {
        method = (SQLMethod) pluginMgr.createExecutableExtension("org.datanucleus.store.rdbms.sql_method", attrNames, attrValues, "evaluator", new Class[] {}, new Object[] {});
        // Register the method
        sqlMethodsByKey.put(getSQLMethodKey(datastoreDependent ? datastoreId : null, className, methodName), method);
        return method;
    } catch (Exception e) {
        throw new NucleusUserException(Localiser.msg("060011", "class=" + className + " method=" + methodName), e);
    }
}
Also used : PluginManager(org.datanucleus.plugin.PluginManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLMethod(org.datanucleus.store.rdbms.sql.method.SQLMethod) NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 48 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException 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 49 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class SQLFunctionMethod 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 ignore, List args) {
    if (args == null || args.size() < 1) {
        throw new NucleusUserException("Cannot invoke SQL_function() without first argument defining the function");
    }
    SQLExpression expr = (SQLExpression) args.get(0);
    if (!(expr instanceof StringLiteral)) {
        throw new NucleusUserException("Cannot use SQL_function() without first argument defining the function");
    }
    String sql = (String) ((StringLiteral) expr).getValue();
    List<SQLExpression> funcArgs = new ArrayList<>();
    if (args.size() > 1) {
        funcArgs.addAll(args.subList(1, args.size()));
    }
    // Return as ObjectExpression with an underlying SQLFunctionMapping
    JavaTypeMapping m = new SQLFunctionMapping();
    m.initialize(stmt.getRDBMSManager(), null);
    ObjectExpression retExpr = new ObjectExpression(stmt, m, sql, funcArgs);
    return retExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression) SQLFunctionMapping(org.datanucleus.store.rdbms.mapping.java.SQLFunctionMapping)

Example 50 with NucleusUserException

use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.

the class MathToRadiansMethod 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 ignore, List<SQLExpression> args) {
    if (args == null || args.size() == 0) {
        throw new NucleusUserException("Cannot invoke Math.toRadians without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    } else if (expr instanceof SQLLiteral) {
        if (expr instanceof ByteLiteral) {
            int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
            BigInteger absValue = new BigInteger(String.valueOf(Math.cos(originalValue)));
            return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof IntegerLiteral) {
            int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
            Double absValue = new Double(Math.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof FloatingPointLiteral) {
            double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
            Double absValue = new Double(Math.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.toRadians()", expr);
    } else {
        // Relay to the equivalent "radians(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "radians", null, args);
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) FloatingPointLiteral(org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral) ByteLiteral(org.datanucleus.store.rdbms.sql.expression.ByteLiteral) BigInteger(java.math.BigInteger) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Aggregations

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)258 NucleusException (org.datanucleus.exceptions.NucleusException)65 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)51 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)46 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)46 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)36 ArrayList (java.util.ArrayList)34 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)30 ObjectProvider (org.datanucleus.state.ObjectProvider)30 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)26 Expression (org.datanucleus.query.expression.Expression)24 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)23 SQLException (java.sql.SQLException)22 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)21 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)21 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)21 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)20 BigInteger (java.math.BigInteger)19