postgresql-jdbc.git / summary / log / commit / refs

commit    b25412af297082590466a3ae47bf8a8da8aabcbe
Author:   ivlo11 <ivlo11@gmail.com>
Date:     Thu Oct 31 06:28:31 2013 +0000

    add support to handle double quote nesting
    
    Changed for cases when there is an array of composite types, for example, needing to be tokenized:
    (2,"honda     ","April 1, 2008"),(3,mitsubishi,"August 12, 1998"),(4,"nissan    ","5"" tall, and 3"" wide") 
    
    Originally this: 
    PGtokenizer token = new PGtokenizer(PGtokenizer.removePara("(2,\"honda     \",\"April 1, 2008\")"),','); 
    
    would be split into 4 tokens with the date split at the comma. Now, if using PGtokenizer on each element the quotes will be taken into account as stated in http://www.postgresql.org/docs/9.3/static/arrays.html: 
    
    "The array output routine will put double quotes around element values if they are empty strings, contain curly braces, delimiter characters, double quotes, backslashes, or white space, or match the word NULL. Double quotes and backslashes embedded in element values will be backslash-escaped. "


org/postgresql/util/PGtokenizer.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/org/postgresql/util/PGtokenizer.java b/org/postgresql/util/PGtokenizer.java index 284c37ae..23861351 100644 --- a/org/postgresql/util/PGtokenizer.java +++ b/org/postgresql/util/PGtokenizer.java @@ -61,18 +61,33 @@ public class PGtokenizer // // Peter 1998 Jan 6 - Added < and > to the nesting rules int nest = 0, p, s; + boolean skipChar = false; + boolean nestedDoubleQuote = false; for (p = 0, s = 0;p < string.length();p++) { char c = string.charAt(p); // increase nesting if an open character is found - if (c == '(' || c == '[' || c == '<') + if (c == '(' || c == '[' || c == '<' || (!nestedDoubleQuote && !skipChar && c == '"')) { nest++; + if (c == '"') { + nestedDoubleQuote = true; + skipChar = true; + } + } // decrease nesting if a close character is found - if (c == ')' || c == ']' || c == '>') + if (c == ')' || c == ']' || c == '>' || (nestedDoubleQuote && !skipChar && c == '"')) { nest--; + if (c == '"') + nestedDoubleQuote = false; + } + + if (c == '\\') + skipChar = true; + else + skipChar = false; if (nest == 0 && c == delim) { [parent: 719d0dafb18c]