Skip to content
Permalink
Browse files

fix: getString for PGObject columns returns null (pgjdbc#1154)

  • Loading branch information...
KimBisgaardDmi authored and rhavermans committed Apr 11, 2018
1 parent b682c27 commit 1d7ef7d6077a0e1dd2475dd2076c61e34463d53f
@@ -1936,7 +1936,13 @@ public String getString(int columnIndex) throws SQLException {
Field field = fields[columnIndex - 1];
Object obj = internalGetObject(columnIndex, field);
if (obj == null) {
return null;
// internalGetObject() knows jdbc-types and some extra like hstore. It does not know of
// PGobject based types like geometric types but getObject does
obj = getObject(columnIndex);
if (obj == null) {
return null;
}
return obj.toString();
}
// hack to be compatible with text protocol
if (obj instanceof java.util.Date) {
@@ -21,37 +21,46 @@
import org.postgresql.util.PGobject;
import org.postgresql.util.PSQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/*
* Test case for geometric type I/O
*/
public class GeometricTest {
private Connection con;
@RunWith(Parameterized.class)
public class GeometricTest extends BaseTest4 {

public GeometricTest(BinaryMode binaryMode) {
setBinaryMode(binaryMode);
}

@Parameterized.Parameters(name = "binary = {0}")
public static Iterable<Object[]> data() {
Collection<Object[]> ids = new ArrayList<Object[]>();
for (BinaryMode binaryMode : BinaryMode.values()) {
ids.add(new Object[]{binaryMode});
}
return ids;
}

// Set up the fixture for this testcase: a connection to a database with
// a table for this test.
@Before
public void setUp() throws Exception {
con = TestUtil.openDB();
super.setUp();
TestUtil.createTable(con, "testgeometric",
"boxval box, circleval circle, lsegval lseg, pathval path, polygonval polygon, pointval point, lineval line");
}

// Tear down the fixture for this test case.
@After
public void tearDown() throws Exception {
public void tearDown() throws SQLException {
TestUtil.dropTable(con, "testgeometric");
TestUtil.closeDB(con);
super.tearDown();
}

private void checkReadWrite(PGobject obj, String column) throws Exception {
@@ -64,7 +73,10 @@ private void checkReadWrite(PGobject obj, String column) throws Exception {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + column + " FROM testgeometric");
assertTrue(rs.next());
assertEquals(obj, rs.getObject(1));
assertEquals("PGObject#equals(rs.getObject)", obj, rs.getObject(1));
PGobject obj2 = (PGobject) obj.clone();
obj2.setValue(rs.getString(1));
assertEquals("PGobject.toString vs rs.getString", obj, obj2);
rs.close();

stmt.executeUpdate("DELETE FROM testgeometric");

0 comments on commit 1d7ef7d

Please sign in to comment.
You can’t perform that action at this time.