Search in sources :

Example 11 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class FunctionNameAsColumnTest method testNoArgFcnAndColumnSameNameAsNoArgFunctionHQL.

@Test
public void testNoArgFcnAndColumnSameNameAsNoArgFunctionHQL() {
    SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction("current_date");
    if (function == null || function.hasParenthesesIfNoArguments()) {
        SkipLog.reportSkip("current_date reuires ()", "tests noarg function that does not require ()");
        return;
    }
    Session s = openSession();
    Transaction t = s.beginTransaction();
    EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
    e1.setCurrentDate("blah blah blah");
    EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
    e2.setCurrentDate("yadda yadda yadda");
    EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
    holder1.getEntityWithNoArgFunctionAsColumns().add(e1);
    EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
    holder2.getEntityWithNoArgFunctionAsColumns().add(e2);
    holder1.setNextHolder(holder2);
    s.save(holder1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    List results = s.createQuery("select str(current_date), currentDate from EntityWithNoArgFunctionAsColumn").list();
    assertEquals(2, results.size());
    assertEquals(((Object[]) results.get(0))[0], ((Object[]) results.get(1))[0]);
    assertTrue(!((Object[]) results.get(0))[0].equals(((Object[]) results.get(0))[1]));
    assertTrue(!((Object[]) results.get(1))[0].equals(((Object[]) results.get(1))[1]));
    assertTrue(((Object[]) results.get(0))[1].equals(e1.getCurrentDate()) || ((Object[]) results.get(0))[1].equals(e2.getCurrentDate()));
    assertTrue(((Object[]) results.get(1))[1].equals(e1.getCurrentDate()) || ((Object[]) results.get(1))[1].equals(e2.getCurrentDate()));
    assertFalse(((Object[]) results.get(0))[1].equals(((Object[]) results.get(1))[1]));
    t.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) SQLFunction(org.hibernate.dialect.function.SQLFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class RowCountProjection method getFunction.

protected SQLFunction getFunction(CriteriaQuery criteriaQuery) {
    final SQLFunctionRegistry sqlFunctionRegistry = criteriaQuery.getFactory().getSqlFunctionRegistry();
    final SQLFunction function = sqlFunctionRegistry.findSQLFunction("count");
    if (function == null) {
        throw new HibernateException("Unable to locate count function mapping");
    }
    return function;
}
Also used : HibernateException(org.hibernate.HibernateException) SQLFunctionRegistry(org.hibernate.dialect.function.SQLFunctionRegistry) SQLFunction(org.hibernate.dialect.function.SQLFunction)

Example 13 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class OrderByFragmentParser method resolveFunction.

@SuppressWarnings("unchecked")
@Override
protected AST resolveFunction(AST ast) {
    /*
		 * Semantic action used during recognition of a *known* function
		 */
    AST child = ast.getFirstChild();
    if (child != null) {
        assert "{param list}".equals(child.getText());
        child = child.getFirstChild();
    }
    final String functionName = ast.getText();
    final SQLFunction function = context.getSqlFunctionRegistry().findSQLFunction(functionName);
    if (function == null) {
        String text = functionName;
        if (child != null) {
            text += '(';
            while (child != null) {
                text += resolveFunctionArgument(child);
                child = child.getNextSibling();
                if (child != null) {
                    text += ", ";
                }
            }
            text += ')';
        }
        return getASTFactory().create(OrderByTemplateTokenTypes.IDENT, text);
    } else {
        ArrayList expressions = new ArrayList();
        while (child != null) {
            expressions.add(resolveFunctionArgument(child));
            child = child.getNextSibling();
        }
        final String text = function.render(null, expressions, context.getSessionFactory());
        return getASTFactory().create(OrderByTemplateTokenTypes.IDENT, text);
    }
}
Also used : AST(antlr.collections.AST) CommonAST(antlr.CommonAST) ArrayList(java.util.ArrayList) SQLFunction(org.hibernate.dialect.function.SQLFunction)

Example 14 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method locateAppropriateDialectFunctionNameForAliasTest.

@SuppressWarnings({ "ForLoopReplaceableByForEach" })
private String locateAppropriateDialectFunctionNameForAliasTest() {
    for (Iterator itr = getDialect().getFunctions().entrySet().iterator(); itr.hasNext(); ) {
        final Map.Entry entry = (Map.Entry) itr.next();
        final SQLFunction function = (SQLFunction) entry.getValue();
        if (!function.hasArguments() && !function.hasParenthesesIfNoArguments()) {
            return (String) entry.getKey();
        }
    }
    return null;
}
Also used : Iterator(java.util.Iterator) SQLFunction(org.hibernate.dialect.function.SQLFunction) Map(java.util.Map)

Example 15 with SqlFunction

use of org.hibernate.dialect.function.SqlFunction in project hibernate-orm by hibernate.

the class IdentNode method getDataType.

@Override
public Type getDataType() {
    Type type = super.getDataType();
    if (type != null) {
        return type;
    }
    FromElement fe = getFromElement();
    if (fe != null) {
        return fe.getDataType();
    }
    SQLFunction sf = getWalker().getSessionFactoryHelper().findSQLFunction(getText());
    if (sf != null) {
        return sf.getReturnType(null, getWalker().getSessionFactoryHelper().getFactory());
    }
    return null;
}
Also used : JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) SQLFunction(org.hibernate.dialect.function.SQLFunction)

Aggregations

SQLFunction (org.hibernate.dialect.function.SQLFunction)18 Test (org.junit.Test)7 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Session (org.hibernate.Session)4 Type (org.hibernate.type.Type)4 Map (java.util.Map)3 Transaction (org.hibernate.Transaction)3 CommonAST (antlr.CommonAST)2 AST (antlr.collections.AST)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Mapping (org.hibernate.engine.spi.Mapping)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 FunctionNode (org.hibernate.hql.internal.ast.tree.FunctionNode)2 Component (org.hibernate.mapping.Component)2 Formula (org.hibernate.mapping.Formula)2 TestForIssue (org.hibernate.testing.TestForIssue)2 HibernateException (org.hibernate.HibernateException)1 SQLQuery (org.hibernate.SQLQuery)1