Search in sources :

Example 6 with StringLiteral

use of com.blazebit.persistence.parser.expression.StringLiteral in project blaze-persistence by Blazebit.

the class TestLiterals method testCharacterLiteralWithBackslash.

@Test
public void testCharacterLiteralWithBackslash() {
    StringLiteral result = (StringLiteral) parse("'\\'");
    assertEquals(new StringLiteral("\\"), result);
}
Also used : StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) Test(org.junit.Test)

Example 7 with StringLiteral

use of com.blazebit.persistence.parser.expression.StringLiteral in project blaze-persistence by Blazebit.

the class BaseFinalSetOperationBuilderImpl method asExpression.

public Expression asExpression(boolean externalRepresentation, boolean quantifiedPredicate) {
    SetOperationManager operationManager = setOperationManager;
    if (operationManager.getOperator() == null || !operationManager.hasSetOperations()) {
        return asExpression(operationManager.getStartQueryBuilder(), externalRepresentation, quantifiedPredicate);
    }
    List<Expression> setOperationArgs = new ArrayList<Expression>(operationManager.getSetOperations().size() + 2);
    // Use prefix because hibernate uses UNION as keyword
    setOperationArgs.add(new StringLiteral("SET_" + operationManager.getOperator().name()));
    setOperationArgs.add(asExpression(operationManager.getStartQueryBuilder(), externalRepresentation, quantifiedPredicate));
    List<AbstractCommonQueryBuilder<?, ?, ?, ?, ?>> setOperands = operationManager.getSetOperations();
    int operandsSize = setOperands.size();
    for (int i = 0; i < operandsSize; i++) {
        setOperationArgs.add(asExpression(setOperands.get(i), externalRepresentation, quantifiedPredicate));
    }
    List<? extends OrderByElement> orderByElements = getOrderByElements();
    if (orderByElements.size() > 0) {
        setOperationArgs.add(new StringLiteral("ORDER_BY"));
        int orderByElementsSize = orderByElements.size();
        for (int i = 0; i < orderByElementsSize; i++) {
            setOperationArgs.add(new StringLiteral(orderByElements.get(i).toString()));
        }
    }
    if (hasLimit()) {
        if (maxResults != Integer.MAX_VALUE) {
            setOperationArgs.add(new StringLiteral("LIMIT"));
            setOperationArgs.add(new NumericLiteral(Integer.toString(maxResults), NumericType.INTEGER));
        }
        if (firstResult != 0) {
            setOperationArgs.add(new StringLiteral("OFFSET"));
            setOperationArgs.add(new NumericLiteral(Integer.toString(firstResult), NumericType.INTEGER));
        }
    }
    Expression expression = new FunctionExpression("FUNCTION", setOperationArgs);
    if (quantifiedPredicate && hasLimit() && !mainQuery.dbmsDialect.supportsLimitInQuantifiedPredicateSubquery()) {
        List<Expression> arguments = new ArrayList<>(2);
        arguments.add(new StringLiteral(QueryWrapperFunction.FUNCTION_NAME));
        arguments.add(expression);
        expression = new FunctionExpression("FUNCTION", arguments);
    }
    return expression;
}
Also used : FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) NumericLiteral(com.blazebit.persistence.parser.expression.NumericLiteral) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayList(java.util.ArrayList)

Example 8 with StringLiteral

use of com.blazebit.persistence.parser.expression.StringLiteral in project blaze-persistence by Blazebit.

the class SizeTransformationVisitor method wrapSubqueryConditionally.

private Expression wrapSubqueryConditionally(SubqueryExpression subquery, boolean wrap) {
    if (wrap) {
        // we need to wrap subqueries in aggregate functions in COALESCE to trick the Hibernate parser
        // see https://hibernate.atlassian.net/browse/HHH-9331
        List<Expression> subqueryFunctionArguments = new ArrayList<>(1);
        subqueryFunctionArguments.add(new StringLiteral(SubqueryFunction.FUNCTION_NAME));
        subqueryFunctionArguments.add(subquery);
        return new FunctionExpression("FUNCTION", subqueryFunctionArguments);
    } else {
        return subquery;
    }
}
Also used : FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) ListIndexExpression(com.blazebit.persistence.parser.expression.ListIndexExpression) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) AggregateExpression(com.blazebit.persistence.parser.expression.AggregateExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) ResolvedExpression(com.blazebit.persistence.impl.ResolvedExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayList(java.util.ArrayList)

Example 9 with StringLiteral

use of com.blazebit.persistence.parser.expression.StringLiteral in project blaze-persistence by Blazebit.

the class ScalarTargetResolvingExpressionVisitor method visit.

@Override
public void visit(FunctionExpression expression) {
    String name = expression.getFunctionName().toLowerCase();
    switch(name) {
        case "function":
            String functionName = ((StringLiteral) expression.getExpressions().get(0)).getValue();
            JpqlFunction jpqlFunction = functions.get(functionName.toLowerCase());
            if (jpqlFunction == null) {
                // Can't reliably resolve the type
                currentPosition.setAttribute(null);
                currentPosition.setCurrentType(null);
            } else {
                // Skip the function name
                resolveFirst(expression.getExpressions().subList(1, expression.getExpressions().size()), true);
                resolveToFunctionReturnType(functionName);
            }
            break;
        case "size":
            // According to our grammar, we can only get a path here
            currentPosition.setAttribute(null);
            currentPosition.setCurrentType(metamodel.type(Long.class));
            break;
        case "coalesce":
            resolveAny(expression.getExpressions(), true);
            resolveToFunctionReturnType(name);
            break;
        default:
            resolveFirst(expression.getExpressions(), true);
            resolveToFunctionReturnType(name);
            break;
    }
}
Also used : JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral)

Example 10 with StringLiteral

use of com.blazebit.persistence.parser.expression.StringLiteral in project blaze-persistence by Blazebit.

the class TestLiterals method testStringLiteral.

@Test
public void testStringLiteral() {
    StringLiteral result = (StringLiteral) parse("'abcd'");
    assertEquals(new StringLiteral("abcd"), result);
}
Also used : StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) Test(org.junit.Test)

Aggregations

StringLiteral (com.blazebit.persistence.parser.expression.StringLiteral)16 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)11 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)11 Expression (com.blazebit.persistence.parser.expression.Expression)10 ArrayList (java.util.ArrayList)10 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)9 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)7 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)7 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)5 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)4 NumericLiteral (com.blazebit.persistence.parser.expression.NumericLiteral)4 Test (org.junit.Test)4 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)3 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)3 Subquery (com.blazebit.persistence.parser.expression.Subquery)3 ResolvedExpression (com.blazebit.persistence.impl.ResolvedExpression)2 AggregateExpression (com.blazebit.persistence.parser.expression.AggregateExpression)2 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)2 MapEntryExpression (com.blazebit.persistence.parser.expression.MapEntryExpression)2 MapValueExpression (com.blazebit.persistence.parser.expression.MapValueExpression)2