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
@@ -793,6 +793,7 @@ private Object buildArray(PgArrayList input, int index, int count) throws SQLExc

throw org.postgresql.Driver.notImplemented(this.getClass(), "getArrayImpl(long,int,Map)");
}
System.out.println("testing get object data in pgarray-->" + ret);

This comment has been minimized.

Copy link
@davecramer

davecramer Jul 16, 2019

Member

Please remove any System.out.println use logging instead

This comment has been minimized.

Copy link
@myowaithant9

myowaithant9 Jul 16, 2019

Author Contributor

@davecramer It is the outdated version and I have already removed System.out.println. Could you please check overall commit? There are only 2 files I have changed, PgConnection and PgResultSet. So, do I still need to use logging for PgArray?


return ret;
}
@@ -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;
@@ -542,15 +544,27 @@ public LargeObjectManager getLargeObjectAPI() throws SQLException {
public Object getObject(String type, String value, byte[] byteValue) throws SQLException {
if (typemap != null) {
Class<?> c = typemap.get(type);
System.out.println("check type c in getOject in PgConnection : " + c);

This comment has been minimized.

Copy link
@davecramer

davecramer Jul 16, 2019

Member

use logger instead

if (c != null) {
// Handle the type (requires SQLInput & SQLOutput classes to be implemented)
throw new PSQLException(GT.tr("Custom type maps are not supported."),
PSQLState.NOT_IMPLEMENTED);
}
}

}
}
/**/
if(type.equals("inet")) {
try {
System.out.println(InetAddress.getByName(value));
return InetAddress.getByName(value);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**/

PGobject obj = null;

if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Constructing object from type={0} value=<{1}>", new Object[]{type, value});
}
@@ -565,8 +579,9 @@ public Object getObject(String type, String value, byte[] byteValue) throws SQLE
// point, etc).

if (klass != null) {
obj = klass.newInstance();
obj = klass.newInstance();
obj.setType(type);
System.out.println("Object type -->" + obj.getType());
if (byteValue != null && obj instanceof PGBinaryObject) {
PGBinaryObject binObj = (PGBinaryObject) obj;
binObj.setByteValue(byteValue, 0);
@@ -580,7 +595,7 @@ public Object getObject(String type, String value, byte[] byteValue) throws SQLE
obj.setType(type);
obj.setValue(value);
}

System.out.println("Object -->" + obj.getValue());
return obj;
} catch (SQLException sx) {
// rethrow the exception. Done because we capture any others next
@@ -210,21 +210,26 @@ protected Object internalGetObject(int columnIndex, Field field) throws SQLExcep
return getClob(columnIndex);
case Types.BLOB:
return getBlob(columnIndex);

default:
String type = getPGType(columnIndex);

// if the backend doesn't know the type then coerce to String
if (type.equals("unknown")) {
return getString(columnIndex);
}

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

if (type.equals("uuid")) {
if (isBinary(columnIndex)) {
return getUUID(thisRow[columnIndex - 1]);
}
return getUUID(getString(columnIndex));
}


// Specialized support for ref cursors is neater.
if (type.equals("refcursor")) {
@@ -2518,6 +2523,14 @@ public Object getObject(int columnIndex) throws SQLException {

field = fields[columnIndex - 1];

// /**/
// if(field.getColumnLabel().equals("inet")) {
// System.out.println("it is net");
// System.out.println(field.getMetadata());
// Object result = internalGetObject(columnIndex, field);
//
// }
// /**/
// some fields can be null, mainly from those returned by MetaData methods
if (field == null) {
wasNullFlag = true;
@@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
@@ -124,6 +125,10 @@ public void setUp() throws Exception {
TestUtil.createTable(con, "testpgobject", "id integer NOT NULL, d date, PRIMARY KEY (id)");
stmt.execute("INSERT INTO testpgobject VALUES(1, '2010-11-3')");

/**/
TestUtil.createTable(con, "testinetobj", "id integer NOT NULL, ip inet, PRIMARY KEY (id)");
stmt.execute("INSERT INTO testinetobj VALUES(1, '74.125.67.100')");
/**/
stmt.close();
}

@@ -138,6 +143,7 @@ public void tearDown() throws SQLException {
TestUtil.dropTable(con, "testboolint");
TestUtil.dropTable(con, "testnumeric");
TestUtil.dropTable(con, "testpgobject");
TestUtil.dropTable(con, "testinetobj");
super.tearDown();
}

@@ -157,7 +163,6 @@ public void testAbsolute() throws SQLException {
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM testrs");

assertTrue(!rs.absolute(0));
assertEquals(0, rs.getRow());

@@ -861,6 +866,63 @@ public void testUpdateWithPGobject() throws SQLException {

stmt.close();
}

/**/
@Test
public void testInetPGobject() throws SQLException {
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

ResultSet rs = stmt.executeQuery("select * from testinetobj where id = 1");

assertTrue(rs.next());
// assertEquals("2010-11-03", rs.getDate("d").toString());

/*test output*/
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount() ; // returns the number of columns
for(int i = 1; i < columnCount + 1; i++){
System.out.println("col name for inet pgobject : " + rsmd.getColumnName(i));
System.out.println("col type for inet pgobject : " + rsmd.getColumnType(i));
Object test = rs.getObject(i);
System.out.println("getObject data for inet pgobject : " + test);
System.out.println("getObject type for inet pgobject : " + test.getClass().getName());
}
// do{
//
// int tmp = rs.getInt("id");
// // ... get uuid and admin level
// System.out.println("ResultSet in inet : " + tmp);
// String tmp1 = rs.getString("i");
// // ... get uuid and admin level
// System.out.println("ResultSet in inet : " + tmp1);
// }while(rs.next());

// PGobject pgobj = new PGobject();
// pgobj.setType("inet");
// pgobj.setValue("127.0.0.2");
// rs.updateObject("i", pgobj);
// rs.updateRow();
rs.close();

// ResultSet rs1 = stmt.executeQuery("select * from testinetobj where id = 1");
// assertTrue(rs1.next());
//// assertEquals("2014-12-23", rs1.getDate("d").toString());
// while(rs1 != null){
//
// int tmp = rs1.getInt("id");
// // ... get uuid and admin level
// System.out.println("ResultSet in inet : " + tmp);
// String tmp1 = rs1.getString("i");
// // ... get uuid and admin level
// System.out.println("ResultSet in inet : " + tmp1);
// }
// rs1.close();

stmt.close();
}

/**/

/**
* Test the behavior of the result set column mapping cache for simple statements.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.