Skip to content

Commit

Permalink
fix: Revert inet default Java type to PGObject and handle values with…
Browse files Browse the repository at this point in the history
… net masks (#1568)

Reverts inet type columns to return their value as a PGObject rather than parsing
them as InetAddress values. This ensures that the mask value is not lost.

Also corrects the getObject(..., InetAddress.class) handling to split out the net mask
and return only the address portion.
  • Loading branch information
sehrope authored and davecramer committed Sep 13, 2019
1 parent e9423dc commit 3df32f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 52 deletions.
11 changes: 0 additions & 11 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Expand Up @@ -39,8 +39,6 @@
import org.postgresql.util.PSQLState;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
Expand Down Expand Up @@ -551,15 +549,6 @@ public Object getObject(String type, String value, byte[] byteValue) throws SQLE
}
}

if (type.equals("inet")) {
try {
return InetAddress.getByName(value);
} catch (UnknownHostException e) {
throw new PSQLException(GT.tr("IP address {0} of a host could not be determined", value),
PSQLState.CONNECTION_FAILURE, e);
}
}

PGobject obj = null;

if (LOGGER.isLoggable(Level.FINEST)) {
Expand Down
26 changes: 9 additions & 17 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Expand Up @@ -36,6 +36,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
Expand Down Expand Up @@ -217,10 +218,6 @@ protected Object internalGetObject(int columnIndex, Field field) throws SQLExcep
return getString(columnIndex);
}

if (type.equals("inet")) {
return getInetAddress(getPGType(columnIndex), getString(columnIndex));
}

if (type.equals("uuid")) {
if (isBinary(columnIndex)) {
return getUUID(thisRow[columnIndex - 1]);
Expand Down Expand Up @@ -3095,16 +3092,6 @@ protected Object getUUID(byte[] data) throws SQLException {
return new UUID(ByteConverter.int8(data, 0), ByteConverter.int8(data, 8));
}

protected Object getInetAddress(String type, String data) throws SQLException {
InetAddress inet;
try {
inet = (InetAddress) connection.getObject(type, data, null);
} catch (IllegalArgumentException iae) {
throw new PSQLException(GT.tr("Invalid Inet data."), PSQLState.INVALID_PARAMETER_VALUE, iae);
}
return inet;
}

private class PrimaryKey {
int index; // where in the result set is this primaryKey
String name; // what is the columnName of this primary Key
Expand Down Expand Up @@ -3355,11 +3342,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else if (type == UUID.class) {
return type.cast(getObject(columnIndex));
} else if (type == InetAddress.class) {
Object addressString = getObject(columnIndex);
if (addressString == null) {
String inetText = getString(columnIndex);
if (inetText == null) {
return null;
}
return type.cast(getObject(columnIndex));
int slash = inetText.indexOf("/");
try {
return type.cast(InetAddress.getByName(slash < 0 ? inetText : inetText.substring(0, slash)));
} catch (UnknownHostException ex) {
throw new PSQLException(GT.tr("Invalid Inet data."), PSQLState.INVALID_PARAMETER_VALUE, ex);
}
// JSR-310 support
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
} else if (type == LocalDate.class) {
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.postgresql.test.TestUtil;
import org.postgresql.util.PGInterval;
import org.postgresql.util.PGmoney;
import org.postgresql.util.PGobject;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -938,42 +939,48 @@ public void testGetInetAddressNull() throws SQLException, UnknownHostException {
}
}

/**
* Test the behavior getObject for inet columns.
*/
@Test
public void testGetInet4Address() throws SQLException, UnknownHostException {
private void testInet(String inet, InetAddress expectedAddr, String expectedText) throws SQLException, UnknownHostException {
PGobject expectedObj = new PGobject();
expectedObj.setType("inet");
expectedObj.setValue(expectedText);
Statement stmt = conn.createStatement();
String expected = "192.168.100.128";
stmt.executeUpdate(TestUtil.insertSQL("table1","inet_column","'" + expected + "'"));

ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "inet_column"));
ResultSet rs = stmt.executeQuery("SELECT '" + inet + "'::inet AS inet_column");
try {
assertTrue(rs.next());
assertEquals(InetAddress.getByName(expected), rs.getObject("inet_column", InetAddress.class));
assertEquals(InetAddress.getByName(expected), rs.getObject(1, InetAddress.class));
assertEquals("The string value of the inet should match when fetched via getString(...)", expectedText, rs.getString(1));
assertEquals("The string value of the inet should match when fetched via getString(...)", expectedText, rs.getString("inet_column"));
assertEquals("The object value of the inet should match when fetched via getObject(...)", expectedObj, rs.getObject(1));
assertEquals("The object value of the inet should match when fetched via getObject(...)", expectedObj, rs.getObject("inet_column"));
assertEquals("The InetAddress value should match when fetched via getObject(..., InetAddress.class)", expectedAddr, rs.getObject("inet_column", InetAddress.class));
assertEquals("The InetAddress value should match when fetched via getObject(..., InetAddress.class)", expectedAddr, rs.getObject(1, InetAddress.class));
} finally {
rs.close();
stmt.close();
}
}

/**
* Test the behavior getObject for inet columns.
* Test the behavior getObject for ipv4 inet columns.
*/
@Test
public void testGetInet6Address() throws SQLException, UnknownHostException {
Statement stmt = conn.createStatement();
String expected = "2001:4f8:3:ba:2e0:81ff:fe22:d1f1";
stmt.executeUpdate(TestUtil.insertSQL("table1","inet_column","'" + expected + "'"));
public void testGetInet4Address() throws SQLException, UnknownHostException {
String inet = "192.168.100.128";
InetAddress addr = InetAddress.getByName(inet);
testInet(inet, addr, inet);
testInet(inet + "/16", addr, inet + "/16");
testInet(inet + "/32", addr, inet);
}

ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "inet_column"));
try {
assertTrue(rs.next());
assertEquals(InetAddress.getByName(expected), rs.getObject("inet_column", InetAddress.class));
assertEquals(InetAddress.getByName(expected), rs.getObject(1, InetAddress.class));
} finally {
rs.close();
}
/**
* Test the behavior getObject for ipv6 inet columns.
*/
@Test
public void testGetInet6Address() throws SQLException, UnknownHostException {
String inet = "2001:4f8:3:ba:2e0:81ff:fe22:d1f1";
InetAddress addr = InetAddress.getByName(inet);
testInet(inet, addr, inet);
testInet(inet + "/16", addr, inet + "/16");
testInet(inet + "/128", addr, inet);
}

}

0 comments on commit 3df32f9

Please sign in to comment.