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 2B105329E3C for ; Thu, 23 Sep 2004 04:09:04 +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 27547-03 for ; Thu, 23 Sep 2004 03:08:58 +0000 (GMT) Received: from smtp9.poczta.onet.pl (smtp9.poczta.onet.pl [213.180.130.49]) by svr1.postgresql.org (Postfix) with ESMTP id 89197329C67 for ; Thu, 23 Sep 2004 04:08:56 +0100 (BST) Received: from bj210.internetdsl.tpnet.pl ([80.53.193.210]:56270 "EHLO pyszny-j.rydnet") by ps9.test.onet.pl with ESMTP id ; Thu, 23 Sep 2004 05:08:20 +0200 Subject: Re: Auto-increment serial (Postgresql JDBC driver w/ From: "Jaroslaw J. Pyszny" To: Kris Jurka Cc: pg@fastcrypt.com, pgsql-jdbc In-Reply-To: References: <1095725344.10857.107.camel@lx.siec> <1095794747.18680.23.camel@lx.siec> <1095798675.1557.122.camel@localhost.localdomain> <1095807031.1556.129.camel@localhost.localdomain> <1095880271.22986.37.camel@lx.siec> Content-Type: multipart/mixed; boundary="=-xDf9jmy1XfMxHwshFfWx" Message-Id: <1095908898.30959.5.camel@lx.siec> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.3 (1.4.3-9.1.aur.2) Date: 23 Sep 2004 05:08:19 +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/156 X-Sequence-Number: 11046 --=-xDf9jmy1XfMxHwshFfWx Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 8bit W liście z sro, 22-09-2004, godz. 21:59, Kris Jurka pisze: > On Wed, 22 Sep 2004, Jaroslaw J. Pyszny wrote: [...] second patch -- 8 \\|||// Jarosław J. Pyszny b (o|o) Linux admin/devel ("Vitae,non scholae,discimus") i --\_/-- Linux user: #96704 (http://counter.li.org) t =->komputer(y) - daj znać jeśli chcesz się pozbyć <-= --=-xDf9jmy1XfMxHwshFfWx Content-Disposition: attachment; filename=jdbc_autoincrement2.patch Content-Type: text/x-patch; name=jdbc_autoincrement2.patch; charset=iso-8859-2 Content-Transfer-Encoding: 7bit diff -crN pgjdbc/org/postgresql/core/Field.java pgjdbc.ai/org/postgresql/core/Field.java *** pgjdbc/org/postgresql/core/Field.java 2004-06-29 08:43:25.000000000 +0200 --- pgjdbc.ai/org/postgresql/core/Field.java 2004-09-23 00:47:33.000000000 +0200 *************** *** 28,33 **** --- 28,34 ---- private final String columnLabel; // Column label private String columnName; // Column name; null if undetermined private Integer nullable; // Is this column nullable? null if undetermined. + private Boolean autoIncrement; // Is this column automatically numbered? private int format = TEXT_FORMAT; // In the V3 protocol each field has a format // 0 = text, 1 = binary *************** *** 179,184 **** --- 180,227 ---- } } + public boolean getAutoIncrement(Connection con) throws SQLException + { + if (autoIncrement != null) + return autoIncrement.booleanValue(); + + if (tableOid == 0 || positionInTable == 0) { + autoIncrement = Boolean.FALSE; + return autoIncrement.booleanValue(); + } + + ResultSet res = null; + PreparedStatement ps = null; + try + { + final 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 = con.prepareStatement(sql); + + ps.setInt(1, tableOid); + ps.setInt(2, positionInTable); + res = ps.executeQuery(); + + int nullResult = ResultSetMetaData.columnNullableUnknown; + if (res.next()) { + autoIncrement = Boolean.TRUE; + } + else { + autoIncrement = Boolean.FALSE; + } + return autoIncrement.booleanValue(); + + } finally { + if (res != null) + res.close(); + if (ps != null) + ps.close(); + } + } + public String getColumnName(Connection con) throws SQLException { if (columnName != null) diff -crN pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java pgjdbc.ai/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java *** pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 2004-08-11 12:34:39.000000000 +0200 --- pgjdbc.ai/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 2004-09-22 21:57:10.000000000 +0200 *************** *** 3501,3506 **** --- 3501,3507 ---- 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,3527 **** 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 // 12 - LOCAL_TYPE_NAME is null // 13 & 14 ? // 15 & 16 are unused so we return null tuple[17] = b10; // everything is base 10 v.addElement(tuple); } rs.close(); --- 3517,3556 ---- tuple[8] = bts; tuple[9] = bf; // false for now - it's signed tuple[10] = bf; // false for now - must handle money ! 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 tuple[17] = b10; // everything is base 10 v.addElement(tuple); + + // add pseudo-type serial, serial4, serial8, bigserial + if ( typname.equals("int4") ) { + byte[][] tuple1 = (byte[][])tuple.clone(); + byte[][] tuple2 = (byte[][])tuple.clone(); + + tuple1[0] = connection.encodeString("serial"); + tuple2[0] = connection.encodeString("serial4"); + + v.addElement(tuple1); + v.addElement(tuple2); + } + else if ( typname.equals("int8") ) { + byte[][] tuple1 = (byte[][])tuple.clone(); + byte[][] tuple2 = (byte[][])tuple.clone(); + + tuple1[0] = connection.encodeString("bigserial"); + tuple2[0] = connection.encodeString("serial8"); + + v.addElement(tuple1); + v.addElement(tuple2); + } + } rs.close(); diff -crN pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java pgjdbc.ai/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java *** pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java 2004-09-21 02:58:59.000000000 +0200 --- pgjdbc.ai/org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.java 2004-09-23 00:08:25.000000000 +0200 *************** *** 48,54 **** */ public boolean isAutoIncrement(int column) throws SQLException { ! return false; } /* --- 48,55 ---- */ public boolean isAutoIncrement(int column) throws SQLException { ! Field field = getField(column); ! return field.getAutoIncrement(connection); } /* --=-xDf9jmy1XfMxHwshFfWx--