Example usage for org.hibernate.dialect Dialect getFunctions

List of usage examples for org.hibernate.dialect Dialect getFunctions

Introduction

In this page you can find the example usage for org.hibernate.dialect Dialect getFunctions.

Prototype

public final Map<String, SQLFunction> getFunctions() 

Source Link

Document

Retrieves a map of the dialect's registered functions (functionName => SQLFunction ).

Usage

From source file:com.blazebit.persistence.impl.hibernate.function.HibernateEntityManagerIntegrator.java

License:Apache License

@Override
public EntityManager registerFunctions(EntityManager em, Map<String, Map<String, JpqlFunction>> dbmsFunctions) {
    Dialect dialect = getDialect(em);

    String dbms;//from   w w w .  jav  a  2 s.co  m

    if (dialect instanceof MySQLDialect) {
        dbms = "mysql";
    } else if (dialect instanceof Oracle8iDialect) {
        dbms = "oracle";
    } else if (dialect instanceof SQLServerDialect) {
        dbms = "microsoft";
    } else if (dialect instanceof SybaseDialect) {
        dbms = "sybase";
    } else if (dialect instanceof H2Dialect) {
        dbms = "h2";
    } else if (dialect instanceof CUBRIDDialect) {
        dbms = "cubrid";
    } else if (dialect instanceof HSQLDialect) {
        dbms = "hsql";
    } else if (dialect instanceof InformixDialect) {
        dbms = "informix";
    } else if (dialect instanceof IngresDialect) {
        dbms = "ingres";
    } else if (dialect instanceof InterbaseDialect) {
        dbms = "interbase";
    } else {
        dbms = null;
    }

    // Implementation detail: Hibernate uses a mutable map, so we can do this
    Map<String, SQLFunction> functions = dialect.getFunctions();

    for (Map.Entry<String, Map<String, JpqlFunction>> functionEntry : dbmsFunctions.entrySet()) {
        String functionName = functionEntry.getKey();
        Map<String, JpqlFunction> 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 {
            functions.put(functionName, new HibernateJpqlFunctionAdapter(function));
        }
    }

    return em;
}

From source file:de.cosmocode.hibernate.PropertyMatchMode.java

License:Apache License

@SuppressWarnings("deprecation")
private static String render(SessionFactoryImplementor factory, Object... args) {
    final Dialect dialect = factory.getDialect();
    final SQLFunction concat = SQLFunction.class.cast(dialect.getFunctions().get("concat"));
    return concat.render(Arrays.asList(args), factory);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

org.hibernate.criterion.Criterion getRestrictionForFunctionCall(FunctionCallingCriterion criterion,
        PersistentEntity entity) {//from   w  w  w.j av  a  2 s .  co  m
    org.hibernate.criterion.Criterion sqlRestriction;

    SessionFactory sessionFactory = ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
    String property = criterion.getProperty();
    Criterion datastoreCriterion = criterion.getPropertyCriterion();
    PersistentProperty pp = entity.getPropertyByName(property);

    if (pp == null)
        throw new InvalidDataAccessResourceUsageException(
                "Cannot execute function defined in query [" + criterion.getFunctionName()
                        + "] on non-existent property [" + property + "] of [" + entity.getJavaClass() + "]");

    String functionName = criterion.getFunctionName();

    Dialect dialect = getDialect(sessionFactory);
    SQLFunction sqlFunction = dialect.getFunctions().get(functionName);
    if (sqlFunction != null) {
        TypeResolver typeResolver = getTypeResolver(sessionFactory);
        BasicType basic = typeResolver.basic(pp.getType().getName());
        if (basic != null && datastoreCriterion instanceof PropertyCriterion) {

            PropertyCriterion pc = (PropertyCriterion) datastoreCriterion;
            final org.hibernate.criterion.Criterion hibernateCriterion = createHibernateCriterionAdapter(
                    getEntity(), datastoreCriterion, alias).toHibernateCriterion(this);
            if (hibernateCriterion instanceof SimpleExpression) {
                SimpleExpression expr = (SimpleExpression) hibernateCriterion;
                Object op = ReflectionUtils.getField(opField, expr);
                PropertyMapping mapping = getEntityPersister(entity.getJavaClass().getName(), sessionFactory);
                String[] columns;
                if (alias != null) {
                    columns = mapping.toColumns(alias, property);
                } else {
                    columns = mapping.toColumns(property);
                }
                String root = render(basic, Arrays.asList(columns), sessionFactory, sqlFunction);
                Object value = pc.getValue();
                if (value != null) {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value,
                            typeResolver.basic(value.getClass().getName()));
                } else {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value, basic);
                }
            } else {
                throw new InvalidDataAccessResourceUsageException(
                        "Unsupported function [" + functionName + "] defined in query for property [" + property
                                + "] with type [" + pp.getType() + "]");
            }
        } else {
            throw new InvalidDataAccessResourceUsageException("Unsupported function [" + functionName
                    + "] defined in query for property [" + property + "] with type [" + pp.getType() + "]");
        }
    } else {
        throw new InvalidDataAccessResourceUsageException(
                "Unsupported function defined in query [" + functionName + "]");
    }
    return sqlRestriction;
}

