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