Search in sources :

Example 6 with JpqlFunction

use of com.blazebit.persistence.spi.JpqlFunction in project blaze-persistence by Blazebit.

the class DataNucleusEntityManagerFactoryIntegrator method registerFunctions.

@SuppressWarnings("unchecked")
@Override
public EntityManagerFactory registerFunctions(EntityManagerFactory entityManagerFactory, Map<String, JpqlFunctionGroup> dbmsFunctions) {
    RDBMSStoreManager storeMgr = (RDBMSStoreManager) entityManagerFactory.unwrap(StoreManager.class);
    SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
    String dbms = VENDOR_TO_DBMS_MAPPING.get(storeMgr.getDatastoreAdapter().getVendorID());
    // Register compatibility functions
    if (!exprFactory.isMethodRegistered(null, CountStarFunction.FUNCTION_NAME)) {
        exprFactory.registerMethod(null, CountStarFunction.FUNCTION_NAME, new DataNucleusJpqlFunctionAdapter(new CountStarFunction(), true), true);
    }
    // DataNucleus4 uses a month function that is 0 based which conflicts with ANSI EXTRACT(MONTH)
    if (MAJOR < 5 && !(exprFactory.getMethod("java.util.Date", "getMonth", null) instanceof DataNucleusJpqlFunctionAdapter)) {
        LOG.warning("Overriding DataNucleus native 'MONTH' function to return months 1-based like ANSI EXTRACT instead of 0-based!");
        JpqlFunctionGroup dbmsFunctionGroup = dbmsFunctions.get("month");
        JpqlFunction function = dbmsFunctionGroup.get(dbms);
        if (function == null && !dbmsFunctionGroup.contains(dbms)) {
            function = dbmsFunctionGroup.get(null);
        }
        SQLMethod method = new DataNucleusJpqlFunctionAdapter(function, dbmsFunctionGroup.isAggregate());
        Set<Object> methodKeys = fieldGet("methodNamesSupported", exprFactory, VERSION);
        for (Object methodKey : methodKeys) {
            if ("getMonth".equals((String) fieldGet("methodName", methodKey, VERSION)) && "java.util.Date".equals((String) fieldGet("clsName", methodKey, VERSION))) {
                ((Map<Object, Object>) fieldGet("methodByClassMethodName", exprFactory, VERSION)).put(methodKey, method);
            }
        }
    }
    // construct map for checking existence of functions in a case-insensitive way
    Map<String, JpqlFunction> registeredFunctions = getRegisteredFunctions(entityManagerFactory);
    Map<String, String> caseInsensitiveRegisteredFunctions = new HashMap<>(registeredFunctions.size());
    for (String registeredFunctionName : registeredFunctions.keySet()) {
        caseInsensitiveRegisteredFunctions.put(registeredFunctionName.toLowerCase(), registeredFunctionName);
    }
    for (Map.Entry<String, JpqlFunctionGroup> functionEntry : dbmsFunctions.entrySet()) {
        String functionName = functionEntry.getKey();
        JpqlFunctionGroup dbmsFunctionGroup = functionEntry.getValue();
        JpqlFunction function = dbmsFunctionGroup.get(dbms);
        if (function == null && !dbmsFunctionGroup.contains(dbms)) {
            function = dbmsFunctionGroup.get(null);
        }
        if (function == null) {
            LOG.warning("Could not register the function '" + functionName + "' because there is neither an implementation for the dbms '" + dbms + "' nor a default implementation!");
        } else if (!caseInsensitiveRegisteredFunctions.containsKey(functionName.toLowerCase())) {
            exprFactory.registerMethod(null, functionName, new DataNucleusJpqlFunctionAdapter(function, dbmsFunctionGroup.isAggregate()), true);
        }
    }
    return entityManagerFactory;
}
Also used : CountStarFunction(com.blazebit.persistence.integration.jpa.function.CountStarFunction) JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) HashMap(java.util.HashMap) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLMethod(org.datanucleus.store.rdbms.sql.method.SQLMethod) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with JpqlFunction

use of com.blazebit.persistence.spi.JpqlFunction in project blaze-persistence by Blazebit.

the class AbstractHibernateEntityManagerFactoryIntegrator method getRegisteredFunctions.

