~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/jdbc/odbc/JdbcOdbcUtils.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2009, 2010 Volker Berlin (i-net software)
 
3
  Copyright (C) 2011 Karsten Heinrich (i-net software)
 
4
 
 
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.
 
8
 
 
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:
 
12
 
 
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.
 
20
 
 
21
  Jeroen Frijters
 
22
  jeroen@frijters.net
 
23
  
 
24
 */
 
25
package sun.jdbc.odbc;
 
26
 
 
27
import ikvm.lang.CIL;
 
28
 
 
29
import java.math.BigDecimal;
 
30
import java.sql.*;
 
31
import java.util.Calendar;
 
32
import java.util.HashMap;
 
33
 
 
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;
 
40
 
 
41
/**
 
42
 * @author Volker Berlin
 
43
 */
 
44
public class JdbcOdbcUtils{
 
45
 
 
46
    private static final HashMap<String, String> classNameMap = new HashMap<String, String>();
 
47
    static{
 
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");
 
56
    }
 
57
 
 
58
 
 
59
    /**
 
60
     * Solve a mapping between .NET class names and the equivalent Java class names for
 
61
     * ResultSetMetaData.getColumnClassName
 
62
     * 
 
63
     * @param netClassName
 
64
     *            the .NET class name
 
65
     * @return the Java class name
 
66
     */
 
67
    public static String getJavaClassName(String netClassName){
 
68
        String javaClassName = classNameMap.get(netClassName);
 
69
        if(javaClassName != null){
 
70
            return javaClassName;
 
71
        }
 
72
        return "java.lang.Object";
 
73
    }
 
74
 
 
75
 
 
76
    /**
 
77
     * Convert a .NET Object in the equals Java Object.
 
78
     * 
 
79
     * @param obj
 
80
     *            the .NET Object
 
81
     * @return a Java Object
 
82
     */
 
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));
 
86
        }
 
87
        if(obj instanceof cli.System.Int32){
 
88
            return Integer.valueOf(CIL.unbox_int(obj));
 
89
        }
 
90
        if(obj instanceof cli.System.Int16){
 
91
            return Short.valueOf(CIL.unbox_short(obj));
 
92
        }
 
93
        if(obj instanceof cli.System.Byte){
 
94
            return Byte.valueOf(CIL.unbox_byte(obj));
 
95
        }
 
96
        if(obj instanceof cli.System.Double){
 
97
            return Double.valueOf(CIL.unbox_double(obj));
 
98
        }
 
99
        if(obj instanceof cli.System.Single){
 
100
            return Float.valueOf(CIL.unbox_float(obj));
 
101
        }
 
102
        if(obj instanceof cli.System.Boolean){
 
103
            return Boolean.valueOf(CIL.unbox_boolean(obj));
 
104
        }
 
105
        if(obj instanceof cli.System.Decimal){
 
106
            return new BigDecimal(((cli.System.Decimal)obj).ToString(CultureInfo.get_InvariantCulture()));
 
107
        }
 
108
        if(obj instanceof cli.System.DateTime){
 
109
                return convertDateTimeToTimestamp((cli.System.DateTime)obj);
 
110
        }
 
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());
 
114
        }
 
115
        if(obj instanceof cli.System.DBNull){
 
116
            return null;
 
117
        }
 
118
        return obj;
 
119
    }
 
120
 
 
121
    /**
 
122
     * Converts a .NET DateTime to a Timestamp in the current Timezone
 
123
     * @param obj the dateTime
 
124
     * @return the conveted time stamp
 
125
     */
 
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 );
 
131
        }
 
132
 
 
133
 
 
134
    /**
 
135
     * Convert a Java Object in the equals .NET Object.
 
136
     * 
 
137
     * @param obj
 
138
     *            Java Object
 
139
     * @param length
 
140
     *            the length of data if obj is a stream
 
141
     * @return .NET Object
 
142
     */
 
