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:
042: package org.netbeans.modules.sql.framework.codegen.oracle9;
043:
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: import org.netbeans.modules.sql.framework.codegen.AbstractGenerator;
049: import org.netbeans.modules.sql.framework.codegen.StatementContext;
050: import org.netbeans.modules.sql.framework.model.SQLConstants;
051: import org.netbeans.modules.sql.framework.model.SQLGenericOperator;
052: import org.netbeans.modules.sql.framework.model.SQLLiteral;
053: import org.netbeans.modules.sql.framework.model.SQLObject;
054: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
055: import org.netbeans.modules.sql.framework.model.SQLOperatorDefinition;
056:
057: import com.sun.sql.framework.exception.BaseException;
058: import com.sun.sql.framework.utils.StringUtil;
059:
060: /**
061: * @author Rupesh Ramachandran
062: * @author Ritesh Adval
063: * @version $Revision$
064: */
065: public class Oracle9DateAddOperatorGenerator extends AbstractGenerator {
066:
067: public String generate(SQLObject obj, StatementContext context)
068: throws BaseException {
069: String result;
070: SQLGenericOperator operator = (SQLGenericOperator) obj;
071: SQLLiteral literal = (SQLLiteral) operator
072: .getArgumentValue("type");
073: if (literal == null) {
074: throw new BaseException("Failed to evaluate "
075: + operator.getOperatorType()
076: + ", \"type\" is null.");
077: }
078:
079: String intervalType = literal.getValue();
080: intervalType = intervalType.toLowerCase();
081:
082: // DATEADD: $date + numtodsinterval($interval, $interval_type)
083: // DATEDIFF: extract($interval from $date1 - $date2)
084:
085: // Get all necessary data.
086: Map params = operator.getSQLObjectMap();
087:
088: // Allow overridable operator factory so other db's can override date add
089: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
090: .getSQLOperatorDefinition(operator.getOperatorType());
091:
092: Map resolvedparams = new HashMap();
093: List args = defn.getArgList();
094:
095: for (int i = 0; i < args.size(); i++) {
096: String key = ((SQLOperatorArg) args.get(i)).getArgName();
097: SQLObject val = (SQLObject) params.get(key);
098: if (val != null) {
099: String eval = this .getGeneratorFactory().generate(val,
100: context);
101:
102: if (intervalType.equalsIgnoreCase("week")) {
103: // use week as 7 days
104: eval += "*7";
105: }
106: if (intervalType.equalsIgnoreCase("quarter")) {
107: // use quarter as 3 months
108: eval += "*3";
109: }
110:
111: resolvedparams.put(key, eval);
112: }
113: }
114:
115: resolvedparams.put("type",
116: translateIntervalTypeToLiteral(intervalType));
117:
118: result = StringUtil.replace(defn.getScript(), resolvedparams,
119: SQLConstants.OPERATOR_VARIABLE_PREFIX);
120:
121: // replace any other strings as needed
122: if (intervalType.equalsIgnoreCase("year")
123: || (intervalType.equalsIgnoreCase("month"))
124: || (intervalType.equalsIgnoreCase("quarter"))) {
125: // replace all day-second intervals with year-month intervals
126: result = result.replaceFirst("NUMTODSINTERVAL",
127: "NUMTOYMINTERVAL");
128: result = result.replaceFirst("DAY TO SECOND",
129: "YEAR TO MONTH");
130: }
131:
132: return result;
133: }
134:
135: /**
136: * converts the string interval type to the appropriate literal representation of the
137: * interval in that DB
138: *
139: * @param intervalType interval type selected from operator drop-down menu
140: * @return interval literal string.
141: */
142: protected String translateIntervalTypeToLiteral(String intervalType) {
143: String literal = intervalType.replaceFirst("week", "day");
144: literal = literal.replaceFirst("quarter", "month");
145: return literal;
146: }
147:
148: }
|