From source file:org.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

org.hibernate.criterion.Criterion getRestrictionForFunctionCall(FunctionCallingCriterion criterion,
        PersistentEntity entity) {/*from w  ww  .  jav a2  s .co  m*/
    org.hibernate.criterion.Criterion sqlRestriction;

    SessionFactory sessionFactory = ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
    String property = criterion.getProperty();
    Criterion datastoreCriterion = criterion.getPropertyCriterion();
    PersistentProperty pp = entity.getPropertyByName(property);

    if (pp == null)
        throw new InvalidDataAccessResourceUsageException(
                "Cannot execute function defined in query [" + criterion.getFunctionName()
                        + "] on non-existent property [" + property + "] of [" + entity.getJavaClass() + "]");

    String functionName = criterion.getFunctionName();

    Dialect dialect = getDialect(sessionFactory);
    SQLFunction sqlFunction = dialect.getFunctions().get(functionName);
    if (sqlFunction != null) {
        TypeResolver typeResolver = getTypeResolver(sessionFactory);
        BasicType basic = typeResolver.basic(pp.getType().getName());
        if (basic != null && datastoreCriterion instanceof PropertyCriterion) {

            PropertyCriterion pc = (PropertyCriterion) datastoreCriterion;
            final org.hibernate.criterion.Criterion hibernateCriterion = getHibernateCriterionAdapter()
                    .toHibernateCriterion(this, datastoreCriterion, alias);
            if (hibernateCriterion instanceof SimpleExpression) {
                SimpleExpression expr = (SimpleExpression) hibernateCriterion;
                Object op = ReflectionUtils.getField(opField, expr);
                PropertyMapping mapping = getEntityPersister(entity.getJavaClass().getName(), sessionFactory);
                String[] columns;
                if (alias != null) {
                    columns = mapping.toColumns(alias, property);
                } else {
                    columns = mapping.toColumns(property);
                }
                String root = render(basic, Arrays.asList(columns), sessionFactory, sqlFunction);
                Object value = pc.getValue();
                if (value != null) {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value,
                            typeResolver.basic(value.getClass().getName()));
                } else {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value, basic);
                }
            } else {
                throw new InvalidDataAccessResourceUsageException(
                        "Unsupported function [" + functionName + "] defined in query for property [" + property
                                + "] with type [" + pp.getType() + "]");
            }
        } else {
            throw new InvalidDataAccessResourceUsageException("Unsupported function [" + functionName
                    + "] defined in query for property [" + property + "] with type [" + pp.getType() + "]");
        }
    } else {
        throw new InvalidDataAccessResourceUsageException(
                "Unsupported function defined in query [" + functionName + "]");
    }
    return sqlRestriction;
}

