org.eclipse.persistence.jpa.jpql.parser.UpdateStatement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eclipselink Show documentation
Show all versions of eclipselink Show documentation
EclipseLink build based upon Git transaction f2b9fc5
/*
* 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.WordParser;
/**
* The UPDATE clause of a query consists of a conditional expression used to select objects
* or values that satisfy the expression. The UPDATE clause restricts the result of a select
* statement or the scope of an update operation.
*
* BNF: update_statement ::= update_clause [where_clause]
*
* @see JPQLExpression
* @see UpdateClause
* @see WhereClause
*
* @version 2.5
* @since 2.3
* @author Pascal Filion
*/
public final class UpdateStatement extends AbstractExpression {
/**
* Determines whether a whitespace was parsed after the UPDATE clause.
*/
private boolean hasSpaceAfterUpdateClause;
/**
* The expression representing the UPDATE clause.
*/
private UpdateClause updateClause;
/**
* The expression representing the WHERE clause.
*/
private AbstractExpression whereClause;
/**
* Creates a new UpdateStatement
.
*
* @param parent The parent of this expression
*/
public UpdateStatement(AbstractExpression parent) {
super(parent);
}
/**
* {@inheritDoc}
*/
public void accept(ExpressionVisitor visitor) {
visitor.visit(this);
}
/**
* {@inheritDoc}
*/
public void acceptChildren(ExpressionVisitor visitor) {
getUpdateClause().accept(visitor);
getWhereClause().accept(visitor);
}
/**
* {@inheritDoc}
*/
@Override
protected void addChildrenTo(Collection children) {
children.add(getUpdateClause());
children.add(getWhereClause());
}
/**
* {@inheritDoc}
*/
@Override
protected void addOrderedChildrenTo(List children) {
// Update clause
children.add(updateClause);
if (hasSpaceAfterUpdateClause) {
children.add(buildStringExpression(SPACE));
}
// Where clause
if (whereClause != null) {
children.add(whereClause);
}
}
/**
* {@inheritDoc}
*/
@Override
public JPQLQueryBNF findQueryBNF(Expression expression) {
if (updateClause.isAncestor(expression)) {
return getQueryBNF(UpdateClauseBNF.ID);
}
if ((whereClause != null) && whereClause.isAncestor(expression)) {
return getQueryBNF(WhereClauseBNF.ID);
}
return super.findQueryBNF(expression);
}
/**
* {@inheritDoc}
*/
public JPQLQueryBNF getQueryBNF() {
return getQueryBNF(UpdateStatementBNF.ID);
}
/**
* Returns the {@link UpdateClause} representing the UPDATE clause.
*
* @return The section of the update statement representing the UPDATE clause
*/
public UpdateClause getUpdateClause() {
return updateClause;
}
/**
* Returns the {@link Expression} representing the WHERE clause.
*
* @return The section of the update statement representing the WHERE clause
*/
public Expression getWhereClause() {
if (whereClause == null) {
whereClause = buildNullExpression();
}
return whereClause;
}
/**
* Determines whether a whitespace was parsed after the UPDATE clause.
*
* @return true
if a whitespace was parsed after the UPDATE clause;
* false
otherwise
*/
public boolean hasSpaceAfterUpdateClause() {
return hasSpaceAfterUpdateClause;
}
/**
* Determines whether the WHERE clause is defined or not.
*
* @return true
if this statement has a WHERE clause; false
if
* it was not parsed
*/
public boolean hasWhereClause() {
return whereClause != null &&
!whereClause.isNull();
}
/**
* {@inheritDoc}
*/
@Override
protected void parse(WordParser wordParser, boolean tolerant) {
// Parse 'UPDATE'
updateClause = new UpdateClause(this);
updateClause.parse(wordParser, tolerant);
hasSpaceAfterUpdateClause = wordParser.skipLeadingWhitespace() > 0;
// Parse 'WHERE'
if (wordParser.startsWithIdentifier(WHERE)) {
whereClause = new WhereClause(this);
whereClause.parse(wordParser, tolerant);
}
// Now fully qualify attribute names with a virtual identification variable
accept(new FullyQualifyPathExpressionVisitor());
}
/**
* {@inheritDoc}
*/
@Override
protected void toParsedText(StringBuilder writer, boolean actual) {
// Update clause
updateClause.toParsedText(writer, actual);
if (hasSpaceAfterUpdateClause) {
writer.append(SPACE);
}
// Where clause
if (whereClause != null) {
whereClause.toParsedText(writer, actual);
}
}
}