001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.model.visitors;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046: import java.util.List;
047: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
048: import org.netbeans.modules.sql.framework.model.SQLGenericOperator;
049: import org.netbeans.modules.sql.framework.model.SQLInputObject;
050: import org.netbeans.modules.sql.framework.model.SQLObject;
051: import org.netbeans.modules.sql.framework.model.SourceColumn;
052: import org.netbeans.modules.sql.framework.model.TargetColumn;
053: import org.netbeans.modules.sql.framework.model.TargetTable;
054: import org.netbeans.modules.sql.framework.model.ValidationInfo;
055: import org.netbeans.modules.sql.framework.model.impl.ValidationInfoImpl;
056:
057: public class SQLGroupByValidationVisitor {
058:
059: private boolean foundAggregateFn = false;
060: private boolean foundScalar = false;
061: private List groupByNodes;
062: private TargetTable targetTable;
063: private List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
064:
065: public SQLGroupByValidationVisitor(TargetTable table, List nodes) {
066: targetTable = table;
067: groupByNodes = nodes;
068: }
069:
070: public List<ValidationInfo> getValidationInfoList() {
071: return this .validationInfoList;
072: }
073:
074: public void reset() {
075: foundAggregateFn = foundScalar = false;
076: validationInfoList.clear();
077: }
078:
079: public void visit(Collection selectList) {
080: for (Iterator iter = selectList.iterator(); iter.hasNext();) {
081: SQLObject sqlObj = (SQLObject) iter.next();
082: visit(sqlObj);
083: }
084:
085: if (groupByNodes == null || groupByNodes.isEmpty()) {
086: foundScalar = true;
087: }
088:
089: if (foundScalar && foundAggregateFn) {
090: ValidationInfo validationInfo = new ValidationInfoImpl(
091: targetTable,
092: "Can't select both scalar values and aggregate functions.",
093: ValidationInfo.VALIDATION_ERROR);
094: validationInfoList.add(validationInfo);
095: } else if (foundScalar && groupByNodes != null
096: && !groupByNodes.isEmpty()) {
097: ValidationInfo validationInfo = new ValidationInfoImpl(
098: targetTable, "Invalid Group By Expression...",
099: ValidationInfo.VALIDATION_ERROR);
100: validationInfoList.add(validationInfo);
101: }
102: }
103:
104: private void visit(SQLObject obj) {
105: if (obj == null) {
106: // do nothing
107: } else if (obj instanceof TargetColumn) {
108: if (groupByNodes != null && groupByNodes.contains(obj)) {
109: return;
110: }
111: visit(((TargetColumn) obj).getValue());
112: } else if (obj instanceof SQLGenericOperator
113: && ((SQLGenericOperator) obj).isAggregateFunction()) {
114: foundAggregateFn = true;
115: } else if (obj instanceof SQLConnectableObject) {
116: SQLConnectableObject expObj = (SQLConnectableObject) obj;
117: if (groupByNodes != null && groupByNodes.contains(expObj)) {
118: return;
119: }
120: Iterator it = expObj.getInputObjectMap().values()
121: .iterator();
122: while (it.hasNext()) {
123: SQLInputObject inObj = (SQLInputObject) it.next();
124: SQLObject sqlObj = inObj.getSQLObject();
125: visit(sqlObj);
126: }
127:
128: List children = expObj.getChildSQLObjects();
129: Iterator cIt = children.iterator();
130: while (cIt.hasNext()) {
131: SQLObject chObj = (SQLObject) cIt.next();
132: visit(chObj);
133: }
134: } else if (obj instanceof SourceColumn && groupByNodes != null) {
135: // column is not part of GroupBy column list
136: if (!groupByNodes.contains(obj)) {
137: foundScalar = true;
138: }
139: }
140: }
141: }
|