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();
}
}
}
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");
}
}
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;
}
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()));
}
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;
}
Aggregations