pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to add data with ResultSet object?
3+ messages / 2 participants
[nested] [flat]

* How to add data with ResultSet object?
@ 2007-11-26 10:21 vasylenko@uksatse.org.ua
  2007-11-26 20:56 ` Re: How to add data with ResultSet object? Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 3+ messages in thread

From: vasylenko@uksatse.org.ua @ 2007-11-26 10:21 UTC (permalink / raw)
  To: pgsql-jdbc

Hi!

I hava base table 

CREATE TABLE forcehol
(
  w_id integer NOT NULL,
  bday date NOT NULL,
  duration integer NOT NULL,
  eday date NOT NULL DEFAULT (now() + '1 mon'::interval),
  CONSTRAINT "force_PK" PRIMARY KEY (w_id, bday),
 
);


and have ths child of it


CREATE TABLE adminhol
(
-- Inherited:   w_id integer NOT NULL,
-- Inherited:   bday date NOT NULL,
-- Inherited:   duration integer NOT NULL,
-- Inherited:   eday date NOT NULL DEFAULT (now() + '1 mon'::interval),
  CONSTRAINT "admin_PK" PRIMARY KEY (w_id, bday),
 
) INHERITS (forcehol)  ;


I have pushed a lot of data into forcehol and adminhol


I use "SELECT * from ONLY forcehol" to see raws.

Now about the question:

Using library postgresql-8.2-505.jdbc3.jar   I have such class


class A{

        private Connection conn;
        private ResultSet rs;

        public A(){

        try{
        //make coonnection to DB
        ...
        //get result
        Statement stat = 
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        rs =  stat.executeQuery("SELECT * from ONLY forcehol");
        //now, I'm trying to add new row into the forcehol table

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
-->     //here we have exception    with the text "No primary key found for table ONLY"
        rs.moveToInsertRow(); 
 

        rs.updateObject(1,...);
        //....
        rs.updateObject(4,...);
        rs.insertRow();
        } 
        catch(SQLException ex)
               {
                         System.out.print(ex.getMessage()+"\n"); 
                  }


        }
}


How can I beat such exception about unccorect analysing of my query with 
ONLY syntax?
ResultSet  keep the nesessary rows, but I can't insert, udate or dalete 
any new rows.


Thanks a lot!

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to add data with ResultSet object?
  2007-11-26 10:21 How to add data with ResultSet object? vasylenko@uksatse.org.ua
@ 2007-11-26 20:56 ` Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Kris Jurka @ 2007-11-26 20:56 UTC (permalink / raw)
  To: vasylenko@uksatse.org.ua; +Cc: pgsql-jdbc



On Mon, 26 Nov 2007, vasylenko@uksatse.org.ua wrote:

> I use "SELECT * from ONLY forcehol" to see raws.
>
> -->     //here we have exception    with the text "No primary key found for table ONLY"
>        rs.moveToInsertRow();
>
> How can I beat such exception about unccorect analysing of my query with
> ONLY syntax?

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 Jurka



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to add data with ResultSet object?
@ 2007-11-27 19:35 Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Kris Jurka @ 2007-11-27 19:35 UTC (permalink / raw)
  To: vasylenko@uksatse.org.ua; +Cc: pgsql-jdbc

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 {
^ permalink raw reply [nested|flat] 3+ messages in thread


end of thread, other threads:[~2007-11-27 19:35 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-11-26 10:21 How to add data with ResultSet object? vasylenko@uksatse.org.ua
2007-11-26 20:56 ` Kris Jurka <books@ejurka.com>
2007-11-27 19:35 Re: How to add data with ResultSet object? Kris Jurka <books@ejurka.com>

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