use of org.eclipse.persistence.jpa.jpql.parser.CollectionExpression in project eclipselink by eclipse-ee4j.
the class ResolverBuilder method visit.
@Override
public void visit(SelectClause expression) {
Expression selectExpression = expression.getSelectExpression();
// visit(CollectionExpression) iterates through the children but for a
// SELECT clause, a CollectionExpression means the result type is Object[]
CollectionExpression collectionExpression = getCollectionExpression(selectExpression);
if (collectionExpression != null) {
resolver = buildClassResolver(Object[].class);
} else {
selectExpression.accept(this);
}
}
use of org.eclipse.persistence.jpa.jpql.parser.CollectionExpression in project eclipselink by eclipse-ee4j.
the class AbstractGrammarValidator method visit.
@Override
public void visit(InExpression expression) {
// Missing expression
if (!expression.hasExpression()) {
int startPosition = position(expression);
addProblem(expression, startPosition, InExpression_MissingExpression);
} else {
Expression leftExpression = expression.getExpression();
JPQLQueryBNF queryBNF = getQueryBNF(expression.getExpressionExpressionQueryBNFId());
// First check for nested array support
if (isNestedArray(leftExpression)) {
if (!queryBNF.handlesNestedArray()) {
int startPosition = position(expression);
int endPosition = startPosition + length(leftExpression);
addProblem(expression, startPosition, endPosition, InExpression_InvalidExpression);
} else {
leftExpression.accept(this);
}
} else // Validate the expression
if (!isValid(leftExpression, queryBNF) || isDateTimeConstant(leftExpression)) {
int startPosition = position(expression);
int endPosition = startPosition + length(leftExpression);
addProblem(expression, startPosition, endPosition, InExpression_InvalidExpression);
} else {
leftExpression.accept(this);
}
}
// Check for "IN :input_parameter" defined in JPA 2.0
boolean singleInputParameter = isNewerThanOrEqual(JPAVersion.VERSION_2_0) && expression.isSingleInputParameter();
// Missing '('
if (!expression.hasLeftParenthesis() && !singleInputParameter) {
int startPosition = position(expression) + length(expression.getExpression()) + (expression.hasExpression() ? 1 : 0) + (expression.hasNot() ? 4 : /* NOT + whitespace */
0) + 2;
addProblem(expression, startPosition, InExpression_MissingLeftParenthesis);
} else // defines the set of values for the IN expression.
if (!expression.hasInItems()) {
int startPosition = position(expression) + length(expression.getExpression()) + (expression.hasExpression() ? 1 : 0) + (expression.hasNot() ? 4 : /* NOT + whitespace */
0) + 2 + /* IN */
(expression.hasSpaceAfterIn() ? 1 : 0) + (expression.hasLeftParenthesis() ? 1 : 0);
addProblem(expression, startPosition, InExpression_MissingInItems);
} else // Make sure the IN items are separated by commas
if (!singleInputParameter) {
Expression inItems = expression.getInItems();
CollectionExpression collectionExpression = getCollectionExpression(inItems);
// Validate the collection of items
if (collectionExpression != null) {
validateCollectionSeparatedByComma(inItems, InExpression_ItemEndsWithComma, InExpression_ItemIsMissingComma);
// Validate each item
JPQLQueryBNF queryBNF = getQueryBNF(expression.getExpressionItemQueryBNFId());
int index = 0;
for (Expression child : collectionExpression.children()) {
index++;
// First check for nested array support
if (isNestedArray(child)) {
if (!queryBNF.handlesNestedArray()) {
addProblem(child, InExpression_ItemInvalidExpression, String.valueOf(index));
} else {
child.accept(this);
}
} else // Invalid item
if (!isValid(child, queryBNF)) {
addProblem(child, InExpression_ItemInvalidExpression, String.valueOf(index));
} else // Validate the item
{
child.accept(this);
}
}
} else // The single item is invalid
if (!isValid(inItems, expression.getExpressionItemQueryBNFId())) {
addProblem(inItems, InExpression_ItemInvalidExpression);
} else // Validate the single item
{
inItems.accept(this);
}
}
// Missing ')'
if (!singleInputParameter && expression.hasInItems() && !expression.hasRightParenthesis()) {
int startPosition = position(expression) + length(expression.getExpression()) + (expression.hasExpression() ? 1 : 0) + (expression.hasNot() ? 4 : /* NOT + whitespace */
0) + 2 + /* IN */
(expression.hasSpaceAfterIn() ? 1 : 0) + (expression.hasLeftParenthesis() ? 1 : 0) + length(expression.getInItems());
addProblem(expression, startPosition, InExpression_MissingRightParenthesis);
}
}
use of org.eclipse.persistence.jpa.jpql.parser.CollectionExpression in project eclipselink by eclipse-ee4j.
the class JPQLExpressionTest1_0 method testGetExpression_2.
@Test
public void testGetExpression_2() {
String query = "SELECT OBJECT(e), COUNT(DISTINCT e) FROM Employee e";
JPQLExpression jpqlExpression = JPQLQueryBuilder.buildQuery(query, getGrammar(), true);
Expression expression = jpqlExpression.getExpression(query, 0);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof JPQLExpression);
expression = jpqlExpression.getExpression(query, 14);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof ObjectExpression);
// In 'SELECT'
for (int index = 1; index < 8; index++) {
expression = jpqlExpression.getExpression(query, index);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof SelectClause);
}
// In 'OBJECT('
for (int index = 8; index < 15; index++) {
expression = jpqlExpression.getExpression(query, index);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof ObjectExpression);
}
// In 'e' of 'OBJECT(e)'
expression = jpqlExpression.getExpression(query, 15);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof IdentificationVariable);
expression = jpqlExpression.getExpression(query, 17);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof CollectionExpression);
expression = jpqlExpression.getExpression(query, 18);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof CollectionExpression);
expression = jpqlExpression.getExpression(query, 24);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof CountFunction);
expression = jpqlExpression.getExpression(query, 35);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof CountFunction);
expression = jpqlExpression.getExpression(query, 36);
assertNotNull(expression);
assertTrue("The expression was " + expression.getClass().getSimpleName(), expression instanceof SelectStatement);
}
use of org.eclipse.persistence.jpa.jpql.parser.CollectionExpression in project eclipselink by eclipse-ee4j.
the class ResolverBuilder method visitCollectionEquivalentExpression.
/**
* Visits the given {@link Expression} and creates a {@link Resolver} that will check the type
* for each of its children. If the type is the same, then it's the {@link Expression}'s type;
* otherwise the type will be {@link Object}.
*
* @param expression The {@link Expression} to calculate the type of its children
* @param extraExpression This {@link Expression} will be resolved, if it's not <code>null</code>
* and its type will be added to the collection of types
*/
protected void visitCollectionEquivalentExpression(Expression expression, Expression extraExpression) {
List<Resolver> resolvers = new ArrayList<>();
CollectionExpression collectionExpression = getCollectionExpression(expression);
// Gather the resolver for all children
if (collectionExpression != null) {
for (Expression child : collectionExpression.children()) {
child.accept(this);
resolvers.add(resolver);
}
} else // Otherwise visit the actual expression
{
expression.accept(this);
resolvers.add(resolver);
}
// Add the resolver for the other expression
if (extraExpression != null) {
extraExpression.accept(this);
resolvers.add(resolver);
}
DeclarationResolver parent = getDeclarationResolver(expression);
resolver = new CollectionEquivalentResolver(parent, resolvers);
}
use of org.eclipse.persistence.jpa.jpql.parser.CollectionExpression in project eclipselink by eclipse-ee4j.
the class AbstractGrammarValidator method visit.
@Override
public void visit(DeleteClause expression) {
// Missing FROM
if (!expression.hasFrom()) {
int startPosition = 6 + /* DELETE */
(expression.hasSpaceAfterDelete() ? 1 : 0);
addProblem(expression, startPosition, DeleteClause_FromMissing);
} else // Missing range variable declaration
if (!expression.hasRangeVariableDeclaration()) {
// The whitespace is added to the position regardless if it was parsed or not
int startPosition = 12;
addProblem(expression, startPosition, DeleteClause_RangeVariableDeclarationMissing);
}
// Validate range variable declaration
if (expression.hasRangeVariableDeclaration()) {
// More than one entity abstract schema type is declared
CollectionExpression collectionExpression = getCollectionExpression(expression.getRangeVariableDeclaration());
if (collectionExpression != null) {
Expression firstChild = collectionExpression.getChild(0);
int startPosition = position(firstChild) + length(firstChild);
int endPosition = position(collectionExpression) + length(collectionExpression);
boolean malformed = false;
for (int index = collectionExpression.childrenSize() - 1; --index >= 0; ) {
if (!collectionExpression.hasComma(index)) {
malformed = true;
}
}
if (collectionExpression.toActualText().endsWith(" ")) {
endPosition--;
}
addProblem(expression, startPosition, endPosition, malformed ? DeleteClause_RangeVariableDeclarationMalformed : DeleteClause_MultipleRangeVariableDeclaration);
} else {
super.visit(expression);
}
}
}
Aggregations