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 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2005-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/Jdbc3CallableStatementTest.java,v 1.6 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc3;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLState;
import junit.framework.TestCase;
/**
* @author davec
*
*/
public class Jdbc3CallableStatementTest extends TestCase
{
Connection con;
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
con = TestUtil.openDB();
Statement stmt = con.createStatement ();
stmt.execute("create temp table numeric_tab (MAX_VAL NUMERIC(30,15), MIN_VAL NUMERIC(30,15), NULL_VAL NUMERIC(30,15) NULL)");
stmt.execute("insert into numeric_tab values ( 999999999999999,0.000000000000001, null)");
stmt.execute("CREATE OR REPLACE FUNCTION myiofunc(a INOUT int, b OUT int) AS 'BEGIN b := a; a := 1; END;' LANGUAGE plpgsql");
stmt.execute("CREATE OR REPLACE FUNCTION myif(a INOUT int, b IN int) AS 'BEGIN a := b; END;' LANGUAGE plpgsql");
stmt.execute("create or replace function "
+ "Numeric_Proc( OUT IMAX NUMERIC(30,15), OUT IMIN NUMERIC(30,15), OUT INUL NUMERIC(30,15)) as "
+ "'begin "
+ "select max_val into imax from numeric_tab;"
+ "select min_val into imin from numeric_tab;"
+ "select null_val into inul from numeric_tab;"
+ " end;' "
+ "language 'plpgsql';");
stmt.execute( "CREATE OR REPLACE FUNCTION test_somein_someout("
+ "pa IN int4,"
+ "pb OUT varchar,"
+ "pc OUT int8)"
+ " AS "
+ "'begin "
+ "pb := ''out'';"
+ "pc := pa + 1;"
+ "end;'"
+ "LANGUAGE 'plpgsql' VOLATILE;"
);
stmt.execute("CREATE OR REPLACE FUNCTION test_allinout("
+ "pa INOUT int4,"
+ "pb INOUT varchar,"
+ "pc INOUT int8)"
+ " AS "
+ "'begin "
+ "pa := pa + 1;"
+ "pb := ''foo out'';"
+ "pc := pa + 1;"
+ "end;'"
+ "LANGUAGE 'plpgsql' VOLATILE;"
);
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
Statement stmt = con.createStatement();
stmt.execute("drop function Numeric_Proc(out decimal, out decimal, out decimal)");
stmt.execute("drop function test_somein_someout(int4)");
stmt.execute("drop function test_allinout( inout int4, inout varchar, inout int8)");
stmt.execute("drop function myiofunc(a INOUT int, b OUT int) ");
stmt.execute("drop function myif(a INOUT int, b IN int)");
stmt.close();
TestUtil.closeDB(con);
}
public void testSomeInOut() throws Throwable
{
CallableStatement call = con.prepareCall( "{ call test_somein_someout(?,?,?) }" ) ;
call.registerOutParameter(2,Types.VARCHAR);
call.registerOutParameter(3,Types.BIGINT);
call.setInt(1,20);
call.execute();
}
public void testNotEnoughParameters() throws Throwable
{
CallableStatement cs = con.prepareCall("{call myiofunc(?,?)}");
cs.setInt(1,2);
cs.registerOutParameter(2,Types.INTEGER);
try
{
cs.execute();
fail("Should throw an exception ");
}
catch( SQLException ex)
{
assertTrue(ex.getSQLState().equalsIgnoreCase(PSQLState.SYNTAX_ERROR.getState()));
}
}
public void testTooManyParameters() throws Throwable
{
CallableStatement cs = con.prepareCall("{call myif(?,?)}");
try
{
cs.setInt(1,1);
cs.setInt(2,2);
cs.registerOutParameter(1,Types.INTEGER);
cs.registerOutParameter(2,Types.INTEGER);
cs.execute();
fail("should throw an exception");
}
catch( SQLException ex )
{
assertTrue(ex.getSQLState().equalsIgnoreCase(PSQLState.SYNTAX_ERROR.getState()));
}
}
public void testAllInOut() throws Throwable
{
CallableStatement call = con.prepareCall( "{ call test_allinout(?,?,?) }" ) ;
call.registerOutParameter(1,Types.INTEGER);
call.registerOutParameter(2,Types.VARCHAR);
call.registerOutParameter(3,Types.BIGINT);
call.setInt(1,20);
call.setString(2,"hi");
call.setInt(3,123);
call.execute();
call.getInt(1);
call.getString(2);
call.getLong(3);
}
public void testNumeric() throws Throwable
{
CallableStatement call = con.prepareCall( "{ call Numeric_Proc(?,?,?) }" ) ;
call.registerOutParameter(1,Types.NUMERIC,15);
call.registerOutParameter(2,Types.NUMERIC,15);
call.registerOutParameter(3,Types.NUMERIC,15);
call.executeUpdate();
java.math.BigDecimal ret = call.getBigDecimal(1);
assertTrue ("correct return from getNumeric () should be 999999999999999.000000000000000 but returned " + ret.toString(),
ret.equals (new java.math.BigDecimal("999999999999999.000000000000000")));
ret=call.getBigDecimal(2);
assertTrue ("correct return from getNumeric ()",
ret.equals (new java.math.BigDecimal("0.000000000000001")));
try
{
ret = call.getBigDecimal(3);
}catch(NullPointerException ex)
{
assertTrue("This should be null",call.wasNull());
}
}
public void testGetObjectDecimal() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table decimal_tab ( max_val numeric(30,15), min_val numeric(30,15), nul_val numeric(30,15) )");
stmt.execute("insert into decimal_tab values (999999999999999.000000000000000,0.000000000000001,null)");
boolean ret = stmt.execute("create or replace function "
+ "decimal_proc( OUT pmax numeric, OUT pmin numeric, OUT nval numeric) as "
+ "'begin "
+ "select max_val into pmax from decimal_tab;"
+ "select min_val into pmin from decimal_tab;"
+ "select nul_val into nval from decimal_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call decimal_proc(?,?,?) }");
cstmt.registerOutParameter(1, Types.DECIMAL);
cstmt.registerOutParameter(2, Types.DECIMAL );
cstmt.registerOutParameter(3, Types.DECIMAL );
cstmt.executeUpdate();
BigDecimal val = (BigDecimal)cstmt.getObject(1);
assertTrue( val.compareTo(new BigDecimal("999999999999999.000000000000000")) == 0 );
val = ( BigDecimal )cstmt.getObject(2);
assertTrue( val.compareTo( new BigDecimal( "0.000000000000001")) == 0);
val = ( BigDecimal )cstmt.getObject(3);
assertTrue( val == null );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function decimal_proc()");
}
catch (Exception ex){}
}
}
public void testVarcharBool() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table vartab( max_val text, min_val text)");
stmt.execute("insert into vartab values ('a','b')");
boolean ret = stmt.execute("create or replace function "
+ "updatevarchar( in imax text, in imin text) returns int as "
+ "'begin "
+ "update vartab set max_val = imax;"
+ "update vartab set min_val = imin;"
+ "return 0;"
+ " end;' "
+ "language 'plpgsql';");
stmt.close();
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
String str = Boolean.TRUE.toString();
CallableStatement cstmt = con.prepareCall("{ call updatevarchar(?,?) }");
cstmt.setObject(1,Boolean.TRUE, Types.VARCHAR);
cstmt.setObject(2,Boolean.FALSE, Types.VARCHAR);
cstmt.executeUpdate();
cstmt.close();
ResultSet rs = con.createStatement().executeQuery("select * from vartab");
assertTrue(rs.next());
assertTrue( rs.getString(1).equals(Boolean.TRUE.toString()) );
assertTrue( rs.getString(2).equals(Boolean.FALSE.toString()) );
rs.close();
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function updatevarchar(text,text)");
}
catch (Exception ex){}
}
}
public void testInOut() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute(createBitTab);
stmt.execute(insertBitTab);
boolean ret = stmt.execute("create or replace function "
+ "insert_bit( inout IMAX boolean, inout IMIN boolean, inout INUL boolean) as "
+ "'begin "
+ "insert into bit_tab values( imax, imin, inul);"
+ "select max_val into imax from bit_tab;"
+ "select min_val into imin from bit_tab;"
+ "select null_val into inul from bit_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call insert_bit(?,?,?) }");
cstmt.setObject(1,"true", Types.BIT);
cstmt.setObject(2,"false", Types.BIT);
cstmt.setNull(3,Types.BIT);
cstmt.registerOutParameter(1, Types.BIT);
cstmt.registerOutParameter(2, Types.BIT);
cstmt.registerOutParameter(3, Types.BIT);
cstmt.executeUpdate();
assertTrue( cstmt.getBoolean(1) == true );
assertTrue( cstmt.getBoolean(2) == false );
cstmt.getBoolean(3);
assertTrue(cstmt.wasNull());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function insert_bit(boolean, boolean, boolean)");
}
catch (Exception ex){}
}
}
private final String createBitTab = "create temp table bit_tab ( max_val boolean, min_val boolean, null_val boolean )";
private final String insertBitTab = "insert into bit_tab values (true,false,null)";
public void testSetObjectBit() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute(createBitTab);
stmt.execute(insertBitTab);
boolean ret = stmt.execute("create or replace function "
+ "update_bit( in IMAX boolean, in IMIN boolean, in INUL boolean) returns int as "
+ "'begin "
+ "update bit_tab set max_val = imax;"
+ "update bit_tab set min_val = imin;"
+ "update bit_tab set min_val = inul;"
+ " return 0;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call update_bit(?,?,?) }");
cstmt.setObject(1,"true", Types.BIT);
cstmt.setObject(2,"false", Types.BIT);
cstmt.setNull(3,Types.BIT);
cstmt.executeUpdate();
cstmt.close();
ResultSet rs = con.createStatement().executeQuery("select * from bit_tab");
assertTrue( rs.next() );
assertTrue( rs.getBoolean(1) == true );
assertTrue( rs.getBoolean(2) == false );
rs.getBoolean(3);
assertTrue( rs.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function update_bit(boolean, boolean, boolean)");
}
catch (Exception ex){}
}
}
public void testGetObjectLongVarchar() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table longvarchar_tab ( t text, null_val text )");
stmt.execute("insert into longvarchar_tab values ('testdata',null)");
boolean ret = stmt.execute("create or replace function "
+ "longvarchar_proc( OUT pcn text, OUT nval text) as "
+ "'begin "
+ "select t into pcn from longvarchar_tab;"
+ "select null_val into nval from longvarchar_tab;"
+ " end;' "
+ "language 'plpgsql';");
ret = stmt.execute("create or replace function "
+ "lvarchar_in_name( IN pcn text) returns int as "
+ "'begin "
+ "update longvarchar_tab set t=pcn;"
+ "return 0;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call longvarchar_proc(?,?) }");
cstmt.registerOutParameter(1, Types.LONGVARCHAR);
cstmt.registerOutParameter(2, Types.LONGVARCHAR );
cstmt.executeUpdate();
String val = (String)cstmt.getObject(1);
assertTrue( val.equals("testdata") );
val = ( String )cstmt.getObject(2);
assertTrue( val == null );
cstmt.close();
cstmt = con.prepareCall("{ call lvarchar_in_name(?) }");
String maxFloat = "3.4E38";
cstmt.setObject( 1, new Float(maxFloat), Types.LONGVARCHAR);
cstmt.executeUpdate();
cstmt.close();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from longvarchar_tab");
assertTrue(rs.next());
String rval = (String)rs.getObject(1);
assertEquals( rval.trim(), maxFloat.trim());
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function longvarchar_proc()");
dstmt.execute("drop function lvarchar_in_name(text)");
}
catch (Exception ex){}
}
}
public void testGetBytes01() throws Throwable
{
byte [] testdata = "TestData".getBytes();
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table varbinary_tab ( vbinary bytea, null_val bytea )");
boolean ret = stmt.execute("create or replace function "
+ "varbinary_proc( OUT pcn bytea, OUT nval bytea) as "
+ "'begin "
+ "select vbinary into pcn from varbinary_tab;"
+ "select null_val into nval from varbinary_tab;"
+ " end;' "
+ "language 'plpgsql';");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("insert into varbinary_tab values (?,?)");
pstmt.setBytes( 1, testdata);
pstmt.setBytes( 2, null );
pstmt.executeUpdate();
pstmt.close();
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call varbinary_proc(?,?) }");
cstmt.registerOutParameter(1, Types.VARBINARY);
cstmt.registerOutParameter(2, Types.VARBINARY );
cstmt.executeUpdate();
byte [] retval = cstmt.getBytes(1);
for( int i = 0; i < testdata.length; i++)
{
assertTrue( testdata[i] == retval[i] );
}
retval = cstmt.getBytes(2);
assertTrue( retval == null );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function varbinary_proc()");
}
catch (Exception ex){}
}
}
private final String createDecimalTab = "create temp table decimal_tab ( max_val float, min_val float, null_val float )";
private final String insertDecimalTab = "insert into decimal_tab values (1.0E125,1.0E-130,null)";
private final String createFloatProc = "create or replace function "
+ "float_proc( OUT IMAX float, OUT IMIN float, OUT INUL float) as "
+ "'begin "
+ "select max_val into imax from decimal_tab;"
+ "select min_val into imin from decimal_tab;"
+ "select null_val into inul from decimal_tab;"
+ " end;' "
+ "language 'plpgsql';";
private final String createUpdateFloat = "create or replace function "
+ "updatefloat_proc ( IN maxparm float, IN minparm float ) returns int as "
+ "'begin "
+ "update decimal_tab set max_val=maxparm;"
+ "update decimal_tab set min_val=minparm;"
+ "return 0;"
+ " end;' "
+ "language 'plpgsql';";
private final String createRealTab = "create temp table real_tab ( max_val float(25), min_val float(25), null_val float(25) )";
private final String insertRealTab = "insert into real_tab values (1.0E37,1.0E-37, null)";
private final String dropFloatProc = "drop function float_proc()";
private final String createUpdateReal = "create or replace function "
+ "update_real_proc ( IN maxparm float(25), IN minparm float(25) ) returns int as "
+ "'begin "
+ "update real_tab set max_val=maxparm;"
+ "update real_tab set min_val=minparm;"
+ "return 0;"
+ " end;' "
+ "language 'plpgsql';";
private final String dropUpdateReal = "drop function update_real_proc(float, float)";
private final double [] doubleValues = {1.0E125, 1.0E-130};
private final float [] realValues = {(float)1.0E37,(float)1.0E-37};
private final int []intValues = {2147483647,-2147483648};
public void testUpdateReal() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute(createRealTab);
boolean ret = stmt.execute(createUpdateReal);
stmt.execute(insertRealTab);
stmt.close();
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call update_real_proc(?,?) }");
BigDecimal val = new BigDecimal( intValues[0] );
float x = val.floatValue();
cstmt.setObject( 1, val, Types.REAL );
val = new BigDecimal( intValues[1]);
cstmt.setObject( 2, val, Types.REAL );
cstmt.executeUpdate();
cstmt.close();
ResultSet rs = con.createStatement().executeQuery("select * from real_tab");
assertTrue ( rs.next() );
Float oVal = new Float( intValues[0]);
Float rVal = new Float(rs.getObject(1).toString());
assertTrue ( oVal.equals(rVal) );
oVal = new Float( intValues[1] );
rVal = new Float(rs.getObject(2).toString());
assertTrue ( oVal.equals(rVal) );
rs.close();
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute(dropUpdateReal);
dstmt.close();
}
catch (Exception ex){}
}
}
public void testUpdateDecimal() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute(createDecimalTab);
boolean ret = stmt.execute(createUpdateFloat);
stmt.close();
PreparedStatement pstmt = con.prepareStatement("insert into decimal_tab values (?,?)");
// note these are reversed on purpose
pstmt.setDouble(1, doubleValues[1]);
pstmt.setDouble(2, doubleValues[0]);
pstmt.executeUpdate();
pstmt.close();
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call updatefloat_proc(?,?) }");
cstmt.setDouble(1, doubleValues[0]);
cstmt.setDouble(2, doubleValues[1]);
cstmt.executeUpdate();
cstmt.close();
ResultSet rs = con.createStatement().executeQuery("select * from decimal_tab");
assertTrue ( rs.next() );
assertTrue ( rs.getDouble(1) == doubleValues[0]);
assertTrue ( rs.getDouble(2) == doubleValues[1]);
rs.close();
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function updatefloat_proc(float, float)");
}
catch (Exception ex){}
}
}
public void testGetBytes02() throws Throwable
{
byte [] testdata = "TestData".getBytes();
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table longvarbinary_tab ( vbinary bytea, null_val bytea )");
boolean ret = stmt.execute("create or replace function "
+ "longvarbinary_proc( OUT pcn bytea, OUT nval bytea) as "
+ "'begin "
+ "select vbinary into pcn from longvarbinary_tab;"
+ "select null_val into nval from longvarbinary_tab;"
+ " end;' "
+ "language 'plpgsql';");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("insert into longvarbinary_tab values (?,?)");
pstmt.setBytes( 1, testdata);
pstmt.setBytes( 2, null );
pstmt.executeUpdate();
pstmt.close();
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call longvarbinary_proc(?,?) }");
cstmt.registerOutParameter(1, Types.LONGVARBINARY);
cstmt.registerOutParameter(2, Types.LONGVARBINARY );
cstmt.executeUpdate();
byte [] retval = cstmt.getBytes(1);
for( int i = 0; i < testdata.length; i++)
{
assertTrue( testdata[i] == retval[i] );
}
retval = cstmt.getBytes(2);
assertTrue( retval == null );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function longvarbinary_proc()");
}
catch (Exception ex){}
}
}
public void testGetObjectFloat() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute( createDecimalTab );
stmt.execute( insertDecimalTab );
boolean ret = stmt.execute(createFloatProc);
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call float_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.FLOAT);
cstmt.registerOutParameter(2,java.sql.Types.FLOAT);
cstmt.registerOutParameter(3,java.sql.Types.FLOAT);
cstmt.executeUpdate();
Double val = (Double)cstmt.getObject(1);
assertTrue( val.doubleValue() == doubleValues[0] );
val = (Double)cstmt.getObject(2);
assertTrue( val.doubleValue() == doubleValues[1]);
val = (Double)cstmt.getObject(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute(dropFloatProc);
}
catch (Exception ex){}
}
}
public void testGetDouble01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table d_tab ( max_val float, min_val float, null_val float )");
stmt.execute("insert into d_tab values (1.0E125,1.0E-130,null)");
boolean ret = stmt.execute("create or replace function "
+ "double_proc( OUT IMAX float, OUT IMIN float, OUT INUL float) as "
+ "'begin "
+ "select max_val into imax from d_tab;"
+ "select min_val into imin from d_tab;"
+ "select null_val into inul from d_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call double_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.DOUBLE);
cstmt.registerOutParameter(2,java.sql.Types.DOUBLE);
cstmt.registerOutParameter(3,java.sql.Types.DOUBLE);
cstmt.executeUpdate();
assertTrue( cstmt.getDouble(1) == 1.0E125 );
assertTrue( cstmt.getDouble(2) == 1.0E-130);
cstmt.getDouble(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function double_proc()");
}
catch (Exception ex){}
}
}
public void testGetDoubleAsReal() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table d_tab ( max_val float, min_val float, null_val float )");
stmt.execute("insert into d_tab values (3.4E38,1.4E-45,null)");
boolean ret = stmt.execute("create or replace function "
+ "double_proc( OUT IMAX float, OUT IMIN float, OUT INUL float) as "
+ "'begin "
+ "select max_val into imax from d_tab;"
+ "select min_val into imin from d_tab;"
+ "select null_val into inul from d_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call double_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.REAL);
cstmt.registerOutParameter(2,java.sql.Types.REAL);
cstmt.registerOutParameter(3,java.sql.Types.REAL);
cstmt.executeUpdate();
assertTrue( cstmt.getFloat(1) == 3.4E38f);
assertTrue( cstmt.getFloat(2) == 1.4E-45f);
cstmt.getFloat(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function double_proc()");
}
catch (Exception ex){}
}
}
public void testGetShort01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table short_tab ( max_val int2, min_val int2, null_val int2 )");
stmt.execute("insert into short_tab values (32767,-32768,null)");
boolean ret = stmt.execute("create or replace function "
+ "short_proc( OUT IMAX int2, OUT IMIN int2, OUT INUL int2) as "
+ "'begin "
+ "select max_val into imax from short_tab;"
+ "select min_val into imin from short_tab;"
+ "select null_val into inul from short_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call short_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.SMALLINT);
cstmt.registerOutParameter(2,java.sql.Types.SMALLINT);
cstmt.registerOutParameter(3,java.sql.Types.SMALLINT);
cstmt.executeUpdate();
assertTrue ( cstmt.getShort(1) == 32767);
assertTrue ( cstmt.getShort(2) == -32768);
cstmt.getShort(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function short_proc()");
}
catch (Exception ex){}
}
}
public void testGetInt01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table i_tab ( max_val int, min_val int, null_val int )");
stmt.execute("insert into i_tab values (2147483647,-2147483648,null)");
boolean ret = stmt.execute("create or replace function "
+ "int_proc( OUT IMAX int, OUT IMIN int, OUT INUL int) as "
+ "'begin "
+ "select max_val into imax from i_tab;"
+ "select min_val into imin from i_tab;"
+ "select null_val into inul from i_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call int_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.INTEGER);
cstmt.registerOutParameter(2,java.sql.Types.INTEGER);
cstmt.registerOutParameter(3,java.sql.Types.INTEGER);
cstmt.executeUpdate();
assertTrue( cstmt.getInt(1) == 2147483647);
assertTrue( cstmt.getInt(2) == -2147483648);
cstmt.getInt(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function int_proc()");
}
catch (Exception ex){}
}
}
public void testGetLong01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table l_tab ( max_val int8, min_val int8, null_val int8 )");
stmt.execute("insert into l_tab values (9223372036854775807,-9223372036854775808,null)");
boolean ret = stmt.execute("create or replace function "
+ "bigint_proc( OUT IMAX int8, OUT IMIN int8, OUT INUL int8) as "
+ "'begin "
+ "select max_val into imax from l_tab;"
+ "select min_val into imin from l_tab;"
+ "select null_val into inul from l_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call bigint_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.BIGINT);
cstmt.registerOutParameter(2,java.sql.Types.BIGINT);
cstmt.registerOutParameter(3,java.sql.Types.BIGINT);
cstmt.executeUpdate();
assertTrue(cstmt.getLong( 1 ) == 9223372036854775807l );
assertTrue( cstmt.getLong(2) == -9223372036854775808l );
cstmt.getLong(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function bigint_proc()");
}
catch (Exception ex){}
}
}
public void testGetBoolean01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute(createBitTab);
stmt.execute(insertBitTab);
boolean ret = stmt.execute("create or replace function "
+ "bit_proc( OUT IMAX boolean, OUT IMIN boolean, OUT INUL boolean) as "
+ "'begin "
+ "select max_val into imax from bit_tab;"
+ "select min_val into imin from bit_tab;"
+ "select null_val into inul from bit_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call bit_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.BIT);
cstmt.registerOutParameter(2,java.sql.Types.BIT);
cstmt.registerOutParameter(3,java.sql.Types.BIT);
cstmt.executeUpdate();
assertTrue(cstmt.getBoolean( 1 ) );
assertTrue( cstmt.getBoolean(2) == false );
cstmt.getBoolean(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function bit_proc()");
}
catch (Exception ex){}
}
}
public void testGetByte01() throws Throwable
{
try
{
Statement stmt = con.createStatement();
stmt.execute("create temp table byte_tab ( max_val int2, min_val int2, null_val int2 )");
stmt.execute("insert into byte_tab values (127,-128,null)");
boolean ret = stmt.execute("create or replace function "
+ "byte_proc( OUT IMAX int2, OUT IMIN int2, OUT INUL int2) as "
+ "'begin "
+ "select max_val into imax from byte_tab;"
+ "select min_val into imin from byte_tab;"
+ "select null_val into inul from byte_tab;"
+ " end;' "
+ "language 'plpgsql';");
}
catch (Exception ex)
{
fail ( ex.getMessage());
throw ex;
}
try
{
CallableStatement cstmt = con.prepareCall("{ call byte_proc(?,?,?) }");
cstmt.registerOutParameter(1,java.sql.Types.TINYINT);
cstmt.registerOutParameter(2,java.sql.Types.TINYINT);
cstmt.registerOutParameter(3,java.sql.Types.TINYINT);
cstmt.executeUpdate();
assertTrue(cstmt.getByte( 1 ) == 127 );
assertTrue( cstmt.getByte(2) == -128 );
cstmt.getByte(3);
assertTrue( cstmt.wasNull() );
}
catch ( Exception ex )
{
fail(ex.getMessage());
}
finally
{
try
{
Statement dstmt = con.createStatement();
dstmt.execute("drop function byte_proc()");
}
catch (Exception ex){}
}
}
public void testMultipleOutExecutions() throws SQLException
{
CallableStatement cs = con.prepareCall("{call myiofunc(?, ?)}");
for (int i=0; i<10; i++) {
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(1, i);
cs.execute();
assertEquals(1, cs.getInt(1));
assertEquals(i, cs.getInt(2));
cs.clearParameters();
}
}
}
|