db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1039268 - in /db/derby/code/trunk/java: testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java
Date Fri, 26 Nov 2010 08:31:14 GMT
Author: kahatlen
Date: Fri Nov 26 08:31:14 2010
New Revision: 1039268

URL: http://svn.apache.org/viewvc?rev=1039268&view=rev
Log:
DERBY-4903: Plan exporter tool produces broken output if query contains less-than operator

Added handling of '&' and expanded test case to test more special characters.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java?rev=1039268&r1=1039267&r2=1039268&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XplainStatisticsTest.java
Fri Nov 26 08:31:14 2010
@@ -2555,15 +2555,23 @@ public class XplainStatisticsTest extend
     }
 
     /**
-     * Test that queries that contain table names with quotation marks are not garbled
-     * by the plan exporter tool. Regression test case for DERBY-4903.
+     * Test that queries that contain characters with a special meaning in
+     * XML are not garbled by the plan exporter tool. Regression test case
+     * for DERBY-4903.
      */
     public void testPlanExporterHandlingSpecialCharacters() throws Exception{
-        String table = "\"A \"\"quoted\"\" table name\"";
-        String queryText = "SELECT * FROM " + table;
+        String table =
+                "A \"double\" and 'single' quoted table name " +
+                "with some other special characters, like <, > and &";
+
+        String escapedTable = JDBC.escape(table);
+
+        String queryText =
+                "SELECT * FROM " + escapedTable +
+                " WHERE X < LENGTH('a & b') AND X > 1";
 
         Statement s = createStatement();
-        s.execute("CREATE TABLE " + table + "(X INT)");
+        s.execute("CREATE TABLE " + escapedTable + "(X INT)");
 
         enableXplainStyle(s);
         JDBC.assertEmpty(s.executeQuery(queryText));
@@ -2588,8 +2596,7 @@ public class XplainStatisticsTest extend
             //for TABLESCAN node, test scanned_object entry
             //is exist and verify its value correctly replaced
             //by special XML symbols.
-            Assert.assertEquals("A \"quoted\" table name",
-                    getNodeAttribute(stmtId,"scanned_object",0));
+            assertEquals(table, getNodeAttribute(stmtId, "scanned_object", 0));
         }
 
     }

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java?rev=1039268&r1=1039267&r2=1039268&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java
(original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/planexporter/AccessDatabase.java
Fri Nov 26 08:31:14 2010
@@ -380,10 +380,7 @@ public class AccessDatabase {
 
                 /*Removing possible occurrences of special XML characters
                  * from XML node attributes in XML representation.*/
-                text = replaceInAttribute(text, '<',"&lt;");
-                text = replaceInAttribute(text, '>',"&gt;");
-                text = replaceInAttribute(text, '\'',"&apos;");
-                text = replaceInAttribute(text, '"',"&quot;");
+                text = escapeInAttribute(text);
 
                 switch(x){
                 case ID:
@@ -479,29 +476,44 @@ public class AccessDatabase {
 
         /*Removing possible occurrences of special XML characters
          * from a query statement with XML representation.*/
-        statement = replace(statement, '<',"&lt;");
-        statement = replace(statement, '>',"&gt;");
-        statement = replace(statement, '\'',"&apos;");
-        statement = replace(statement, '"',"&quot;");
+        statement = escapeForXML(statement);
 
         return "<statement>"+statement+"</statement>\n";
     }
 
     /**
+     * Escape characters that have a special meaning in XML.
      *
-     * @param text text to be checked
-     * @param expr string to be removed
-     * @param replace string to be added
-     * @return modified string
+     * @param text the text to escape
+     * @return the text with special characters escaped
      */
-    private String replace(String text, char expr, String replace){
-         int idx = text.indexOf(expr);
-    	 while (idx >= 0)
-    	 {
-             text = text.substring(0, idx) + replace + text.substring(idx+1);
-             idx = text.indexOf(expr);
-    	 }
-         return text;
+    private static String escapeForXML(String text) {
+        StringBuffer sb = new StringBuffer();
+
+        for (int i = 0; i < text.length(); i++) {
+            char ch = text.charAt(i);
+            switch (ch) {
+                case '&':
+                    sb.append("&amp;");
+                    break;
+                case '<':
+                    sb.append("&lt;");
+                    break;
+                case '>':
+                    sb.append("&gt;");
+                    break;
+                case '\'':
+                    sb.append("&apos;");
+                    break;
+                case '"':
+                    sb.append("&quot;");
+                    break;
+                default:
+                    sb.append(ch);
+            }
+        }
+
+        return sb.toString();
     }
 
     /**
@@ -511,14 +523,13 @@ public class AccessDatabase {
      * scanned_object="A &quot;quoted&quot;  table name";
      *
      * @param text attribute string to be checked
-     * @param expr string to be removed
-     * @param replace string to be added
      * @return modified string
      */
-    private String replaceInAttribute(String text, char expr, String replace){
+    private String escapeInAttribute(String text) {
         if (text.indexOf('"') == -1)
             return text;
-        String correctXMLString = replace(text.substring(text.indexOf('"')+1, text.length()-1),
expr, replace);
+        String correctXMLString = escapeForXML(
+                text.substring(text.indexOf('"') + 1, text.length() - 1));
         return text.substring(0,text.indexOf('"')+1)+correctXMLString+"\"";
     }
    



Mime
View raw message