001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.parsing;
038:
039: /**
040: * INTERNAL
041: * <p><b>Purpose</b>: Represent a MOD
042: * <p><b>Responsibilities</b>:<ul>
043: * <li> Answer the correct expression for a MOD
044: * <li> Maintain the parts of a MOD statement
045: *
046: * e.g.
047: * SELECT ... FROM ... WHERE MOD(emp.salary, 2) > 1000
048: * </ul>
049: * @author Jon Driscoll and Joel Lucuik
050: * @since TopLink 4.0
051: */
052: import oracle.toplink.essentials.expressions.*;
053: import oracle.toplink.essentials.exceptions.EJBQLException;
054:
055: public class ModNode extends ArithmeticFunctionNode {
056:
057: private Node denominator = null;
058:
059: /**
060: * INTERNAL
061: * Check the child nodes for an unqualified field access and if so,
062: * replace it by a qualified field access.
063: */
064: public Node qualifyAttributeAccess(ParseTreeContext context) {
065: if (left != null) {
066: left = left.qualifyAttributeAccess(context);
067: }
068: if (denominator != null) {
069: denominator = denominator.qualifyAttributeAccess(context);
070: }
071: return this ;
072: }
073:
074: /**
075: * INTERNAL
076: * Validate node and calculate its type.
077: */
078: public void validate(ParseTreeContext context) {
079: TypeHelper typeHelper = context.getTypeHelper();
080: if (left != null) {
081: left.validate(context);
082: left.validateParameter(context, typeHelper.getIntType());
083:
084: Object type = left.getType();
085: if (!typeHelper.isIntegralType(type))
086: throw EJBQLException.invalidFunctionArgument(context
087: .getQueryInfo(), left.getLine(), left
088: .getColumn(), "MOD", left.getAsString(),
089: "integral type");
090: }
091:
092: if (denominator != null) {
093: denominator.validate(context);
094: denominator.validateParameter(context, typeHelper
095: .getIntType());
096:
097: Object denominatorType = denominator.getType();
098: if (!typeHelper.isIntegralType(denominatorType))
099: throw EJBQLException.invalidFunctionArgument(context
100: .getQueryInfo(), denominator.getLine(),
101: denominator.getColumn(), "MOD", denominator
102: .getAsString(), "integral type");
103: }
104:
105: setType(typeHelper.getIntType());
106: }
107:
108: /** */
109: public Expression generateExpression(GenerationContext context) {
110: return ExpressionMath.mod(
111: getLeft().generateExpression(context), getDenominator()
112: .generateExpression(context));
113: }
114:
115: // Accessors
116: public Node getDenominator() {
117: return denominator;
118: }
119:
120: public void setDenominator(Node denominator) {
121: this.denominator = denominator;
122: }
123: }
|