@Override
public Map<String, JpqlFunction> getRegisteredFunctions(EntityManagerFactory entityManagerFactory) {
    EntityManager em = null;
    try {
        em = entityManagerFactory.createEntityManager();
        Session s = em.unwrap(Session.class);
        SessionFactoryImplementor sf = (SessionFactoryImplementor) s.getSessionFactory();
        Map<String, SQLFunction> functions = getFunctions(s);
        Map<String, JpqlFunction> map = new HashMap<>(functions.size());
        for (Map.Entry<String, SQLFunction> entry : functions.entrySet()) {
            SQLFunction function = entry.getValue();
            if (function instanceof HibernateJpqlFunctionAdapter) {
                map.put(entry.getKey(), ((HibernateJpqlFunctionAdapter) function).unwrap());
            } else {
                map.put(entry.getKey(), new HibernateSQLFunctionAdapter(sf, entry.getValue()));
            }
        }
        return map;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
Also used : JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManager(javax.persistence.EntityManager) SQLFunction(org.hibernate.dialect.function.SQLFunction) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(org.hibernate.Session)

Example 8 with JpqlFunction

use of com.blazebit.persistence.spi.JpqlFunction in project blaze-persistence by Blazebit.

the class ScalarTargetResolvingExpressionVisitor method resolveToFunctionReturnType.

private void resolveToFunctionReturnType(String functionName) {
    JpqlFunction function = functions.get(functionName.toLowerCase());
    List<PathPosition> currentPositions = pathPositions;
    int positionsSize = currentPositions.size();
    if (function == null) {
        if (positionsSize == 0) {
            currentPositions.add(new PathPosition(null, null));
        } else {
            for (int i = 0; i < positionsSize; i++) {
                PathPosition position = currentPositions.get(i);
                position.setAttribute(null);
                position.setCurrentType(null);
            }
        }
    } else {
        if (positionsSize == 0) {
            Class<?> returnType = function.getReturnType(null);
            currentPositions.add(new PathPosition(returnType == null ? null : metamodel.type(returnType), null));
        } else {
            for (int i = 0; i < positionsSize; i++) {
                PathPosition position = currentPositions.get(i);
                Class<?> returnType = function.getReturnType(position.getCurrentClass());
                position.setAttribute(null);
                position.setCurrentType(returnType == null ? null : metamodel.type(returnType));
            }
        }
    }
}
Also used : JpqlFunction(com.blazebit.persistence.spi.JpqlFunction)

Example 9 with JpqlFunction

use of com.blazebit.persistence.spi.JpqlFunction in project blaze-persistence by Blazebit.

the class EclipseLinkEntityManagerIntegrator method registerFunctions.

@Override
public EntityManagerFactory registerFunctions(EntityManagerFactory entityManagerFactory, Map<String, JpqlFunctionGroup> dbmsFunctions) {
    AbstractSession session = entityManagerFactory.unwrap(JpaEntityManagerFactory.class).getDatabaseSession();
    DatabasePlatform platform = session.getPlatform();
    @SuppressWarnings("unchecked") Map<Integer, ExpressionOperator> platformOperators = platform.getPlatformOperators();
    String dbms;
    // Register compatibility functions
    if (!dbmsFunctions.containsKey(CountStarFunction.FUNCTION_NAME)) {
        JpqlFunctionGroup jpqlFunctionGroup = new JpqlFunctionGroup(CountStarFunction.FUNCTION_NAME, true);
        jpqlFunctionGroup.add(null, new CountStarFunction());
        dbmsFunctions.put(CountStarFunction.FUNCTION_NAME, jpqlFunctionGroup);
    }
    platform.setShouldBindLiterals(false);
    if (platform.isMySQL()) {
        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = null;
        boolean startedTransaction = false;
        try {
            tx = em.getTransaction();
            startedTransaction = !tx.isActive();
            if (startedTransaction) {
                tx.begin();
            }
            Connection connection = em.unwrap(Connection.class);
            if (connection.getMetaData().getDatabaseMajorVersion() > 7) {
                dbms = "mysql8";
            } else {
                dbms = "mysql";
            }
        } catch (Exception ex) {
            throw new RuntimeException("Could not determine the MySQL Server version!", ex);
        } finally {
            if (startedTransaction) {
                tx.commit();
            }
            em.close();
        }
    } else if (platform.isOracle()) {
        dbms = "oracle";
    } else if (platform.isSQLServer()) {
        dbms = "microsoft";
    } else if (platform.isSybase()) {
        dbms = "sybase";
    } else if (platform.isH2()) {
        dbms = "h2";
    } else {
        dbms = null;
    }
    final Map<Class<?>, String> classTypes = getClassToTypeMap(platform);
    for (Map.Entry<String, JpqlFunctionGroup> functionEntry : dbmsFunctions.entrySet()) {
        String functionName = functionEntry.getKey();
        JpqlFunctionGroup dbmsFunctionMap = functionEntry.getValue();
        JpqlFunction function = dbmsFunctionMap.get(dbms);
        if (function == null) {
            function = dbmsFunctionMap.get(null);
        }
        if (function == null) {
            LOG.warning("Could not register the function '" + functionName + "' because there is neither an implementation for the dbms '" + dbms + "' nor a default implementation!");
        } else {
            addFunction(platformOperators, functionName, function, session, classTypes);
        }
    }
    return entityManagerFactory;
}
Also used : CountStarFunction(com.blazebit.persistence.integration.jpa.function.CountStarFunction) EntityTransaction(javax.persistence.EntityTransaction) JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) Connection(java.sql.Connection) DatabasePlatform(org.eclipse.persistence.platform.database.DatabasePlatform) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator) EntityManager(javax.persistence.EntityManager) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 10 with JpqlFunction

