org.eclipse.persistence.jpa.jpql.parser.DeleteClause Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* 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.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.persistence.jpa.jpql.ExpressionTools;
import org.eclipse.persistence.jpa.jpql.WordParser;
/**
* This is the delete clause of the delete statement.
*
* A DELETE statement provides bulk operations over sets of entities of a single entity class
* (together with its subclasses, if any). Only one entity abstract schema type may be specified in
* the UPDATE clause.
*
*
BNF: delete_clause ::= DELETE FROM abstract_schema_name [[AS] identification_variable]
*
* Example: DELETE FROM Employee e
*
* @version 2.5
* @since 2.3
* @author Pascal Filion
*/
public final class DeleteClause extends AbstractExpression {
/**
* The actual DELETE identifier found in the string representation of the JPQL query.
*/
private String deleteIdentifier;
/**
* The actual FROM identifier found in the string representation of the JPQL query.
*/
private String fromIdentifier;
/**
* Determines whether a whitespace was parsed after DELETE.
*/
private boolean hasSpaceAfterDelete;
/**
* Determines whether a whitespace was parsed after FROM.
*/
private boolean hasSpaceAfterFrom;
/**
* The {@link Expression} representing the range variable declaration.
*/
private AbstractExpression rangeVariableDeclaration;
/**
* Creates a new DeleteClause
.
*
* @param parent The parent of this expression
*/
public DeleteClause(AbstractExpression parent) {
super(parent, DELETE);
}
/**
* {@inheritDoc}
*/
public void accept(ExpressionVisitor visitor) {
visitor.visit(this);
}
/**
* {@inheritDoc}
*/
public void acceptChildren(ExpressionVisitor visitor) {
getRangeVariableDeclaration().accept(visitor);
}
/**
* {@inheritDoc}
*/
@Override
protected void addChildrenTo(Collection children) {
children.add(getRangeVariableDeclaration());
}
/**
* {@inheritDoc}
*/
@Override
protected void addOrderedChildrenTo(List children) {
// 'DELETE'
children.add(buildStringExpression(DELETE));
if (hasSpaceAfterDelete) {
children.add(buildStringExpression(SPACE));
}
// 'FROM'
if (fromIdentifier != null) {
children.add(buildStringExpression(FROM));
}
if (hasSpaceAfterFrom) {
children.add(buildStringExpression(SPACE));
}
// Range declaration variable
if (rangeVariableDeclaration != null) {
children.add(rangeVariableDeclaration);
}
}
/**
* Creates a new {@link CollectionExpression} that will wrap the single range variable declaration.
*
* @return The single range variable declaration represented by a temporary collection
*/
public CollectionExpression buildCollectionExpression() {
List children = new ArrayList(1);
children.add((AbstractExpression) getRangeVariableDeclaration());
List commas = new ArrayList(1);
commas.add(Boolean.FALSE);
List spaces = new ArrayList(1);
spaces.add(Boolean.FALSE);
return new CollectionExpression(this, children, commas, spaces, true);
}
/**
* {@inheritDoc}
*/
@Override
public JPQLQueryBNF findQueryBNF(Expression expression) {
if ((rangeVariableDeclaration != null) && rangeVariableDeclaration.isAncestor(expression)) {
return getQueryBNF(DeleteClauseRangeVariableDeclarationBNF.ID);
}
return super.findQueryBNF(expression);
}
/**
* Returns the actual DELETE found in the string representation of the JPQL query, which
* has the actual case that was used.
*
* @return The DELETE identifier that was actually parsed, or an empty string if it was
* not parsed
*/
public String getActualDeleteIdentifier() {
return (deleteIdentifier != null) ? deleteIdentifier : ExpressionTools.EMPTY_STRING;
}
/**
* Returns the actual FROM identifier found in the string representation of the JPQL
* query, which has the actual case that was used.
*
* @return The FROM identifier that was actually parsed, or an empty string if it was
* not parsed
*/
public String getActualFromIdentifier() {
return (fromIdentifier != null) ? fromIdentifier : ExpressionTools.EMPTY_STRING;
}
/**
* {@inheritDoc}
*/
public JPQLQueryBNF getQueryBNF() {
return getQueryBNF(DeleteClauseBNF.ID);
}
/**
* Returns the {@link Expression} representing the range variable declaration.
*
* @return The expression representing the range variable declaration
*/
public Expression getRangeVariableDeclaration() {
if (rangeVariableDeclaration == null) {
rangeVariableDeclaration = buildNullExpression();
}
return rangeVariableDeclaration;
}
/**
* Determines whether the identifier FROM was parsed.
*
* @return true
if FROM was parsed; false
otherwise
*/
public boolean hasFrom() {
return fromIdentifier != null;
}
/**
* Determines whether the range variable declaration was parsed.
*
* @return true
if the range variable declaration was parsed; false
otherwise
*/
public boolean hasRangeVariableDeclaration() {
return rangeVariableDeclaration != null &&
!rangeVariableDeclaration.isNull();
}
/**
* Determines whether a whitespace was found after the identifier DELETE.
*
* @return true
if there was a whitespace after the identifier DELETE;
* false
otherwise
*/
public boolean hasSpaceAfterDelete() {
return hasSpaceAfterDelete;
}
/**
* Determines whether a whitespace was found after the identifier FROM.
*
* @return true
if there was a whitespace after the identifier FROM;
* false
otherwise
*/
public boolean hasSpaceAfterFrom() {
return hasSpaceAfterFrom;
}
/**
* {@inheritDoc}
*/
@Override
protected void parse(WordParser wordParser, boolean tolerant) {
// Parse 'DELETE'
deleteIdentifier = wordParser.moveForward(DELETE);
hasSpaceAfterDelete = wordParser.skipLeadingWhitespace() > 0;
// Parse 'FROM'
if (!tolerant || wordParser.startsWithIdentifier(FROM)) {
fromIdentifier = wordParser.moveForward(FROM);
hasSpaceAfterFrom = wordParser.skipLeadingWhitespace() > 0;
}
// Parse the range variable declaration
if (tolerant) {
rangeVariableDeclaration = parse(
wordParser,
DeleteClauseRangeVariableDeclarationBNF.ID,
tolerant
);
}
else {
rangeVariableDeclaration = new RangeVariableDeclaration(this);
rangeVariableDeclaration.parse(wordParser, tolerant);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void toParsedText(StringBuilder writer, boolean actual) {
// 'DELETE'
writer.append(actual ? deleteIdentifier : DELETE);
if (hasSpaceAfterDelete) {
writer.append(SPACE);
}
// 'FROM'
if (fromIdentifier != null) {
writer.append(actual ? fromIdentifier : FROM);
}
if (hasSpaceAfterFrom) {
writer.append(SPACE);
}
// Range variable declaration
if (rangeVariableDeclaration != null) {
rangeVariableDeclaration.toParsedText(writer, actual);
}
}
}