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.oracle8;
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 Oracle8DateAddOperatorGenerator 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: // get all necessary data.
083: Map params = operator.getSQLObjectMap();
084:
085: // allow overridable operator factory so other db's can override date add
086: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
087: .getSQLOperatorDefinition(operator.getOperatorType());
088:
089: Map resolvedparams = new HashMap();
090: List args = defn.getArgList();
091:
092: for (int i = 0; i < args.size(); i++) {
093: String key = ((SQLOperatorArg) args.get(i)).getArgName();
094: SQLObject val = (SQLObject) params.get(key);
095: if (val != null) {
096: resolvedparams.put(key, this .getGeneratorFactory()
097: .generate(val, context));
098: }
099: }
100:
101: resolvedparams.put("type",
102: translateIntervalTypeToLiteral(intervalType));
103:
104: // for day, hour, minute, second, use:
105: // DATEDIFF: ($timestamp1 - $timestamp2) * ($type)
106: // DATEADD: $timestamp1 + ($interval / ($type))
107:
108: // for month,quarter,year:
109: // DATEDIFF: MONTHS_BETWEEN($timestamp1, $timestamp2)/$type (type in months)
110: // DATEADD: ADD_MONTHS($timestamp1, $interval*$type) (type in months)
111:
112: if (intervalType.equals("month")
113: || intervalType.equals("quarter")
114: || intervalType.equals("year")) {
115: result = "ADD_MONTHS(" + resolvedparams.get("timestamp1")
116: + ", " + resolvedparams.get("interval") + " * "
117: + resolvedparams.get("type") + ")";
118:
119: } else {
120: // use operator script for day,hour,minute,second
121: result = StringUtil.replace(defn.getScript(),
122: resolvedparams,
123: SQLConstants.OPERATOR_VARIABLE_PREFIX);
124: }
125:
126: return result;
127: }
128:
129: /**
130: * converts the string interval type to the appropriate literal representation of the
131: * interval in that DB
132: *
133: * @param intervalType interval type selected from operator drop-down menu
134: * @return interval literal string.
135: */
136: protected String translateIntervalTypeToLiteral(String intervalType) {
137: String literal = "<UNKNOWN_INTERVAL_TYPE>";
138: // convert interval to units within day.
139: // e.g. 24 hours in 1 day, return '24' for 'hour'
140: if (intervalType.equals("second"))
141: literal = "86400";
142: else if (intervalType.equals("minute"))
143: literal = "1440";
144: else if (intervalType.equals("hour"))
145: literal = "24";
146: else if (intervalType.equals("day"))
147: literal = "1";
148: else if (intervalType.equals("week"))
149: literal = "1/7";
150: // convert interval to months. e.g. quarter = 3 months
151: else if (intervalType.equals("month"))
152: literal = "1";
153: else if (intervalType.equals("quarter"))
154: literal = "3";
155: else if (intervalType.equals("year"))
156: literal = "12";
157: return literal;
158: }
159:
160: }
|