/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package com.akiban.sql.parser;
import com.akiban.sql.StandardException;
/**
* A BinaryListOperatorNode represents a built-in "binary" operator with a single
* operand on the left of the operator and a list of operands on the right.
* This covers operators such as IN and BETWEEN.
*
*/
public abstract class BinaryListOperatorNode extends ValueNode
{
protected String methodName;
/* operator used for error messages */
protected String operator;
protected ValueNode leftOperand;
protected ValueNodeList rightOperandList;
/**
* Initializer for a BinaryListOperatorNode
*
* @param leftOperand The left operand of the node
* @param rightOperandList The right operand list of the node
* @param operator String representation of operator
*/
public void init(Object leftOperand, Object rightOperandList,
Object operator, Object methodName) {
this.leftOperand = (ValueNode)leftOperand;
this.rightOperandList = (ValueNodeList)rightOperandList;
this.operator = (String)operator;
this.methodName = (String)methodName;
}
/**
* Fill this node with a deep copy of the given node.
*/
public void copyFrom(QueryTreeNode node) throws StandardException {
super.copyFrom(node);
BinaryListOperatorNode other = (BinaryListOperatorNode)node;
this.methodName = other.methodName;
this.operator = other.operator;
this.leftOperand = (ValueNode)
getNodeFactory().copyNode(other.leftOperand, getParserContext());
this.rightOperandList = (ValueNodeList)
getNodeFactory().copyNode(other.rightOperandList, getParserContext());
}
/**
* Convert this object to a String. See comments in QueryTreeNode.java
* for how this should be done for tree printing.
*
* @return This object as a String
*/
public String toString() {
return "operator: " + operator + "\n" +
"methodName: " + methodName + "\n" +
super.toString();
}
/**
* Prints the sub-nodes of this object. See QueryTreeNode.java for
* how tree printing is supposed to work.
*
* @param depth The depth of this node in the tree
*/
public void printSubNodes(int depth) {
super.printSubNodes(depth);
if (leftOperand != null) {
printLabel(depth, "leftOperand: ");
leftOperand.treePrint(depth + 1);
}
if (rightOperandList != null) {
printLabel(depth, "rightOperandList: ");
rightOperandList.treePrint(depth + 1);
}
}
/**
* Set the leftOperand to the specified ValueNode
*
* @param newLeftOperand The new leftOperand
*/
public void setLeftOperand(ValueNode newLeftOperand) {
leftOperand = newLeftOperand;
}
/**
* Get the leftOperand
*
* @return The current leftOperand.
*/
public ValueNode getLeftOperand() {
return leftOperand;
}
/**
* Set the rightOperandList to the specified ValueNodeList
*
* @param newRightOperandList The new rightOperandList
*
*/
public void setRightOperandList(ValueNodeList newRightOperandList) {
rightOperandList = newRightOperandList;
}
/**
* Get the rightOperandList
*
* @return The current rightOperandList.
*/
public ValueNodeList getRightOperandList() {
return rightOperandList;
}
/**
* Return whether or not this expression tree represents a constant expression.
*
* @return Whether or not this expression tree represents a constant expression.
*/
public boolean isConstantExpression() {
return (leftOperand.isConstantExpression() &&
rightOperandList.isConstantExpression());
}
/**
* Accept the visitor for all visitable children of this node.
*
* @param v the visitor
*
* @exception StandardException on error
*/
void acceptChildren(Visitor v) throws StandardException {
super.acceptChildren(v);
if (leftOperand != null) {
leftOperand = (ValueNode)leftOperand.accept(v);
}
if (rightOperandList != null) {
rightOperandList = (ValueNodeList)rightOperandList.accept(v);
}
}
/**
* @inheritDoc
*/
protected boolean isEquivalent(ValueNode o) throws StandardException {
if (!isSameNodeType(o)) {
return false;
}
BinaryListOperatorNode other = (BinaryListOperatorNode)o;
if (!operator.equals(other.operator) ||
!leftOperand.isEquivalent(other.getLeftOperand())) {
return false;
}
if (!rightOperandList.isEquivalent(other.rightOperandList)) {
return false;
}
return true;
}
}