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

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/jdbc/odbc/JdbcOdbcObject.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 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 java.io.ByteArrayInputStream;
 
28
import java.io.InputStream;
 
29
import java.io.Reader;
 
30
import java.io.StringReader;
 
31
import java.math.BigDecimal;
 
32
import java.net.URL;
 
33
import java.sql.Array;
 
34
import java.sql.Blob;
 
35
import java.sql.Clob;
 
36
import java.sql.Date;
 
37
import java.sql.NClob;
 
38
import java.sql.Ref;
 
39
import java.sql.RowId;
 
40
import java.sql.SQLException;
 
41
import java.sql.SQLXML;
 
42
import java.sql.Time;
 
43
import java.sql.Timestamp;
 
44
import java.util.Calendar;
 
45
import java.util.Map;
 
46
 
 
47
import cli.System.Convert;
 
48
import cli.System.DBNull;
 
49
import cli.System.IConvertible;
 
50
import cli.System.Int16;
 
51
import cli.System.Int32;
 
52
import cli.System.Int64;
 
53
import cli.System.Single;
 
54
 
 
55
/**
 
56
 * @author Volker Berlin
 
57
 */
 
58
public abstract class JdbcOdbcObject{
 
59
 
 
60
    private boolean wasNull;
 
61
 
 
62
 
 
63
    /**
 
64
     * Maps the given ResultSet column label to its ResultSet column index or the given CallableStatment parameter name
 
65
     * to the parameter index.
 
66
     * 
 
67
     * @param columnLabel
 
68
     *            the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified,
 
69
     *            then the label is the name of the column
 
70
     * @return the column index of the given column name
 
71
     * @throws SQLException
 
72
     *             if the ResultSet object does not contain a column labeled columnLabel, a database access error occurs
 
73
     *             or this method is called on a closed result set
 
74
     */
 
75
    public abstract int findColumn(String columnLabel) throws SQLException;
 
76
 
 
77
 
 
78
    /**
 
79
     * Read an Object from the current row store at the current row on the given column.
 
80
     * 
 
81
     * @param columnIndex
 
82
     *            a JDBC column index starting with 1
 
83
     * @return a .NET Object, DBNull or null
 
84
     * @throws SQLException
 
85
     *             if the result is closed or any other error occur.
 
86
     */
 
87
    protected abstract Object getObjectImpl(int columnIndex) throws SQLException;
 
88
 
 
89
 
 
90
    /**
 
91
     * Read an Object from the current row store at the current row on the given column. Set the flag wasNull.
 
92
     * 
 
93
     * @param columnIndex
 
94
     *            a JDBC column index starting with 1
 
95
     * @return a .NET Object or null
 
96
     * @throws SQLException
 
97
     *             if the result is closed or any other error occur.
 
98
     */
 
99
    private final Object getObjectSetWasNull(int columnIndex) throws SQLException{
 
100
        Object obj = getObjectImpl(columnIndex);
 
101
        if(obj == null || obj == DBNull.Value){
 
102
            wasNull = true;
 
103
            return null;
 
104
        }
 
105
        wasNull = false;
 
106
        return obj;
 
107
    }
 
108
 
 
109
 
 
110
    public final Array getArray(int columnIndex){
 
111
        throw new UnsupportedOperationException();
 
112
    }
 
113
 
 
114
 
 
115
    public final Array getArray(String columnLabel) throws SQLException{
 
116
        return getArray(findColumn(columnLabel));
 
117
    }
 
118
 
 
119
 
 
120
    public final InputStream getAsciiStream(int columnIndex) throws SQLException{
 
121
        try{
 
122
            String str = getString(columnIndex);
 
123
            if(str == null){
 
124
                return null;
 
125
            }
 
126
            return new ByteArrayInputStream(str.getBytes("Ascii"));
 
127
        }catch(Throwable th){
 
128
            throw JdbcOdbcUtils.createSQLException(th);
 
129
        }
 
130
    }
 
131
 
 
132
 
 
133
    public final InputStream getAsciiStream(String columnLabel) throws SQLException{
 
134
        return getAsciiStream(findColumn(columnLabel));
 
135
    }
 
136
 
 
137
 
 
138
    public final BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException{
 
139
        BigDecimal dec = getBigDecimal(columnIndex);
 
140
        if(dec == null){
 
141
            return null;
 
142
        }
 
143
        if(dec.scale() != scale){
 
144
            return dec.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
 
145
        }
 
146
        return dec;
 
147
    }
 
148
 
 
149
 
 
150
    public final BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException{
 
151
        return getBigDecimal(findColumn(columnLabel), scale);
 
152
    }
 
153
 
 
154
 
 
155
    public final BigDecimal getBigDecimal(int columnIndex) throws SQLException{
 
156
        try{
 
157
            Object obj = getObjectSetWasNull(columnIndex);
 
158
            if(wasNull){
 
159
                return null;
 
160
            }
 
161
            String str = obj.toString();
 
162
            return new BigDecimal(str);
 
163
        }catch(Throwable th){
 
164
            throw JdbcOdbcUtils.createSQLException(th);
 
165
        }
 
166
    }
 
167
 
 
168
 
 
169
    public final BigDecimal getBigDecimal(String columnLabel) throws SQLException{
 
170
        return getBigDecimal(findColumn(columnLabel));
 
171
    }
 
172
 
 
173
 
 
174
    public final InputStream getBinaryStream(int columnIndex) throws SQLException{
 
175
        byte[] data = getBytes(columnIndex);
 
176
        if(data == null){
 
177
            return null;
 
178
        }
 
179
        return new ByteArrayInputStream(data);
 
180
    }
 
181
 
 
182
 
 
183
    public final InputStream getBinaryStream(String columnLabel) throws SQLException{
 
184
        return getBinaryStream(findColumn(columnLabel));
 
185
    }
 
186
 
 
187
 
 
188
    public final Blob getBlob(int columnIndex){
 
189
        throw new UnsupportedOperationException();
 
190
    }
 
191
 
 
192
 
 
193
    public final Blob getBlob(String columnLabel) throws SQLException{
 
194
        return getBlob(findColumn(columnLabel));
 
195
    }
 
196
 
 
197
 
 
198
    public final boolean getBoolean(int columnIndex) throws SQLException{
 
199
        try{
 
200
            Object obj = getObjectSetWasNull(columnIndex);
 
201
            if(wasNull){
 
202
                return false;
 
203
            }
 
204
            if(obj instanceof IConvertible){
 
205
                return Convert.ToBoolean(obj);
 
206
            }
 
207
            String str = obj.toString();
 
208
            if(str.length() > 0){
 
209
                // special handling for boolean representation in old databases
 
210
                char ch = str.charAt(0);
 
211
                if(ch == 'T' || ch == 't'){
 
212
                    return true;
 
213
                }
 
214
                if(ch == 'F' || ch == 'f'){
 
215
                    return true;
 
216
                }
 
217
            }
 
218
            return cli.System.Boolean.Parse(str);
 
219
        }catch(Throwable th){
 
220
            throw JdbcOdbcUtils.createSQLException(th);
 
221
        }
 
222
    }
 
223
 
 
224
 
 
225
    public final boolean getBoolean(String columnLabel) throws SQLException{
 
226
        return getBoolean(findColumn(columnLabel));
 
227
    }
 
228
 
 
229
 
 
230
    public final byte getByte(int columnIndex) throws SQLException{
 
231
        try{
 
232
            Object obj = getObjectSetWasNull(columnIndex);
 
233
            if(wasNull){
 
234
                return 0;
 
235
            }
 
236
            if(obj instanceof IConvertible){
 
237
                return Convert.ToByte(obj);
 
238
            }
 
239
            String str = obj.toString();
 
240
            return cli.System.Byte.Parse(str);
 
241
        }catch(Throwable th){
 
242
            throw JdbcOdbcUtils.createSQLException(th);
 
243
        }
 
244
    }
 
245
 
 
246
 
 
247
    public final byte getByte(String columnLabel) throws SQLException{
 
248
        return getByte(findColumn(columnLabel));
 
249
    }
 
250
 
 
251
 
 
252
    public final byte[] getBytes(int columnIndex) throws SQLException{
 
253
        try{
 
254
            Object obj = getObjectSetWasNull(columnIndex);
 
255
            if(wasNull){
 
256
                return null;
 
257
            }
 
258
            if(obj instanceof byte[]){
 
259
                return (byte[])obj;
 
260
            }
 
261
            String str = obj.toString();
 
262
            return str.getBytes(); // which encoding?
 
263
        }catch(Throwable th){
 
264
            throw JdbcOdbcUtils.createSQLException(th);
 
265
        }
 
266
    }
 
267
 
 
268
 
 
269
    public final byte[] getBytes(String columnLabel) throws SQLException{
 
270
        return getBytes(findColumn(columnLabel));
 
271
    }
 
272
 
 
273
 
 
274
    public final Reader getCharacterStream(int columnIndex) throws SQLException{
 
275
        String str = getString(columnIndex);
 
276
        if(str == null){
 
277
            return null;
 
278
        }
 
279
        return new StringReader(str);
 
280
    }
 
281
 
 
282
 
 
283
    public final Reader getCharacterStream(String columnLabel) throws SQLException{
 
284
        return getCharacterStream(findColumn(columnLabel));
 
285
    }
 
286
 
 
287
 
 
288
    public final Clob getClob(int columnIndex){
 
289
        throw new UnsupportedOperationException();
 
290
    }
 
291
 
 
292
 
 
293
    public final Clob getClob(String columnLabel) throws SQLException{
 
294
        return getClob(findColumn(columnLabel));
 
295
    }
 
296
 
 
297
 
 
298
    public final Date getDate(int columnIndex) throws SQLException{
 
299
        try{
 
300
            Object obj = getObjectSetWasNull(columnIndex);
 
301
            if(wasNull){
 
302
                return null;
 
303
            }
 
304
            if(obj instanceof cli.System.DateTime){
 
305
                cli.System.DateTime dt = (cli.System.DateTime)obj;
 
306
                return new Date(dt.get_Year() - 1900, dt.get_Month() - 1, dt.get_Day());
 
307
            }
 
308
            String str = obj.toString();
 
309
            return Date.valueOf(str);
 
310
        }catch(Throwable th){
 
311
            throw JdbcOdbcUtils.createSQLException(th);
 
312
        }
 
313
    }
 
314
 
 
315
 
 
316
    public final Date getDate(String columnLabel) throws SQLException{
 
317
        return getDate(findColumn(columnLabel));
 
318
    }
 
319
 
 
320
 
 
321
    public final Date getDate(int columnIndex, Calendar cal) throws SQLException{
 
322
        Date date = getDate(columnIndex);
 
323
        JdbcOdbcUtils.convertLocalToCalendarDate(date, cal);
 
324
        return date;
 
325
    }
 
326
 
 
327
 
 
328
    public final Date getDate(String columnLabel, Calendar cal) throws SQLException{
 
329
        return getDate(findColumn(columnLabel), cal);
 
330
    }
 
331
 
 
332
 
 
333
    public final double getDouble(int columnIndex) throws SQLException{
 
334
        try{
 
335
            Object obj = getObjectSetWasNull(columnIndex);
 
336
            if(wasNull){
 
337
                return 0;
 
338
            }
 
339
            if(obj instanceof IConvertible){
 
340
                return Convert.ToDouble(obj);
 
341
            }
 
342
            String str = obj.toString();
 
343
            return cli.System.Double.Parse(str);
 
344
        }catch(Throwable th){
 
345
            throw JdbcOdbcUtils.createSQLException(th);
 
346
        }
 
347
    }
 
348
 
 
349
 
 
350
    public final double getDouble(String columnLabel) throws SQLException{
 
351
        return getDouble(findColumn(columnLabel));
 
352
    }
 
353
 
 
354
 
 
355
    public final float getFloat(int columnIndex) throws SQLException{
 
356
        try{
 
357
            Object obj = getObjectSetWasNull(columnIndex);
 
358
            if(wasNull){
 
359
                return 0;
 
360
            }
 
361
            if(obj instanceof IConvertible){
 
362
                return Convert.ToSingle(obj);
 
363
            }
 
364
            String str = obj.toString();
 
365
            return Single.Parse(str);
 
366
        }catch(Throwable th){
 
367
            throw JdbcOdbcUtils.createSQLException(th);
 
368
        }
 
369
    }
 
370
 
 
371
 
 
372
    public final float getFloat(String columnLabel) throws SQLException{
 
373
        return getFloat(findColumn(columnLabel));
 
374
    }
 
375
 
 
376
 
 
377
    public final int getInt(int columnIndex) throws SQLException{
 
378
        try{
 
379
            Object obj = getObjectSetWasNull(columnIndex);
 
380
            if(wasNull){
 
381
                return 0;
 
382
            }
 
383
            if(obj instanceof IConvertible){
 
384
                return Convert.ToInt32(obj);
 
385
            }
 
386
            String str = obj.toString();
 
387
            return Int32.Parse(str);
 
388
        }catch(Throwable th){
 
389
            throw JdbcOdbcUtils.createSQLException(th);
 
390
        }
 
391
    }
 
392
 
 
393
 
 
394
    public final int getInt(String columnLabel) throws SQLException{
 
395
        return getInt(findColumn(columnLabel));
 
396
    }
 
397
 
 
398
 
 
399
    public final long getLong(int columnIndex) throws SQLException{
 
400
        try{
 
401
            Object obj = getObjectSetWasNull(columnIndex);
 
402
            if(wasNull){
 
403
                return 0;
 
404
            }
 
405
            if(obj instanceof IConvertible){
 
406
                return Convert.ToInt64(obj);
 
407
            }
 
408
            String str = obj.toString();
 
409
            return Int64.Parse(str);
 
410
        }catch(Throwable th){
 
411
            throw JdbcOdbcUtils.createSQLException(th);
 
412
        }
 
413
    }
 
414
 
 
415
 
 
416
    public final long getLong(String columnLabel) throws SQLException{
 
417
        return getLong(findColumn(columnLabel));
 
418
    }
 
419
 
 
420
 
 
421
    public final Reader getNCharacterStream(int columnIndex) throws SQLException{
 
422
        return getCharacterStream(columnIndex);
 
423
    }
 
424
 
 
425
 
 
426
    public final Reader getNCharacterStream(String columnLabel) throws SQLException{
 
427
        return getCharacterStream(columnLabel);
 
428
    }
 
429
 
 
430
 
 
431
    public final NClob getNClob(int columnIndex){
 
432
        throw new UnsupportedOperationException();
 
433
    }
 
434
 
 
435
 
 
436
    public final NClob getNClob(String columnLabel) throws SQLException{
 
437
        return getNClob(findColumn(columnLabel));
 
438
    }
 
439
 
 
440
 
 
441
    public final String getNString(int columnIndex) throws SQLException{
 
442
        return getString(columnIndex);
 
443
    }
 
444
 
 
445
 
 
446
    public final String getNString(String columnLabel) throws SQLException{
 
447
        return getString(columnLabel);
 
448
    }
 
449
 
 
450
 
 
451
    public final Object getObject(int columnIndex) throws SQLException{
 
452
        return JdbcOdbcUtils.convertNet2Java(getObjectSetWasNull(columnIndex));
 
453
    }
 
454
 
 
455
 
 
456
    public final Object getObject(String columnLabel) throws SQLException{
 
457
        return getObject(findColumn(columnLabel));
 
458
    }
 
459
 
 
460
 
 
461
    public final Object getObject(int columnIndex, Map<String, Class<?>> map){
 
462
        throw new UnsupportedOperationException();
 
463
    }
 
464
 
 
465
 
 
466
    public final Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException{
 
467
        return getObject(findColumn(columnLabel), map);
 
468
    }
 
469
 
 
470
 
 
471
    public final Ref getRef(int columnIndex){
 
472
        throw new UnsupportedOperationException();
 
473
    }
 
474
 
 
475
 
 
476
    public final Ref getRef(String columnLabel) throws SQLException{
 
477
        return getRef(findColumn(columnLabel));
 
478
    }
 
479
 
 
480
 
 
481
    public final RowId getRowId(int columnIndex){
 
482
        throw new UnsupportedOperationException();
 
483
    }
 
484
 
 
485
 
 
486
    public final RowId getRowId(String columnLabel) throws SQLException{
 
487
        return getRowId(findColumn(columnLabel));
 
488
    }
 
489
 
 
490
 
 
491
    public final SQLXML getSQLXML(int columnIndex){
 
492
        throw new UnsupportedOperationException();
 
493
    }
 
494
 
 
495
 
 
496
    public final SQLXML getSQLXML(String columnLabel) throws SQLException{
 
497
        return getSQLXML(findColumn(columnLabel));
 
498
    }
 
499
 
 
500
 
 
501
    public final short getShort(int columnIndex) throws SQLException{
 
502
        try{
 
503
            Object obj = getObjectSetWasNull(columnIndex);
 
504
            if(wasNull){
 
505
                return 0;
 
506
            }
 
507
            if(obj instanceof IConvertible){
 
508
                return Convert.ToInt16(obj);
 
509
            }
 
510
            String str = obj.toString();
 
511
            return Int16.Parse(str);
 
512
        }catch(Throwable th){
 
513
            throw JdbcOdbcUtils.createSQLException(th);
 
514
        }
 
515
    }
 
516
 
 
517
 
 
518
    public final short getShort(String columnLabel) throws SQLException{
 
519
        return getShort(findColumn(columnLabel));
 
520
    }
 
521
 
 
522
 
 
523
    public final String getString(int columnIndex) throws SQLException{
 
524
        try{
 
525
            Object obj = getObjectSetWasNull(columnIndex);
 
526
            if(wasNull){
 
527
                return null;
 
528
            }
 
529
            return obj.toString();
 
530
        }catch(Throwable th){
 
531
            throw JdbcOdbcUtils.createSQLException(th);
 
532
        }
 
533
    }
 
534
 
 
535
 
 
536
    public final String getString(String columnLabel) throws SQLException{
 
537
        return getString(findColumn(columnLabel));
 
538
    }
 
539
 
 
540
 
 
541
    public final Time getTime(int columnIndex) throws SQLException{
 
542
        try{
 
543
            Object obj = getObjectSetWasNull(columnIndex);
 
544
            if(wasNull){
 
545
                return null;
 
546
            }
 
547
            if(obj instanceof cli.System.DateTime){
 
548
                cli.System.DateTime dt = (cli.System.DateTime)obj;
 
549
                return new Time(dt.get_Hour(), dt.get_Minute() - 1, dt.get_Second());
 
550
            }
 
551
            if(obj instanceof cli.System.TimeSpan){
 
552
                cli.System.TimeSpan ts = (cli.System.TimeSpan)obj;
 
553
                return new Time(ts.get_Hours(), ts.get_Minutes() - 1, ts.get_Seconds());
 
554
            }
 
555
            String str = obj.toString();
 
556
            return Time.valueOf(str);
 
557
        }catch(Throwable th){
 
558
            throw JdbcOdbcUtils.createSQLException(th);
 
559
        }
 
560
    }
 
561
 
 
562
 
 
563
    public final Time getTime(String columnLabel) throws SQLException{
 
564
        return getTime(findColumn(columnLabel));
 
565
    }
 
566
 
 
567
 
 
568
    public final Time getTime(int columnIndex, Calendar cal) throws SQLException{
 
569
        Time time = getTime(columnIndex);
 
570
        JdbcOdbcUtils.convertLocalToCalendarDate(time, cal);
 
571
        return time;
 
572
    }
 
573
 
 
574
 
 
575
    public final Time getTime(String columnLabel, Calendar cal) throws SQLException{
 
576
        return getTime(findColumn(columnLabel), cal);
 
577
    }
 
578
 
 
579
 
 
580
    public final Timestamp getTimestamp(int columnIndex) throws SQLException{
 
581
        try{
 
582
            Object obj = getObjectSetWasNull(columnIndex);
 
583
            if(wasNull){
 
584
                return null;
 
585
            }
 
586
            if(obj instanceof cli.System.DateTime){
 
587
                return JdbcOdbcUtils.convertDateTimeToTimestamp((cli.System.DateTime)obj);
 
588
            }
 
589
            String str = obj.toString();
 
590
            return Timestamp.valueOf(str);
 
591
        }catch(Throwable th){
 
592
            throw JdbcOdbcUtils.createSQLException(th);
 
593
        }
 
594
    }
 
595
 
 
596
 
 
597
    public final Timestamp getTimestamp(String columnLabel) throws SQLException{
 
598
        return getTimestamp(findColumn(columnLabel));
 
599
    }
 
600
 
 
601
 
 
602
    public final Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException{
 
603
        Timestamp ts = getTimestamp(columnIndex);
 
604
        JdbcOdbcUtils.convertLocalToCalendarDate(ts, cal);
 
605
        return ts;
 
606
    }
 
607
 
 
608
 
 
609
    public final Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException{
 
610
        return getTimestamp(findColumn(columnLabel), cal);
 
611
    }
 
612
 
 
613
 
 
614
    public final URL getURL(int columnIndex) throws SQLException{
 
615
        try{
 
616
            String url = getString(columnIndex);
 
617
            if(wasNull){
 
618
                return null;
 
619
            }
 
620
            return new URL(url);
 
621
        }catch(Throwable th){
 
622
            throw JdbcOdbcUtils.createSQLException(th);
 
623
        }
 
624
    }
 
625
 
 
626
 
 
627
    public final URL getURL(String columnLabel) throws SQLException{
 
628
        return getURL(findColumn(columnLabel));
 
629
    }
 
630
 
 
631
 
 
632
    public final InputStream getUnicodeStream(int columnIndex) throws SQLException{
 
633
        try{
 
634
            return new ByteArrayInputStream(getString(columnIndex).getBytes("UTF16"));
 
635
        }catch(Throwable th){
 
636
            throw JdbcOdbcUtils.createSQLException(th);
 
637
        }
 
638
    }
 
639
 
 
640
 
 
641
    public final InputStream getUnicodeStream(String columnLabel) throws SQLException{
 
642
        return getUnicodeStream(findColumn(columnLabel));
 
643
    }
 
644
 
 
645
 
 
646
    public final boolean wasNull(){
 
647
        return wasNull;
 
648
    }
 
649
 
 
650
}