1 /* Version 1.0 based on Apache Software License 1.1
2 *
3 * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. The end-user documentation included with the redistribution, if any,
18 * must include the following acknowledgment:
19 *
20 * "This product includes software developed by DBMonster developers
21 * (http://dbmonster.kernelpanic.pl/)."
22 *
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The name "DBMonster" must not be used to endorse or promote products
27 * derived from this software without prior written permission. For
28 * written permission, please contact piotr.maj@kernelpanic.pl.
29 *
30 * 5. Products derived from this software may not be called "DBMonster",
31 * nor may "DBMonster" appear in their name, without prior written
32 * permission of Piotr Maj.
33 *
34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
45 */
46
47 package pl.kernelpanic.dbmonster.sql;
48
49 import java.util.Map;
50 import java.util.HashMap;
51 import java.sql.Types;
52
53 /***
54 * This class is used to give support to extended types specific for each JDBC
55 * driver.
56 *
57 * @author Antonio Petrelli
58 */
59 public class ExtendedTypes {
60
61 /***
62 * ExtendedTypes instance.
63 */
64 private static final ExtendedTypes single = new ExtendedTypes();
65
66 /***
67 * Type map.
68 */
69 private Map jdbcDriver2typeMap;
70
71 /***
72 * Creates a new instance of ExtendedTypes
73 */
74 private ExtendedTypes() {
75 Map typeMap;
76
77 jdbcDriver2typeMap = new HashMap();
78 typeMap = new HashMap();
79 jdbcDriver2typeMap.put("net.sourceforge.jtds.jdbc.Driver", typeMap);
80
81 // Type NVARCHAR for Microsoft SQL Server
82 typeMap.put(new Integer(-9), new Integer(Types.VARCHAR));
83 }
84
85 /***
86 * Returns ExtendedTypes instance.
87 *
88 * @return single instance of ExtendedTypes
89 */
90 public static final ExtendedTypes getInstance() {
91 return single;
92 }
93
94 /***
95 * Checks if given type is an extended type.
96 *
97 * @param driverName driver name
98 * @param type SQL Type
99 * @return <code>true</code> if type is extended type
100 */
101 public final boolean isExtended(final String driverName, final int type) {
102 Map typeMap;
103 boolean retValue = false;
104 Integer tempInt;
105
106 typeMap = (Map) jdbcDriver2typeMap.get(driverName);
107 if (typeMap != null) {
108 tempInt = (Integer) typeMap.get(new Integer(type));
109 if (tempInt != null)
110 retValue = true;
111 }
112 return retValue;
113 }
114
115 /***
116 * Returns standard alias for given extended type.
117 *
118 * @param driverName driver name
119 * @param nonStandardType non standard type
120 * @return standard type alias
121 */
122 public final int getStandardAlias(
123 final String driverName,
124 final int nonStandardType) {
125
126 Map typeMap;
127 Integer tempInt;
128 int retValue;
129
130 retValue = nonStandardType;
131 typeMap = (Map) jdbcDriver2typeMap.get(driverName);
132 if (typeMap != null) {
133 tempInt = (Integer) typeMap.get(new Integer(nonStandardType));
134 if (tempInt != null)
135 retValue = tempInt.intValue();
136 }
137 return retValue;
138 }
139
140 /***
141 * Returns type map.
142 *
143 * @param driverName driver name
144 * @return map of the types
145 */
146 public final Map getTypeMap(final String driverName) {
147 return (Map) jdbcDriver2typeMap.get(driverName);
148 }
149 }