All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.persistence.jpa.jpql.parser.RangeVariableDeclaration Maven / Gradle / Ivy

There is a newer version: 5.0.0-B03
Show newest version
/*
 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation
//
package org.eclipse.persistence.jpa.jpql.parser;

import java.util.Collection;
import java.util.List;
import org.eclipse.persistence.jpa.jpql.ExpressionTools;
import org.eclipse.persistence.jpa.jpql.WordParser;

/**
 * Range variable declarations allow the developer to designate a "root" for objects which may not
 * be reachable by navigation. In order to select values by comparing more than one instance of an
 * entity abstract schema type, more than one identification variable ranging over the abstract
 * schema type is needed in the FROM clause.
 * 

* JPA: *

BNF: range_variable_declaration ::= abstract_schema_name [AS] identification_variable
*

* EclipseLink 2.4: *

BNF: range_variable_declaration ::= { root_object } [AS] identification_variable
* *
BNF: root_object ::= abstract_schema_name | (subquery)
* * @version 2.5 * @since 2.3 * @author Pascal Filion */ public final class RangeVariableDeclaration extends AbstractExpression { /** * The actual AS identifier found in the string representation of the JPQL query. */ private String asIdentifier; /** * Determines whether a whitespace was parsed after AS. */ private boolean hasSpaceAfterAs; /** * Determines whether a whitespace was parsed after the "root" object. */ private boolean hasSpaceAfterRootObject; /** * The {@link Expression} representing the identification variable. */ private AbstractExpression identificationVariable; /** * The {@link Expression} representing the "root" object. */ private AbstractExpression rootObject; /** * Determines whether the identification variable is virtual, meaning it's not part of the query * but is required for proper navigability. */ private boolean virtualIdentificationVariable; /** * Creates a new RangeVariableDeclaration. * * @param parent The parent of this expression */ public RangeVariableDeclaration(AbstractExpression parent) { super(parent); } /** * Creates a new RangeVariableDeclaration, which is used as a virtual declaration. * * @param entityName The name of the entity to be accessible with the given variable name * @param variableName The identification variable used to navigate to the entity */ public RangeVariableDeclaration(String entityName, String variableName) { super(null); rootObject = new AbstractSchemaName(this, entityName); identificationVariable = new IdentificationVariable(this, variableName); } /** * {@inheritDoc} */ public void accept(ExpressionVisitor visitor) { visitor.visit(this); } /** * {@inheritDoc} */ public void acceptChildren(ExpressionVisitor visitor) { getRootObject().accept(visitor); getIdentificationVariable().accept(visitor); } /** * {@inheritDoc} */ @Override protected void addChildrenTo(Collection children) { children.add(getRootObject()); children.add(getIdentificationVariable()); } /** * {@inheritDoc} */ @Override protected void addOrderedChildrenTo(List children) { // "Root" object if (rootObject != null) { children.add(rootObject); } // Space if (hasSpaceAfterRootObject) { children.add(buildStringExpression(SPACE)); } // 'AS' if (asIdentifier != null) { children.add(buildStringExpression(AS)); } if (hasSpaceAfterAs) { children.add(buildStringExpression(SPACE)); } // Identification variable if (identificationVariable != null) { children.add(identificationVariable); } } /** * {@inheritDoc} */ @Override public JPQLQueryBNF findQueryBNF(Expression expression) { if ((rootObject != null) && rootObject.isAncestor(expression)) { return getQueryBNF(RangeDeclarationBNF.ID); } if ((identificationVariable != null) && identificationVariable.isAncestor(expression)) { return getQueryBNF(IdentificationVariableBNF.ID); } return super.findQueryBNF(expression); } /** * Returns the actual AS found in the string representation of the JPQL query, which has * the actual case that was used. * * @return The AS identifier that was actually parsed, or an empty string if it was not * parsed */ public String getActualAsIdentifier() { return (asIdentifier != null) ? asIdentifier : ExpressionTools.EMPTY_STRING; } /** * Returns the {@link Expression} that represents the identification variable. * * @return The expression that was parsed representing the identification variable */ public Expression getIdentificationVariable() { if (identificationVariable == null) { identificationVariable = buildNullExpression(); } return identificationVariable; } /** * {@inheritDoc} */ public JPQLQueryBNF getQueryBNF() { return getQueryBNF(RangeVariableDeclarationBNF.ID); } /** * Returns the {@link Expression} that represents the "root" object. * * @return The expression that was parsed representing the "root" object */ public Expression getRootObject() { if (rootObject == null) { rootObject = buildNullExpression(); } return rootObject; } /** * Determines whether the identifier AS was parsed. * * @return true if the identifier AS was parsed; false otherwise */ public boolean hasAs() { return asIdentifier != null; } /** * Determines whether the identification variable was parsed. * * @return true if the identification variable was parsed; false otherwise */ public boolean hasIdentificationVariable() { return identificationVariable != null && !identificationVariable.isNull() && !identificationVariable.isVirtual(); } /** * Determines whether the "root" object was parsed. * * @return true if the "root" object was parsed; false otherwise */ public boolean hasRootObject() { return rootObject != null && !rootObject.isNull(); } /** * Determines whether a whitespace was parsed after AS. * * @return true if there was a whitespace after AS; false otherwise */ public boolean hasSpaceAfterAs() { return hasSpaceAfterAs; } /** * Determines whether a whitespace was parsed after the "root" object. * * @return true if there was a whitespace after "root" object; * false otherwise */ public boolean hasSpaceAfterRootObject() { return hasSpaceAfterRootObject; } /** * Determines whether this identification variable is virtual, meaning it's not part of the * query but is required for proper navigability. * * @return true if this identification variable was virtually created to fully * qualify path expression; false if it was parsed */ public boolean hasVirtualIdentificationVariable() { return virtualIdentificationVariable; } /** * {@inheritDoc} */ @Override protected boolean isParsingComplete(WordParser wordParser, String word, Expression expression) { return word.equalsIgnoreCase(AS) || word.equalsIgnoreCase(SET) || word.equalsIgnoreCase(INNER) || word.equalsIgnoreCase(JOIN) || word.equalsIgnoreCase(LEFT) || super.isParsingComplete(wordParser, word, expression); } /** * {@inheritDoc} */ @Override protected void parse(WordParser wordParser, boolean tolerant) { // Parse the "root" object rootObject = parse(wordParser, RangeDeclarationBNF.ID, tolerant); hasSpaceAfterRootObject = wordParser.skipLeadingWhitespace() > 0; // Parse 'AS' if (wordParser.startsWithIdentifier(AS)) { asIdentifier = wordParser.moveForward(AS); hasSpaceAfterAs = wordParser.skipLeadingWhitespace() > 0; } // Special case when parsing the range variable declaration of an UPDATE clause that does // not have an identification variable, e.g. "UPDATE DateTime SET date = CURRENT_DATE" if (!wordParser.startsWithIdentifier(SET)) { if (tolerant) { identificationVariable = parse(wordParser, IdentificationVariableBNF.ID, tolerant); } else { identificationVariable = new IdentificationVariable(this, wordParser.word()); identificationVariable.parse(wordParser, tolerant); } } } /** * Sets a virtual identification variable because the "root" object was parsed without one. This * is valid in an UPDATE and DELETE queries. Example: *

* UPDATE DateTime SET date = CURRENT_DATE *

* is equivalent to *

* UPDATE DateTime d SET d.date = CURRENT_DATE * * @param variableName A virtual identification variable that will identify the "root" object */ public void setVirtualIdentificationVariable(String variableName) { virtualIdentificationVariable = true; identificationVariable = new IdentificationVariable(this, variableName, true); } /** * Sets a virtual identification variable to qualify the "root" object. The "root" object is a * derived path that does not start with an identification variable. Example: *

* UPDATE Employee SET firstName = 'MODIFIED' * WHERE (SELECT COUNT(m) FROM managedEmployees m) {@literal >} 0 *

* 'managedEmployees' is a derived path and will become qualified with the given virtual * identification variable. * * @param variableName The identification variable that was generated to qualify the "root" object * @param path The path that was parsed as a "root" object */ public void setVirtualIdentificationVariable(String variableName, String path) { CollectionValuedPathExpression expression = new CollectionValuedPathExpression(this, path); expression.setVirtualIdentificationVariable(variableName); rootObject = expression; } /** * {@inheritDoc} */ @Override protected boolean shouldParseWithFactoryFirst() { return false; } /** * {@inheritDoc} */ @Override protected void toParsedText(StringBuilder writer, boolean actual) { // "Root" object if (rootObject != null) { rootObject.toParsedText(writer, actual); } if (hasSpaceAfterRootObject) { writer.append(SPACE); } // 'AS' if (asIdentifier != null) { writer.append(actual ? asIdentifier : AS); } if (hasSpaceAfterAs) { writer.append(SPACE); } // Identification variable if ((identificationVariable != null) && !virtualIdentificationVariable) { identificationVariable.toParsedText(writer, actual); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy