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.List;
044:
045: import org.apache.velocity.VelocityContext;
046: import org.netbeans.modules.sql.framework.codegen.AbstractGeneratorFactory;
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.BaseCaseGenerator;
050: import org.netbeans.modules.sql.framework.model.SQLCaseOperator;
051: import org.netbeans.modules.sql.framework.model.SQLConstants;
052: import org.netbeans.modules.sql.framework.model.SQLObject;
053: import org.netbeans.modules.sql.framework.model.SQLPredicate;
054: import org.netbeans.modules.sql.framework.model.SQLWhen;
055:
056: import com.sun.sql.framework.exception.BaseException;
057:
058: /**
059: * Velocity-based Generator that generates SQL Case expressions with Oracle 8i-specific
060: * syntax quirks.
061: *
062: * @author Jonathan Giron
063: * @version $Revision$
064: */
065: public class Oracle8CaseGenerator extends BaseCaseGenerator {
066:
067: public String generate(SQLObject obj, StatementContext context)
068: throws BaseException {
069: if (obj == null || getObjectType(obj) != SQLConstants.CASE) {
070: throw new BaseException(
071: "Bad SQLObject type - case expected.");
072: }
073:
074: SQLCaseOperator operator = (SQLCaseOperator) obj;
075: List whenObjects = operator.getWhenList();
076: SQLObject defaultAction = operator
077: .getSQLObject(SQLCaseOperator.DEFAULT);
078:
079: StringBuilder whenClause = new StringBuilder(100);
080: final int count = operator.getWhenCount();
081: for (int i = 0; i < count; i++) {
082: SQLWhen when = (SQLWhen) whenObjects.get(i);
083: SQLObject wcondition = when.getSQLObject(SQLWhen.CONDITION);
084: if (wcondition == null) {
085: throw new BaseException(
086: "Missing condition in when clause of case object.");
087: }
088:
089: whenClause.append(genWhenExpression(when, (i == count - 1),
090: defaultAction, context));
091: }
092:
093: VelocityContext vContext = new VelocityContext();
094: vContext.put("whenClause", whenClause.toString());
095:
096: String nestedIndent = (String) context
097: .getClientProperty("nestedIndent");
098: vContext.put("nestedIndent",
099: ((nestedIndent != null) ? nestedIndent : " "));
100: return TemplateBuilder.generateSql(this .getDB()
101: .getTemplateFileName("caseWhen"), vContext);
102: }
103:
104: private String genWhenExpression(SQLWhen when, boolean isLast,
105: SQLObject def, StatementContext context)
106: throws BaseException {
107: String op = ((SQLPredicate) when
108: .getSQLObject(SQLWhen.CONDITION)).getOperatorType()
109: .trim();
110:
111: SQLObject left = ((SQLPredicate) when
112: .getSQLObject(SQLWhen.CONDITION))
113: .getSQLObject(SQLPredicate.LEFT);
114:
115: SQLObject right = ((SQLPredicate) when
116: .getSQLObject(SQLWhen.CONDITION))
117: .getSQLObject(SQLPredicate.RIGHT);
118:
119: SQLObject then = when.getSQLObject(SQLWhen.RETURN);
120:
121: final AbstractGeneratorFactory genFactory = getDB()
122: .getGeneratorFactory();
123:
124: String result = "DECODE(SIGN("
125: + genFactory.generate(left, context);
126: result += " - ";
127: result += genFactory.generate(right, context) + "), ";
128:
129: if (op.equals("=")) {
130: result += " 0, " + genFactory.generate(then, context)
131: + ", ";
132: if (isLast) {
133: result += genFactory.generate(def, context) + ")";
134: }
135: return (result);
136: }
137:
138: if (op.equals(">")) {
139: result += " 1, " + genFactory.generate(then, context)
140: + ", ";
141: if (isLast) {
142: result += genFactory.generate(def, context) + ")";
143: }
144: return (result);
145: }
146:
147: if (op.equals("<")) {
148: result += " -1, " + genFactory.generate(then, context)
149: + ", ";
150: if (isLast) {
151: result += genFactory.generate(def, context) + ")";
152: }
153: return (result);
154: }
155:
156: if (op.equals(">=")) {
157: result += " 0, " + genFactory.generate(then, context)
158: + ", ";
159: result += " 1, " + genFactory.generate(then, context)
160: + ", ";
161: if (isLast) {
162: result += genFactory.generate(def, context) + ")";
163: }
164: return (result);
165: }
166:
167: if (op.equals("=<")) {
168: result += " 0, " + genFactory.generate(then, context)
169: + ", ";
170: result += " -1, " + genFactory.generate(then, context)
171: + ", ";
172: if (isLast) {
173: result += genFactory.generate(def, context) + ")";
174: }
175: return (result);
176: }
177:
178: if (op.equals("<>") || op.equals("!=")) {
179: result += " -1, " + genFactory.generate(then, context)
180: + ", ";
181: result += " 1, " + genFactory.generate(then, context)
182: + ", ";
183: if (isLast) {
184: result += genFactory.generate(def, context) + ")";
185: }
186: return (result);
187: }
188:
189: throw new BaseException("Unknown operator in when predicate.");
190: }
191: }
|