[hg] main-silver: #225079: SQLIdentifiers.Quoter does not correc...

  • From: Jaroslav Havlin < >
  • To:
  • Subject: [hg] main-silver: #225079: SQLIdentifiers.Quoter does not correc...
  • Date: Fri, 08 Feb 2013 20:37:15 -0800

changeset 2faf2562a671 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/2faf2562a671
description:
        #225079: SQLIdentifiers.Quoter does not correctly quote identifiers 
containing the quote character itself

diffstat:

 db.sql.editor/src/org/netbeans/modules/db/sql/lexer/SQLLexer.java            
   |   8 +---
 
db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/lexer/SQLLexerTest.java
 |  15 +++++++-
 db/src/org/netbeans/api/db/sql/support/SQLIdentifiers.java                   
   |  10 ++++-
 db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java             
   |  19 ++++++++++
 4 files changed, 44 insertions(+), 8 deletions(-)

diffs (129 lines):

diff --git 
a/db.sql.editor/src/org/netbeans/modules/db/sql/lexer/SQLLexer.java 
b/db.sql.editor/src/org/netbeans/modules/db/sql/lexer/SQLLexer.java
--- a/db.sql.editor/src/org/netbeans/modules/db/sql/lexer/SQLLexer.java
+++ b/db.sql.editor/src/org/netbeans/modules/db/sql/lexer/SQLLexer.java
@@ -197,11 +197,9 @@
                         if (!isEndIdentifierQuoteChar(startQuoteChar, 
actChar)) {
                             break;
                         } else {
-                            if (actChar == '"') {
                                 state = State.ISA_QUOTE_IN_IDENTIFIER;
                                 break;
                         }
-                        }
                     } else {
                         if (Character.isLetterOrDigit(actChar) || actChar == 
'_' || actChar == '#') {
                             break;
@@ -214,11 +212,9 @@
                     return 
factory.createToken(testKeyword(input.readText()));
 
                 case ISA_QUOTE_IN_IDENTIFIER:
-                    switch (actChar) {
-                        case '"':
+                    if (isEndIdentifierQuoteChar(startQuoteChar, actChar)) {
                             state = State.ISI_IDENTIFIER;
-                            break;
-                        default:
+                    } else {
                             state = State.INIT;
                             startQuoteChar = -1;
                             input.backup(1);
diff --git 
a/db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/lexer/SQLLexerTest.java
 
b/db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/lexer/SQLLexerTest.java
--- 
a/db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/lexer/SQLLexerTest.java
+++ 
b/db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/lexer/SQLLexerTest.java
@@ -80,6 +80,17 @@
                 SQLTokenId.WHITESPACE);
     }
 
+    /**
+     * Test escape in quoted identifiers following SQL99 methology
+     */
+    public void testQuotedIdentifiers2() throws Exception {
+        TokenSequence<SQLTokenId> seq = 
getTokenSequence("\"derby\"\"\",`mysql```,[mssql]]]");
+        assertTokens(seq, SQLTokenId.IDENTIFIER,
+                SQLTokenId.COMMA, SQLTokenId.IDENTIFIER,
+                SQLTokenId.COMMA, SQLTokenId.IDENTIFIER,
+                SQLTokenId.WHITESPACE);
+    }
+
     public void testSimpleSQL99Quoting() throws Exception {
         TokenSequence<SQLTokenId> seq = getTokenSequence("select -/ from 
'a''' + 1, dto");
         assertTokens(seq, SQLTokenId.KEYWORD, SQLTokenId.WHITESPACE, 
SQLTokenId.OPERATOR,
@@ -199,11 +210,13 @@
     }
 
     private static TokenSequence<SQLTokenId> getTokenSequence(String sql) 
throws BadLocationException {
-        Document doc = new ModificationTextDocument();
+        ModificationTextDocument doc = new ModificationTextDocument();
         doc.insertString(0, sql, null);
         doc.putProperty(Language.class, SQLTokenId.language());
         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
+        doc.readLock();
         TokenSequence<SQLTokenId> seq = 
hi.tokenSequence(SQLTokenId.language());
+        doc.readUnlock();
         seq.moveStart();
         return seq;
     }
diff --git a/db/src/org/netbeans/api/db/sql/support/SQLIdentifiers.java 
b/db/src/org/netbeans/api/db/sql/support/SQLIdentifiers.java
--- a/db/src/org/netbeans/api/db/sql/support/SQLIdentifiers.java
+++ b/db/src/org/netbeans/api/db/sql/support/SQLIdentifiers.java
@@ -124,16 +124,22 @@
         public String unquote(String identifier) {
             Parameters.notNull("identifier", identifier);
 
+            boolean startQuoted = false;
+
             String result = identifier;
 
             if (result.startsWith(quoteString)) {
                 result = result.substring(quoteString.length());
+                startQuoted = true;
             }
 
             if (result.endsWith(quoteString)) {
                 result = result.substring(0, 
result.lastIndexOf(quoteString));
             }
 
+            if(startQuoted) {
+                result = result.replace(quoteString + quoteString, 
quoteString);
+            }
             return result;
         }
 
@@ -147,7 +153,9 @@
         }
 
         String doQuote(String identifier) {
-            return quoteString + identifier + quoteString;
+            return quoteString
+                    + identifier.replace(quoteString, quoteString + 
quoteString)
+                    + quoteString;
         }
     }
 
diff --git a/db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java 
b/db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java
--- a/db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java
+++ b/db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java
@@ -169,4 +169,23 @@
         assertEquals("id", quoter.unquote("id" + quoteString));
         assertEquals("id", quoter.unquote(quoteString + "id" + quoteString));
     }
+
+    // Check SQL99-Quoting
+    public void testQuoteIdentifierContainingQuotingChar() {
+        String quoteString = quoter.getQuoteString();
+
+        String unquoted = "test" + quoteString + "xx";
+        String quoted = quoteString + "test" + quoteString + quoteString + 
"xx"
+                + quoteString;
+
+        assertEquals(quoted, quoter.quoteAlways(unquoted));
+        assertEquals(unquoted, quoter.unquote(quoted));
+
+        String unquoted2 = "test" + quoteString + "xx" + quoteString + 
quoteString;
+        String quoted2 = quoteString + "test" + quoteString + quoteString + 
"xx"
+                + quoteString + quoteString + quoteString + quoteString + 
quoteString;
+
+        assertEquals(quoted2, quoter.quoteAlways(unquoted2));
+        assertEquals(unquoted2, quoter.unquote(quoted2));
 }
+}

[hg] main-silver: #225079: SQLIdentifiers.Quoter does not correc...

Jaroslav Havlin 02/09/2013

Project Features

About this Project

DB was started in November 2009, is owned by Antonin Nebuzelsky, and has 113 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close