pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jaroslaw J. Pyszny <arghil@poczta.onet.pl>
To: pgsql-jdbc <pgsql-jdbc@postgresql.org>
Subject: Auto-increment serial (Postgresql JDBC driver w/ OpenOffice.org ;) )
Date: 21 Sep 2004 02:09:04 +0200
Message-ID: <1095725344.10857.107.camel@lx.siec> (raw)

Hi,

<comment>
I know my English is very strange ;)
</comment>

Under OO 'serial' columns don't work, because OO doesn't see
'serial' as auto-increment type. I know it's pseuso-type, but
we can detect it. What do you think, about add this feature 
to driver?

This patch is my simple implementation.

Best regards
Jarek

-- 
8  \\|||//  Jaroslaw J. Pyszny <arghil@poczta.onet.pl>
b   (o|o)    Linux admin/devel ("Vitae,non scholae,discimus")
i  --\_/--   Linux user: #96704    (http://counter.li.org)
t =->computer - daj znac jesli chcesz sie pozbyc <-=

Attachments:

jdbc_autoincrement.patch (text/x-patch, 3.4K) download diff -urN pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java java/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java --- pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 2004-08-11 12:34:39.000000000 +0200 +++ java/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 2004-09-18 23:36:21.000000000 +0200 @@ -3501,6 +3501,7 @@ byte b9[] = connection.encodeString("9"); byte b10[] = connection.encodeString("10"); byte bf[] = connection.encodeString("f"); + byte bt[] = connection.encodeString("t"); byte bnn[] = connection.encodeString(Integer.toString(java.sql.DatabaseMetaData.typeNoNulls)); byte bts[] = connection.encodeString(Integer.toString(java.sql.DatabaseMetaData.typeSearchable)); @@ -3516,7 +3517,12 @@ tuple[8] = bts; tuple[9] = bf; // false for now - it's signed tuple[10] = bf; // false for now - must handle money - tuple[11] = bf; // false for now - handle autoincrement + if ( typname.equals("int4") || typname.equals("int8") ) { + tuple[11] = bt; + } + else { + tuple[11] = bf; + } // 12 - LOCAL_TYPE_NAME is null // 13 & 14 ? // 15 & 16 are unused so we return null diff -urN pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java java/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java --- pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java 2004-08-05 12:01:21.000000000 +0200 +++ java/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java 2004-09-19 01:11:00.000000000 +0200 @@ -15,7 +15,8 @@ private Hashtable tableNameCache; private Hashtable schemaNameCache; - + private Hashtable autoIncrementNameCache; + /* * Initialise for a result with a tuple set and * a field descriptor set @@ -42,6 +43,7 @@ /* * Is the column automatically numbered (and thus read-only) * I believe that PostgreSQL does not support this feature. + * It's serial in PostgreSQL * * @param column the first column is 1, the second is 2... * @return true if so @@ -49,7 +51,59 @@ */ public boolean isAutoIncrement(int column) throws SQLException { - return false; + Field field = getField(column); + int tableOID = field.getTableOid(); + + if (field.getTableOid() == 0) + { + return false; + } + + String tabID = Integer.toString(tableOID) + "_" + Integer.toString(column); + + if (autoIncrementNameCache == null) + { + autoIncrementNameCache = new Hashtable(); + } + + Boolean autoIncrementVal = (Boolean) autoIncrementNameCache.get(tabID); + + if (autoIncrementVal != null) + { + return autoIncrementVal.booleanValue(); + } + else + { + ResultSet res = null; + PreparedStatement ps = null; + try + { + String sql = "SELECT def.adsrc FROM pg_catalog.pg_class c " + + "JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid) " + + "LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid AND a.attnum = def.adnum) " + + "WHERE c.oid = ? and attnum = ? AND def.adsrc LIKE '%nextval(%'"; + ps = ((Connection)connection).prepareStatement(sql); + ps.setInt(1, tableOID); + ps.setInt(2, column); + res = ps.executeQuery(); + if (res.next()) + { + autoIncrementNameCache.put(tabID, Boolean.TRUE); + return true; + } + else + { + autoIncrementNameCache.put(tabID, Boolean.FALSE); + return false; + } + } finally + { + if (res != null) + res.close(); + if (ps != null) + ps.close(); + } + } } /*

view thread (13+ messages)  latest in thread

Message-ID: <1095725344.10857.107.camel@lx.siec>
Permalink:  ../1095725344.10857.107.camel@lx.siec/
Also on:    postgresql.org/message-id/1095725344.10857.107.camel@lx.siec

 ·  · 

reply

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: arghil@poczta.onet.pl
  Subject: Re: Auto-increment serial (Postgresql JDBC driver w/ OpenOffice.org ;) )
  In-Reply-To: <1095725344.10857.107.camel@lx.siec>
  References: <1095725344.10857.107.camel@lx.siec>

* 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