Search in sources :

Example 6 with JpqlFunctionGroup

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

the class AbstractHibernateEntityManagerFactoryIntegrator method registerFunctions.

@Override
public EntityManagerFactory registerFunctions(EntityManagerFactory entityManagerFactory, Map<String, JpqlFunctionGroup> dbmsFunctions) {
    EntityManager em = null;
    try {
        em = entityManagerFactory.createEntityManager();
        Session s = em.unwrap(Session.class);
        Map<String, SQLFunction> originalFunctions = getFunctions(s);
        Map<String, SQLFunction> functions = new TreeMap<String, SQLFunction>(String.CASE_INSENSITIVE_ORDER);
        functions.putAll(originalFunctions);
        Dialect dialect = getDialect(s);
        String dbms = getDbmsName(entityManagerFactory, em, dialect);
        for (Map.Entry<String, JpqlFunctionGroup> functionEntry : dbmsFunctions.entrySet()) {
            String functionName = functionEntry.getKey();
            JpqlFunctionGroup dbmsFunctionMap = functionEntry.getValue();
            JpqlFunction function = dbmsFunctionMap.get(dbms);
            if (function == null && !dbmsFunctionMap.contains(dbms)) {
                function = dbmsFunctionMap.get(null);
            }
            if (function == null) {
                if (functions.containsKey(functionName)) {
                    LOG.finest("Using ORM registered function '" + functionName + "' because there is neither an implementation for the dbms '" + dbms + "' nor a default implementation.");
                } else {
                    LOG.warning("Could not register the function '" + functionName + "' because there is neither an implementation for the dbms '" + dbms + "' nor a default implementation!");
                }
            } else {
                functions.put(functionName, new HibernateJpqlFunctionAdapter(function));
            }
        }
        replaceFunctions(s, functions);
        return entityManagerFactory;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) Dialect(org.hibernate.dialect.Dialect) SQLFunction(org.hibernate.dialect.function.SQLFunction) TreeMap(java.util.TreeMap) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(org.hibernate.Session)

Example 7 with JpqlFunctionGroup

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

the class AbstractCoreTest method configure.

@Override
protected void configure(CriteriaBuilderConfiguration config) {
    super.configure(config);
    config.registerFunction(new JpqlFunctionGroup("zero", new ZeroFunction()));
    config.registerFunction(new JpqlFunctionGroup("concatenate", new ConcatenateFunction()));
    config.registerMacro("prefix", new PrefixJpqlMacro());
    dbms = config.getEntityManagerIntegrators().get(0).getDbms(emf);
    if ("postgresql".equals(dbms)) {
        config.setProperty(ConfigurationProperties.RETURNING_CLAUSE_CASE_SENSITIVE, "false");
    }
}
Also used : PrefixJpqlMacro(com.blazebit.persistence.testsuite.macro.PrefixJpqlMacro) ZeroFunction(com.blazebit.persistence.testsuite.function.ZeroFunction) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup) ConcatenateFunction(com.blazebit.persistence.testsuite.function.ConcatenateFunction)

Example 8 with JpqlFunctionGroup

use of com.blazebit.persistence.spi.JpqlFunctionGroup 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 9 with JpqlFunctionGroup

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

the class SelectTest method configure.

@Override
protected void configure(CriteriaBuilderConfiguration config) {
    super.configure(config);
    config.registerFunction(new JpqlFunctionGroup("array", new ZeroFunction()));
    config.registerFunction(new JpqlFunctionGroup("unnest", new ZeroFunction()));
}
Also used : ZeroFunction(com.blazebit.persistence.testsuite.function.ZeroFunction) JpqlFunctionGroup(com.blazebit.persistence.spi.JpqlFunctionGroup)

Example 10 with JpqlFunctionGroup

use of com.blazebit.persistence.spi.JpqlFunctionGroup 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)

Aggregations

JpqlFunctionGroup (com.blazebit.persistence.spi.JpqlFunctionGroup)12 HashMap (java.util.HashMap)5 Map (java.util.Map)5 JpqlFunction (com.blazebit.persistence.spi.JpqlFunction)4 CountStarFunction (com.blazebit.persistence.integration.jpa.function.CountStarFunction)3 ZeroFunction (com.blazebit.persistence.testsuite.function.ZeroFunction)2 StoreManager (org.datanucleus.store.StoreManager)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 CockroachSQLDbmsDialect (com.blazebit.persistence.impl.dialect.CockroachSQLDbmsDialect)1 DB2DbmsDialect (com.blazebit.persistence.impl.dialect.DB2DbmsDialect)1 DefaultDbmsDialect (com.blazebit.persistence.impl.dialect.DefaultDbmsDialect)1 H2DbmsDialect (com.blazebit.persistence.impl.dialect.H2DbmsDialect)1 MSSQLDbmsDialect (com.blazebit.persistence.impl.dialect.MSSQLDbmsDialect)1 MySQL8DbmsDialect (com.blazebit.persistence.impl.dialect.MySQL8DbmsDialect)1 MySQLDbmsDialect (com.blazebit.persistence.impl.dialect.MySQLDbmsDialect)1 OracleDbmsDialect (com.blazebit.persistence.impl.dialect.OracleDbmsDialect)1 PostgreSQLDbmsDialect (com.blazebit.persistence.impl.dialect.PostgreSQLDbmsDialect)1 AliasFunction (com.blazebit.persistence.impl.function.alias.AliasFunction)1 Base64Function (com.blazebit.persistence.impl.function.base64.Base64Function)1