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