2
Copyright (C) 2009, 2010 Volker Berlin (i-net software)
3
Copyright (C) 2011 Karsten Heinrich (i-net software)
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
25
package sun.jdbc.odbc;
29
import java.math.BigDecimal;
31
import java.util.Calendar;
32
import java.util.HashMap;
34
import cli.System.DBNull;
35
import cli.System.TimeSpan;
36
import cli.System.Data.DbType;
37
import cli.System.Data.Common.DbException;
38
import cli.System.Data.Odbc.*;
39
import cli.System.Globalization.CultureInfo;
42
* @author Volker Berlin
44
public class JdbcOdbcUtils{
46
private static final HashMap<String, String> classNameMap = new HashMap<String, String>();
48
classNameMap.put("System.String", "java.lang.String");
49
classNameMap.put("System.Int16", "java.lang.Short");
50
classNameMap.put("System.Int32", "java.lang.Integer");
51
classNameMap.put("System.Int64", "java.lang.Long");
52
classNameMap.put("System.Double", "java.lang.Double");
53
classNameMap.put("System.Decimal", "java.math.BigDecimal");
54
classNameMap.put("System.DateTime", "java.sql.Timestamp");
55
classNameMap.put("System.TimeSpan", "java.sql.Time");
60
* Solve a mapping between .NET class names and the equivalent Java class names for
61
* ResultSetMetaData.getColumnClassName
65
* @return the Java class name
67
public static String getJavaClassName(String netClassName){
68
String javaClassName = classNameMap.get(netClassName);
69
if(javaClassName != null){
72
return "java.lang.Object";
77
* Convert a .NET Object in the equals Java Object.
81
* @return a Java Object
83
public static java.lang.Object convertNet2Java(java.lang.Object obj){
84
if(obj instanceof cli.System.Int64){
85
return Long.valueOf(CIL.unbox_long(obj));
87
if(obj instanceof cli.System.Int32){
88
return Integer.valueOf(CIL.unbox_int(obj));
90
if(obj instanceof cli.System.Int16){
91
return Short.valueOf(CIL.unbox_short(obj));
93
if(obj instanceof cli.System.Byte){
94
return Byte.valueOf(CIL.unbox_byte(obj));
96
if(obj instanceof cli.System.Double){
97
return Double.valueOf(CIL.unbox_double(obj));
99
if(obj instanceof cli.System.Single){
100
return Float.valueOf(CIL.unbox_float(obj));
102
if(obj instanceof cli.System.Boolean){
103
return Boolean.valueOf(CIL.unbox_boolean(obj));
105
if(obj instanceof cli.System.Decimal){
106
return new BigDecimal(((cli.System.Decimal)obj).ToString(CultureInfo.get_InvariantCulture()));
108
if(obj instanceof cli.System.DateTime){
109
return convertDateTimeToTimestamp((cli.System.DateTime)obj);
111
if(obj instanceof cli.System.TimeSpan){
112
cli.System.TimeSpan ts = (cli.System.TimeSpan)obj;
113
return new Time(ts.get_Hours(), ts.get_Minutes(), ts.get_Seconds());
115
if(obj instanceof cli.System.DBNull){
122
* Converts a .NET DateTime to a Timestamp in the current Timezone
123
* @param obj the dateTime
124
* @return the conveted time stamp
126
public static Timestamp convertDateTimeToTimestamp( cli.System.DateTime obj) {
127
long javaMillis = getJavaMillis(obj);
128
int seconds = (int)(javaMillis / 1000);
129
int nanos = (int)((javaMillis % 1000) * 1000000);
130
return new Timestamp( 70, 0, 1, 0, 0, seconds, nanos );
135
* Convert a Java Object in the equals .NET Object.
140
* the length of data if obj is a stream
141
* @return .NET Object
143
public static Object convertJava2Net(Object obj, int length){
144
// TODO use the length with streams
145
return convertJava2Net(obj);
150
* Convert a Java Object in the equals .NET Object.
154
* @return a .NET Object
156
public static Object convertJava2Net(Object obj){
160
if(obj instanceof Double){
161
return CIL.box_double(((Double)obj).doubleValue());
163
if(obj instanceof Float){
164
return CIL.box_float(((Float)obj).floatValue());
166
if(obj instanceof Long){
167
return CIL.box_long(((Long)obj).longValue());
169
if(obj instanceof Integer){
170
return CIL.box_int(((Integer)obj).intValue());
172
if(obj instanceof Short){
173
return CIL.box_short(((Short)obj).shortValue());
175
if(obj instanceof Byte){
176
return CIL.box_byte(((Byte)obj).byteValue());
178
if(obj instanceof Boolean){
179
return CIL.box_boolean(((Boolean)obj).booleanValue());
181
if(obj instanceof Time){
183
return new TimeSpan(ts.getHours(), ts.getMinutes(), ts.getSeconds());
185
if(obj instanceof java.util.Date){
186
long ticks = getNetTicks((java.util.Date)obj);
187
return new cli.System.DateTime(ticks);
189
if(obj instanceof BigDecimal){
190
return cli.System.Decimal.Parse(obj.toString(), CultureInfo.get_InvariantCulture());
197
* Get the milliseconds in the Java range from a .NET DateTime object.
200
* the DateTime object
201
* @return the milliseconds since 1970-01-01
203
public static long getJavaMillis(cli.System.DateTime dt){
204
// calculation copied from System.currentTimeMillis()
205
long january_1st_1970 = 62135596800000L;
206
return dt.get_Ticks() / 10000L - january_1st_1970;
211
* Get the ticks for a System.DateTime from a java.util.Date
217
public static long getNetTicks(java.util.Date date){
218
// inverse from getJavaMillis
219
long january_1st_1970 = 62135596800000L;
220
return (date.getTime() + january_1st_1970) * 10000L;
225
* Convert a local (current default) Date to a Date in the time zone of the given calendar. Do nothing if date or
229
* the converting Date
231
* the Calendar with the time zone
233
public static void convertLocalToCalendarDate(java.util.Date date, Calendar cal){
234
if(date == null || cal == null){
237
cal.set(date.getYear() + 1900, date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date
239
long millis = cal.getTimeInMillis() / 1000 * 1000 + date.getTime() % 1000;
240
date.setTime(millis);
245
* Convert a Date in the calendar time zone to a date in the local (current default) time zone. Do nothing if date
246
* or calendar is null.
251
public static void convertCalendarToLocalDate(java.util.Date date, Calendar cal){
252
if(date == null || cal == null){
255
cal.setTimeInMillis(date.getTime());
256
date.setYear(cal.get(Calendar.YEAR) - 1900);
257
date.setMonth(cal.get(Calendar.MONTH));
258
date.setDate(cal.get(Calendar.DAY_OF_MONTH));
259
date.setHours(cal.get(Calendar.HOUR_OF_DAY));
260
date.setMinutes(cal.get(Calendar.MINUTE));
261
date.setSeconds(cal.get(Calendar.SECOND));
266
* The only valid Exception for JDBC is a SQLException. Here we create one based on the real exception.
269
* any Throwable that occur
270
* @return a SQLException, never null
272
public static SQLException createSQLException(Throwable th){
273
if(th instanceof SQLException){
274
return (SQLException)th;
276
if(th instanceof OdbcException){
277
SQLException sqlEx = null;
278
OdbcErrorCollection errors = ((OdbcException)th).get_Errors();
279
for(int e = 0; e < errors.get_Count(); e++){
280
OdbcError err = errors.get_Item(e);
281
SQLException newEx = new SQLException(err.get_Message(), err.get_SQLState(), err.get_NativeError());
285
sqlEx.setNextException(newEx);
293
if(th instanceof DbException){
294
DbException dbEx = (DbException)th;
295
return new SQLException(dbEx.get_Message(), "S1000", dbEx.get_ErrorCode(), th);
297
return new SQLException(th);
302
* Convert a value from java.sql.Types to a value from to a System.Data.DbType
306
* @return a ADO.NET type
307
* @throws SQLException
308
* if the type can not be converted
310
public static int convertJdbc2AdoNetType(int type) throws SQLException{
316
case Types.LONGVARBINARY:
317
case Types.VARBINARY:
318
return DbType.Binary;
321
return DbType.Boolean;
323
return DbType.AnsiStringFixedLength;
326
case Types.LONGVARCHAR:
327
case Types.NULL: // we hope that the DBMS can map any NULL values from VARCHAR
329
return DbType.AnsiString;
334
return DbType.Decimal;
336
return DbType.Double;
339
return DbType.Single;
342
case Types.JAVA_OBJECT:
343
return DbType.Object;
344
case Types.LONGNVARCHAR:
347
return DbType.String;
349
return DbType.StringFixedLength;
358
case Types.TIMESTAMP:
359
return DbType.DateTime;
370
throw new SQLException("Not supported JDBC type:" + type);