cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5713] Escaping % character, patch from Romain Castan applied
Date Mon, 28 Apr 2014 12:43:10 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 0b4fe87bb -> d84cdcca6


[CXF-5713] Escaping % character, patch from Romain Castan applied


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/d84cdcca
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/d84cdcca
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/d84cdcca

Branch: refs/heads/master
Commit: d84cdcca61f74315eeb4ec70a7b51e1e4ac88c5e
Parents: 0b4fe87
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Apr 28 13:42:53 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Apr 28 13:42:53 2014 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/jaxrs/ext/search/SearchUtils.java | 15 ++++++++++++---
 .../ext/search/jpa/AbstractJPATypedQueryVisitor.java |  8 ++++++--
 .../jaxrs/ext/search/sql/SQLPrinterVisitorTest.java  |  6 +++---
 3 files changed, 21 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d84cdcca/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
index 25536ac..472f39b 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
@@ -53,10 +53,15 @@ public final class SearchUtils {
     }
     
     public static String toSqlWildcardString(String value, boolean alwaysWildcard) {
+        if (value.contains("\\")) {
+            value = value.replaceAll("\\\\", "\\\\\\\\"); 
+        }
+        if (value.contains("%")) {
+            value = value.replaceAll("%", "\\\\%");
+        }
         if (!value.contains("*")) {
             return alwaysWildcard ? "%" + value + "%" : value;
         }
-        
         if (value.startsWith("*")) { 
             value = "%" + value.substring(1);
         }
@@ -66,6 +71,10 @@ public final class SearchUtils {
         return value;
     }
     
+    public static boolean containsEscapedChar(String value) {
+        return value.contains("\\%") || value.contains("\\\\");
+    }
+    
     public static void startSqlQuery(StringBuilder sb, 
                                      String table,
                                      String tableAlias,
@@ -94,10 +103,10 @@ public final class SearchUtils {
         String op;
         switch (ct) {
         case EQUALS:
-            op = value.contains("%") ? "LIKE" : "=";
+            op = containsEscapedChar(value) || value.contains("%") ? "LIKE" : "=";
             break;
         case NOT_EQUALS:
-            op = value.contains("%") ? "NOT LIKE" : "<>";
+            op = containsEscapedChar(value) || value.contains("%") ? "NOT LIKE" : "<>";
             break;
         case GREATER_THAN:
             op = ">";

http://git-wip-us.apache.org/repos/asf/cxf/blob/d84cdcca/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
index 819639a..474f97c 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
@@ -198,7 +198,9 @@ public abstract class AbstractJPATypedQueryVisitor<T, T1, E>
         case EQUALS:
             if (clazz.equals(String.class)) {
                 String theValue = SearchUtils.toSqlWildcardString(value.toString(), isWildcardStringMatch());
-                if (theValue.contains("%")) {
+                if (SearchUtils.containsEscapedChar(theValue)) {
+                    pred = builder.like((Expression<String>)exp, theValue, '\\');
+                } else if (theValue.contains("%")) {
                     pred = builder.like((Expression<String>)exp, theValue);
                 } else {
                     pred = builder.equal(exp, clazz.cast(value));
@@ -210,7 +212,9 @@ public abstract class AbstractJPATypedQueryVisitor<T, T1, E>
         case NOT_EQUALS:
             if (clazz.equals(String.class)) {
                 String theValue = SearchUtils.toSqlWildcardString(value.toString(), isWildcardStringMatch());
-                if (theValue.contains("%")) {
+                if (SearchUtils.containsEscapedChar(theValue)) {
+                    pred = builder.notLike((Expression<String>)exp, theValue, '\\');
+                } else if (theValue.contains("%")) {
                     pred = builder.notLike((Expression<String>)exp, theValue);
                 } else {
                     pred = builder.notEqual(exp, clazz.cast(value));

http://git-wip-us.apache.org/repos/asf/cxf/blob/d84cdcca/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
index d59b9dc..6c3db74 100644
--- a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
@@ -37,13 +37,13 @@ public class SQLPrinterVisitorTest extends Assert {
 
     @Test
     public void testSQL1() throws SearchParseException {
-        SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
+        SearchCondition<Condition> filter = parser.parse("name==ami%*;level=gt=10");
         SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
         filter.accept(visitor.visitor());
         String sql = visitor.getQuery();
         
-        assertTrue("SELECT * FROM table WHERE (name LIKE 'ami%') AND (level > '10')".equals(sql)
-                   || "SELECT * FROM table WHERE (level > '10') AND (name LIKE 'ami%')".equals(sql));
+        assertTrue("SELECT * FROM table WHERE (name LIKE 'ami\\%%') AND (level > '10')".equals(sql)
+                   || "SELECT * FROM table WHERE (level > '10') AND (name LIKE 'ami\\%%')".equals(sql));
     }
     
     @Test


Mime
View raw message