/
PreparedStatementTest.java
1531 lines (1322 loc) · 52.9 KB
/
PreparedStatementTest.java
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
/*
* Copyright (c) 2004, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.PGStatement;
import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.PgStatement;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;
import org.postgresql.test.util.BrokenInputStream;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
@RunWith(Parameterized.class)
public class PreparedStatementTest extends BaseTest4 {
public PreparedStatementTest(BinaryMode binaryMode) {
setBinaryMode(binaryMode);
}
@Parameterized.Parameters(name = "binary = {0}")
public static Iterable<Object[]> data() {
Collection<Object[]> ids = new ArrayList<Object[]>();
for (BinaryMode binaryMode : BinaryMode.values()) {
ids.add(new Object[]{binaryMode});
}
return ids;
}
@Override
public void setUp() throws Exception {
super.setUp();
TestUtil.createTable(con, "streamtable", "bin bytea, str text");
TestUtil.createTable(con, "texttable", "ch char(3), te text, vc varchar(3)");
TestUtil.createTable(con, "intervaltable", "i interval");
TestUtil.createTable(con, "inttable", "a int");
TestUtil.createTable(con, "bool_tab", "bool_val boolean, null_val boolean, tf_val boolean, "
+ "truefalse_val boolean, yn_val boolean, yesno_val boolean, "
+ "onoff_val boolean, onezero_val boolean");
}
@Override
public void tearDown() throws SQLException {
TestUtil.dropTable(con, "streamtable");
TestUtil.dropTable(con, "texttable");
TestUtil.dropTable(con, "intervaltable");
TestUtil.dropTable(con, "inttable");
TestUtil.dropTable(con, "bool_tab");
super.tearDown();
}
private int getNumberOfServerPreparedStatements(String sql)
throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = con.prepareStatement(
"select count(*) from pg_prepared_statements where statement = ?");
pstmt.setString(1, sql);
rs = pstmt.executeQuery();
rs.next();
return rs.getInt(1);
} finally {
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(pstmt);
}
}
@Test
public void testSetBinaryStream() throws SQLException {
assumeByteaSupported();
ByteArrayInputStream bais;
byte[] buf = new byte[10];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) i;
}
bais = null;
doSetBinaryStream(bais, 0);
bais = new ByteArrayInputStream(new byte[0]);
doSetBinaryStream(bais, 0);
bais = new ByteArrayInputStream(buf);
doSetBinaryStream(bais, 0);
bais = new ByteArrayInputStream(buf);
doSetBinaryStream(bais, 10);
}
@Test
public void testSetAsciiStream() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, "ASCII"));
pw.println("Hello");
pw.flush();
ByteArrayInputStream bais;
bais = new ByteArrayInputStream(baos.toByteArray());
doSetAsciiStream(bais, 0);
bais = new ByteArrayInputStream(baos.toByteArray());
doSetAsciiStream(bais, 6);
bais = new ByteArrayInputStream(baos.toByteArray());
doSetAsciiStream(bais, 100);
}
@Test
public void testExecuteStringOnPreparedStatement() throws Exception {
PreparedStatement pstmt = con.prepareStatement("SELECT 1");
try {
pstmt.executeQuery("SELECT 2");
fail("Expected an exception when executing a new SQL query on a prepared statement");
} catch (SQLException e) {
}
try {
pstmt.executeUpdate("UPDATE streamtable SET bin=bin");
fail("Expected an exception when executing a new SQL update on a prepared statement");
} catch (SQLException e) {
}
try {
pstmt.execute("UPDATE streamtable SET bin=bin");
fail("Expected an exception when executing a new SQL statement on a prepared statement");
} catch (SQLException e) {
}
}
@Test
public void testBinaryStreamErrorsRestartable() throws SQLException {
byte[] buf = new byte[10];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) i;
}
// InputStream is shorter than the length argument implies.
InputStream is = new ByteArrayInputStream(buf);
runBrokenStream(is, buf.length + 1);
// InputStream throws an Exception during read.
is = new BrokenInputStream(new ByteArrayInputStream(buf), buf.length / 2);
runBrokenStream(is, buf.length);
// Invalid length < 0.
is = new ByteArrayInputStream(buf);
runBrokenStream(is, -1);
// Total Bind message length too long.
is = new ByteArrayInputStream(buf);
runBrokenStream(is, Integer.MAX_VALUE);
}
private void runBrokenStream(InputStream is, int length) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("INSERT INTO streamtable (bin,str) VALUES (?,?)");
pstmt.setBinaryStream(1, is, length);
pstmt.setString(2, "Other");
pstmt.executeUpdate();
fail("This isn't supposed to work.");
} catch (SQLException sqle) {
// don't need to rollback because we're in autocommit mode
pstmt.close();
// verify the connection is still valid and the row didn't go in.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM streamtable");
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
rs.close();
stmt.close();
}
}
private void doSetBinaryStream(ByteArrayInputStream bais, int length) throws SQLException {
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO streamtable (bin,str) VALUES (?,?)");
pstmt.setBinaryStream(1, bais, length);
pstmt.setString(2, null);
pstmt.executeUpdate();
pstmt.close();
}
private void doSetAsciiStream(InputStream is, int length) throws SQLException {
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO streamtable (bin,str) VALUES (?,?)");
pstmt.setBytes(1, null);
pstmt.setAsciiStream(2, is, length);
pstmt.executeUpdate();
pstmt.close();
}
@Test
public void testTrailingSpaces() throws SQLException {
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO texttable (ch, te, vc) VALUES (?, ?, ?) ");
String str = "a ";
pstmt.setString(1, str);
pstmt.setString(2, str);
pstmt.setString(3, str);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("SELECT ch, te, vc FROM texttable WHERE ch=? AND te=? AND vc=?");
pstmt.setString(1, str);
pstmt.setString(2, str);
pstmt.setString(3, str);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(str, rs.getString(1));
assertEquals(str, rs.getString(2));
assertEquals(str, rs.getString(3));
rs.close();
pstmt.close();
}
@Test
public void testBinds() throws SQLException {
// braces around (42) are required to puzzle the parser
String query = "INSERT INTO inttable(a) VALUES (?);SELECT (42)";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, 100500);
ps.execute();
ResultSet rs = ps.getResultSet();
Assert.assertNull("insert produces no results ==> getResultSet should be null", rs);
Assert.assertTrue("There are two statements => getMoreResults should be true", ps.getMoreResults());
rs = ps.getResultSet();
Assert.assertNotNull("select produces results ==> getResultSet should be not null", rs);
Assert.assertTrue("select produces 1 row ==> rs.next should be true", rs.next());
Assert.assertEquals("second result of query " + query, 42, rs.getInt(1));
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(ps);
}
@Test
public void testSetNull() throws SQLException {
// valid: fully qualified type to setNull()
PreparedStatement pstmt = con.prepareStatement("INSERT INTO texttable (te) VALUES (?)");
pstmt.setNull(1, Types.VARCHAR);
pstmt.executeUpdate();
// valid: fully qualified type to setObject()
pstmt.setObject(1, null, Types.VARCHAR);
pstmt.executeUpdate();
// valid: setObject() with partial type info and a typed "null object instance"
org.postgresql.util.PGobject dummy = new org.postgresql.util.PGobject();
dummy.setType("text");
dummy.setValue(null);
pstmt.setObject(1, dummy, Types.OTHER);
pstmt.executeUpdate();
// setObject() with no type info
pstmt.setObject(1, null);
pstmt.executeUpdate();
// setObject() with insufficient type info
pstmt.setObject(1, null, Types.OTHER);
pstmt.executeUpdate();
// setNull() with insufficient type info
pstmt.setNull(1, Types.OTHER);
pstmt.executeUpdate();
pstmt.close();
assumeMinimumServerVersion(ServerVersion.v8_3);
pstmt = con.prepareStatement("select 'ok' where ?=? or (? is null) ");
pstmt.setObject(1, UUID.randomUUID(), Types.OTHER);
pstmt.setNull(2, Types.OTHER, "uuid");
pstmt.setNull(3, Types.OTHER, "uuid");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals("ok",rs.getObject(1));
rs.close();
pstmt.close();
}
@Test
public void testSingleQuotes() throws SQLException {
String[] testStrings = new String[]{
"bare ? question mark",
"quoted \\' single quote",
"doubled '' single quote",
"octal \\060 constant",
"escaped \\? question mark",
"double \\\\ backslash",
"double \" quote",};
String[] testStringsStdConf = new String[]{
"bare ? question mark",
"quoted '' single quote",
"doubled '' single quote",
"octal 0 constant",
"escaped ? question mark",
"double \\ backslash",
"double \" quote",};
String[] expected = new String[]{
"bare ? question mark",
"quoted ' single quote",
"doubled ' single quote",
"octal 0 constant",
"escaped ? question mark",
"double \\ backslash",
"double \" quote",};
boolean oldStdStrings = TestUtil.getStandardConformingStrings(con);
Statement stmt = con.createStatement();
// Test with standard_conforming_strings turned off.
stmt.execute("SET standard_conforming_strings TO off");
for (int i = 0; i < testStrings.length; ++i) {
PreparedStatement pstmt = con.prepareStatement("SELECT '" + testStrings[i] + "'");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(expected[i], rs.getString(1));
rs.close();
pstmt.close();
}
// Test with standard_conforming_strings turned off...
// ... using the escape string syntax (E'').
stmt.execute("SET standard_conforming_strings TO on");
for (int i = 0; i < testStrings.length; ++i) {
PreparedStatement pstmt = con.prepareStatement("SELECT E'" + testStrings[i] + "'");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(expected[i], rs.getString(1));
rs.close();
pstmt.close();
}
// ... using standard conforming input strings.
for (int i = 0; i < testStrings.length; ++i) {
PreparedStatement pstmt = con.prepareStatement("SELECT '" + testStringsStdConf[i] + "'");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(expected[i], rs.getString(1));
rs.close();
pstmt.close();
}
stmt.execute("SET standard_conforming_strings TO " + (oldStdStrings ? "on" : "off"));
stmt.close();
}
@Test
public void testDoubleQuotes() throws SQLException {
String[] testStrings = new String[]{
"bare ? question mark",
"single ' quote",
"doubled '' single quote",
"doubled \"\" double quote",
"no backslash interpretation here: \\",
};
for (String testString : testStrings) {
PreparedStatement pstmt =
con.prepareStatement("CREATE TABLE \"" + testString + "\" (i integer)");
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("DROP TABLE \"" + testString + "\"");
pstmt.executeUpdate();
pstmt.close();
}
}
@Test
public void testDollarQuotes() throws SQLException {
// dollar-quotes are supported in the backend since version 8.0
PreparedStatement st;
ResultSet rs;
st = con.prepareStatement("SELECT $$;$$ WHERE $x$?$x$=$_0$?$_0$ AND $$?$$=?");
st.setString(1, "?");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals(";", rs.getString(1));
assertFalse(rs.next());
st.close();
st = con.prepareStatement(
"SELECT $__$;$__$ WHERE ''''=$q_1$'$q_1$ AND ';'=?;"
+ "SELECT $x$$a$;$x $a$$x$ WHERE $$;$$=? OR ''=$c$c$;$c$;"
+ "SELECT ?");
st.setString(1, ";");
st.setString(2, ";");
st.setString(3, "$a$ $a$");
assertTrue(st.execute());
rs = st.getResultSet();
assertTrue(rs.next());
assertEquals(";", rs.getString(1));
assertFalse(rs.next());
assertTrue(st.getMoreResults());
rs = st.getResultSet();
assertTrue(rs.next());
assertEquals("$a$;$x $a$", rs.getString(1));
assertFalse(rs.next());
assertTrue(st.getMoreResults());
rs = st.getResultSet();
assertTrue(rs.next());
assertEquals("$a$ $a$", rs.getString(1));
assertFalse(rs.next());
st.close();
}
@Test
public void testDollarQuotesAndIdentifiers() throws SQLException {
// dollar-quotes are supported in the backend since version 8.0
PreparedStatement st;
con.createStatement().execute("CREATE TEMP TABLE a$b$c(a varchar, b varchar)");
st = con.prepareStatement("INSERT INTO a$b$c (a, b) VALUES (?, ?)");
st.setString(1, "a");
st.setString(2, "b");
st.executeUpdate();
st.close();
con.createStatement().execute("CREATE TEMP TABLE e$f$g(h varchar, e$f$g varchar) ");
st = con.prepareStatement("UPDATE e$f$g SET h = ? || e$f$g");
st.setString(1, "a");
st.executeUpdate();
st.close();
}
@Test
public void testComments() throws SQLException {
PreparedStatement st;
ResultSet rs;
st = con.prepareStatement("SELECT /*?*/ /*/*/*/**/*/*/*/1;SELECT ?;--SELECT ?");
st.setString(1, "a");
assertTrue(st.execute());
assertTrue(st.getMoreResults());
assertFalse(st.getMoreResults());
st.close();
st = con.prepareStatement("SELECT /**/'?'/*/**/*/ WHERE '?'=/*/*/*?*/*/*/--?\n?");
st.setString(1, "?");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals("?", rs.getString(1));
assertFalse(rs.next());
st.close();
}
@Test
public void testDoubleQuestionMark() throws SQLException {
PreparedStatement st;
ResultSet rs;
st = con.prepareStatement("select ??- lseg '((-1,0),(1,0))';");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals("t", rs.getString(1));
assertFalse(rs.next());
st.close();
st = con.prepareStatement("select lseg '((-1,0),(1,0))' ??# box '((-2,-2),(2,2))';");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals("t", rs.getString(1));
assertFalse(rs.next());
st.close();
}
@Test
public void testDouble() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE TEMP TABLE double_tab (max_double float, min_double float, null_value float)");
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("insert into double_tab values (?,?,?)");
pstmt.setDouble(1, 1.0E125);
pstmt.setDouble(2, 1.0E-130);
pstmt.setNull(3, Types.DOUBLE);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from double_tab");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
double d = rs.getDouble(1);
assertTrue(rs.getDouble(1) == 1.0E125);
assertTrue(rs.getDouble(2) == 1.0E-130);
rs.getDouble(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}
@Test
public void testFloat() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE TEMP TABLE float_tab (max_float real, min_float real, null_value real)");
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("insert into float_tab values (?,?,?)");
pstmt.setFloat(1, (float) 1.0E37);
pstmt.setFloat(2, (float) 1.0E-37);
pstmt.setNull(3, Types.FLOAT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from float_tab");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
float f = rs.getFloat(1);
assertTrue("expected 1.0E37,received " + rs.getFloat(1), rs.getFloat(1) == (float) 1.0E37);
assertTrue("expected 1.0E-37,received " + rs.getFloat(2), rs.getFloat(2) == (float) 1.0E-37);
rs.getDouble(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}
@Test
public void testNaNLiteralsSimpleStatement() throws SQLException {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select 'NaN'::numeric, 'NaN'::real, 'NaN'::double precision");
checkNaNLiterals(stmt, rs);
}
@Test
public void testNaNLiteralsPreparedStatement() throws SQLException {
PreparedStatement stmt = con.prepareStatement("select 'NaN'::numeric, 'NaN'::real, 'NaN'::double precision");
checkNaNLiterals(stmt, stmt.executeQuery());
}
private void checkNaNLiterals(Statement stmt, ResultSet rs) throws SQLException {
rs.next();
assertTrue("Double.isNaN((Double) rs.getObject", Double.isNaN((Double) rs.getObject(3)));
assertTrue("Double.isNaN(rs.getDouble", Double.isNaN(rs.getDouble(3)));
assertTrue("Float.isNaN((Float) rs.getObject", Float.isNaN((Float) rs.getObject(2)));
assertTrue("Float.isNaN(rs.getFloat", Float.isNaN(rs.getFloat(2)));
assertTrue("Double.isNaN((Double) rs.getObject", Double.isNaN((Double) rs.getObject(1)));
assertTrue("Double.isNaN(rs.getDouble", Double.isNaN(rs.getDouble(1)));
rs.close();
stmt.close();
}
@Test
public void testNaNSetDoubleFloat() throws SQLException {
PreparedStatement ps = con.prepareStatement("select ?, ?");
ps.setFloat(1, Float.NaN);
ps.setDouble(2, Double.NaN);
checkNaNParams(ps);
}
@Test
public void testNaNSetObject() throws SQLException {
PreparedStatement ps = con.prepareStatement("select ?, ?");
ps.setObject(1, Float.NaN);
ps.setObject(2, Double.NaN);
checkNaNParams(ps);
}
private void checkNaNParams(PreparedStatement ps) throws SQLException {
ResultSet rs = ps.executeQuery();
rs.next();
assertTrue("Float.isNaN((Float) rs.getObject", Float.isNaN((Float) rs.getObject(1)));
assertTrue("Float.isNaN(rs.getFloat", Float.isNaN(rs.getFloat(1)));
assertTrue("Double.isNaN(rs.getDouble", Double.isNaN(rs.getDouble(2)));
assertTrue("Double.isNaN(rs.getDouble", Double.isNaN(rs.getDouble(2)));
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(ps);
}
@Test
public void testBoolean() throws SQLException {
testBoolean(0);
testBoolean(1);
testBoolean(5);
testBoolean(-1);
}
public void testBoolean(int prepareThreshold) throws SQLException {
PreparedStatement pstmt = con.prepareStatement("insert into bool_tab values (?,?,?,?,?,?,?,?)");
((org.postgresql.PGStatement) pstmt).setPrepareThreshold(prepareThreshold);
// Test TRUE values
pstmt.setBoolean(1, true);
pstmt.setObject(1, Boolean.TRUE);
pstmt.setNull(2, Types.BIT);
pstmt.setObject(3, 't', Types.BIT);
pstmt.setObject(3, 'T', Types.BIT);
pstmt.setObject(3, "t", Types.BIT);
pstmt.setObject(4, "true", Types.BIT);
pstmt.setObject(5, 'y', Types.BIT);
pstmt.setObject(5, 'Y', Types.BIT);
pstmt.setObject(5, "Y", Types.BIT);
pstmt.setObject(6, "YES", Types.BIT);
pstmt.setObject(7, "On", Types.BIT);
pstmt.setObject(8, '1', Types.BIT);
pstmt.setObject(8, "1", Types.BIT);
assertEquals("one row inserted, true values", 1, pstmt.executeUpdate());
// Test FALSE values
pstmt.setBoolean(1, false);
pstmt.setObject(1, Boolean.FALSE);
pstmt.setNull(2, Types.BOOLEAN);
pstmt.setObject(3, 'f', Types.BOOLEAN);
pstmt.setObject(3, 'F', Types.BOOLEAN);
pstmt.setObject(3, "F", Types.BOOLEAN);
pstmt.setObject(4, "false", Types.BOOLEAN);
pstmt.setObject(5, 'n', Types.BOOLEAN);
pstmt.setObject(5, 'N', Types.BOOLEAN);
pstmt.setObject(5, "N", Types.BOOLEAN);
pstmt.setObject(6, "NO", Types.BOOLEAN);
pstmt.setObject(7, "Off", Types.BOOLEAN);
pstmt.setObject(8, "0", Types.BOOLEAN);
pstmt.setObject(8, '0', Types.BOOLEAN);
assertEquals("one row inserted, false values", 1, pstmt.executeUpdate());
// Test weird values
pstmt.setObject(1, (byte) 0, Types.BOOLEAN);
pstmt.setObject(2, BigDecimal.ONE, Types.BOOLEAN);
pstmt.setObject(3, 0L, Types.BOOLEAN);
pstmt.setObject(4, 0x1, Types.BOOLEAN);
pstmt.setObject(5, new Float(0), Types.BOOLEAN);
pstmt.setObject(5, 1.0d, Types.BOOLEAN);
pstmt.setObject(5, 0.0f, Types.BOOLEAN);
pstmt.setObject(6, Integer.valueOf("1"), Types.BOOLEAN);
pstmt.setObject(7, new java.math.BigInteger("0"), Types.BOOLEAN);
pstmt.clearParameters();
pstmt.close();
pstmt = con.prepareStatement("select * from bool_tab");
((org.postgresql.PGStatement) pstmt).setPrepareThreshold(prepareThreshold);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue("expected true, received " + rs.getBoolean(1), rs.getBoolean(1));
rs.getFloat(2);
assertTrue(rs.wasNull());
assertTrue("expected true, received " + rs.getBoolean(3), rs.getBoolean(3));
assertTrue("expected true, received " + rs.getBoolean(4), rs.getBoolean(4));
assertTrue("expected true, received " + rs.getBoolean(5), rs.getBoolean(5));
assertTrue("expected true, received " + rs.getBoolean(6), rs.getBoolean(6));
assertTrue("expected true, received " + rs.getBoolean(7), rs.getBoolean(7));
assertTrue("expected true, received " + rs.getBoolean(8), rs.getBoolean(8));
assertTrue(rs.next());
assertFalse("expected false, received " + rs.getBoolean(1), rs.getBoolean(1));
rs.getBoolean(2);
assertTrue(rs.wasNull());
assertFalse("expected false, received " + rs.getBoolean(3), rs.getBoolean(3));
assertFalse("expected false, received " + rs.getBoolean(4), rs.getBoolean(4));
assertFalse("expected false, received " + rs.getBoolean(5), rs.getBoolean(5));
assertFalse("expected false, received " + rs.getBoolean(6), rs.getBoolean(6));
assertFalse("expected false, received " + rs.getBoolean(7), rs.getBoolean(7));
assertFalse("expected false, received " + rs.getBoolean(8), rs.getBoolean(8));
rs.close();
pstmt.close();
pstmt = con.prepareStatement("TRUNCATE TABLE bool_tab");
pstmt.executeUpdate();
pstmt.close();
}
@Test
public void testBadBoolean() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("INSERT INTO bad_bool VALUES (?)");
try {
pstmt.setObject(1, "this is not boolean", Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"this is not boolean\"", e.getMessage());
}
try {
pstmt.setObject(1, 'X', Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"X\"", e.getMessage());
}
try {
java.io.File obj = new java.io.File("");
pstmt.setObject(1, obj, Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean", e.getMessage());
}
try {
pstmt.setObject(1, "1.0", Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"1.0\"", e.getMessage());
}
try {
pstmt.setObject(1, "-1", Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"-1\"", e.getMessage());
}
try {
pstmt.setObject(1, "ok", Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"ok\"", e.getMessage());
}
try {
pstmt.setObject(1, 0.99f, Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"0.99\"", e.getMessage());
}
try {
pstmt.setObject(1, -0.01d, Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"-0.01\"", e.getMessage());
}
try {
pstmt.setObject(1, new java.sql.Date(0), Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean", e.getMessage());
}
try {
pstmt.setObject(1, new java.math.BigInteger("1000"), Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"1000\"", e.getMessage());
}
try {
pstmt.setObject(1, Math.PI, Types.BOOLEAN);
fail();
} catch (SQLException e) {
assertEquals(org.postgresql.util.PSQLState.CANNOT_COERCE.getState(), e.getSQLState());
assertEquals("Cannot cast to boolean: \"3.141592653589793\"", e.getMessage());
}
pstmt.close();
}
@Test
public void testSetFloatInteger() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE temp TABLE float_tab (max_val float8, min_val float, null_val float8)");
pstmt.executeUpdate();
pstmt.close();
Integer maxInteger = new Integer(2147483647);
Integer minInteger = new Integer(-2147483648);
Double maxFloat = new Double(2147483647);
Double minFloat = new Double(-2147483648);
pstmt = con.prepareStatement("insert into float_tab values (?,?,?)");
pstmt.setObject(1, maxInteger, Types.FLOAT);
pstmt.setObject(2, minInteger, Types.FLOAT);
pstmt.setNull(3, Types.FLOAT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from float_tab");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue("expected " + maxFloat + " ,received " + rs.getObject(1),
rs.getObject(1).equals(maxFloat));
assertTrue("expected " + minFloat + " ,received " + rs.getObject(2),
rs.getObject(2).equals(minFloat));
rs.getFloat(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}
@Test
public void testSetFloatString() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE temp TABLE float_tab (max_val float8, min_val float8, null_val float8)");
pstmt.executeUpdate();
pstmt.close();
String maxStringFloat = "1.0E37";
String minStringFloat = "1.0E-37";
Double maxFloat = new Double(1.0E37);
Double minFloat = new Double(1.0E-37);
pstmt = con.prepareStatement("insert into float_tab values (?,?,?)");
pstmt.setObject(1, maxStringFloat, Types.FLOAT);
pstmt.setObject(2, minStringFloat, Types.FLOAT);
pstmt.setNull(3, Types.FLOAT);
pstmt.executeUpdate();
pstmt.setObject(1, "1.0", Types.FLOAT);
pstmt.setObject(2, "0.0", Types.FLOAT);
pstmt.setNull(3, Types.FLOAT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from float_tab");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue(((Double) rs.getObject(1)).equals(maxFloat));
assertTrue(((Double) rs.getObject(2)).equals(minFloat));
assertTrue(rs.getDouble(1) == maxFloat);
assertTrue(rs.getDouble(2) == minFloat);
rs.getFloat(3);
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertTrue("expected true, received " + rs.getBoolean(1), rs.getBoolean(1));
assertFalse("expected false,received " + rs.getBoolean(2), rs.getBoolean(2));
rs.close();
pstmt.close();
}
@Test
public void testSetFloatBigDecimal() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE temp TABLE float_tab (max_val float8, min_val float8, null_val float8)");
pstmt.executeUpdate();
pstmt.close();
BigDecimal maxBigDecimalFloat = new BigDecimal("1.0E37");
BigDecimal minBigDecimalFloat = new BigDecimal("1.0E-37");
Double maxFloat = new Double(1.0E37);
Double minFloat = new Double(1.0E-37);
pstmt = con.prepareStatement("insert into float_tab values (?,?,?)");
pstmt.setObject(1, maxBigDecimalFloat, Types.FLOAT);
pstmt.setObject(2, minBigDecimalFloat, Types.FLOAT);
pstmt.setNull(3, Types.FLOAT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from float_tab");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue("expected " + maxFloat + " ,received " + rs.getObject(1),
((Double) rs.getObject(1)).equals(maxFloat));
assertTrue("expected " + minFloat + " ,received " + rs.getObject(2),
((Double) rs.getObject(2)).equals(minFloat));
rs.getFloat(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}
@Test
public void testSetTinyIntFloat() throws SQLException {
PreparedStatement pstmt = con
.prepareStatement("CREATE temp TABLE tiny_int (max_val int4, min_val int4, null_val int4)");
pstmt.executeUpdate();
pstmt.close();
Integer maxInt = new Integer(127);
Integer minInt = new Integer(-127);
Float maxIntFloat = new Float(127);
Float minIntFloat = new Float(-127);
pstmt = con.prepareStatement("insert into tiny_int values (?,?,?)");
pstmt.setObject(1, maxIntFloat, Types.TINYINT);
pstmt.setObject(2, minIntFloat, Types.TINYINT);
pstmt.setNull(3, Types.TINYINT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from tiny_int");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals("maxInt as rs.getObject", maxInt, rs.getObject(1));
assertEquals("minInt as rs.getObject", minInt, rs.getObject(2));
rs.getObject(3);
assertTrue("rs.wasNull after rs.getObject", rs.wasNull());
assertEquals("maxInt as rs.getInt", maxInt, (Integer) rs.getInt(1));
assertEquals("minInt as rs.getInt", minInt, (Integer) rs.getInt(2));
rs.getInt(3);
assertTrue("rs.wasNull after rs.getInt", rs.wasNull());
assertEquals("maxInt as rs.getLong", Long.valueOf(maxInt), (Long) rs.getLong(1));
assertEquals("minInt as rs.getLong", Long.valueOf(minInt), (Long) rs.getLong(2));
rs.getLong(3);
assertTrue("rs.wasNull after rs.getLong", rs.wasNull());
assertEquals("maxInt as rs.getBigDecimal", BigDecimal.valueOf(maxInt), rs.getBigDecimal(1));
assertEquals("minInt as rs.getBigDecimal", BigDecimal.valueOf(minInt), rs.getBigDecimal(2));
assertNull("rs.getBigDecimal", rs.getBigDecimal(3));
assertTrue("rs.getBigDecimal after rs.getLong", rs.wasNull());
assertEquals("maxInt as rs.getBigDecimal(scale=0)", BigDecimal.valueOf(maxInt),
rs.getBigDecimal(1, 0));
assertEquals("minInt as rs.getBigDecimal(scale=0)", BigDecimal.valueOf(minInt),
rs.getBigDecimal(2, 0));
assertNull("rs.getBigDecimal(scale=0)", rs.getBigDecimal(3, 0));
assertTrue("rs.getBigDecimal after rs.getLong", rs.wasNull());
assertEquals("maxInt as rs.getBigDecimal(scale=1)",
BigDecimal.valueOf(maxInt).setScale(1, BigDecimal.ROUND_HALF_EVEN), rs.getBigDecimal(1, 1));
assertEquals("minInt as rs.getBigDecimal(scale=1)",
BigDecimal.valueOf(minInt).setScale(1, BigDecimal.ROUND_HALF_EVEN), rs.getBigDecimal(2, 1));
rs.getFloat(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}
@Test
public void testSetSmallIntFloat() throws SQLException {
PreparedStatement pstmt = con.prepareStatement(
"CREATE temp TABLE small_int (max_val int4, min_val int4, null_val int4)");
pstmt.executeUpdate();
pstmt.close();
Integer maxInt = new Integer(32767);
Integer minInt = new Integer(-32768);
Float maxIntFloat = new Float(32767);
Float minIntFloat = new Float(-32768);
pstmt = con.prepareStatement("insert into small_int values (?,?,?)");
pstmt.setObject(1, maxIntFloat, Types.SMALLINT);
pstmt.setObject(2, minIntFloat, Types.SMALLINT);
pstmt.setNull(3, Types.TINYINT);
pstmt.executeUpdate();
pstmt.close();
pstmt = con.prepareStatement("select * from small_int");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue("expected " + maxInt + " ,received " + rs.getObject(1),
rs.getObject(1).equals(maxInt));
assertTrue("expected " + minInt + " ,received " + rs.getObject(2),
rs.getObject(2).equals(minInt));
rs.getFloat(3);
assertTrue(rs.wasNull());
rs.close();
pstmt.close();
}