Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JDBC type for ctid in postgres?

I cannot seem to set the proper type in a prepared statement. This code:

String sql = "delete from foo where ctid = ?";
PreparedStatement deleteStmt = conn.prepareStatement( sql );
deleteStmt.setString(1, "(0,43)");  // select ctid from foo shows (0,43) exists....
int a = deleteStmt.executeUpdate();

throws this exception:

org.postgresql.util.PSQLException: ERROR: operator does not exist: tid = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.   Position: 28

Note that from psql, the delete works using a string:

mydb=# DELETE FROM foo where ctid = '(0,43)';
DELETE 1

What is the proper type/encoding for a tid in a JDBC PreparedStatement? I have tried setRowId() (throws ava.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4PreparedStatement.setRowId(int, RowId) is not yet implemented.) and setBytes() (throws ...operator does not exist: tid = byte)

like image 427
Buzz Moschetti Avatar asked Sep 17 '13 14:09

Buzz Moschetti


1 Answers

Solved! You have to manually create a PGO object and set the type and value and pass it to JDBC as a object. This now works:

sql = "delete from foo where ctid = ?";
deleteStmt = conn.prepareStatement( sql );
org.postgresql.util.PGobject pgo = new org.postgresql.util.PGobject();
pgo.setType("tid");
pgo.setValue("(0,54)");  // value is a string as might be returned in select ctid from foo and then resultSet.getString(1);
deleteStmt.setObject(1, pgo);

int a = deleteStmt.executeUpdate();
System.out.println("delete returns " + a);
like image 192
Buzz Moschetti Avatar answered Oct 01 '22 18:10

Buzz Moschetti