Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JdbcOdbcUtils.java « odbc « jdbc « sun « openjdk - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ef74913d601a051881235dcbc176ed513821194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
  Copyright (C) 2009 Volker Berlin (i-net software)

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
 */
package sun.jdbc.odbc;

import ikvm.lang.CIL;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Calendar;
import java.util.HashMap;

import cli.System.Data.Common.DbException;
import cli.System.Data.Odbc.*;

/**
 * @author Volker Berlin
 */
public class JdbcOdbcUtils{

    private static final HashMap<String, String> classNameMap = new HashMap<String, String>();
    static{
        classNameMap.put("System.String", "java.lang.String");
        classNameMap.put("System.Int16", "java.lang.Short");
        classNameMap.put("System.Int32", "java.lang.Integer");
        classNameMap.put("System.Int64", "java.lang.Long");
        classNameMap.put("System.Double", "java.lang.Double");
        classNameMap.put("System.Decimal", "java.math.BigDecimal");
        classNameMap.put("System.DateTime", "java.sql.Timestamp");
        classNameMap.put("System.TimeSpan", "java.sql.Time");
    }


    /**
     * Solve a mapping between .NET class names and the equivalent Java class names for
     * ResultSetMetaData.getColumnClassName
     * 
     * @param netClassName
     *            the .NET class name
     * @return the Java class name
     */
    public static String getJavaClassName(String netClassName){
        String javaClassName = classNameMap.get(netClassName);
        if(javaClassName != null){
            return javaClassName;
        }
        return "java.lang.Object";
    }


    /**
     * Convert a .NET Object in the equals Java Object.
     * 
     * @param obj
     *            the .NET Object
     * @return a Java Object
     */
    public static java.lang.Object convertNet2Java(java.lang.Object obj){
        if(obj instanceof cli.System.Int32){
            return Integer.valueOf(CIL.unbox_int(obj));
        }
        if(obj instanceof cli.System.Int16){
            return Short.valueOf(CIL.unbox_short(obj));
        }
        if(obj instanceof cli.System.Byte){
            return Byte.valueOf(CIL.unbox_byte(obj));
        }
        if(obj instanceof cli.System.Decimal){
            return new BigDecimal(((cli.System.Decimal)obj).toString());
        }
        if(obj instanceof cli.System.DateTime){
            return new Timestamp(getJavaMillis((cli.System.DateTime)obj));
        }
        if(obj instanceof cli.System.TimeSpan){
            cli.System.TimeSpan ts = (cli.System.TimeSpan)obj;
            return new Time(ts.get_Hours(), ts.get_Minutes() - 1, ts.get_Seconds());
        }
        return obj;
    }


    /**
     * Get the milliseconds in the Java range from a .NET DateTime object.
     * 
     * @param dt
     *            the DateTime object
     * @return the milliseconds since 1970-01-01
     */
    public static long getJavaMillis(cli.System.DateTime dt){
        // calculation copied from System.currentTimeMillis()
        long january_1st_1970 = 62135596800000L;
        return dt.get_Ticks() / 10000L - january_1st_1970;
    }


    /**
     * Convert a local (current default) Date to a Date in the time zone of the given calendar. Do nothing if date or
     * calendar is null.
     * 
     * @param date
     *            the converting Date
     * @param cal
     *            the Calendar with the time zone
     */
    public static void convertLocalToCalendarDate(java.util.Date date, Calendar cal){
        if(date == null || cal == null){
            return;
        }
        cal.set(date.getYear() + 1900, date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date
                .getSeconds());
        long millis = cal.getTimeInMillis() / 1000 * 1000 + date.getTime() % 1000;
        date.setTime(millis);
    }


    /**
     * Convert a Date in the calendar time zone to a date in the local (current default) time zone. Do nothing if date
     * or calendar is null.
     * 
     * @param date
     * @param cal
     */
    public static void convertCalendarToLocalDate(java.util.Date date, Calendar cal){
        if(date == null || cal == null){
            return;
        }
        cal.setTimeInMillis(date.getTime());
        date.setYear(cal.get(Calendar.YEAR)-1900);
        date.setMonth(cal.get(Calendar.MONTH));
        date.setDate(cal.get(Calendar.DAY_OF_MONTH));
        date.setHours(cal.get(Calendar.HOUR_OF_DAY));
        date.setMinutes(cal.get(Calendar.MINUTE));
        date.setSeconds(cal.get(Calendar.SECOND));
     }


    /**
     * The only valid Exception for JDBC is a SQLException. Here we create one based on the real exception.
     * 
     * @param th
     *            any Throwable that occur
     * @return a SQLException, never null
     */
    public static SQLException createSQLException(Throwable th){
        if(th instanceof SQLException){
            return (SQLException)th;
        }
        if(th instanceof OdbcException){
            SQLException sqlEx = null;
            OdbcErrorCollection errors = ((OdbcException)th).get_Errors();
            for(int e = 0; e < errors.get_Count(); e++){
                OdbcError err = errors.get_Item(e);
                SQLException newEx = new SQLException(err.get_Message(), err.get_SQLState(), err.get_NativeError());
                if(sqlEx == null){
                    sqlEx = newEx;
                }else{
                    sqlEx.setNextException(newEx);
                }
            }
            if(sqlEx != null){
                sqlEx.initCause(th);
                return sqlEx;
            }
        }
        if(th instanceof DbException){
            DbException dbEx = (DbException)th;
            return new SQLException(dbEx.get_Message(), "S1000", dbEx.get_ErrorCode(), th);
        }
        return new SQLException(th);
    }

}