Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TLdvc-0008Kb-Dg for pgsql-jdbc@postgresql.org; Tue, 09 Oct 2012 17:49:12 +0000 Received: from nm4-vm0.bullet.mail.ac4.yahoo.com ([98.139.53.206]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TLdvY-0002f7-TZ for pgsql-jdbc@postgresql.org; Tue, 09 Oct 2012 17:49:12 +0000 Received: from [98.139.52.193] by nm4.bullet.mail.ac4.yahoo.com with NNFMP; 09 Oct 2012 17:49:07 -0000 Received: from [76.13.13.40] by tm6.bullet.mail.ac4.yahoo.com with NNFMP; 09 Oct 2012 17:49:07 -0000 Received: from [127.0.0.1] by smtp101.prem.mail.ac4.yahoo.com with NNFMP; 09 Oct 2012 17:49:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1349804947; bh=qEJ+lGU5plzZo7TOSgMkg5NOwkRxWvrcY301nOljTdo=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=ksoXdfIqFvpjdD7wwxHF2fsD7XGEcj6/YuFj97OmXD9vDG7G3T14EGMZYFEdcW8bfe1oy4ejwFRYSTP4m8ajELUQB1iYeDFQ71RQaMqsAHqigZdcwIYU5eeU4JsT5aGoyGwo00sEKxBMl8jrs4dsQ1KWh1hNUAvW4vDSgw5UWZs= X-Yahoo-Newman-Id: 134569.88820.bm@smtp101.prem.mail.ac4.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: qFPenOMVM1lAY23k558MLF0hPAuFViZMKsGHy.GrtlYBUWQ BCJuqv51HdFdHV7_wVws1RaHhNHX5Q2rvfNNfSA2Z83gxoXBAGecX_uyOS2j hOEPqYBwRMg.do5kTX1nkpGWx0_LH5zVWGLmpwFSQfra8JkCNKv6CwLX4EiZ 6mHPIjND4ejYHszD8GBZ4SHw7JewyPYaLn742olGdIY__Pj6Js3956MfCsrZ 2G0dwgSD4F.1H6SeIZ0YarXFqOFLOtF0_4v8tUnDPWQu8HmD9X4KMGZceic4 qfDKR1UOQLQRv1kyhtnGmXKH7f2BlPIokq4Ed5eGnrGjO3lHyZeVENu2yAxW rkDbaU2SHYsc18k7jXlyYzYIxsW81F0mQ43Vr2hw3dIdJwscNE94NDCl3N8E tUuY2GWCBMxKlCprvjeG3sbUVaoaUFW6B6PwJe6oU4edvRB46s9hJbKn91kZ HMN5V6HPCxDkCMl8PqBGOzbX7znYnEKBB_CrwtcQAxXgTeb5CjaTryr6WPli hH3oIhSdWxhGx_ER2fI7xnWc2wnLnrNxg7isnbV4X7h09eLPxYz5z_fh5DuR e X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp101.prem.mail.ac4.yahoo.com with SMTP; 09 Oct 2012 10:49:06 -0700 PDT From: "David Johnston" To: , References: <31630_1348140231_505AFCC7_31630_19463_1_E4DFA2E3210FA443B032684B39D16BB4092FB05DA1@THSNCOA06MXS02P.ONE-06.GRP> <00f501cd9bf7$57195dc0$054c1940$@yahoo.com> <5077_1348672047_50631A2F_5077_14708_1_E4DFA2E3210FA443B032684B39D16BB4092FB673D5@THSNCOA06MXS02P.ONE-06.GRP> <9945522a-bc49-44e4-906b-cfdacc801b41@googlegroups.com> In-Reply-To: <9945522a-bc49-44e4-906b-cfdacc801b41@googlegroups.com> Subject: Re: Bug in AbstracJdbc2Statement.replaceProcessing when using dollar quoting? Date: Tue, 9 Oct 2012 13:48:26 -0400 Message-ID: <019b01cda646$492d00a0$db8701e0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQI+ZV0iQ92YvBLV3Nq2AMHb27DsQQMFYdbSAmuKORMBYzWnRwEg5mnYlpAUfwA= Content-Language: en-us X-Pg-Spam-Score: -4.1 (----) X-Archive-Number: 201210/34 X-Sequence-Number: 23358 Hi, See below for my thoughts on how this is limited and inefficient. > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc- > owner@postgresql.org] On Behalf Of marc.mg75@googlemail.com > Sent: Tuesday, October 09, 2012 7:15 AM > To: pgsql-jdbc@postgresql.org > Subject: Re: [JDBC] Bug in AbstracJdbc2Statement.replaceProcessing when > using dollar quoting? > > Am Donnerstag, 27. September 2012 15:02:28 UTC+2 schrieb Florent > Guillaume: > > > Oracle is well known for its V$SESSION and other similar tables. > > Hi, > even if Oracle uses V$SESSIONs, the $ here is not at the beginning, and it also > does not fit the sql standard. > > I made a small patch doing what was said above. While parsing the sql code it > checks if there is a $ while in the state IN_SQLCODE, if so it stops the parsing > and returns the original sql. > > I ran the testsuite and it passed with no error. I also checked my example and > the result returned is what would be expected. > > > --- /org/postgresql/jdbc2/AbstractJdbc2Statement.java_orig Tue Oct 09 > 10:25:10 2012 > +++ /org/postgresql/jdbc2/AbstractJdbc2Statement.java Tue Oct 09 > 10:31:50 2012 > @@ -895,6 +895,7 @@ > int len = p_sql.length(); > StringBuffer newsql = new StringBuffer(len); > int i=0; > + try { > while (i > i=parseSql(p_sql,i,newsql,false,connection.getStandardConformingStrings()) > ; > // We need to loop here in case we encounter invalid @@ -907,6 > +908,11 @@ > i++; > } > } > + } catch (final PGDollarQuoteParsingException e) { > + // found dollar quoting in the sql string. do > not parse for > + // escape clauses and return the original sql. > + return p_sql; > + } > return newsql.toString(); > } > else > @@ -929,7 +935,7 @@ > * @return the position we stopped processing at > */ > protected static int parseSql(String p_sql,int i,StringBuffer newsql, boolean > stopOnComma, > - boolean stdStrings)throws SQLException{ > + boolean stdStrings)throws > + SQLException, PGDollarQuoteParsingException { > short state = IN_SQLCODE; > int len = p_sql.length(); > int nestedParenthesis=0; > @@ -955,6 +961,10 @@ > endOfNested=true; > break; > } > + } else if (c == '$') { // start of a dollar quoted string > + // dollar quoted strings are postgreSql only, > throw an > + // exception to stop parsing for db > indepentend syntax. > + throw new > PGDollarQuoteParsingException(); > } else if (stopOnComma && c==',' && nestedParenthesis==0) { > endOfNested=true; > break; > @@ -1066,7 +1076,7 @@ > * @param stdStrings whether standard_conforming_strings is on > * @return the right postgreSql sql > */ > - protected static String escapeFunction(String functionName, String args, > boolean stdStrings) throws SQLException{ > + protected static String escapeFunction(String functionName, String > + args, boolean stdStrings) throws SQLException, > + PGDollarQuoteParsingException{ > // parse function arguments > int len = args.length(); > int i=0; > > > > > And i added an exception for this: > > > package org.postgresql.core; > > public class PGDollarQuoteParsingException extends Exception { > > } > > > Using this exception i handle to stop the parsing. It could also be used to print > a warning or whatever. > This patch appears to throw the exception when an unquoted identifier includes a dollar-sign. I cannot speak to the standard but it would seem that presence of the dollar-sign should only be evaluated if we are "IN_SQLCODE" and the preceding character is NOT (alphanumeric or underscore) {specifically whatever characters are allowed to begin an unquoted identifier}. Also, with the logic provided the addition of an exception and a try/catch block appears to add unnecessary overhead. At the point the exception is thrown I would suggest that we instead: "return p_sql;" and let the replaceProcessing method remain ignorant of whether the SQL it is getting back is the original SQL or a modified version. The fact is the presence of dollar-quoting with enabled escaping is going to be the common situation. Combine that with the fact we are not prompting the user to "fix" anything means that using the exception mechanism a poor choice - it is not Exceptional but rather ordinary. Everything that needs to be checked and confirmed can be done in the parseSQL method and since it has access to the original statement it can return the original when necessary. David J.