Search in sources :

Example 1 with SqlFunction

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

the class InformixDialectTestCase method testCurrentTimestampFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentTimestampFunction() {
    Map<String, SQLFunction> functions = dialect.getFunctions();
    SQLFunction sqlFunction = functions.get("current_timestamp");
    Type firstArgumentType = null;
    Mapping mapping = null;
    assertEquals(StandardBasicTypes.TIMESTAMP, sqlFunction.getReturnType(firstArgumentType, mapping));
    firstArgumentType = null;
    List arguments = Collections.emptyList();
    SessionFactoryImplementor factory = null;
    assertEquals("current", sqlFunction.render(firstArgumentType, arguments, factory));
}
Also used : Type(org.hibernate.type.Type) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Mapping(org.hibernate.engine.spi.Mapping) List(java.util.List) SQLFunction(org.hibernate.dialect.function.SQLFunction) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with SqlFunction

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

the class InformixDialectTestCase method testCurrentDateFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentDateFunction() {
    Map<String, SQLFunction> functions = dialect.getFunctions();
    SQLFunction sqlFunction = functions.get("current_date");
    Type firstArgumentType = null;
    Mapping mapping = null;
    assertEquals(StandardBasicTypes.DATE, sqlFunction.getReturnType(firstArgumentType, mapping));
    firstArgumentType = null;
    List arguments = Collections.emptyList();
    SessionFactoryImplementor factory = null;
    assertEquals("today", sqlFunction.render(firstArgumentType, arguments, factory));
}
Also used : Type(org.hibernate.type.Type) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Mapping(org.hibernate.engine.spi.Mapping) List(java.util.List) SQLFunction(org.hibernate.dialect.function.SQLFunction) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with SqlFunction

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

the class CompositeElementTest method afterMetadataBuilt.

@Override
protected void afterMetadataBuilt(Metadata metadata) {
    Collection children = metadata.getCollectionBinding(Parent.class.getName() + ".children");
    Component childComponents = (Component) children.getElement();
    Formula f = (Formula) childComponents.getProperty("bioLength").getValue().getColumnIterator().next();
    SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("length");
    if (lengthFunction != null) {
        ArrayList args = new ArrayList();
        args.add("bio");
        f.setFormula(lengthFunction.render(StandardBasicTypes.INTEGER, args, null));
    }
}
Also used : Formula(org.hibernate.mapping.Formula) ArrayList(java.util.ArrayList) Collection(org.hibernate.mapping.Collection) SQLFunction(org.hibernate.dialect.function.SQLFunction) Component(org.hibernate.mapping.Component)

Example 4 with SqlFunction

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

the class FunctionNameAsColumnTest method testGetMultiColumnSameNameAsNoArgFunctionCriteria.

@Test
public void testGetMultiColumnSameNameAsNoArgFunctionCriteria() {
    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();
    holder1 = (EntityWithFunctionAsColumnHolder) s.createCriteria(EntityWithFunctionAsColumnHolder.class).add(Restrictions.isNotNull("nextHolder")).setFetchMode("entityWithNoArgFunctionAsColumns", FetchMode.JOIN).setFetchMode("nextHolder", FetchMode.JOIN).setFetchMode("nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN).uniqueResult();
    assertTrue(Hibernate.isInitialized(holder1.getEntityWithNoArgFunctionAsColumns()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder()));
    assertTrue(Hibernate.isInitialized(holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns()));
    assertEquals(1, holder1.getEntityWithNoArgFunctionAsColumns().size());
    e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("blah blah blah", e1.getCurrentDate());
    assertEquals(1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size());
    e2 = (EntityWithNoArgFunctionAsColumn) (holder1.getNextHolder()).getEntityWithNoArgFunctionAsColumns().iterator().next();
    assertEquals("yadda yadda yadda", e2.getCurrentDate());
    t.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) SQLFunction(org.hibernate.dialect.function.SQLFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with SqlFunction

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

the class HQLTest method testExpressionInFunction.

@SuppressWarnings({ "unchecked" })
@Test
public void testExpressionInFunction() throws Exception {
    assertTranslation("from Animal an where an.bodyWeight > abs(3-5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3/5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3+5)");
    assertTranslation("from Animal an where an.bodyWeight > abs(3*5)");
    SQLFunction concat = sessionFactory().getSqlFunctionRegistry().findSQLFunction("concat");
    List list = new ArrayList();
    list.add("'fat'");
    list.add("'skinny'");
    assertTranslation("from Animal an where an.description = " + concat.render(StringType.INSTANCE, list, sessionFactory()));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SQLFunction(org.hibernate.dialect.function.SQLFunction) Test(org.junit.Test)

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