X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2A7D1DBC50 for ; Thu, 24 Nov 2005 09:25:31 -0400 (AST) 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 11255-07 for ; Thu, 24 Nov 2005 13:25:33 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id C0A39DBBD9 for ; Thu, 24 Nov 2005 09:25:27 -0400 (AST) Received: (qmail 2288 invoked from network); 24 Nov 2005 13:25:30 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.2?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 24 Nov 2005 13:25:30 -0000 In-Reply-To: <438548DB.8070407@opencloud.com> References: <200511211718.26679.xinxincute@gmail.com> <1132616246.4154.10.camel@archimedes> <6F909ADC-23ED-40CF-A2B5-41DE61606A26@fastcrypt.com> <438548DB.8070407@opencloud.com> Mime-Version: 1.0 (Apple Message framework v746.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <73985A37-FA66-4E31-95C1-045F5AE31726@fastcrypt.com> Cc: Mark Lewis , Kris Jurka , Tjioe Ai Xin , pgsql-jdbc@postgresql.org Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Can PostgreSQL do data type automated casting in Date: Thu, 24 Nov 2005 08:25:22 -0500 To: Oliver Jowett X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.009 required=5 tests=[AWL=0.009] X-Spam-Score: 0.009 X-Spam-Level: X-Archive-Number: 200511/190 X-Sequence-Number: 14288 Interesting. Looking at the test case is this a realistic situation ? Would anyone really want to change the types of a parameter of a statement ? Dave On 24-Nov-05, at 12:00 AM, Oliver Jowett wrote: > Dave Cramer wrote: >> The easier way to deal with this has already been discussed. >> Simply bind String to the Oid.Unknown type, and let the server >> deal with it. >> How about we make this a configuration parameter. > > I've implemented this and it seems ok with one exception. This is > from ServerPreparedStmtTest: > > public void testTypeChange() throws Exception { > PreparedStatement pstmt = con.prepareStatement("SELECT ?"); > ((PGStatement)pstmt).setUseServerPrepare(true); > > // Prepare with int parameter. > pstmt.setInt(1, 1); > ResultSet rs = pstmt.executeQuery(); > assertTrue(rs.next()); > assertEquals(1, rs.getInt(1)); > assertTrue(!rs.next()); > > // Change to text parameter, check it still works. > pstmt.setString(1, "test string"); > rs = pstmt.executeQuery(); > assertTrue(rs.next()); > assertEquals("test string", rs.getString(1)); > assertTrue(!rs.next()); > } > > With String bound to OID 0 this fails with: > > ERROR: invalid input syntax for integer: "test string" > > What is happening is that the use of setInt() prepares a server- > side statement with the parameter typed as integer. When setString > () is later used, it feeds oid 0 down into the query executor which > decides to reuse the existing statement since it is "compatible > enough" as the parameter which was previously integer now has an > unspecified type. Then the server tries to parse the string as an > integer and breaks. > > IIRC this was originally done to avoid re-preparing the statement > when setNull(x,Types.OTHER) was done (or equivalently, setObject > (x,null), which just calls setNull) -- which can pass oid 0 to an > existing query with resolved types. > > I guess that we should tighten the checks in the query executor so > that it will only consider types "compatible enough" if the new > value is both of unspecified type *and* null? > > (I am also renaming Oid.INVALID to Oid.UNSPECIFIED in the driver to > reduce confusion -- InvalidOid in the backend is indeed 0 but > "invalid" is not the protocol-level meaning for oid 0 here, and > "unknown" is an actual pseudotype that has a non-zero oid). > > -O > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org >