From: Kris Jurka <books@ejurka.com>
To: vasylenko@uksatse.org.ua
Cc: pgsql-jdbc@postgresql.org
Subject: Re: How to add data with ResultSet object?
Date: Tue, 27 Nov 2007 12:35:07 -0700
Message-ID: <474C716B.3090406@ejurka.com> (raw)
In-Reply-To: <OF794E3BCE.796187BC-ONC22573A0.00276545-C22573A0.002E32F3@uksatse.org.ua>
References: <OF794E3BCE.796187BC-ONC22573A0.00276545-C22573A0.002E32F3@uksatse.org.ua>
vasylenko@uksatse.org.ua wrote:
> >
> >This is a known driver bug. The parser to determine the base table of an
> >updatable result set is *very* easily confused. Aside from fixing the
> >driver the only way to fix this would be to use a view to hide the ONLY
> >from the driver. CREATE VIEW myonlyview AS SELECT * FROM ONLY forcehol
> >and then writing appropriate insert rules on it so that you can have the
> >driver select from and insert into the view.
> >
>
> Kris, thanks a lot for an advice!
> That what I have done in my DB.
>
>
> I succeed with update,insert and delete calls from pgsql consol.
> But using jdbc driver I'v got another exception attempting to insert,
> update or delete a row : "No primary key found for table "adminhol_view".
>
Oops, sorry for pointing you down the wrong path.
> I think it is more easy to change a bit parseQuery() from
>
>
> -> //I add a couple of code string to aviod this problem
> -> if(tableName.toLowerCase().equals("only"))
> -> tableName = st.nextToken();
>
It needs a little bit more than that. You must use ONLY when issuing
delete or update statements because primary keys do not inherit and you
could end up updating or deleting child table data. I've applied the
attached patch to CVS for 8.0, 8.1, 8.2, and HEAD.
Kris Jurka
Attachments:only.patch (text/x-patch, 4.0K) download
? .only.txt.swp
? ChangeLog
? only.patch
Index: org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.98
diff -c -r1.98 AbstractJdbc2ResultSet.java
*** org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 27 Jul 2007 22:55:28 -0000 1.98
--- org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 27 Nov 2007 19:26:22 -0000
***************
*** 43,48 ****
--- 43,49 ----
private boolean usingOID = false; // are we using the OID for the primary key?
private Vector primaryKeys; // list of primary keys
private boolean singleTable = false;
+ private String onlyTable = "";
private String tableName = null;
private PreparedStatement updateStatement = null;
private PreparedStatement insertStatement = null;
***************
*** 748,754 ****
{
! StringBuffer deleteSQL = new StringBuffer("DELETE FROM " ).append(tableName).append(" where " );
for ( int i = 0; i < numKeys; i++ )
{
--- 749,755 ----
{
! StringBuffer deleteSQL = new StringBuffer("DELETE FROM " ).append(onlyTable).append(tableName).append(" where " );
for ( int i = 0; i < numKeys; i++ )
{
***************
*** 1183,1189 ****
}
}
! selectSQL.append(" from " ).append(tableName).append(" where ");
int numKeys = primaryKeys.size();
--- 1184,1190 ----
}
}
! selectSQL.append(" from " ).append(onlyTable).append(tableName).append(" where ");
int numKeys = primaryKeys.size();
***************
*** 1247,1253 ****
if (!doingUpdates)
return; // No work pending.
! StringBuffer updateSQL = new StringBuffer("UPDATE " + tableName + " SET ");
int numColumns = updateValues.size();
Iterator columns = updateValues.keySet().iterator();
--- 1248,1254 ----
if (!doingUpdates)
return; // No work pending.
! StringBuffer updateSQL = new StringBuffer("UPDATE " + onlyTable + tableName + " SET ");
int numColumns = updateValues.size();
Iterator columns = updateValues.keySet().iterator();
***************
*** 1642,1647 ****
--- 1643,1652 ----
if (name.toLowerCase().equals("from"))
{
tableName = st.nextToken();
+ if (tableName.toLowerCase().equals("only")) {+ tableName = st.nextToken();+ onlyTable = "ONLY ";+ }
tableFound = true;
}
}
Index: org/postgresql/test/jdbc2/UpdateableResultTest.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java,v
retrieving revision 1.27
diff -c -r1.27 UpdateableResultTest.java
*** org/postgresql/test/jdbc2/UpdateableResultTest.java 14 Nov 2007 03:17:50 -0000 1.27
--- org/postgresql/test/jdbc2/UpdateableResultTest.java 27 Nov 2007 19:26:22 -0000
***************
*** 428,434 ****
rs.moveToInsertRow();
rs.close();
st.close();
! }
public void testUpdateReadOnlyResultSet() throws Exception
{
--- 428,445 ----
rs.moveToInsertRow();
rs.close();
st.close();
! }
!
! public void testUpdateSelectOnly() throws Exception
! {
! Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
! ResultSet.CONCUR_UPDATABLE);
!
! ResultSet rs = st.executeQuery( "select * from only second");
! assertTrue(rs.next());
! rs.updateInt(1, 2);
! rs.updateRow();
! }
public void testUpdateReadOnlyResultSet() throws Exception
{
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: books@ejurka.com, vasylenko@uksatse.org.ua
Subject: Re: How to add data with ResultSet object?
In-Reply-To: <474C716B.3090406@ejurka.com>
References: <OF794E3BCE.796187BC-ONC22573A0.00276545-C22573A0.002E32F3@uksatse.org.ua> <474C716B.3090406@ejurka.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox