Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 3DD86633667 for ; Fri, 23 Apr 2010 12:57:28 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 43472-10 for ; Fri, 23 Apr 2010 15:57:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og101.obsmtp.com (exprod7og101.obsmtp.com [64.18.2.155]) by mail.postgresql.org (Postfix) with SMTP id E99DD63306D for ; Fri, 23 Apr 2010 12:57:16 -0300 (ADT) Received: from source ([209.85.219.216]) by exprod7ob101.postini.com ([64.18.6.12]) with SMTP ID DSNKS9HDW+RRxsslyrDYoROL3SMrNxj3C3BL@postini.com; Fri, 23 Apr 2010 08:57:17 PDT Received: by ewy8 with SMTP id 8so2890301ewy.28 for ; Fri, 23 Apr 2010 08:57:14 -0700 (PDT) Received: by 10.213.42.73 with SMTP id r9mr480035ebe.40.1272038234556; Fri, 23 Apr 2010 08:57:14 -0700 (PDT) Received: from [192.168.1.117] (dsl-hkibrasgw2-ff67c300-165.dhcp.inet.fi [88.195.103.165]) by mx.google.com with ESMTPS id 16sm409117ewy.3.2010.04.23.08.57.12 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 23 Apr 2010 08:57:13 -0700 (PDT) Message-ID: <4BD1C357.90506@enterprisedb.com> Date: Fri, 23 Apr 2010 18:57:11 +0300 From: Heikki Linnakangas Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 To: Heikki Hiltunen CC: pgsql-jdbc@postgresql.org Subject: Re: Problem with prepareStatement and Statement.RETURN_GENERATED_KEYS in PostgreSQL JDBC driver 8.4 References: <4BD196B4.3040607@smilehouse.com> In-Reply-To: <4BD196B4.3040607@smilehouse.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.997 tagged_above=-10 required=5 tests=AWL=-0.097, BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/62 X-Sequence-Number: 20473 Heikki Hiltunen wrote: > When using 8.4 JDBC drivers, calling prepareStatement(String sql, int > autoGeneratedKeys) with Statement.RETURN_GENERATED_KEYS seems to add > "RETURNING *" to the end of the SQL even with select statements. > According to Javadoc for prepareStatement(String sql, int > autoGeneratedKeys) in java.sql.Connection: > > "The given constant tells the driver whether it should make > auto-generated keys available for retrieval. This parameter is ignored > if the SQL statement is not an INSERT statement, or an SQL statement > able to return auto-generated keys (the list of such statements is > vendor-specific). " Yeah, the driver just blindly tacks a " RETURNING *" to the end of the SQL string if you specify RETURN_GENERATED_KEYS. I'm tempted to do something like this: *** AbstractJdbc3Connection.java 23 Dec 2009 10:28:40 +0200 1.21 --- AbstractJdbc3Connection.java 23 Apr 2010 18:49:44 +0300 *************** *** 359,364 **** --- 359,381 ---- throws SQLException { checkClosed(); + + /* + * We implement fetching auto-generated keys by tacking a + * " RETURNING *" to the end of the string. Don't try to do that + * with other statements than INSERT/UPDATE/DELETE. + * + * XXX this gets fooled by comments at the beginning of the SQL string + */ + if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) + { + String trimmedSql = sql.trim(); + if (!trimmedSql.regionMatches(true, 0, "INSERT", 0, 6) && + !trimmedSql.regionMatches(true, 0, "UPDATE", 0, 6) && + !trimmedSql.regionMatches(true, 0, "DELETE", 0, 6)) + autoGeneratedKeys = Statement.NO_GENERATED_KEYS; + } + if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) sql = AbstractJdbc3Statement.addReturning(this, sql, new String[]{"*"}, false); But that's not very bullet-proof, and will fail to detect the statement as an INSERT if it e.g begins with a comment. We could add a mini-parser to detect comments too, but it's not a very robust approach. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com