Search in sources :

Example 1 with SqmTrimSpecification

use of org.hibernate.query.sqm.tree.expression.SqmTrimSpecification in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitTrimFunction.

@Override
public SqmExpression<?> visitTrimFunction(HqlParser.TrimFunctionContext ctx) {
    final SqmExpression<?> source = (SqmExpression<?>) ctx.getChild(ctx.getChildCount() - 2).accept(this);
    final SqmTrimSpecification trimSpec;
    final SqmLiteral<Character> trimChar;
    int index = 2;
    ParseTree parseTree = ctx.getChild(index);
    if (parseTree instanceof HqlParser.TrimSpecificationContext) {
        trimSpec = visitTrimSpecification((HqlParser.TrimSpecificationContext) parseTree);
        index = 3;
    } else {
        trimSpec = visitTrimSpecification(null);
    }
    parseTree = ctx.getChild(index);
    if (parseTree instanceof HqlParser.TrimCharacterContext) {
        trimChar = visitTrimCharacter((HqlParser.TrimCharacterContext) parseTree);
    } else {
        trimChar = visitTrimCharacter(null);
    }
    return getFunctionDescriptor("trim").generateSqmExpression(asList(trimSpec, trimChar, source), null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with SqmTrimSpecification

use of org.hibernate.query.sqm.tree.expression.SqmTrimSpecification in project hibernate-orm by hibernate.

the class SqmCriteriaNodeBuilder method createTrimNode.

private SqmFunction<String> createTrimNode(TrimSpec trimSpecification, SqmExpression<Character> trimCharacter, SqmExpression<String> source) {
    if (trimSpecification == null) {
        trimSpecification = TrimSpec.BOTH;
    }
    if (trimCharacter == null) {
        trimCharacter = new SqmLiteral<>(' ', getTypeConfiguration().standardBasicTypeForJavaType(Character.class), this);
    }
    final ArrayList<SqmTypedNode<?>> arguments = new ArrayList<>(3);
    arguments.add(new SqmTrimSpecification(trimSpecification, this));
    arguments.add(trimCharacter);
    arguments.add(source);
    return getFunctionDescriptor("trim").generateSqmExpression(arguments, null, getQueryEngine(), getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) ArrayList(java.util.ArrayList)

Example 3 with SqmTrimSpecification

use of org.hibernate.query.sqm.tree.expression.SqmTrimSpecification in project hibernate-orm by hibernate.

the class LpadRpadPadEmulation method generateSqmFunctionExpression.

@Override
protected <T> SelfRenderingSqmFunction<T> generateSqmFunctionExpression(List<? extends SqmTypedNode<?>> arguments, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    SqmTrimSpecification padSpec = (SqmTrimSpecification) arguments.get(2);
    String padName = padSpec.getSpecification() == TrimSpec.LEADING ? "lpad" : "rpad";
    return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor(padName).generateSqmExpression(arguments.size() > 3 ? asList(arguments.get(0), arguments.get(1), arguments.get(3)) : asList(arguments.get(0), arguments.get(1)), impliedResultType, queryEngine, typeConfiguration);
}
Also used : SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification)

Example 4 with SqmTrimSpecification

use of org.hibernate.query.sqm.tree.expression.SqmTrimSpecification in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitPadFunction.

@Override
public SqmExpression<?> visitPadFunction(HqlParser.PadFunctionContext ctx) {
    final SqmExpression<?> source = (SqmExpression<?>) ctx.getChild(2).accept(this);
    final SqmExpression<?> length = (SqmExpression<?>) ctx.getChild(4).accept(this);
    final SqmTrimSpecification padSpec = visitPadSpecification((HqlParser.PadSpecificationContext) ctx.getChild(5));
    final SqmLiteral<Character> padChar;
    if (ctx.getChildCount() == 8) {
        padChar = visitPadCharacter((HqlParser.PadCharacterContext) ctx.getChild(6));
    } else {
        padChar = null;
    }
    return getFunctionDescriptor("pad").generateSqmExpression(padChar != null ? asList(source, length, padSpec, padChar) : asList(source, length, padSpec), null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) HqlParser(org.hibernate.grammars.hql.HqlParser)

Example 5 with SqmTrimSpecification

use of org.hibernate.query.sqm.tree.expression.SqmTrimSpecification in project hibernate-orm by hibernate.

the class ArgumentTypesValidator method validate.

/**
 * We do an initial validation phase with just the SQM tree, even though we don't
 * have all typing information available here (in particular, we don't have the
 * final JDBC type codes for things with converters) because this is the phase
 * that is run at startup for named queries, and can be done in an IDE.
 */
@Override
public void validate(List<? extends SqmTypedNode<?>> arguments, String functionName, QueryEngine queryEngine) {
    delegate.validate(arguments, functionName, queryEngine);
    int count = 0;
    for (SqmTypedNode<?> argument : arguments) {
        JdbcTypeIndicators indicators = queryEngine.getTypeConfiguration().getCurrentBaseSqlTypeIndicators();
        SqmExpressible<?> nodeType = argument.getNodeType();
        FunctionParameterType type = count < types.length ? types[count++] : types[types.length - 1];
        if (nodeType != null) {
            JavaType<?> javaType = nodeType.getExpressibleJavaType();
            if (javaType != null) {
                try {
                    JdbcType jdbcType = javaType.getRecommendedJdbcType(indicators);
                    checkType(count, functionName, type, jdbcType.getJdbcTypeCode(), javaType.getJavaTypeClass());
                } catch (JdbcTypeRecommendationException e) {
                // it's a converter or something like that, and we will check it later
                }
            }
            switch(type) {
                case TEMPORAL_UNIT:
                    if (!(argument instanceof SqmExtractUnit) && !(argument instanceof SqmDurationUnit)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
                // something crazy by the parser
                case TRIM_SPEC:
                    if (!(argument instanceof SqmTrimSpecification)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
                case COLLATION:
                    if (!(argument instanceof SqmCollation)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
            }
        } else {
        // TODO: appropriate error?
        }
    }
}
Also used : JdbcTypeRecommendationException(org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException) JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) SqmExtractUnit(org.hibernate.query.sqm.tree.expression.SqmExtractUnit) SqmDurationUnit(org.hibernate.query.sqm.tree.expression.SqmDurationUnit) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) SqmCollation(org.hibernate.query.sqm.tree.expression.SqmCollation)

Aggregations

SqmTrimSpecification (org.hibernate.query.sqm.tree.expression.SqmTrimSpecification)5 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)1 SqmCollation (org.hibernate.query.sqm.tree.expression.SqmCollation)1 SqmDurationUnit (org.hibernate.query.sqm.tree.expression.SqmDurationUnit)1 SqmExtractUnit (org.hibernate.query.sqm.tree.expression.SqmExtractUnit)1 JdbcTypeRecommendationException (org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException)1 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)1 JdbcTypeIndicators (org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators)1