chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1390522 - in /chemistry/opencmis/trunk/chemistry-opencmis-client: chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/ chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runti...
Date Wed, 26 Sep 2012 14:33:08 GMT
Author: jens
Date: Wed Sep 26 14:33:07 2012
New Revision: 1390522

URL: http://svn.apache.org/viewvc?rev=1390522&view=rev
Log:
Client API: Fix a bug when escaping CONTAINS statements 

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementTest.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java?rev=1390522&r1=1390521&r2=1390522&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
Wed Sep 26 14:33:07 2012
@@ -94,8 +94,28 @@ public interface QueryStatement extends 
     void setStringLike(int parameterIndex, String str);
 
     /**
-     * Sets the designated parameter to the given string. It does not escape
-     * backslashes ('\') in front of '*',  '?' and '-'.
+     * Sets the designated parameter to the given string in a CMIS contains statement.
+     * Note that the CMIS specification requires two levels of escaping. The first level
+     * escapes ', ", \ characters to \', \" and \\. The characters *, ? and - are
+     * interpreted as text search operators and are not escaped on first level. If *, ?,
-  
+     * shall be used as literals, they must be passed escaped with \*, \? and \- to 
+     * this method.
+     * 
+     * For all statements in a CONTAINS() clause it is required to isolate those
+     * from a query statement. Therefore a second level escaping is performed. On
+     * the second level grammar ", ', - and \ are escaped with a \. See the spec for
+     * further details.
+     *  
+     *  Summary (input --> first level escaping --> second level escaping and output):
+     *      * --> * --> *
+     *      ? --> ? --> ?
+     *      - --> - --> -
+     *      \ --> \\ --> \\\\  (for any other character following other than * ? -)
+     *      \* --> \* --> \\*
+     *      \? --> \? --> \\?
+     *      \- --> \- --> \\-
+     *      ' --> \' --> \\\'
+     *      " --> \" --> \\\"
      */
     void setStringContains(int parameterIndex, String str);
     

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java?rev=1390522&r1=1390521&r2=1390522&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
Wed Sep 26 14:33:07 2012
@@ -433,14 +433,10 @@ public class QueryStatementImpl implemen
         for (int i = 0; i < str.length(); i++) {
             char c = str.charAt(i);
 
-            if (c == '\'') {
-                sb.append("\\\\");
-            } else if (c == '\\') {
-                if (i + 1 < str.length() && (str.charAt(i + 1) == '*' || str.charAt(i
+ 1) == '?' || str.charAt(i + 1) == '-')) {
-                    // no additional back slash
-                } else {
-                    sb.append("\\\\");
-                }
+            if (c == '\\') {
+                sb.append("\\");
+            } else if (c == '\'' || c == '\"') {
+                sb.append("\\\\\\");
             }
 
             sb.append(c);

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementTest.java?rev=1390522&r1=1390521&r2=1390522&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementTest.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementTest.java
Wed Sep 26 14:33:07 2012
@@ -80,12 +80,42 @@ public class QueryStatementTest {
                 st.toQueryString());
 
         // contains
+
+        // *, ? and - are treated as text search operators: 1st level escaping: none, 2nd
level escaping: none
+        // \*, \? and \- are used as literals, 1st level escaping: none, 2nd level escaping:
\\*, \\?, \\-
+        // ' and " are used as literals, 1st level escaping: \', \", 2nd level escaping:
 \\\', \\\",
+        // \ plus any other character, 1st level escaping \\ plus character, 2nd level: \\\\
plus character
+
         query = "SELECT * FROM cmis:document WHERE CONTAINS(?)";
         st = new QueryStatementImpl(session, query);
-        st.setStringContains(1, "John'sPresentation\\-Version2");
-        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('John\\\\'sPresentation\\-Version2')",
+        st.setStringContains(1, "John's");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('John\\\\\\'s')",
                 st.toQueryString());
-
+        st.setStringContains(1, "foo -bar");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo -bar')",
+                st.toQueryString());
+        st.setStringContains(1, "foo*");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo*')",
+                st.toQueryString());
+        st.setStringContains(1, "foo?");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo?')",
+                st.toQueryString());
+        st.setStringContains(1, "foo\\-bar");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo\\\\-bar')",
+                st.toQueryString());
+        st.setStringContains(1, "foo\\*");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo\\\\*')",
+                st.toQueryString());
+        st.setStringContains(1, "foo\\?");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('foo\\\\?')",
+                st.toQueryString());
+        st.setStringContains(1, "\"Cool\"");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('\\\\\\\"Cool\\\\\\\"')",
+                st.toQueryString());
+        st.setStringContains(1, "c:\\MyDcuments");
+        assertEquals("SELECT * FROM cmis:document WHERE CONTAINS('c:\\\\MyDcuments')",
+                st.toQueryString());
+       
         // ids
         query = "SELECT * FROM cmis:document WHERE abc:id = ?";
         st = new QueryStatementImpl(session, query);
@@ -140,4 +170,24 @@ public class QueryStatementTest {
         assertEquals("SELECT * FROM cmis:document WHERE abc:dateTime "
                 + "IN (TIMESTAMP '2012-02-02T03:04:05.000Z',TIMESTAMP '2012-02-02T03:04:05.000Z')",
st.toQueryString());
     }
+    
+//    @Test
+    public void testQueryApiEscaping() {
+        // contains
+        Session session = new SessionImpl(new HashMap<String, String>(), null, null,
null);
+
+        String query = "SELECT * FROM cmis:document WHERE CONTAINS(?)";
+        String ss = "a\\xc";
+        assertEquals(4, ss.length());
+        System.out.println(ss);
+        QueryStatement st = new QueryStatementImpl(session, query);
+        st.setStringContains(1, "John's");
+        System.out.println("setStringContains: "+ st.toQueryString());
+        String expected = "SELECT * FROM cmis:document WHERE CONTAINS('John\\\'s')";
+        System.out.println("Expected: " + expected);
+        
+        assertEquals(expected, st.toQueryString());
+
+    }
+
 }



Mime
View raw message