Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 7E57E9FB1E4 for ; Mon, 23 Jul 2007 04:38:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 58378-03 for ; Mon, 23 Jul 2007 04:38:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from gw02.mail.saunalahti.fi (gw02.mail.saunalahti.fi [195.197.172.116]) by postgresql.org (Postfix) with ESMTP id AB7C99FB20D for ; Mon, 23 Jul 2007 04:38:28 -0300 (ADT) Received: from [10.0.0.4] (a88-113-10-143.elisa-laajakaista.fi [88.113.10.143]) (using SSLv3 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by gw02.mail.saunalahti.fi (Postfix) with ESMTP id D485F139C08 for ; Mon, 23 Jul 2007 10:38:25 +0300 (EEST) Subject: Fix database metadata tuple copying to work with binary data From: Mikko Tiihonen To: pgsql-jdbc Content-Type: text/plain Date: Mon, 23 Jul 2007 10:38:25 +0300 Message-Id: <1185176305.1632.97.camel@dual.local> Mime-Version: 1.0 X-Mailer: Evolution 2.10.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/105 X-Sequence-Number: 17279 Hi, The following patch makes DatabaseMetaData work if result set can contain tuples in binary format. An alternative to copying the OID + format flag is to just always do getString() and re-encode it. But it can be now changed easily in one place at the copyValue method. The patch just fixes the only thing unit tests revealed when running with binary transfer patches. Most probably the copyValue should be used in lots of other places too. If this patch is acceptable I can comb through the remaining copies and convert them. Index: org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v retrieving revision 1.36 diff -u -r1.36 AbstractJdbc2DatabaseMetaData.java --- org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 15 Jul 2007 15:33:33 -0000 1.36 +++ org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java 23 Jul 2007 07:25:36 -0000 @@ -2321,9 +2321,9 @@ int typeMod = rs.getInt("atttypmod"); tuple[0] = null; // Catalog name, not supported - tuple[1] = rs.getBytes("nspname"); // Schema - tuple[2] = rs.getBytes("relname"); // Table name - tuple[3] = rs.getBytes("attname"); // Column name + copyValue(rs, "nspname", 1, f, tuple); // Schema + copyValue(rs, "relname", 2, f, tuple); // Table name + copyValue(rs, "attname", 3, f, tuple); // Column name tuple[4] = connection.encodeString(Integer.toString(connection.getSQLType(typeOid))); String pgType = connection.getPGType(typeOid); tuple[5] = connection.encodeString(pgType); // Type name @@ -2364,12 +2364,12 @@ } tuple[10] = connection.encodeString(Integer.toString(rs.getBoolean("attnotnull") ? java.sql.DatabaseMetaData.columnNoNulls : java.sql.DatabaseMetaData.columnNullable)); // Nullable - tuple[11] = rs.getBytes("description"); // Description (if any) - tuple[12] = rs.getBytes("adsrc"); // Column default + copyValue(rs, "description", 11, f, tuple); // Description (if any) + copyValue(rs, "adsrc", 12, f, tuple); // Column default tuple[13] = null; // sql data type (unused) tuple[14] = null; // sql datetime sub (unused) tuple[15] = tuple[6]; // char octet length - tuple[16] = rs.getBytes("attnum"); // ordinal position + copyValue(rs, "attnum", 16, f, tuple); // ordinal position tuple[17] = connection.encodeString(rs.getBoolean("attnotnull") ? "NO" : "YES"); // Is nullable v.addElement(tuple); @@ -4024,4 +4024,29 @@ return ((AbstractJdbc2Connection)connection).createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY); } + /** + * Copies one value from real result set to target result set under + * construction. + * + * @param rs The source. + * @param columnName The name of the column to copy. + * @param pos The position in target result set. + * @param f The fields of the target result set. + * @param tuple The values of the target result set. + * @throws SQLException If an error occurs. + */ + protected void copyValue(ResultSet rs, String columnName, int pos, + Field[] f, byte[][] tuple) throws SQLException { + int col = rs.findColumn(columnName) - 1; + AbstractJdbc2ResultSet res = (AbstractJdbc2ResultSet) rs; + Field fromField = res.fields[col]; + if (fromField.getFormat() != Field.TEXT_FORMAT) { + // The OID must match for binary types + if (fromField.getOID() != f[pos].getOID()) { + f[pos] = new Field(f[pos].getColumnLabel(), fromField.getOID()); + } + f[pos].setFormat(fromField.getFormat()); + } + tuple[pos] = res.this_row[col]; + } }