143
    public static Object convertJava2Net(Object obj, int length){
 
144
        // TODO use the length with streams
 
145
        return convertJava2Net(obj);
 
146
    }
 
147
 
 
148
 
 
149
    /**
 
150
     * Convert a Java Object in the equals .NET Object.
 
151
     * 
 
152
     * @param obj
 
153
     *            Java Object
 
154
     * @return a .NET Object
 
155
     */
 
156
    public static Object convertJava2Net(Object obj){
 
157
        if(obj == null){
 
158
            return DBNull.Value;
 
159
        }
 
160
        if(obj instanceof Double){
 
161
            return CIL.box_double(((Double)obj).doubleValue());
 
162
        }
 
163
        if(obj instanceof Float){
 
164
            return CIL.box_float(((Float)obj).floatValue());
 
165
        }
 
166
        if(obj instanceof Long){
 
167
            return CIL.box_long(((Long)obj).longValue());
 
168
        }
 
169
        if(obj instanceof Integer){
 
170
            return CIL.box_int(((Integer)obj).intValue());
 
171
        }
 
172
        if(obj instanceof Short){
 
173
            return CIL.box_short(((Short)obj).shortValue());
 
174
        }
 
175
        if(obj instanceof Byte){
 
176
            return CIL.box_byte(((Byte)obj).byteValue());
 
177
        }
 
178
        if(obj instanceof Boolean){
 
179
            return CIL.box_boolean(((Boolean)obj).booleanValue());
 
180
        }
 
181
        if(obj instanceof Time){
 
182
            Time ts = (Time)obj;
 
183
            return new TimeSpan(ts.getHours(), ts.getMinutes(), ts.getSeconds());
 
184
        }
 
185
        if(obj instanceof java.util.Date){
 
186
            long ticks = getNetTicks((java.util.Date)obj);
 
187
            return new cli.System.DateTime(ticks);
 
188
        }
 
189
        if(obj instanceof BigDecimal){
 
190
            return cli.System.Decimal.Parse(obj.toString(), CultureInfo.get_InvariantCulture());
 
191
        }
 
192
        return obj;
 
193
    }
 
194
 
 
195
 
 
196
    /**
 
197
     * Get the milliseconds in the Java range from a .NET DateTime object.
 
198
     * 
 
199
     * @param dt
 
200
     *            the DateTime object
 
201
     * @return the milliseconds since 1970-01-01
 
202
     */
 
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;
 
207
    }
 
208
 
 
209
 
 
210
    /**
 
211
     * Get the ticks for a System.DateTime from a java.util.Date
 
212
     * 
 
213
     * @param date
 
214
     *            the java.util.Date
 
215
     * @return ticks
 
216
     */
 
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;
 
221
    }
 
222
 
 
223
 
 
224
    /**
 
225
     * Convert a local (current default) Date to a Date in the time zone of the given calendar. Do nothing if date or
 
226
     * calendar is null.
 
227
     * 
 
228
     * @param date
 
229
     *            the converting Date
 
230
     * @param cal
 
231
     *            the Calendar with the time zone
 
232
     */
 
233
    public static void convertLocalToCalendarDate(java.util.Date date, Calendar cal){
 
234
        if(date == null || cal == null){
 
235
            return;
 
236
        }
 
237
        cal.set(date.getYear() + 1900, date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date
 
238
                .getSeconds());
 
239
        long millis = cal.getTimeInMillis() / 1000 * 1000 + date.getTime() % 1000;
 
240
        date.setTime(millis);
 
241
    }
 
242
 
 
243
 
 
244
    /**
 
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.
 
247
     * 
 
248
     * @param date
 
249
     * @param cal
 
250
     */
 
251
    public static void convertCalendarToLocalDate(java.util.Date date, Calendar cal){
 
252
        if(date == null || cal == null){
 
253
            return;
 
254
        }
 
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));
 
262
    }
 
