Search in sources :

Example 1 with SqlAppender

use of org.hibernate.sql.ast.spi.SqlAppender in project hibernate-orm by hibernate.

the class InformixDialectTestCase method testCurrentTimestampFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentTimestampFunction() {
    SqmFunctionDescriptor functionDescriptor = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("current_timestamp");
    SelfRenderingSqmFunction<Object> sqmExpression = functionDescriptor.generateSqmExpression(null, queryEngine, new TypeConfiguration());
    BasicType<?> basicType = (BasicType<?>) sqmExpression.getNodeType();
    assertEquals(JdbcTimestampJavaType.INSTANCE, basicType.getJavaTypeDescriptor());
    assertEquals(TimestampJdbcType.INSTANCE, basicType.getJdbcType());
    SqlAppender appender = new StringBuilderSqlAppender();
    sqmExpression.getRenderingSupport().render(appender, Collections.emptyList(), null);
    assertEquals("current", appender.toString());
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) BasicType(org.hibernate.type.BasicType) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with SqlAppender

use of org.hibernate.sql.ast.spi.SqlAppender in project hibernate-orm by hibernate.

the class AnsiTrimEmulationFunctionTest method render.

private String render(Dialect dialect, TrimFunction function, TrimSpec trimSpec, char trimCharacter, String trimSource) {
    SessionFactoryImplementor factory = Mockito.mock(SessionFactoryImplementor.class);
    JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
    Mockito.doReturn(jdbcServices).when(factory).getJdbcServices();
    Mockito.doReturn(dialect).when(jdbcServices).getDialect();
    StandardSqlAstTranslator<JdbcOperation> walker = new StandardSqlAstTranslator<>(factory, null);
    List<SqlAstNode> sqlAstArguments = new ArrayList<>();
    sqlAstArguments.add(new TrimSpecification(trimSpec));
    sqlAstArguments.add(new QueryLiteral<>(trimCharacter, new BasicTypeImpl<>(CharacterJavaType.INSTANCE, CharJdbcType.INSTANCE)));
    sqlAstArguments.add(new SelfRenderingExpression() {

        @Override
        public void renderToSql(SqlAppender sqlAppender, SqlAstTranslator<?> walker, SessionFactoryImplementor sessionFactory) {
            sqlAppender.appendSql(trimSource);
        }

        @Override
        public JdbcMappingContainer getExpressionType() {
            return null;
        }
    });
    function.render(walker, sqlAstArguments, walker);
    return walker.getSql();
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) JdbcOperation(org.hibernate.sql.exec.spi.JdbcOperation) ArrayList(java.util.ArrayList) TrimSpecification(org.hibernate.sql.ast.tree.expression.TrimSpecification) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) JdbcMappingContainer(org.hibernate.metamodel.mapping.JdbcMappingContainer) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) StandardSqlAstTranslator(org.hibernate.sql.ast.spi.StandardSqlAstTranslator)

Example 3 with SqlAppender

use of org.hibernate.sql.ast.spi.SqlAppender in project hibernate-orm by hibernate.

the class InformixDialectTestCase method testCurrentDateFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentDateFunction() {
    SqmFunctionDescriptor functionDescriptor = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("current_date");
    SelfRenderingSqmFunction<Object> sqmExpression = functionDescriptor.generateSqmExpression(null, queryEngine, new TypeConfiguration());
    BasicType<?> basicType = (BasicType<?>) sqmExpression.getNodeType();
    assertEquals(JdbcDateJavaType.INSTANCE, basicType.getJavaTypeDescriptor());
    assertEquals(DateJdbcType.INSTANCE, basicType.getJdbcType());
    SqlAppender appender = new StringBuilderSqlAppender();
    sqmExpression.getRenderingSupport().render(appender, Collections.emptyList(), null);
    assertEquals("today", appender.toString());
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) BasicType(org.hibernate.type.BasicType) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with SqlAppender

use of org.hibernate.sql.ast.spi.SqlAppender in project hibernate-orm by hibernate.

the class CaseStatementDiscriminatorMappingImpl method createCaseSearchedExpression.

private Expression createCaseSearchedExpression(TableGroup entityTableGroup) {
    return new SelfRenderingExpression() {

        CaseSearchedExpression caseSearchedExpression;

        @Override
        public void renderToSql(SqlAppender sqlAppender, SqlAstTranslator<?> walker, SessionFactoryImplementor sessionFactory) {
            if (caseSearchedExpression == null) {
                // todo (6.0): possible optimization is to omit cases for table reference joins, that touch a super class, where a subclass is inner joined due to pruning
                caseSearchedExpression = new CaseSearchedExpression(CaseStatementDiscriminatorMappingImpl.this);
                tableDiscriminatorDetailsMap.forEach((tableName, tableDiscriminatorDetails) -> {
                    final TableReference tableReference = entityTableGroup.getTableReference(entityTableGroup.getNavigablePath(), tableName, false, false);
                    if (tableReference == null) {
                        // assume this is because it is a table that is not part of the processing entity's sub-hierarchy
                        return;
                    }
                    final Predicate predicate = new NullnessPredicate(new ColumnReference(tableReference, tableDiscriminatorDetails.getCheckColumnName(), false, null, null, getJdbcMapping(), getSessionFactory()), true);
                    caseSearchedExpression.when(predicate, new QueryLiteral<>(tableDiscriminatorDetails.getDiscriminatorValue(), getUnderlyingJdbcMappingType()));
                });
            }
            caseSearchedExpression.accept(walker);
        }

        @Override
        public JdbcMappingContainer getExpressionType() {
            return CaseStatementDiscriminatorMappingImpl.this;
        }
    };
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlAstTranslator(org.hibernate.sql.ast.SqlAstTranslator) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

SqlAppender (org.hibernate.sql.ast.spi.SqlAppender)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)2 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)2 TestForIssue (org.hibernate.testing.TestForIssue)2 BasicType (org.hibernate.type.BasicType)2 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)1 SqlAstTranslator (org.hibernate.sql.ast.SqlAstTranslator)1 StandardSqlAstTranslator (org.hibernate.sql.ast.spi.StandardSqlAstTranslator)1 SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 TrimSpecification (org.hibernate.sql.ast.tree.expression.TrimSpecification)1 TableReference (org.hibernate.sql.ast.tree.from.TableReference)1 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)1 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)1