Search in sources :

Example 6 with ArrayExpression

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

the class JoinManager method getJoinAlias.

private String getJoinAlias(ArrayExpression expr) {
    StringBuilder sb = new StringBuilder(expr.getBase().toString());
    Expression indexExpr = expr.getIndex();
    if (indexExpr instanceof ParameterExpression) {
        ParameterExpression indexParamExpr = (ParameterExpression) indexExpr;
        sb.append('_');
        sb.append(indexParamExpr.getName());
    } else if (indexExpr instanceof NumericLiteral) {
        sb.append('_');
        sb.append(((NumericLiteral) indexExpr).getValue());
    } else if (indexExpr instanceof StringLiteral) {
        sb.append('_');
        sb.append(((StringLiteral) indexExpr).getValue());
    } else {
        sb.append('_');
        String indexStringExpr = indexExpr.toString();
        for (int i = 0; i < indexStringExpr.length(); i++) {
            final char c = indexStringExpr.charAt(i);
            if (Character.isJavaIdentifierPart(c)) {
                sb.append(c);
            } else {
                sb.append('_');
            }
        }
    }
    return sb.toString();
}
Also used : NumericLiteral(com.blazebit.persistence.parser.expression.NumericLiteral) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) ListIndexExpression(com.blazebit.persistence.parser.expression.ListIndexExpression) Expression(com.blazebit.persistence.parser.expression.Expression) QualifiedExpression(com.blazebit.persistence.parser.expression.QualifiedExpression) TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) MapEntryExpression(com.blazebit.persistence.parser.expression.MapEntryExpression) MapValueExpression(com.blazebit.persistence.parser.expression.MapValueExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression)

Example 7 with ArrayExpression

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

the class JpqlMacroAwareExpressionFactory method createJoinPathExpression.

@Override
public Expression createJoinPathExpression(String expression, MacroConfiguration macroConfiguration, Set<String> usedMacros) {
    Expression pathExpression = createPathExpression(expression, macroConfiguration, usedMacros);
    if (pathExpression instanceof PathExpression) {
        List<PathElementExpression> expressions = ((PathExpression) pathExpression).getExpressions();
        PathElementExpression first;
        if (expressions.size() > 1 || (first = expressions.get(0)) instanceof PropertyExpression || first instanceof ArrayExpression) {
            return pathExpression;
        }
        return first;
    }
    return pathExpression;
}
Also used : PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 8 with ArrayExpression

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

the class AbstractParserTest method array.