use of com.blazebit.persistence.spi.JpqlFunction in project blaze-persistence by Blazebit.

the class DataNucleus51EntityManagerFactoryIntegrator method registerFunctions.

@SuppressWarnings("unchecked")
@Override
public EntityManagerFactory registerFunctions(EntityManagerFactory entityManagerFactory, Map<String, JpqlFunctionGroup> dbmsFunctions) {
    RDBMSStoreManager storeMgr = (RDBMSStoreManager) entityManagerFactory.unwrap(StoreManager.class);
    SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
    String dbms = VENDOR_TO_DBMS_MAPPING.get(storeMgr.getDatastoreAdapter().getVendorID());
    // Register compatibility functions
    if (!exprFactory.isMethodRegistered(null, CountStarFunction.FUNCTION_NAME)) {
        exprFactory.registerMethod(null, CountStarFunction.FUNCTION_NAME, new DataNucleusJpqlFunctionAdapter(new CountStarFunction(), true));
    }
    // construct map for checking existence of functions in a case-insensitive way
    Map<String, JpqlFunction> registeredFunctions = getRegisteredFunctions(entityManagerFactory);
    Map<String, String> caseInsensitiveRegisteredFunctions = new HashMap<>(registeredFunctions.size());
    for (String registeredFunctionName : registeredFunctions.keySet()) {
        caseInsensitiveRegisteredFunctions.put(registeredFunctionName.toLowerCase(), registeredFunctionName);
    }
    for (Map.Entry<String, JpqlFunctionGroup> functionEntry : dbmsFunctions.entrySet()) {
        String functionName = functionEntry.getKey();
        JpqlFunctionGroup dbmsFunctionGroup = functionEntry.getValue();
        JpqlFunction function = dbmsFunctionGroup.get(dbms);
        if (function == null && !dbmsFunctionGroup.contains(dbms)) {
            function = dbmsFunctionGroup.get(null);
        }
        if (function == null) {
            LOG.warning("Could not register the function '" + functionName + "' because there is neither an implementation for the dbms '" + dbms + "' nor a default implementation!");
        } else if (!caseInsensitiveRegisteredFunctions.containsKey(functionName.toLowerCase())) {
            exprFactory.registerMethod(null, functionName, new DataNucleusJpqlFunctionAdapter(function, dbmsFunctionGroup.isAggregate()));
        }
    }
    return entityManagerFactory;
}
Also used : CountStarFunction(com.blazebit.persistence.integration.jpa.function.CountStarFunction) JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) HashMap(java.util.HashMap) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JpqlFunction (com.blazebit.persistence.spi.JpqlFunction)12 HashMap (java.util.HashMap)8 JpqlFunctionGroup (com.blazebit.persistence.spi.JpqlFunctionGroup)5 Map (java.util.Map)5 StoreManager (org.datanucleus.store.StoreManager)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)4 CountStarFunction (com.blazebit.persistence.integration.jpa.function.CountStarFunction)3 EntityManager (javax.persistence.EntityManager)3 SQLMethod (org.datanucleus.store.rdbms.sql.method.SQLMethod)3 IOException (java.io.IOException)2 Constructor (java.lang.reflect.Constructor)2 TreeMap (java.util.TreeMap)2 NucleusContext (org.datanucleus.NucleusContext)2 QueryGenerator (org.datanucleus.store.rdbms.query.QueryGenerator)2 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)2 ExpressionOperator (org.eclipse.persistence.expressions.ExpressionOperator)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)2 DatabasePlatform (org.eclipse.persistence.platform.database.DatabasePlatform)2