Search in sources :

Example 1 with CountStarFunction

use of com.blazebit.persistence.integration.jpa.function.CountStarFunction 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 2 with CountStarFunction

use of com.blazebit.persistence.integration.jpa.function.CountStarFunction 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 3 with CountStarFunction

use of com.blazebit.persistence.integration.jpa.function.CountStarFunction 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

CountStarFunction (com.blazebit.persistence.integration.jpa.function.CountStarFunction)3 JpqlFunction (com.blazebit.persistence.spi.JpqlFunction)3 JpqlFunctionGroup (com.blazebit.persistence.spi.JpqlFunctionGroup)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 StoreManager (org.datanucleus.store.StoreManager)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 Connection (java.sql.Connection)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 SQLMethod (org.datanucleus.store.rdbms.sql.method.SQLMethod)1 ExpressionOperator (org.eclipse.persistence.expressions.ExpressionOperator)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)1 DatabasePlatform (org.eclipse.persistence.platform.database.DatabasePlatform)1