From source file:org.n52.sos.ds.hibernate.util.HibernateHelper.java

License:Open Source License

/**
 * Check if the requested function is supported by the requested dialect
 *
 * @param dialect/*  w  w  w . ja  va2 s .co  m*/
 *            Dialect to check
 * @param function
 *            Function to check
 * @return <code>true</code>, if function is supported
 */
public static boolean supportsFunction(Dialect dialect, String function) {
    return dialect.getFunctions().containsKey(function);
}

From source file:org.openbravo.dal.service.OBDal.java

License:Open Source License

/**
 * Register a sql function in the session factory, after this call it can be used by queries.
 *//*from  www .  j  a v a 2 s.  c  om*/
public void registerSQLFunction(String name, SQLFunction function) {
    final DalSessionFactory dalSessionFactory = (DalSessionFactory) SessionFactoryController.getInstance()
            .getSessionFactory();

    final Dialect dialect = ((SessionFactoryImpl) dalSessionFactory.getDelegateSessionFactory()).getDialect();
    dialect.getFunctions().put(name, function);
}

From source file:org.postgis.hibernate.ContainsExpression.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    Dialect dialect = criteriaQuery.getFactory().getDialect();
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);

    if (columns.length != 1)
        throw new HibernateException("\"contains\" may only be used with single-column properties");
    if (dialect instanceof PostGISDialect) {
        StandardSQLFunction function = (StandardSQLFunction) dialect.getFunctions()
                .get(PostGISDialect.NAMESPACE + "contains");
        List args = new ArrayList();
        args.add(columns[0]);/*from   w w w . j a v  a 2  s  . com*/
        args.add("?");

        return function.render(args, criteriaQuery.getFactory());
    } else {
        throw new HibernateException("\"contains\" may only be used with a spatial hibernate dialect");
    }
}

From source file:org.postgis.hibernate.IntersectsExpression.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    Dialect dialect = criteriaQuery.getFactory().getDialect();
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);

    if (columns.length != 1)
        throw new HibernateException("\"intersects\" may only be used with single-column properties");
    if (dialect instanceof PostGISDialect) {
        StandardSQLFunction function = (StandardSQLFunction) dialect.getFunctions()
                .get(PostGISDialect.NAMESPACE + "intersects");
        List args = new ArrayList();
        args.add(columns[0]);/* w w  w  .  j  a v a 2s .  c  o m*/
        args.add("?");

        return function.render(args, criteriaQuery.getFactory());
    } else {
        throw new HibernateException("\"intersects\" may only be used with a spatial hibernate dialect");
    }
}

From source file:org.postgis.hibernate.WithinExpression.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    Dialect dialect = criteriaQuery.getFactory().getDialect();
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);

    if (columns.length != 1)
        throw new HibernateException("\"within\" may only be used with single-column properties");
    if (dialect instanceof PostGISDialect) {
        StandardSQLFunction function = (StandardSQLFunction) dialect.getFunctions()
                .get(PostGISDialect.NAMESPACE + "within");
        List args = new ArrayList();
        args.add(columns[0]);//ww w.  j  a  v  a 2 s  .c  o  m
        args.add("?");

        return function.render(args, criteriaQuery.getFactory());
    } else {
        throw new HibernateException("\"within\" may only be used with a spatial hibernate dialect");
    }
}

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void addBitwiseOperationsToDialect() {
    SessionFactoryImplementor hibSessionFactory = (SessionFactoryImplementor) new _RootDAO().getSession()
            .getSessionFactory();//from   w  ww.ja  va 2s.co m
    Dialect dialect = hibSessionFactory.getDialect();
    if (!dialect.getFunctions().containsKey("bit_and")) {
        if (isOracle())
            dialect.getFunctions().put("bit_and", new StandardSQLFunction("bitand", IntegerType.INSTANCE));
        else
            dialect.getFunctions().put("bit_and", new SQLFunctionTemplate(IntegerType.INSTANCE, "?1 & ?2"));
    }
}