01: /*
02: * The contents of this file are subject to the terms of the Common Development
03: * and Distribution License (the License). You may not use this file except in
04: * compliance with the License.
05: *
06: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
07: * or http://www.netbeans.org/cddl.txt.
08: *
09: * When distributing Covered Code, include this CDDL Header Notice in each file
10: * and include the License file at http://www.netbeans.org/cddl.txt.
11: * If applicable, add the following below the CDDL Header, with the fields
12: * enclosed by brackets [] replaced by your own identifying information:
13: * "Portions Copyrighted [year] [name of copyright owner]"
14: *
15: * The Original Software is NetBeans. The Initial Developer of the Original
16: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17: * Microsystems, Inc. All Rights Reserved.
18: */
19:
20: package org.netbeans.modules.sql.framework.codegen.base;
21:
22: import java.util.HashMap;
23: import java.util.List;
24: import java.util.Map;
25: import org.netbeans.modules.sql.framework.codegen.StatementContext;
26: import org.netbeans.modules.sql.framework.codegen.TypeGenerator;
27: import org.netbeans.modules.sql.framework.model.SQLCastOperator;
28: import org.netbeans.modules.sql.framework.model.SQLConstants;
29: import org.netbeans.modules.sql.framework.model.SQLObject;
30: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
31: import org.netbeans.modules.sql.framework.model.SQLOperatorDefinition;
32: import com.sun.sql.framework.exception.BaseException;
33: import com.sun.sql.framework.utils.StringUtil;
34:
35: /**
36: * Handles evaluation of cast-as SQL operator.
37: *
38: * @author Ritesh Adval
39: * @author Jonathan Giron
40: * @author Ahimanikya Satapathy
41: * @version $Revision$
42: */
43: public class BaseCastAsOperatorGenerator extends BaseOperatorGenerator {
44:
45: /**
46: * Overrides parent implementation to correctly generate a cast-as operator expression
47: * from the given SQLObject, using the given StatementContext
48: *
49: * @param obj SQLObject (SQLCastOperator) to be evaluated
50: * @param context StatementContext to use in generating expression
51: * @return cast-as operator expression
52: */
53: @Override
54: public String generate(SQLObject obj, StatementContext context)
55: throws BaseException {
56: String result;
57: SQLCastOperator operator = (SQLCastOperator) obj;
58:
59: // Get all necessary data.
60: Map params = operator.getSQLObjectMap();
61:
62: // Allow overrideable operator factory so other db's can override date add
63: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
64: .getSQLOperatorDefinition(operator.getOperatorType());
65:
66: Map<String, String> resolvedparams = new HashMap<String, String>();
67: List args = defn.getArgList();
68:
69: for (int i = 0; i < args.size(); i++) {
70: String key = ((SQLOperatorArg) args.get(i)).getArgName();
71: SQLObject val = (SQLObject) params.get(key);
72:
73: if (val != null) {
74: resolvedparams.put(key, this .getGeneratorFactory()
75: .generate(val, context));
76: }
77: }
78:
79: int jdbcTypeInt = operator.getJdbcType();
80: int precision = operator.getPrecision();
81: int scale = operator.getScale();
82:
83: TypeGenerator typeGenerator = this .getDB().getTypeGenerator();
84: resolvedparams.put("type", typeGenerator.generate(jdbcTypeInt,
85: precision, scale));
86:
87: result = StringUtil.replace(defn.getScript(), resolvedparams,
88: SQLConstants.OPERATOR_VARIABLE_PREFIX);
89: return result;
90: }
91: }
|