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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
|
/*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package virtuoso.jdbc2;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.math.*;
import java.io.*;
import openlink.util.*;
import java.lang.reflect.Method;
/**
* The VirtuosoRow class is designed to store and retrieve a row from the
* result set.
*
* @version 1.0 (JDBC API 2.0 implementation)
*/
class VirtuosoRow
{
// Content of each column in this row
private openlink.util.Vector content;
// Max number of columns in this row
protected int maxCol;
// The result set which contains this row
private VirtuosoResultSet resultSet;
/**
* Construct a new VirtuosoRow class which stores the row's content.
*
* @param Vector Content of the row.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
*/
VirtuosoRow(VirtuosoResultSet resultSet, openlink.util.Vector args) throws VirtuosoException
{
// Set the row and the column count
//System.out.println ("new VirtuosoRow : ");
//System.out.println (args);
this.resultSet = resultSet;
content = args;
maxCol = resultSet.metaData.getColumnCount();
}
/**
* Copy the content of the line into an array.
*/
protected void getContent(Object anArray[])
{
for(int i = 0;i < anArray.length && i < maxCol;i++)
anArray[i] = content.elementAt(i);
}
/**
* Gets the value of a column in the row as a stream of
* ASCII characters. The value can then be read in chunks from the
* stream. This method is particularly
* suitable for retrieving large LONGVARCHAR values. The JDBC driver will
* do any necessary conversion from the database format into ASCII.
*
* @param column The first column is 1, the second is 2, ...
* @return VirtuosoAsciiInputStream A Java input stream that delivers the database column value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getAsciiStream
*/
protected InputStream getAsciiStream(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// Return the buffer
resultSet.wasNull((obj != null)?false:true);
if(obj != null && obj instanceof VirtuosoBlob)
return ((VirtuosoBlob)obj).getAsciiStream();
try
{
if(obj != null && obj instanceof String)
return new ByteArrayInputStream (((String)obj).getBytes ("8859_1"));
}
catch (java.io.UnsupportedEncodingException e)
{
throw new VirtuosoException (e, VirtuosoException.CASTERROR);
}
if(obj != null && obj instanceof byte[])
return new ByteArrayInputStream((byte[])obj);
return null;
}
/**
* Gets the value of a column in the current row as a stream of
* uninterpreted bytes.
*
* @param column The first column is 1, the second is 2, ...
* @return ByteArrayInputStream A Java input stream that delivers the database column value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getBinaryStream
*/
protected InputStream getBinaryStream(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// Return the buffer
resultSet.wasNull((obj != null)?false:true);
if(obj != null && obj instanceof VirtuosoBlob)
return ((VirtuosoBlob)obj).getBinaryStream();
try
{
if(obj != null && obj instanceof String)
return new ByteArrayInputStream (((String)obj).getBytes ("8859_1"));
}
catch (java.io.UnsupportedEncodingException e)
{
throw new VirtuosoException (e, VirtuosoException.CASTERROR);
}
if(obj != null && obj instanceof byte[])
return new ByteArrayInputStream((byte[])obj);
return null;
}
/**
* Gets the value of a column in the current row as a stream of
* uninterpreted characters.
*
* @param column The first column is 1, the second is 2, ...
* @return CharArrayReader A Java input stream that delivers the database column value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getCharacterStream
*/
protected Reader getCharacterStream(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// Return the buffer
resultSet.wasNull((obj != null)?false:true);
if(obj != null && obj instanceof VirtuosoBlob)
return ((VirtuosoBlob)obj).getCharacterStream();
//if (obj != null)
// System.out.println ("getString obj = " + obj.getClass().getName());
// else
// System.out.println ("getString obj = null");
if(obj != null && obj instanceof String)
{
String str = (String)obj;
VirtuosoStatement stmt = (VirtuosoStatement)resultSet.getStatement();
if (stmt.connection.charset != null && obj instanceof String)
{
int dtp = resultSet.metaData.getColumnDtp (column);
switch (dtp)
{
case VirtuosoTypes.DV_SHORT_STRING_SERIAL:
case VirtuosoTypes.DV_STRING:
case VirtuosoTypes.DV_STRICT_STRING:
try
{
str = stmt.connection.uncharsetBytes ((String)obj);
}
catch (Exception e)
{
str = (String)obj;
}
}
}
return new StringReader(str);
}
if(obj != null && obj instanceof char[])
return new CharArrayReader((char[])obj);
return null;
}
/**
* Gets the value of a column in the row as a java.math.BigDecimal
* object with full precision.
*
* @param column The first column is 1, the second is 2, ...
* @return BigDecimal The column value (full precision).
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getBigDecimal
*/
protected BigDecimal getBigDecimal(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// Return the object
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof BigDecimal)
return (BigDecimal)obj;
else
if(obj instanceof Number)
try
{
return new BigDecimal(((Number)obj).doubleValue());
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a number.",VirtuosoException.CASTERROR);
}
else
try
{
return new BigDecimal(obj.toString());
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a number.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the value of a column in the row as a boolean.
*
* @param column The first column is 1, the second is 2, ...
* @return boolean The column boolean value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getBoolean
*/
protected boolean getBoolean(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if (obj instanceof Number)
return (((Number)obj).intValue() == 0)?false:true;
if (obj instanceof String)
return (java.lang.Integer.parseInt((String)obj) == 0)?false:true;
if (obj instanceof byte[])
return (java.lang.Integer.parseInt(new String((byte[])obj)) == 0)?false:true;
return new Boolean(obj.toString()).booleanValue();
}
resultSet.wasNull(true);
return false;
}
/**
* Gets the value of a column in the row as a byte.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column byte value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getByte
*/
protected byte getByte(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).byteValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a byte.",VirtuosoException.CASTERROR);
}
else
try
{
return new Byte(obj.toString()).byteValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a byte.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0;
}
/**
* Gets the value of a column in the row as a byte array.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column byte array value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getBytes
*/
protected byte[] getBytes(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if (obj instanceof VirtuosoBlob)
{
VirtuosoBlob blob = (VirtuosoBlob) obj;
if (blob.length() > Integer.MAX_VALUE)
throw new VirtuosoException (
"Will not return more than " +
Integer.MAX_VALUE +
" for a BLOB column. Use getBinaryStream() instead",
VirtuosoException.ERRORONTYPE);
return blob.getBytes(1, (int) blob.length());
}
else if (obj instanceof byte[])
return (byte[])obj;
else if (obj instanceof java.lang.String)
{
try {
return ((String)obj).getBytes ("8859_1");
} catch (UnsupportedEncodingException e) { return null; }
}
else
throw new VirtuosoException ("getBytes() return undefined on a value of type " + obj.getClass(),
VirtuosoException.ERRORONTYPE);
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the value of a column in the row as a double.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column double value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getDouble
*/
protected double getDouble(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).doubleValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a double.",VirtuosoException.CASTERROR);
}
else
try
{
return new Double(obj.toString()).doubleValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a double.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0.0d;
}
/**
* Gets the value of a column in the row as a Date.
*
* @param column The first column is 1, the second is 2, ...
* @return Date The column date value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getDate
*/
protected java.sql.Date getDate(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof java.sql.Date)
return java.sql.Date.valueOf (((java.sql.Date)obj).toString());
else if (obj instanceof String)
return java.sql.Date.valueOf ((String)obj);
else
return java.sql.Date.valueOf (((java.util.Date)obj).toString());
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the value of a column in the row as a Time.
*
* @param column The first column is 1, the second is 2, ...
* @return Time The column time value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getTime
*/
protected java.sql.Time getTime(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof java.sql.Time)
return java.sql.Time.valueOf (((java.sql.Time)obj).toString());
else if (obj instanceof java.util.Date)
{
java.util.Date dt = (java.util.Date) obj;
java.sql.Time tm = new java.sql.Time (dt.getTime());
return java.sql.Time.valueOf(tm.toString());
}
else if(obj instanceof String)
{
return java.sql.Time.valueOf((String)obj);
}
else return null;
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the value of a column in the row as a Timestamp.
*
* @param column The first column is 1, the second is 2, ...
* @return Timestamp The column timestamp value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getTimestamp
*/
protected java.sql.Timestamp getTimestamp(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof java.sql.Timestamp)
{
//System.err.println ("Was a Timestamp");
return (java.sql.Timestamp)obj;
}
else if (obj instanceof java.sql.Date)
{
//System.err.println ("Was a Date");
java.sql.Date dt = (java.sql.Date)obj;
return new java.sql.Timestamp (dt.getTime());
}
else if (obj instanceof java.sql.Time)
{
//System.err.println ("Was a Time");
java.sql.Time tm = (java.sql.Time)obj;
return new java.sql.Timestamp (tm.getTime());
}
else if(obj instanceof String)
{
//System.err.println ("Was a String");
java.sql.Timestamp ts;
ts = Timestamp.valueOf((String)obj);
return ts;
}
return null;
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the value of a column in the row as a float.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column float value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getFloat
*/
protected float getFloat(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).floatValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a float.",VirtuosoException.CASTERROR);
}
else
try
{
return new Float(obj.toString()).floatValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a float.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0.0f;
}
/**
* Gets the value of a column in the row as a int.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column int value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getInt
*/
protected int getInt(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).intValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain an int.",VirtuosoException.CASTERROR);
}
else
try
{
return new Integer(obj.toString()).intValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not an int.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0;
}
/**
* Gets the value of a column in the row as a long.
*
* @param column The first column is 1, the second is 2, ...
* @return byte The column long value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getLong
*/
protected long getLong(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).longValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a long.",VirtuosoException.CASTERROR);
}
else
try
{
return new Long(obj.toString()).longValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a long.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0l;
}
/**
* Gets the value of a column in the row as a short.
*
* @param column The first column is 1, the second is 2, ...
* @return short The column short value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getShort
*/
protected short getShort(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if(obj instanceof Number)
try
{
return ((Number)obj).shortValue();
}
catch(ClassCastException e)
{
throw new VirtuosoException("Column does not contain a short.",VirtuosoException.CASTERROR);
}
else
try
{
return new Short(obj.toString()).shortValue();
}
catch(NumberFormatException e)
{
throw new VirtuosoException(obj.toString() + " is not a short.",VirtuosoException.BADFORMAT);
}
}
resultSet.wasNull(true);
return 0;
}
/**
* Gets the value of a column in the row as a String.
*
* @param column The first column is 1, the second is 2, ...
* @return String The column string value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getString
*/
protected String getString(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
int dtp = resultSet.metaData.getColumnDtp (column);
resultSet.wasNull(false);
VirtuosoStatement stmt = (VirtuosoStatement)resultSet.getStatement();
if (stmt.connection.charset != null && obj instanceof String)
{
//System.out.println ("in getString: dtp=" + dtp);
switch (dtp)
{
case VirtuosoTypes.DV_SHORT_STRING_SERIAL:
case VirtuosoTypes.DV_STRING:
case VirtuosoTypes.DV_STRICT_STRING:
try
{
return stmt.connection.uncharsetBytes ((String)obj);
}
catch (Exception e)
{
return (String)obj;
}
}
}
if (obj instanceof java.sql.Timestamp)
{
java.sql.Timestamp ts = (java.sql.Timestamp) obj;
switch (dtp)
{
case VirtuosoTypes.DV_DATE:
return (new java.sql.Date (ts.getTime())).toString();
case VirtuosoTypes.DV_TIME:
return (new java.sql.Time (ts.getTime())).toString();
}
}
else if (obj instanceof VirtuosoBlob)
{
try {
Reader r = ((VirtuosoBlob)obj).getCharacterStream();
char[] data = new char[1024];
StringWriter w = new StringWriter();
int l;
while((l = r.read(data)) != -1)
w.write(data,0,l);
return w.toString();
} catch (IOException e) {
throw new VirtuosoException(e, VirtuosoException.MISCERROR);
}
}
return obj.toString();
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the blob of a column in the row.
*
* @param column The first column is 1, the second is 2, ...
* @return Blob The blob.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getString
*/
protected VirtuosoBlob getBlob(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
VirtuosoBlob obj = (content.elementAt(column - 1) instanceof String) ?
new VirtuosoBlob(((String)content.elementAt(column - 1)).getBytes()) :
(VirtuosoBlob)(content.elementAt(column - 1));
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
return obj;
}
resultSet.wasNull(true);
return null;
}
/**
* Gets the clob of a column in the row.
*
* @param column The first column is 1, the second is 2, ...
* @return Clob The clob.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getString
*/
protected
#if JDK_VER >= 12
VirtuosoBlob
#else
VirtuosoClob
#endif
getClob(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
#if JDK_VER >= 12
VirtuosoBlob obj = (content.elementAt(column - 1) instanceof String) ?
new VirtuosoBlob(((String)content.elementAt(column - 1)).getBytes()) :
(VirtuosoBlob)(content.elementAt(column - 1));
#else
VirtuosoClob obj = (content.elementAt(column - 1) instanceof String) ?
new VirtuosoClob(((String)content.elementAt(column - 1)).getBytes()) :
(VirtuosoClob)(content.elementAt(column - 1));
#endif
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
return obj;
}
resultSet.wasNull(true);
return null;
}
protected Object createDOMT (Object data) throws VirtuosoException
{
Object db = null;
Method parse_mtd = null;
// calls javax.xml.parsers.DocumentBuilderFactory.newInstance ()
try
{
#if JDK_VER >= 16
Class<?> dbfact_cls = Class.forName ("javax.xml.parsers.DocumentBuilderFactory");
#else
Class dbfact_cls = Class.forName ("javax.xml.parsers.DocumentBuilderFactory");
#endif
Object dbfact;
Method newInstMtd, newDbMtd;
newInstMtd = dbfact_cls.getDeclaredMethod ("newInstance", new Class [0]);
newDbMtd = dbfact_cls.getDeclaredMethod ("newDocumentBuilder", new Class [0]);
// dbfact = javax.xml.parsers.DocumentBuilderFactory.newInstance ();
dbfact = newInstMtd.invoke (null, new Object [0]);
// db = dbfact.newDocumentBuilder ();
db = newDbMtd.invoke (dbfact, new Object [0]);
#if JDK_VER >= 16
Class<?> db_cls = Class.forName ("javax.xml.parsers.DocumentBuilder");
Class<?> parse_args = Class.forName ("java.io.InputStream");
parse_mtd = db_cls.getDeclaredMethod ("parse", parse_args);
#else
Class db_cls = Class.forName ("javax.xml.parsers.DocumentBuilder");
Class [] parse_args = { Class.forName ("java.io.InputStream") };
//System.out.println ("xml=[" + ((String)data) + "]");
parse_mtd = db_cls.getDeclaredMethod ("parse", parse_args);
#endif
}
catch (Throwable t)
{
db = null;
}
if (db == null || parse_mtd == null)
return data;
try
{
if (data instanceof String)
{
// db.parse (data)
//System.out.println ("xml=[" + ((String)data) + "]");
Object [] args = { new ByteArrayInputStream ( ((String)data).getBytes("UTF-8")) };
data = parse_mtd.invoke (db, args );
}
else if (data instanceof VirtuosoBlob)
{
// db.parse (data)
Object [] args = { ((VirtuosoBlob)data).getBinaryStream () };
data = parse_mtd.invoke (db, args);
}
}
catch (Exception e)
{
throw new VirtuosoException (e, VirtuosoException.CASTERROR);
}
return data;
}
/**
* Gets the value of a column in the row as an Object.
*
* @param column The first column is 1, the second is 2, ...
* @return Object The column Object value.
* @exception virtuoso.jdbc2.VirtuosoException An internal error occurred.
* @see java.sql.ResultSet#getObject
*/
protected Object getObject(int column) throws VirtuosoException
{
// Get and check parameter
if(column < 1 || column > maxCol)
throw new VirtuosoException("Bad column number : " + column + " not in 1<n<" + maxCol,VirtuosoException.BADPARAM);
// Get the object in the corresponding column
Object obj = content.elementAt(column - 1);
// JDBC api spec
if(obj != null)
{
resultSet.wasNull(false);
if (resultSet.metaData.isXml (column))
{
obj = createDOMT (obj);
}
else if(obj instanceof java.sql.Date)
{
obj = java.sql.Date.valueOf (((java.sql.Date)obj).toString());
}
else if(obj instanceof java.sql.Time)
{
obj = java.sql.Time.valueOf (((java.sql.Time)obj).toString());
}
return obj;
}
resultSet.wasNull(true);
return null;
}
/**
* Get the line number (used only with scrollable cursor)
*
* @return The line number in the rowset
*/
protected int getRow()
{
// Check if a linenumber exist
return (maxCol != content.size()) ? ((Number)content.elementAt(maxCol)).intValue() : 0;
}
/**
* Get the line number (used only with scrollable cursor)
*
* @return The line number in the rowset
*/
protected String getRef()
{
// Check if a linenumber exist
//System.err.println ("In GetRef maxCol = " + maxCol + " content_size=" + content.size());
if (maxCol != content.size())
{
openlink.util.Vector v1 = (openlink.util.Vector)content.elementAt(maxCol);
openlink.util.Vector v2 = (openlink.util.Vector)v1.elementAt (1);
Object ret = v2.elementAt (0);
//System.err.println ("GetRef returned (" + ret.getClass().getName() + ")=" + ret.toString());
return ret.toString();
}
else
return "";
}
/**
* Get the line number (used only with scrollable cursor)
*
* @return The bookmark of the line
*/
protected openlink.util.Vector getBookmark()
{
// Check if a linenumber exist
if (maxCol != content.size())
return (openlink.util.Vector)content.elementAt(maxCol);
else
return content;
}
/**
* Method runs when the garbage collector want to erase the object
*/
public void finalize() throws Throwable
{
if(content != null)
{
content.removeAllElements();
content = null;
}
resultSet = null;
}
public String toString ()
{
if (content == null)
return super.toString();
StringBuffer buf = new StringBuffer();
buf.append("{ROW ");
buf.append (content.toString());
buf.append("}");
return buf.toString();
}
}
|