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.mashup.db.common;
042:
043: import java.sql.Types;
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048:
049: /**
050: * Utility class supplying lookup and conversion methods for SQL-related tasks.
051: *
052: * @author Sudhendra Seshachala
053: * @version $Revision$
054: */
055: public class SQLUtils {
056:
057: /** Designates undefined JDBC typecode. */
058: public static final int JDBCSQL_TYPE_UNDEFINED = -65536;
059:
060: /* Map of String SQL types to JDBC typecodes */
061: private static final Map SQL_JDBC_MAP = new HashMap();
062: private static final Map JDBC_SQL_MAP = new HashMap();
063:
064: static {
065: SQL_JDBC_MAP.put("numeric", String.valueOf(Types.NUMERIC));
066: SQL_JDBC_MAP.put("time", String.valueOf(Types.TIME));
067: SQL_JDBC_MAP.put("timestamp", String.valueOf(Types.TIMESTAMP));
068: SQL_JDBC_MAP.put("varchar", String.valueOf(Types.VARCHAR));
069: }
070:
071: static {
072: JDBC_SQL_MAP.put(String.valueOf(Types.NUMERIC), "numeric");
073: JDBC_SQL_MAP.put(String.valueOf(Types.TIME), "time");
074: JDBC_SQL_MAP.put(String.valueOf(Types.TIMESTAMP), "timestamp");
075: JDBC_SQL_MAP.put(String.valueOf(Types.VARCHAR), "varchar");
076: }
077:
078: /**
079: * Gets JDBC int type, if any, corresponding to the given SQL datatype string.
080: *
081: * @param dataType SQL datatype whose equivalent JDBC int type is sought
082: * @return java.sql.Types value equivalent to dataType
083: */
084: public static int getStdJdbcType(String dataType) {
085: if (dataType == null) {
086: dataType = "";
087: }
088:
089: Object intStr = SQL_JDBC_MAP.get(dataType.toLowerCase().trim());
090: try {
091: return Integer.parseInt(intStr.toString());
092: } catch (Exception e) {
093: return JDBCSQL_TYPE_UNDEFINED;
094: }
095: }
096:
097: /**
098: * Gets SQL datatype string, if any, corresponding to the given JDBC int value.
099: *
100: * @param dataType SQL datatype whose corresopnding JDBC int type is sought
101: * @return SQL datatype string corresponding to dataType, or null if no such datatype
102: * is mapped to dataType
103: */
104: public static String getStdSqlType(int dataType) {
105: return (String) JDBC_SQL_MAP.get(String.valueOf(dataType));
106: }
107:
108: /**
109: * Gets the stdJdbcType attribute of the Database class
110: *
111: * @param jdbcType instance of Types
112: * @return The stdJdbcType value
113: */
114: public static synchronized boolean isStdJdbcType(int jdbcType) {
115: return JDBC_SQL_MAP.containsKey(String.valueOf(jdbcType));
116: }
117:
118: /**
119: * Gets List of Strings representing standard SQL datatypes.
120: *
121: * @return List of standard SQL datatypes.
122: */
123: public static List getStdSqlTypes() {
124: return new ArrayList(SQL_JDBC_MAP.keySet());
125: }
126:
127: /* Private no-arg constructor; this class should not be instantiable. */
128: private SQLUtils() {
129: }
130: }
|