protected ArrayExpression array(String expr) {
    int firstIndex = expr.indexOf('[');
    int lastIndex = expr.indexOf(']');
    String base = expr.substring(0, firstIndex);
    String index = expr.substring(firstIndex + 1, lastIndex);
    Expression indexExpr;
    /**
     * TODO: change this to not use parse here - we actually do not want to rely on parsing for constructing the
     * comparison expressions
     */
    indexExpr = parse(index);
    return new ArrayExpression(new PropertyExpression(base), indexExpr);
}
Also used : Expression(com.blazebit.persistence.parser.expression.Expression) TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) ArithmeticExpression(com.blazebit.persistence.parser.expression.ArithmeticExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) TypeFunctionExpression(com.blazebit.persistence.parser.expression.TypeFunctionExpression) MapEntryExpression(com.blazebit.persistence.parser.expression.MapEntryExpression) AggregateExpression(com.blazebit.persistence.parser.expression.AggregateExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) MapValueExpression(com.blazebit.persistence.parser.expression.MapValueExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 9 with ArrayExpression

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

the class SimpleCachingExpressionFactoryTest method testCreateSimpleExpressionCacheWithMacros.

@Test
public void testCreateSimpleExpressionCacheWithMacros() {
    ExpressionFactory ef = new SimpleCachingExpressionFactory(new ExpressionFactoryImpl(new HashMap<String, FunctionKind>(), true, true));
    MacroConfiguration macroConfiguration = MacroConfiguration.of(Collections.singletonMap("my_macro", (MacroFunction) new MacroFunction() {

        @Override
        public Expression apply(List<Expression> expressions) {
            PathExpression p;
            if (expressions.get(0) instanceof PathExpression) {
                p = (PathExpression) expressions.get(0);
            } else {
                p = new PathExpression(new ArrayList<>(Arrays.asList((PathElementExpression) expressions.get(0))));
            }
            p.getExpressions().add(new ArrayExpression(new PropertyExpression("lsls"), new PathExpression(Arrays.<PathElementExpression>asList(new PropertyExpression("a"), new PropertyExpression("b"), new PropertyExpression("c"), new PropertyExpression("d"), new PropertyExpression("e")))));
            return p;
        }

        @Override
        public Object[] getState() {
            return new Object[0];
        }

        @Override
        public boolean supportsCaching() {
            return true;
        }

        @Override
        public int hashCode() {
            return getClass().hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return obj.getClass() == getClass();
        }
    }));
    String expressionString = "SIZE(my_macro(Hello.world[:hahaha].criteria[1].api)) + SIZE(my_macro(Hello.world[:hahaha].criteria[1].api))";
    Expression expr1 = ef.createSimpleExpression(expressionString, false, true, false, macroConfiguration, null);
    Expression expr2 = ef.createSimpleExpression(expressionString, false, true, false, macroConfiguration, null);
    Assert.assertFalse(expr1 == expr2);
    Assert.assertEquals(expr1, expr2);
}
Also used : ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) HashMap(java.util.HashMap) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) ExpressionFactoryImpl(com.blazebit.persistence.parser.expression.ExpressionFactoryImpl) MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Expression(com.blazebit.persistence.parser.expression.Expression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Test(org.junit.Test)

Example 10 with ArrayExpression

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

the class CollectionJoinMappingGathererExpressionVisitor method visit.

@Override
public void visit(PathExpression expression) {
    List<PathElementExpression> expressions = expression.getExpressions();
    int size = expressions.size();
    StringBuilder sb = new StringBuilder(size * 10);
    ManagedType<?> t = managedType;
    Attribute<?, ?> jpaAttribute = null;
    for (int i = 0; i < size; i++) {
        String baseName;
        Expression e = expressions.get(i);
        if (e instanceof ArrayExpression) {
            ArrayExpression arrayExpression = (ArrayExpression) e;
            arrayExpression.getIndex().accept(this);
            continue;
        } else {
            baseName = e.toString();
        }
        if (i != 0) {
            sb.append('.');
        }
        sb.append(e.toString());
        try {
            jpaAttribute = t.getAttribute(baseName);
        } catch (IllegalArgumentException ex) {
            // Ignore non existing attributes
            jpaAttribute = null;
        }
        // NOTE: Attribute could be null because this model might contain errors
        if (jpaAttribute != null) {
            t = metamodel.getManagedType(JpaMetamodelUtils.resolveFieldClass(t.getJavaType(), jpaAttribute));
            if (jpaAttribute instanceof PluralAttribute<?, ?, ?>) {
                paths.add(sb.toString());
            }
        }
    }
    if (jpaAttribute instanceof PluralAttribute<?, ?, ?>) {
        paths.add(sb.toString());
    }
}
Also used : PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Expression(com.blazebit.persistence.parser.expression.Expression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) PluralAttribute(javax.persistence.metamodel.PluralAttribute) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression)

Aggregations

ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)18 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)16 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)15 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)14 Expression (com.blazebit.persistence.parser.expression.Expression)13 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)11 TreatExpression (com.blazebit.persistence.parser.expression.TreatExpression)10 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)9 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)9 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)8 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)7 MapEntryExpression (com.blazebit.persistence.parser.expression.MapEntryExpression)7 MapValueExpression (com.blazebit.persistence.parser.expression.MapValueExpression)7 QualifiedExpression (com.blazebit.persistence.parser.expression.QualifiedExpression)7 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)6 ArrayList (java.util.ArrayList)4 EntityLiteral (com.blazebit.persistence.parser.expression.EntityLiteral)3 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)3 EntityType (javax.persistence.metamodel.EntityType)3 ManagedType (javax.persistence.metamodel.ManagedType)3