2
Copyright (C) 2009 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;
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;
33
import java.sql.Array;
37
import java.sql.NClob;
39
import java.sql.RowId;
40
import java.sql.SQLException;
41
import java.sql.SQLXML;
43
import java.sql.Timestamp;
44
import java.util.Calendar;
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;
56
* @author Volker Berlin
58
public abstract class JdbcOdbcObject{
60
private boolean wasNull;
64
* Maps the given ResultSet column label to its ResultSet column index or the given CallableStatment parameter name
65
* to the parameter index.
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
75
public abstract int findColumn(String columnLabel) throws SQLException;
79
* Read an Object from the current row store at the current row on the given column.
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.
87
protected abstract Object getObjectImpl(int columnIndex) throws SQLException;
91
* Read an Object from the current row store at the current row on the given column. Set the flag wasNull.
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.
99
private final Object getObjectSetWasNull(int columnIndex) throws SQLException{
100
Object obj = getObjectImpl(columnIndex);
101
if(obj == null || obj == DBNull.Value){
110
public final Array getArray(int columnIndex){
111
throw new UnsupportedOperationException();
115
public final Array getArray(String columnLabel) throws SQLException{
116
return getArray(findColumn(columnLabel));
120
public final InputStream getAsciiStream(int columnIndex) throws SQLException{
122
String str = getString(columnIndex);
126
return new ByteArrayInputStream(str.getBytes("Ascii"));
127
}catch(Throwable th){
128
throw JdbcOdbcUtils.createSQLException(th);
133
public final InputStream getAsciiStream(String columnLabel) throws SQLException{
134
return getAsciiStream(findColumn(columnLabel));
138
public final BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException{
139
BigDecimal dec = getBigDecimal(columnIndex);
143
if(dec.scale() != scale){
144
return dec.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
150
public final BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException{
151
return getBigDecimal(findColumn(columnLabel), scale);
155
public final BigDecimal getBigDecimal(int columnIndex) throws SQLException{
157
Object obj = getObjectSetWasNull(columnIndex);
161
String str = obj.toString();
162
return new BigDecimal(str);
163
}catch(Throwable th){
164
throw JdbcOdbcUtils.createSQLException(th);
169
public final BigDecimal getBigDecimal(String columnLabel) throws SQLException{
170
return getBigDecimal(findColumn(columnLabel));
174
public final InputStream getBinaryStream(int columnIndex) throws SQLException{
175
byte[] data = getBytes(columnIndex);
179
return new ByteArrayInputStream(data);
183
public final InputStream getBinaryStream(String columnLabel) throws SQLException{
184
return getBinaryStream(findColumn(columnLabel));
188
public final Blob getBlob(int columnIndex){
189
throw new UnsupportedOperationException();
193
public final Blob getBlob(String columnLabel) throws SQLException{
194
return getBlob(findColumn(columnLabel));
198
public final boolean getBoolean(int columnIndex) throws SQLException{
200
Object obj = getObjectSetWasNull(columnIndex);
204
if(obj instanceof IConvertible){
205
return Convert.ToBoolean(obj);
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'){
214
if(ch == 'F' || ch == 'f'){
218
return cli.System.Boolean.Parse(str);
219
}catch(Throwable th){
220
throw JdbcOdbcUtils.createSQLException(th);
225
public final boolean getBoolean(String columnLabel) throws SQLException{
226
return getBoolean(findColumn(columnLabel));
230
public final byte getByte(int columnIndex) throws SQLException{
232
Object obj = getObjectSetWasNull(columnIndex);
236
if(obj instanceof IConvertible){
237
return Convert.ToByte(obj);
239
String str = obj.toString();
240
return cli.System.Byte.Parse(str);
241
}catch(Throwable th){
242
throw JdbcOdbcUtils.createSQLException(th);
247
public final byte getByte(String columnLabel) throws SQLException{
248
return getByte(findColumn(columnLabel));
252
public final byte[] getBytes(int columnIndex) throws SQLException{
254
Object obj = getObjectSetWasNull(columnIndex);
258
if(obj instanceof byte[]){
261
String str = obj.toString();
262
return str.getBytes(); // which encoding?
263
}catch(Throwable th){
264
throw JdbcOdbcUtils.createSQLException(th);
269
public final byte[] getBytes(String columnLabel) throws SQLException{
270
return getBytes(findColumn(columnLabel));
274
public final Reader getCharacterStream(int columnIndex) throws SQLException{
275
String str = getString(columnIndex);
279
return new StringReader(str);
283
public final Reader getCharacterStream(String columnLabel) throws SQLException{
284
return getCharacterStream(findColumn(columnLabel));
288
public final Clob getClob(int columnIndex){
289
throw new UnsupportedOperationException();
293
public final Clob getClob(String columnLabel) throws SQLException{
294
return getClob(findColumn(columnLabel));
298
public final Date getDate(int columnIndex) throws SQLException{
300
Object obj = getObjectSetWasNull(columnIndex);
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());
308
String str = obj.toString();
309
return Date.valueOf(str);
310
}catch(Throwable th){
311
throw JdbcOdbcUtils.createSQLException(th);
316
public final Date getDate(String columnLabel) throws SQLException{
317
return getDate(findColumn(columnLabel));
321
public final Date getDate(int columnIndex, Calendar cal) throws SQLException{
322
Date date = getDate(columnIndex);
323
JdbcOdbcUtils.convertLocalToCalendarDate(date, cal);
328
public final Date getDate(String columnLabel, Calendar cal) throws SQLException{
329
return getDate(findColumn(columnLabel), cal);
333
public final double getDouble(int columnIndex) throws SQLException{
335
Object obj = getObjectSetWasNull(columnIndex);
339
if(obj instanceof IConvertible){
340
return Convert.ToDouble(obj);
342
String str = obj.toString();
343
return cli.System.Double.Parse(str);
344
}catch(Throwable th){
345
throw JdbcOdbcUtils.createSQLException(th);
350
public final double getDouble(String columnLabel) throws SQLException{
351
return getDouble(findColumn(columnLabel));
355
public final float getFloat(int columnIndex) throws SQLException{
357
Object obj = getObjectSetWasNull(columnIndex);
361
if(obj instanceof IConvertible){
362
return Convert.ToSingle(obj);
364
String str = obj.toString();
365
return Single.Parse(str);
366
}catch(Throwable th){
367
throw JdbcOdbcUtils.createSQLException(th);
372
public final float getFloat(String columnLabel) throws SQLException{
373
return getFloat(findColumn(columnLabel));
377
public final int getInt(int columnIndex) throws SQLException{
379
Object obj = getObjectSetWasNull(columnIndex);
383
if(obj instanceof IConvertible){
384
return Convert.ToInt32(obj);
386
String str = obj.toString();
387
return Int32.Parse(str);
388
}catch(Throwable th){
389
throw JdbcOdbcUtils.createSQLException(th);
394
public final int getInt(String columnLabel) throws SQLException{
395
return getInt(findColumn(columnLabel));
399
public final long getLong(int columnIndex) throws SQLException{
401
Object obj = getObjectSetWasNull(columnIndex);
405
if(obj instanceof IConvertible){
406
return Convert.ToInt64(obj);
408
String str = obj.toString();
409
return Int64.Parse(str);
410
}catch(Throwable th){
411
throw JdbcOdbcUtils.createSQLException(th);
416
public final long getLong(String columnLabel) throws SQLException{
417
return getLong(findColumn(columnLabel));
421
public final Reader getNCharacterStream(int columnIndex) throws SQLException{
422
return getCharacterStream(columnIndex);
426
public final Reader getNCharacterStream(String columnLabel) throws SQLException{
427
return getCharacterStream(columnLabel);
431
public final NClob getNClob(int columnIndex){
432
throw new UnsupportedOperationException();
436
public final NClob getNClob(String columnLabel) throws SQLException{
437
return getNClob(findColumn(columnLabel));
441
public final String getNString(int columnIndex) throws SQLException{
442
return getString(columnIndex);
446
public final String getNString(String columnLabel) throws SQLException{
447
return getString(columnLabel);
451
public final Object getObject(int columnIndex) throws SQLException{
452
return JdbcOdbcUtils.convertNet2Java(getObjectSetWasNull(columnIndex));
456
public final Object getObject(String columnLabel) throws SQLException{
457
return getObject(findColumn(columnLabel));
461
public final Object getObject(int columnIndex, Map<String, Class<?>> map){
462
throw new UnsupportedOperationException();
466
public final Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException{
467
return getObject(findColumn(columnLabel), map);
471
public final Ref getRef(int columnIndex){
472
throw new UnsupportedOperationException();
476
public final Ref getRef(String columnLabel) throws SQLException{
477
return getRef(findColumn(columnLabel));
481
public final RowId getRowId(int columnIndex){
482
throw new UnsupportedOperationException();
486
public final RowId getRowId(String columnLabel) throws SQLException{
487
return getRowId(findColumn(columnLabel));
491
public final SQLXML getSQLXML(int columnIndex){
492
throw new UnsupportedOperationException();
496
public final SQLXML getSQLXML(String columnLabel) throws SQLException{
497
return getSQLXML(findColumn(columnLabel));
501
public final short getShort(int columnIndex) throws SQLException{
503
Object obj = getObjectSetWasNull(columnIndex);
507
if(obj instanceof IConvertible){
508
return Convert.ToInt16(obj);
510
String str = obj.toString();
511
return Int16.Parse(str);
512
}catch(Throwable th){
513
throw JdbcOdbcUtils.createSQLException(th);
518
public final short getShort(String columnLabel) throws SQLException{
519
return getShort(findColumn(columnLabel));
523
public final String getString(int columnIndex) throws SQLException{
525
Object obj = getObjectSetWasNull(columnIndex);
529
return obj.toString();
530
}catch(Throwable th){
531
throw JdbcOdbcUtils.createSQLException(th);
536
public final String getString(String columnLabel) throws SQLException{
537
return getString(findColumn(columnLabel));
541
public final Time getTime(int columnIndex) throws SQLException{
543
Object obj = getObjectSetWasNull(columnIndex);
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());
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());
555
String str = obj.toString();
556
return Time.valueOf(str);
557
}catch(Throwable th){
558
throw JdbcOdbcUtils.createSQLException(th);
563
public final Time getTime(String columnLabel) throws SQLException{
564
return getTime(findColumn(columnLabel));
568
public final Time getTime(int columnIndex, Calendar cal) throws SQLException{
569
Time time = getTime(columnIndex);
570
JdbcOdbcUtils.convertLocalToCalendarDate(time, cal);
575
public final Time getTime(String columnLabel, Calendar cal) throws SQLException{
576
return getTime(findColumn(columnLabel), cal);
580
public final Timestamp getTimestamp(int columnIndex) throws SQLException{
582
Object obj = getObjectSetWasNull(columnIndex);
586
if(obj instanceof cli.System.DateTime){
587
return JdbcOdbcUtils.convertDateTimeToTimestamp((cli.System.DateTime)obj);
589
String str = obj.toString();
590
return Timestamp.valueOf(str);
591
}catch(Throwable th){
592
throw JdbcOdbcUtils.createSQLException(th);
597
public final Timestamp getTimestamp(String columnLabel) throws SQLException{
598
return getTimestamp(findColumn(columnLabel));
602
public final Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException{
603
Timestamp ts = getTimestamp(columnIndex);
604
JdbcOdbcUtils.convertLocalToCalendarDate(ts, cal);
609
public final Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException{
610
return getTimestamp(findColumn(columnLabel), cal);
614
public final URL getURL(int columnIndex) throws SQLException{
616
String url = getString(columnIndex);
621
}catch(Throwable th){
622
throw JdbcOdbcUtils.createSQLException(th);
627
public final URL getURL(String columnLabel) throws SQLException{
628
return getURL(findColumn(columnLabel));
632
public final InputStream getUnicodeStream(int columnIndex) throws SQLException{
634
return new ByteArrayInputStream(getString(columnIndex).getBytes("UTF16"));
635
}catch(Throwable th){
636
throw JdbcOdbcUtils.createSQLException(th);
641
public final InputStream getUnicodeStream(String columnLabel) throws SQLException{
642
return getUnicodeStream(findColumn(columnLabel));
646
public final boolean wasNull(){