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.codegen.oracle8;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import org.apache.velocity.VelocityContext;
047: import org.netbeans.modules.sql.framework.codegen.StatementContext;
048: import org.netbeans.modules.sql.framework.codegen.TemplateBuilder;
049: import org.netbeans.modules.sql.framework.codegen.base.BaseJoinGenerator;
050: import org.netbeans.modules.sql.framework.model.SQLCondition;
051: import org.netbeans.modules.sql.framework.model.SQLConstants;
052: import org.netbeans.modules.sql.framework.model.SQLJoinOperator;
053: import org.netbeans.modules.sql.framework.model.SQLJoinTable;
054: import org.netbeans.modules.sql.framework.model.SQLObject;
055:
056: import com.sun.sql.framework.exception.BaseException;
057: import com.sun.sql.framework.utils.StringUtil;
058:
059: /**
060: * Overrides parent implementation to handle Oracle 8-specific join syntax.
061: *
062: * @author Jonathan Giron
063: * @version $Revision$
064: */
065: public class Oracle8JoinGenerator extends BaseJoinGenerator {
066:
067: public String generate(SQLObject obj, StatementContext context)
068: throws BaseException {
069: SQLJoinOperator join = (SQLJoinOperator) obj;
070:
071: SQLObject left = join.getSQLObject(SQLJoinOperator.LEFT);
072: SQLObject right = join.getSQLObject(SQLJoinOperator.RIGHT);
073: SQLCondition joinCondition = join.getJoinCondition();
074: SQLObject predicate = joinCondition.getRootPredicate();
075:
076: if (left == null || right == null) {
077: throw new BaseException(
078: "Bad SQLJoin object. No left and right input.");
079: }
080:
081: if (left.getObjectType() == SQLConstants.JOIN_TABLE) {
082: left = ((SQLJoinTable) left).getSourceTable();
083: }
084:
085: if (right.getObjectType() == SQLConstants.JOIN_TABLE) {
086: right = ((SQLJoinTable) right).getSourceTable();
087: }
088:
089: String leftObj = this .getGeneratorFactory().generate(left,
090: context);
091: String rightObj = this .getGeneratorFactory().generate(right,
092: context);
093:
094: // Add join condition(s) to the where list in the statement context.
095: List whereList = (List) context
096: .getClientProperty(StatementContext.WHERE_CONDITION_LIST);
097: if (whereList == null) {
098: whereList = new ArrayList(3);
099: context.putClientProperty(
100: StatementContext.WHERE_CONDITION_LIST, whereList);
101: }
102:
103: String condition = "";
104: if (predicate != null) {
105: context.putClientProperty(StatementContext.JOIN_OPERATOR,
106: join);
107: condition = this .getGeneratorFactory().generate(predicate,
108: context);
109: if (!StringUtil.isNullString(condition)) {
110: whereList.add(condition);
111: }
112: context.putClientProperty(StatementContext.JOIN_OPERATOR,
113: null);
114: }
115:
116: VelocityContext vContext = new VelocityContext();
117: vContext.put("leftObj", leftObj);
118: vContext.put("rightObj", rightObj);
119:
120: return TemplateBuilder.generateSql(this .getDB()
121: .getTemplateFileName("join"), vContext);
122: }
123: }
|