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

Issue 1134 Map inet type to InetAddress #1527

Merged
merged 10 commits into from Jul 23, 2019
@@ -39,6 +39,8 @@
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;
@@ -549,6 +551,15 @@ 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 of a host could not be determined", type),

This comment has been minimized.

Copy link
@davecramer

davecramer Jul 18, 2019

Member

for debugging purposes it would be useful to include the value of the host in the Exception.

This comment has been minimized.

Copy link
@myowaithant9

myowaithant9 Jul 19, 2019

Author Contributor

@davecramer Thanks for your suggestion and I have changed a little bit.

This comment has been minimized.

Copy link
@davecramer

davecramer Jul 21, 2019

Member

Why not add the hostname instead ?

This comment has been minimized.

Copy link
@myowaithant9

myowaithant9 Jul 22, 2019

Author Contributor

@davecramer To get hostName from IP Address is like below:

InetAddress addr = InetAddress.getByName("192.168.190.62");
String host = addr.getHostName();
System.out.println(host);

So, I use InetAddress.getByName(value).getHostName() in Exception. But when I add that code, it shows me error to add throw declaration again. If I add it, the other files such as PgArray and PgResultset got error because of that declaration. That's why I use value only in Exception.

This comment has been minimized.

Copy link
@davecramer

davecramer Jul 22, 2019

Member

Yes, I do want just the value. If I'm not mistaken the code above has type not value

This comment has been minimized.

Copy link
@myowaithant9

myowaithant9 Jul 22, 2019

Author Contributor

@davecramer If so, I have already changed it in my last commit.
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)) {
@@ -36,7 +36,6 @@
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;
@@ -219,6 +218,10 @@ 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]);
@@ -3056,6 +3059,16 @@ 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
@@ -3310,11 +3323,7 @@ public void updateArray(String columnName, Array x) throws SQLException {
if (addressString == null) {
return null;
}
try {
return type.cast(InetAddress.getByName(((PGobject) addressString).getValue()));
} catch (UnknownHostException e) {
throw new SQLException("could not create inet address from string '" + addressString + "'");
}
return type.cast(getObject(columnIndex));
// JSR-310 support
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
} else if (type == LocalDate.class) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.