Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix LocalDate.MAX, special case for postgresql time column. Use 24:00… #992

Merged
merged 4 commits into from Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Expand Up @@ -529,7 +529,13 @@ public Time getTime(int i, java.util.Calendar cal) throws SQLException {
}

String string = getString(i);
return connection.getTimestampUtils().toTime(cal, string);
if (string.equals("24:00:00")) {
return Time.valueOf(string);
} else if (string.equals("00:00:00")) {
return new Time(0);
} else {
return connection.getTimestampUtils().toTime(cal, string);
}
}

//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
Expand Down
14 changes: 9 additions & 5 deletions pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
Expand Up @@ -405,6 +405,11 @@ public LocalTime toLocalTime(String s) throws SQLException {
if (s == null) {
return null;
}

if (s.equals("24:00:00")) {
return LocalTime.MAX;
}

try {
return LocalTime.parse(s);
} catch (DateTimeParseException nfe) {
Expand Down Expand Up @@ -692,14 +697,13 @@ public synchronized String toString(LocalDate localDate) {
}

public synchronized String toString(LocalTime localTime) {
if (LocalTime.MAX.equals(localTime)) {
return "infinity";
} else if (LocalTime.MIN.equals(localTime)) {
return "-infinity";
}

sbuf.setLength(0);

if (localTime.equals( LocalTime.MAX )) {
return "24:00:00";
}

appendTime(sbuf, localTime);

return sbuf.toString();
Expand Down
Expand Up @@ -8,11 +8,14 @@
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Assert;
import org.junit.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalTime;


public class PreparedStatementTest extends BaseTest4 {
Expand All @@ -22,12 +25,14 @@ 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();
}

Expand Down Expand Up @@ -60,4 +65,31 @@ public void testTimeTzSetNull() throws SQLException {

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.assertEquals(true, rs.next());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertTrue here was fine.


LocalTime localTime = (LocalTime)rs.getObject(1,LocalTime.class);


Assert.assertEquals( LocalTime.MAX, localTime);

Assert.assertEquals(true, rs.next());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertTrue here was fine.


localTime = (LocalTime)rs.getObject(1, LocalTime.class);

Assert.assertEquals( LocalTime.MIN, localTime);


}
}