-
Notifications
You must be signed in to change notification settings - Fork 819
/
PreparedStatementTest.java
118 lines (93 loc) · 3.39 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
/*
* Copyright (c) 2004, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc42;
import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalTime;
import java.util.Properties;
public class PreparedStatementTest extends BaseTest4 {
protected void updateProperties(Properties props) {
PGProperty.PREFER_QUERY_MODE.set(props, "simple");
}
@Override
public void setUp() throws Exception {
super.setUp();
TestUtil.createTable(con, "timestamptztable", "tstz timestamptz");
TestUtil.createTable(con, "timetztable", "ttz timetz");
TestUtil.createTable(con, "timetable", "id serial, tt time");
}
@Override
public void tearDown() throws SQLException {
TestUtil.dropTable(con, "timestamptztable");
TestUtil.dropTable(con, "timetztable");
TestUtil.dropTable(con, "timetable");
super.tearDown();
}
@Test
public void testSetNumber() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ? * 2");
pstmt.setBigDecimal(1, new BigDecimal("1.6"));
ResultSet rs = pstmt.executeQuery();
rs.next();
BigDecimal d = rs.getBigDecimal(1);
pstmt.close();
Assert.assertEquals(new BigDecimal("3.2"), d);
}
@Test
public void testSetBoolean() throws SQLException {
try (PreparedStatement ps = con.prepareStatement("select false union select (select ?)")) {
ps.setBoolean(1, true);
try (ResultSet rs = ps.executeQuery()) {
assert (rs.next());
rs.getBoolean(1);
}
}
}
@Test
public void testTimestampTzSetNull() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("INSERT INTO timestamptztable (tstz) VALUES (?)");
// valid: fully qualified type to setNull()
pstmt.setNull(1, Types.TIMESTAMP_WITH_TIMEZONE);
pstmt.executeUpdate();
// valid: fully qualified type to setObject()
pstmt.setObject(1, null, Types.TIMESTAMP_WITH_TIMEZONE);
pstmt.executeUpdate();
pstmt.close();
}
@Test
public void testTimeTzSetNull() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("INSERT INTO timetztable (ttz) VALUES (?)");
// valid: fully qualified type to setNull()
pstmt.setNull(1, Types.TIME_WITH_TIMEZONE);
pstmt.executeUpdate();
// valid: fully qualified type to setObject()
pstmt.setObject(1, null, Types.TIME_WITH_TIMEZONE);
pstmt.executeUpdate();
pstmt.close();
}
@Test
public void testLocalTimeMax() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("INSERT INTO timetable (tt) VALUES (?)");
pstmt.setObject(1, LocalTime.MAX);
pstmt.executeUpdate();
pstmt.setObject(1, LocalTime.MIN);
pstmt.executeUpdate();
ResultSet rs = con.createStatement().executeQuery("select tt from timetable order by id asc");
Assert.assertTrue(rs.next());
LocalTime localTime = (LocalTime) rs.getObject(1, LocalTime.class);
Assert.assertEquals(LocalTime.MAX, localTime);
Assert.assertTrue(rs.next());
localTime = (LocalTime) rs.getObject(1, LocalTime.class);
Assert.assertEquals(LocalTime.MIN, localTime);
}
}