001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.sql.framework.codegen.db2v7;
021:
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import org.netbeans.modules.sql.framework.codegen.AbstractGenerator;
026: import org.netbeans.modules.sql.framework.codegen.StatementContext;
027: import org.netbeans.modules.sql.framework.model.SQLConstants;
028: import org.netbeans.modules.sql.framework.model.SQLGenericOperator;
029: import org.netbeans.modules.sql.framework.model.SQLLiteral;
030: import org.netbeans.modules.sql.framework.model.SQLObject;
031: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
032: import org.netbeans.modules.sql.framework.model.SQLOperatorDefinition;
033: import com.sun.sql.framework.exception.BaseException;
034: import com.sun.sql.framework.utils.StringUtil;
035:
036: /**
037: * @author Rupesh Ramachandran
038: * @author Ritesh Adval
039: * @version $Revision$
040: */
041: public class DB2V7DateAddOperatorGenerator extends AbstractGenerator {
042:
043: public String generate(SQLObject obj, StatementContext context)
044: throws BaseException {
045: String result;
046: SQLGenericOperator operator = (SQLGenericOperator) obj;
047:
048: SQLLiteral literal = (SQLLiteral) operator
049: .getArgumentValue("type");
050: if (literal == null) {
051: throw new BaseException("Failed to evaluate "
052: + operator.getOperatorType()
053: + ", \"type\" is null.");
054: }
055:
056: String intervalType = literal.getValue();
057: intervalType = intervalType.toLowerCase();
058:
059: // DATEADD: $timestamp1 + $interval $type
060: // e.g. TS_COL + 2 day
061: // get all necessary data.
062: Map params = operator.getSQLObjectMap();
063:
064: // allow overridable operator factory so other db's can override date add
065: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
066: .getSQLOperatorDefinition(operator.getOperatorType());
067:
068: Map<String, String> resolvedparams = new HashMap<String, String>();
069: List args = defn.getArgList();
070:
071: for (int i = 0; i < args.size(); i++) {
072: String key = ((SQLOperatorArg) args.get(i)).getArgName();
073: SQLObject val = (SQLObject) params.get(key);
074: if (val != null) {
075: String eval = this .getGeneratorFactory().generate(val,
076: context);
077: // replace 'week' and 'quarter' intervals using days/months
078: if (key.equals("interval")) {
079: if (intervalType.equalsIgnoreCase("week")) {
080: // use week as 7 days
081: eval = "(" + eval + "*7)";
082: }
083: if (intervalType.equalsIgnoreCase("quarter")) {
084: // use quarter as 3 months
085: eval = "(" + eval + "*3)";
086: }
087: }
088: resolvedparams.put(key, eval);
089: }
090: }
091:
092: // use string; week and quarter not supported, so replace
093: String intType = intervalType.replaceFirst("week", "day");
094: intType = intType.replaceFirst("quarter", "month");
095: resolvedparams.put("type", intType);
096:
097: result = StringUtil.replace(defn.getScript(), resolvedparams,
098: SQLConstants.OPERATOR_VARIABLE_PREFIX);
099:
100: return result;
101: }
102:
103: /**
104: * converts the string interval type to the appropriate literal representation of the
105: * interval in that DB
106: *
107: * @param intervalType interval type selected from operator drop-down menu
108: * @return interval literal string.
109: */
110: protected String translateIntervalTypeToLiteral(String intervalType) {
111: String literal = "<UNKNOWN_INTERVAL_TYPE>";
112: if (intervalType.equals("second")) {
113: literal = "2";
114: } else if (intervalType.equals("minute")) {
115: literal = "4";
116: } else if (intervalType.equals("hour")) {
117: literal = "8";
118: } else if (intervalType.equals("day")) {
119: literal = "16";
120: } else if (intervalType.equals("week")) {
121: literal = "32";
122: } else if (intervalType.equals("month")) {
123: literal = "64";
124: } else if (intervalType.equals("quarter")) {
125: literal = "128";
126: } else if (intervalType.equals("year")) {
127: literal = "256";
128: }
129: return literal;
130: }
131: }
|