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: package org.netbeans.modules.sql.framework.model.impl;
042:
043: import java.util.Collections;
044: import java.util.List;
045:
046: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
047: import org.netbeans.modules.sql.framework.model.SQLConstants;
048: import org.netbeans.modules.sql.framework.model.SQLLiteral;
049: import org.netbeans.modules.sql.framework.model.VisibleSQLLiteral;
050: import org.netbeans.modules.sql.framework.model.utils.GeneratorUtil;
051:
052: import com.sun.sql.framework.exception.BaseException;
053:
054: /**
055: * Represents a string or number literal value.
056: *
057: * @author Ritesh Adval, Sudhi Seshachala
058: * @version $Revision$
059: */
060: public class SQLLiteralImpl extends AbstractSQLObject implements
061: SQLLiteral {
062:
063: static {
064: VALID_TYPE_NAMES = SQLUtils.getSupportedLiteralTypes();
065: }
066:
067: /** Array of Strings representing available SQL datatypes */
068: protected static final List VALID_TYPE_NAMES;
069:
070: /**
071: * Returns list of Strings representing valid SQL datatypes for an SQLLiteral object.
072: *
073: * @return read-only List of valid SQL datatypes
074: */
075: public static final List getValidTypeNames() {
076: return Collections.unmodifiableList(VALID_TYPE_NAMES);
077: }
078:
079: /** Creates a new default instance of SQLLiteral */
080: public SQLLiteralImpl() {
081: type = SQLConstants.LITERAL;
082: }
083:
084: /**
085: * Constructs a new instance of SQLLiteral, copying the contents of the given
086: * SQLLiteral.
087: *
088: * @param src SQLLiteral whose contents are to be copied to this new instance
089: */
090: public SQLLiteralImpl(SQLLiteral src) {
091: this ();
092:
093: if (src == null) {
094: throw new IllegalArgumentException(
095: "Cannot create SQLLiteral using copy constructor - src is null");
096: }
097:
098: copyFrom(src);
099: }
100:
101: /**
102: * Constructs a new instance of SQLLiteral with the given display name, value, and
103: * JDBC type.
104: *
105: * @param name display name of new instance
106: * @param value value of new instance
107: * @param jdbcType JDBC type of new instance
108: * @throws BaseException if name is null or empty, or value is null.
109: */
110: public SQLLiteralImpl(String name, String value, int jdbcType)
111: throws BaseException {
112: this ();
113:
114: if (value == null) {
115: String msg = "null value";
116: throw new BaseException(msg);
117: }
118:
119: setDisplayName(name);
120: setJdbcType(jdbcType);
121: setValue(value);
122: }
123:
124: public Object clone() {
125: return new SQLLiteralImpl(this );
126: }
127:
128: public void copyFrom(SQLLiteral src) {
129: super .copyFromSource(src);
130:
131: // If src is VisibleSQLLiteral, ensure type is set to non-visible variety as value
132: // of type is cloned from src by AbstractSQLObject and overrides the value set in
133: // constructor. Comparing src introduces side-effect, make non-visible literal
134: // only if this is not visible literal instead.
135: if (!(this instanceof VisibleSQLLiteral)) {
136: type = SQLConstants.LITERAL;
137: }
138: }
139:
140: /**
141: * Gets JDBC type of this literal.
142: *
143: * @return JDBC type
144: */
145: public int getJdbcType() {
146: String aType = (String) getAttributeObject(ATTR_TYPE);
147: if (aType != null && aType.equals(VARCHAR_UNQUOTED_STR)) {
148: return VARCHAR_UNQUOTED;
149:
150: }
151: return SQLUtils.getStdJdbcType(aType);
152: }
153:
154: public int getPrecision() {
155: return 0;
156: }
157:
158: /**
159: * Gets value of this literal.
160: *
161: * @return current value
162: */
163: public String getValue() {
164: return (String) getAttributeObject(ATTR_VALUE);
165: }
166:
167: /**
168: * Sets JDBC type of this literal.
169: *
170: * @param jdbcType new JDBC type
171: */
172: public void setJdbcType(int jdbcType) {
173: if (jdbcType == VARCHAR_UNQUOTED) {
174: setAttribute(ATTR_TYPE, VARCHAR_UNQUOTED_STR);
175: } else {
176: setAttribute(ATTR_TYPE, SQLUtils.getStdSqlType(jdbcType));
177: }
178: }
179:
180: /**
181: * Sets value of this literal to the given value.
182: *
183: * @param val new value
184: */
185: public void setValue(String val) {
186: setAttribute(ATTR_VALUE, val);
187: }
188:
189: public String toString() {
190: try {
191: return GeneratorUtil.getInstance().getEvaluatedString(this );
192: } catch (BaseException ignore) {
193: return "Unknown";
194: }
195: }
196: }
|