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.codegen.TypeGenerator;
028: import org.netbeans.modules.sql.framework.model.SQLConstants;
029: import org.netbeans.modules.sql.framework.model.SQLGenericOperator;
030: import org.netbeans.modules.sql.framework.model.SQLLiteral;
031: import org.netbeans.modules.sql.framework.model.SQLObject;
032: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
033: import org.netbeans.modules.sql.framework.model.SQLOperatorDefinition;
034: import org.netbeans.modules.sql.framework.model.SourceColumn;
035: import com.sun.sql.framework.exception.BaseException;
036: import com.sun.sql.framework.jdbc.SQLUtils;
037: import com.sun.sql.framework.utils.StringUtil;
038:
039: /**
040: * @author Rupesh Ramachandran
041: * @author Ritesh Adval
042: * @version $Revision$
043: */
044: public class DB2V7CastAsOperatorGenerator extends AbstractGenerator {
045:
046: public String generate(SQLObject obj, StatementContext context)
047: throws BaseException {
048: String result;
049: SQLGenericOperator operator = (SQLGenericOperator) obj;
050:
051: SQLLiteral literal = (SQLLiteral) operator
052: .getArgumentValue("type");
053: if (literal == null) {
054: throw new BaseException("Failed to evaluate "
055: + operator.getOperatorType()
056: + ", \"type\" is null.");
057: }
058:
059: String castType = literal.getValue();
060: castType = castType.toLowerCase();
061:
062: // get all necessary data.
063: Map params = operator.getSQLObjectMap();
064:
065: // allow overridable operator factory so other db's can override cast
066: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
067: .getSQLOperatorDefinition(operator.getOperatorType());
068:
069: Map<String, String> resolvedparams = new HashMap<String, String>();
070: List args = defn.getArgList();
071:
072: for (int i = 0; i < args.size(); i++) {
073: String key = ((SQLOperatorArg) args.get(i)).getArgName();
074: SQLObject val = (SQLObject) params.get(key);
075:
076: if (val != null) {
077: // if casting from date/time to timestamp, use TIMESTAMP_ISO()
078: // fn. instead
079: if (val instanceof SourceColumn) {
080: if (((val.getJdbcType() == SQLUtils
081: .getStdJdbcType("date")) || (val
082: .getJdbcType() == SQLUtils
083: .getStdJdbcType("time")))
084: && (castType.equals("timestamp"))) {
085: result = "TIMESTAMP_ISO("
086: + this .getGeneratorFactory().generate(
087: val, context) + ")";
088: return result;
089: }
090: }
091: resolvedparams.put(key, this .getGeneratorFactory()
092: .generate(val, context));
093: }
094: }
095:
096: TypeGenerator typeGenerator = this .getDB().getTypeGenerator();
097: int jdbcTypeInt = SQLUtils.getStdJdbcType(castType);
098: resolvedparams.put("type", typeGenerator.generate(jdbcTypeInt,
099: 255, 0));
100:
101: result = StringUtil.replace(defn.getScript(), resolvedparams,
102: SQLConstants.OPERATOR_VARIABLE_PREFIX);
103: return result;
104: }
105: }
|