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: */
040: public class DB2V7DateDiffOperatorGenerator extends AbstractGenerator {
041:
042: public String generate(SQLObject obj, StatementContext context)
043: throws BaseException {
044: String result;
045: SQLGenericOperator operator = (SQLGenericOperator) obj;
046: SQLLiteral literal = (SQLLiteral) operator
047: .getArgumentValue("type");
048: if (literal == null) {
049: throw new BaseException("Failed to evaluate "
050: + operator.getOperatorType()
051: + ", \"type\" is null.");
052: }
053:
054: String intervalType = literal.getValue();
055: intervalType = intervalType.toLowerCase();
056:
057: // get all necessary data.
058: Map params = operator.getSQLObjectMap();
059:
060: // allow overridable operator factory so other db's can override date add
061: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
062: .getSQLOperatorDefinition(operator.getOperatorType());
063:
064: Map<String, String> resolvedparams = new HashMap<String, String>();
065: List args = defn.getArgList();
066:
067: for (int i = 0; i < args.size(); i++) {
068: String key = ((SQLOperatorArg) args.get(i)).getArgName();
069: SQLObject val = (SQLObject) params.get(key);
070: if (val != null) {
071: String eval = this .getGeneratorFactory().generate(val,
072: context);
073: resolvedparams.put(key, eval);
074: }
075: }
076:
077: // use numeric constants for datediff
078: resolvedparams.put("type",
079: translateIntervalTypeToLiteral(intervalType));
080:
081: result = StringUtil.replace(defn.getScript(), resolvedparams,
082: SQLConstants.OPERATOR_VARIABLE_PREFIX);
083:
084: return result;
085: }
086:
087: /**
088: * converts the string interval type to the appropriate literal representation of the
089: * interval in that DB
090: *
091: * @param intervalType interval type selected from operator drop-down menu
092: * @return interval literal string.
093: */
094: protected String translateIntervalTypeToLiteral(String intervalType) {
095: String literal = "<UNKNOWN_INTERVAL_TYPE>";
096: if (intervalType.equals("second")) {
097: literal = "2";
098: } else if (intervalType.equals("minute")) {
099: literal = "4";
100: } else if (intervalType.equals("hour")) {
101: literal = "8";
102: } else if (intervalType.equals("day")) {
103: literal = "16";
104: } else if (intervalType.equals("week")) {
105: literal = "32";
106: } else if (intervalType.equals("month")) {
107: literal = "64";
108: } else if (intervalType.equals("quarter")) {
109: literal = "128";
110: } else if (intervalType.equals("year")) {
111: literal = "256";
112: }
113: return literal;
114: }
115: }
|