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 DD94832B30E for ; Sun, 10 Oct 2004 17:05:08 +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 66362-10 for ; Sun, 10 Oct 2004 16:05:04 +0000 (GMT) Received: from ms-dienst.rz.rwth-aachen.de (ms-1.rz.RWTH-Aachen.DE [134.130.3.130]) by svr1.postgresql.org (Postfix) with ESMTP id 90A7C32B2C1 for ; Sun, 10 Oct 2004 17:05:00 +0100 (BST) Received: from r220-1 (r220-1.rz.RWTH-Aachen.DE [134.130.3.31]) by ms-dienst.rz.rwth-aachen.de (iPlanet Messaging Server 5.2 HotFix 1.12 (built Feb 13 2003)) with ESMTP id <0I5D006HDM0AYO@ms-dienst.rz.rwth-aachen.de> for pgsql-jdbc@postgresql.org; Sun, 10 Oct 2004 18:04:58 +0200 (MEST) Received: from relay.rwth-aachen.de ([134.130.3.1]) by r220-1 (MailMonitor for SMTP v1.2.2 ) ; Sun, 10 Oct 2004 18:04:57 +0200 (MEST) Received: from post.halifax.rwth-aachen.de (post.halifax.RWTH-Aachen.DE [134.130.49.251]) by relay.rwth-aachen.de (8.13.0/8.13.0/1) with ESMTP id i9AG4vr3020663 for ; Sun, 10 Oct 2004 18:04:57 +0200 (MEST) Received: (qmail 1644 invoked from network); Sun, 10 Oct 2004 16:05:02 +0000 Received: from darkenemy.halifax.rwth-aachen.de ([134.130.48.36]) (envelope-sender ) by mail.halifax.rwth-aachen.de (qmail-ldap-1.03) with SMTP for ; Sun, 10 Oct 2004 16:05:02 +0000 Date: Sun, 10 Oct 2004 18:04:53 +0200 From: Ulrich Meis Subject: proposal concerning the bug about updating a table named "two words" To: pgsql-jdbc@postgresql.org Message-id: <200410101804.53076.kenobi@halifax.rwth-aachen.de> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_l2k4ZMvJy/QAsESEomCong)" User-Agent: KMail/1.7 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: 200410/79 X-Sequence-Number: 11184 --Boundary_(ID_l2k4ZMvJy/QAsESEomCong) Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Content-disposition: inline Back in may I posted that bug and I keep running into it and merging my fix with new versions, so I thought I'd write a jdk 1.2 compliant fix and I hope you will consider incorporating it. I don't know how thinks like this are handled here, but I attached my implementation of a simple parser class. The parseQuery method in org.postgresql.jdbc2.AbstractResultSet determines two things: tableName and singleTable. I included a little main method that checks queries you enter and outputs singleTable and tableName. Currently, I can't think of any shortcomings of my code within sql standard. Shortcomings of the current implementation are things like select foo from "two word" select foo from bar join tender select foo as "foo from bar" from bar select foo from bar union select ... If you like the code I would be happy to write a patch for AbstractJdbc2ResultSet Kind Regards, Uli --Boundary_(ID_l2k4ZMvJy/QAsESEomCong) Content-type: text/x-java; charset=us-ascii; name=QueryParser.java Content-transfer-encoding: 7BIT Content-disposition: attachment; filename=QueryParser.java package org.postgresql.util; import java.io.IOException; import java.util.ArrayList; /** * Extracts keywords from a query with regard to quotes * */ public class QueryParser { /** * somewhere in between */ public static final int SPACE = 0; /** * within a word */ public static final int WORD = 1; /** * within a word quoted with ' */ public static final int QA_WORD = 2; /** * within a word quoted with " */ public static final int QB_WORD = 3; /** * if one of these items occurs after from, it marks the end of the from clause */ public static final String[] endsfromitem = {"WHERE", "GROUP", "HAVING", "UNION", "INTERSECT", "EXCEPT", "ORDER", "LIMIT", "OFFSET"}; /** * quote character A */ public static final char QA = '\''; /** * quote character B */ public static final char QB = '"'; /** * considered as space */ public static final char D1 = ' '; /** * considered as space */ public static final char D2 = '\n'; /** * considered as space */ public static final char D3 = '\t'; /** * marks end of a word and start of a new one */ public static final char COMMA = ','; /** * saves the position of the from keyword in the last run of parseQuery */ private int frompos; /** * holds the "words" determined by parseQuery */ private String[] words; /** * holds the tableName determined by parseQuery */ private String tableName; /** * saves whether this is a query to a single table */ private boolean nonsingle; /** * Parses the given string and outputs unquoted fractions */ private String[] parseQuery(String input) { // current position int pos = 0; // current state int state = SPACE; // words are added to this list ArrayList output = new ArrayList(); // already read fraction of a word String word = ""; // holds current and next character respectively char cur, next; // go ones through the string while (pos < input.length()) { cur = input.charAt(pos); next = (pos + 1) == input.length() ? ' ' : input.charAt(pos + 1); // decides new state upon current state and current character switch (state) { case SPACE: switch (cur) { case QA: state = QA_WORD; break; case QB: state = QB_WORD; break; case D1: case D2: case D3: break; case COMMA: output.add(","); break; default: state = WORD; break; } ; break; case WORD: switch (cur) { case QA: state = QA_WORD; break; case QB: state = QB_WORD; break; case D1: case D2: case D3: output.add(word); word = ""; state = SPACE; break; case COMMA: output.add(word); output.add(","); word = ""; state = SPACE; break; default: break; } ; break; case QA_WORD: if (cur == QA) { if (next == QA) { word += cur; pos++; } else { state = WORD; } } ; break; case QB_WORD: if (cur == QB) { if (next == QB) { word += cur; pos++; } else { state = WORD; } } ; break; } // if we read a word character, append it if (state != SPACE) word += cur; pos++; } // save last word if there is one if (state == WORD) output.add(word); // convert to array and check for from keyword and nonsingle String[] ret = new String[output.size()]; frompos = -1; // not a single table nonsingle = false; // are we in a from clause? boolean fromitem = false; // did we encounter the from keyword before? boolean hadfrom = false; for (int i = 0; i < output.size(); i++) { ret[i] = (String) output.get(i); // check for join and comma if (fromitem) { if (ret[i].equals(",") || ret[i].equals("join")) nonsingle = true; else for (int k = 0; k < endsfromitem.length; k++) { if (ret[i].equalsIgnoreCase(endsfromitem[k])) { fromitem = false; break; } } } if (ret[i].equals("from")) { frompos = i; // second from means non single if (hadfrom) nonsingle = true; fromitem = true;hadfrom=true; // save tableName in word after from if it's not only String nextword = (String) output.get(i + 1); if (nextword.equalsIgnoreCase("only")) tableName = (String) output.get(i + 2); else tableName = (String) output.get(i + 1); } } return words = ret; } public boolean singleTable() { return !nonsingle; } public String getTableName() { return tableName; } public static void main(String[] args) { byte[] bytes = new byte[100]; while (true) { System.out.print("Your Query Please: "); try { int count = System.in.read(bytes); if (count <= 2) System.exit(0); String input = new String(bytes, 0, count); QueryParser qp = new QueryParser(); String[] words = qp.parseQuery(input); System.out.println("Table : " + qp.getTableName() + " Single: " + qp.singleTable()); for (int i = 0; i < words.length; i++) System.out.println(words[i]); } catch (IOException e) { e.printStackTrace(); } } } } --Boundary_(ID_l2k4ZMvJy/QAsESEomCong)--