001: package org.mandarax.jdbc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.sql.SQLException;
021: import java.sql.Types;
022: import org.mandarax.util.logging.LogCategories;
023:
024: /**
025: * Some utilities. This class in in particular used to support the migration from
026: * mandarax 2.0 to mandarax 3.0. Some functions implemented here (e.g., gathering
027: * predicates) will later be provided by other classes (a meta object attached to
028: * the knowledge base)
029: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
030: * @version 3.3.2 <29 December 2004>
031: * @since 3.0
032: */
033:
034: public class JDBCUtils implements LogCategories {
035: private static final String SLOT = "slot";
036: public static final String URL_PREFIX = "jdbc:mandarax";
037: public static final String NET = ":net";
038: public static final String URL_NET_PREFIX = URL_PREFIX + NET;
039: public static final String URL_SEPARATOR = ":";
040:
041: /**
042: * Indicates that a certain feature is not supported by this driver.
043: */
044: public static void nsf() throws UnsupportedFeatureException {
045: throw new UnsupportedFeatureException(
046: "This feature is not supported by the mandarax jdbc driver - the driver is read only");
047: }
048:
049: /**
050: * Specifies the SQL type associated with a java type.
051: * @see java.sql.Types
052: * @param jtype a java type
053: * @return SQL type from java.sql.Types
054: */
055: public static int getTypeMapping(Class jtype) {
056: if (jtype == String.class)
057: return Types.VARCHAR;
058: else if (jtype == Integer.class)
059: return Types.INTEGER;
060: else if (jtype == Double.class)
061: return Types.DOUBLE;
062: else if (jtype == Float.class)
063: return Types.FLOAT;
064: else if (jtype == Short.class)
065: return Types.SMALLINT;
066: else if (jtype == Long.class)
067: return Types.BIGINT;
068: else if (jtype == Boolean.class)
069: return Types.BOOLEAN;
070: else if (jtype == java.sql.Date.class)
071: return Types.DATE;
072: // warning: util.Date instance also handled as dates !!
073: // TODO more testing and investigation of side effects
074: else if (jtype == java.util.Date.class)
075: return Types.DATE;
076: else if (jtype == java.sql.Time.class)
077: return Types.TIME;
078: else if (jtype == java.sql.Timestamp.class)
079: return Types.TIMESTAMP;
080: else
081: return Types.JAVA_OBJECT;
082: }
083:
084: /**
085: * Specifies the SQL type name associated with a java type.
086: * @see java.sql.Types
087: * @param jtype a java type
088: * @return an SQL type name
089: */
090: public static String getSQLTypeName(Class jtype) {
091: if (jtype == String.class)
092: return "varchar";
093: else if (jtype == Integer.class)
094: return "int";
095: else if (jtype == Double.class)
096: return "double";
097: else if (jtype == Float.class)
098: return "float";
099: else if (jtype == Short.class)
100: return "short";
101: else if (jtype == Long.class)
102: return "long";
103: else if (jtype == Boolean.class)
104: return "boolean";
105: else if (jtype == java.sql.Date.class)
106: return "Date";
107: else if (jtype == java.util.Date.class)
108: return "java.util.Date";
109: else if (jtype == java.sql.Time.class)
110: return "Time";
111: else if (jtype == java.sql.Timestamp.class)
112: return "Timestamp";
113: else
114: return jtype.getName();
115: }
116:
117: /**
118: * Specifies the SQL type name associated with a java type.
119: * @see java.sql.Types
120: * @param type an SQL type
121: * @return an SQL type name
122: */
123: public static String getSQLTypeName(int type) {
124: if (type == Types.VARCHAR)
125: return "varchar";
126: else if (type == Types.CHAR)
127: return "char";
128: else if (type == Types.INTEGER)
129: return "int";
130: else if (type == Types.DOUBLE)
131: return "double";
132: else if (type == Types.FLOAT)
133: return "float";
134: else if (type == Types.SMALLINT)
135: return "short";
136: else if (type == Types.BIGINT)
137: return "long";
138: else if (type == Types.BOOLEAN)
139: return "boolean";
140: else if (type == Types.DATE)
141: return "Date";
142: else if (type == Types.TIME)
143: return "Time";
144: else if (type == Types.TIMESTAMP)
145: return "Timestamp";
146: else
147: return "Object";
148: }
149:
150: /**
151: * Specifies the class name whose instances are returned.
152: * @see java.sql.Types
153: * @param type an SQL type
154: * @return a java class
155: */
156: public static Class getClassName(int type) {
157: if (type == Types.CHAR)
158: return String.class;
159: if (type == Types.VARCHAR)
160: return String.class;
161: else if (type == Types.INTEGER)
162: return Integer.class;
163: else if (type == Types.DOUBLE)
164: return Double.class;
165: else if (type == Types.FLOAT)
166: return Float.class;
167: else if (type == Types.SMALLINT)
168: return Short.class;
169: else if (type == Types.BIGINT)
170: return Long.class;
171: else if (type == Types.BOOLEAN)
172: return Boolean.class;
173: else if (type == Types.DATE)
174: return java.sql.Date.class;
175: else if (type == Types.TIME)
176: return java.sql.Time.class;
177: else if (type == Types.TIMESTAMP)
178: return java.sql.Timestamp.class;
179: else
180: return Object.class;
181: }
182:
183: /**
184: * Log a call of an unsupported method.
185: * @param methodName the name of the method
186: * @param obj the object that received the method
187: */
188: public static void notSupported(String methodName, Object obj) {
189: if (LOG_JDBC.isDebugEnabled())
190: LOG_JDBC.debug("Attempt to call unsupported method "
191: + obj.getClass().getName() + "#" + methodName
192: + " - ignore");
193: }
194:
195: /**
196: * Log a call of an unsupported method.
197: * This call throws an exception.
198: * @param methodName the name of the method
199: * @param obj the object that received the method
200: */
201: public static void notSupportedThrowException(String methodName,
202: Object obj) throws SQLException {
203: LOG_JDBC.warn("Attempt to call unsupported method "
204: + obj.getClass().getName() + "#" + methodName
205: + " - throw exception");
206: throw new UnsupportedFeatureException(
207: "This feature is not supported by the mandarax (read only) driver: "
208: + obj.getClass().getName() + "#" + methodName);
209: }
210:
211: /**
212: * Split a mandarax jdbc net driver url in three parts:
213: * <ol>
214: * <li>a local protocol name (zkb, ruleml, ref, xkb)</li>
215: * <li>a local path
216: * <li>a server name
217: * </ol>
218: * E.g., running this method with "jdbc.mandarax.net:ref:mypackage.myserver@http://localhost:8080/mandaraxjdbc"
219: * yields {ref,mypackage.myserver,http://localhost:8080/mandaraxjdbc}
220: * @param url a url
221: * @return an array containing three strings, or null indicating that the url is not
222: * a mandarax jdbc net url
223: */
224: public static String[] parseNetUrl(String url) {
225: try {
226: if (!url.startsWith(URL_NET_PREFIX))
227: return null;
228: String[] arr = new String[3];
229: String txt = url.substring(URL_NET_PREFIX.length() + 1);
230:
231: String[] parts = split(txt, URL_SEPARATOR);
232: if (parts == null)
233: return null;
234: arr[0] = parts[0];
235:
236: parts = split(parts[1], "@");
237: if (parts == null)
238: return null;
239:
240: arr[1] = parts[0];
241: arr[2] = parts[1];
242: return arr;
243:
244: } catch (Exception x) {
245: return null;
246: }
247: }
248:
249: /**
250: * Get the server address from a mandarax jdbc net url.
251: * @param url a URL
252: * @return a server url
253: */
254: public static String getServerPart(String url) {
255: String[] parts = parseNetUrl(url);
256: if (url == null)
257: return null;
258: else
259: return parts[2];
260: }
261:
262: /**
263: * Split a string. E.g., calling this method with "23"+"54","+" yields {"23","54"}
264: * @param txt a string
265: * @param sep a separator
266: * @return an array of two strings or null if the separator has not be found
267: */
268: public static String[] split(String txt, String sep) {
269: int position = txt.indexOf(sep);
270: if (position == -1)
271: return null;
272: String[] parts = new String[2];
273: parts[0] = txt.substring(0, position);
274: parts[1] = txt.substring(position + sep.length());
275: return parts;
276: }
277:
278: public static void main(String[] args) {
279: System.out
280: .println(getServerPart("jdbc:mandarax:net:/exampledata/examplefamily.zkb@http://localhost:8080"));
281: }
282: }
|