X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A30E5329E87 for ; Tue, 21 Sep 2004 01:09:16 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68211-08 for ; Tue, 21 Sep 2004 00:09:07 +0000 (GMT) Received: from smtp8.poczta.onet.pl (smtp8.poczta.onet.pl [213.180.130.48]) by svr1.postgresql.org (Postfix) with ESMTP id 8BFD6329E8B for ; Tue, 21 Sep 2004 01:09:09 +0100 (BST) Received: from bj210.internetdsl.tpnet.pl ([80.53.193.210]:64414 "EHLO pyszny-j.rydnet") by ps8.test.onet.pl with ESMTP id ; Tue, 21 Sep 2004 02:09:05 +0200 Subject: Auto-increment serial (Postgresql JDBC driver w/ OpenOffice.org ;) ) From: "Jaroslaw J. Pyszny" To: pgsql-jdbc Content-Type: multipart/mixed; boundary="=-zIGvXZpxOKXSJBLhoA4O" Message-Id: <1095725344.10857.107.camel@lx.siec> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.3 (1.4.3-9.1.aur.2) Date: 21 Sep 2004 02:09:04 +0200 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/131 X-Sequence-Number: 11021 --=-zIGvXZpxOKXSJBLhoA4O Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi, I know my English is very strange ;) 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 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 <-= --=-zIGvXZpxOKXSJBLhoA4O Content-Disposition: attachment; filename=jdbc_autoincrement.patch Content-Type: text/x-patch; name=jdbc_autoincrement.patch; charset=ISO-8859-2 Content-Transfer-Encoding: 7bit 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(); + } + } } /* --=-zIGvXZpxOKXSJBLhoA4O--