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