263
 
 
264
 
 
265
    /**
 
266
     * The only valid Exception for JDBC is a SQLException. Here we create one based on the real exception.
 
267
     * 
 
268
     * @param th
 
269
     *            any Throwable that occur
 
270
     * @return a SQLException, never null
 
271
     */
 
272
    public static SQLException createSQLException(Throwable th){
 
273
        if(th instanceof SQLException){
 
274
            return (SQLException)th;
 
275
        }
 
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());
 
282
                if(sqlEx == null){
 
283
                    sqlEx = newEx;
 
284
                }else{
 
285
                    sqlEx.setNextException(newEx);
 
286
                }
 
287
            }
 
288
            if(sqlEx != null){
 
289
                sqlEx.initCause(th);
 
290
                return sqlEx;
 
291
            }
 
292
        }
 
293
        if(th instanceof DbException){
 
294
            DbException dbEx = (DbException)th;
 
295
            return new SQLException(dbEx.get_Message(), "S1000", dbEx.get_ErrorCode(), th);
 
296
        }
 
297
        return new SQLException(th);
 
298
    }
 
299
 
 
300
 
 
301
    /**
 
302
     * Convert a value from java.sql.Types to a value from to a System.Data.DbType
 
303
     * 
 
304
     * @param type
 
305
     *            a JDBC type
 
306
     * @return a ADO.NET type
 
307
     * @throws SQLException
 
308
     *             if the type can not be converted
 
309
     */
 
310
    public static int convertJdbc2AdoNetType(int type) throws SQLException{
 
311
        switch(type){
 
312
            case Types.BIGINT:
 
313
                return DbType.Int64;
 
314
            case Types.BINARY:
 
315
            case Types.BLOB:
 
316
            case Types.LONGVARBINARY:
 
317
            case Types.VARBINARY:
 
318
                return DbType.Binary;
 
319
            case Types.BIT:
 
320
            case Types.BOOLEAN:
 
321
                return DbType.Boolean;
 
322
            case Types.CHAR:
 
323
                return DbType.AnsiStringFixedLength;
 
324
            case Types.CLOB:
 
325
            case Types.DATALINK:
 
326
            case Types.LONGVARCHAR:
 
327
            case Types.NULL: // we hope that the DBMS can map any NULL values from VARCHAR
 
328
            case Types.VARCHAR:
 
329
                return DbType.AnsiString;
 
330
            case Types.DATE:
 
331
                return DbType.Date;
 
332
            case Types.DECIMAL:
 
333
            case Types.NUMERIC:
 
334
                return DbType.Decimal;
 
335
            case Types.DOUBLE:
 
336
                return DbType.Double;
 
337
            case Types.FLOAT:
 
338
            case Types.REAL:
 
339
                return DbType.Single;
 
340
            case Types.INTEGER:
 
341
                return DbType.Int32;
 
342
            case Types.JAVA_OBJECT:
 
343
                return DbType.Object;
 
344
            case Types.LONGNVARCHAR:
 
345
            case Types.NCLOB:
 
346
            case Types.NVARCHAR:
 
347
                return DbType.String;
 
348
            case Types.NCHAR:
 
349
                return DbType.StringFixedLength;
 
350
            case Types.ROWID:
 
351
                return DbType.Guid;
 
352
            case Types.SMALLINT:
 
353
                return DbType.Int16;
 
354
            case Types.SQLXML:
 
355
                return DbType.Xml;
 
356
            case Types.TIME:
 
357
                return DbType.Time;
 
358
            case Types.TIMESTAMP:
 
359
                return DbType.DateTime;
 
360
            case Types.TINYINT:
 
361
                return DbType.Byte;
 
362
            case Types.ARRAY:
 
363
            case Types.DISTINCT:
 
364
            case Types.OTHER:
 
365
            case Types.REF:
 
366
            case Types.STRUCT:
 
367
                break;
 
368
 
 
369
        }
 
370
        throw new SQLException("Not supported JDBC type:" + type);
 
371
    }
 
372
}