cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1060874 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/ext/search/ main/java/org/apache/cxf/jaxrs/ext/search/sql/ test/java/org/apache/cxf/jaxrs/ext/search/sql/
Date Wed, 19 Jan 2011 17:23:48 GMT
Author: sergeyb
Date: Wed Jan 19 17:23:47 2011
New Revision: 1060874

URL: http://svn.apache.org/viewvc?rev=1060874&view=rev
Log:
[CXF-3145] Minor follow-up update: renaming SQLPrinterConditionVisitor to SQLPrinterVisitor
and moving it to sql subpackage

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
      - copied, changed from r1059967, cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SQLPrinterConditionVisitor.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
  (with props)
Removed:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SQLPrinterConditionVisitor.java
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java?rev=1060874&r1=1060873&r2=1060874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
Wed Jan 19 17:23:47 2011
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.jaxrs.ext.search;
 
+import org.apache.cxf.jaxrs.ext.search.sql.SQLPrinterVisitor;
+
 public final class SearchUtils {
     
     private SearchUtils() {
@@ -25,7 +27,7 @@ public final class SearchUtils {
     }
     
     public static <T> String toSQL(SearchCondition<T> sc, String table, String...
columns) {
-        SearchConditionVisitor<T> visitor = new SQLPrinterConditionVisitor<T>(table,
columns);
+        SearchConditionVisitor<T> visitor = new SQLPrinterVisitor<T>(table, columns);
         sc.accept(visitor);
         return visitor.getResult();
     }

Copied: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
(from r1059967, cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SQLPrinterConditionVisitor.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java?p2=cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java&p1=cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SQLPrinterConditionVisitor.java&r1=1059967&r2=1060874&rev=1060874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SQLPrinterConditionVisitor.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
Wed Jan 19 17:23:47 2011
@@ -16,22 +16,37 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.jaxrs.ext.search;
+package org.apache.cxf.jaxrs.ext.search.sql;
 
+import java.util.Map;
+import java.util.logging.Logger;
 
-public class SQLPrinterConditionVisitor<T> implements SearchConditionVisitor<T>
{
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchConditionVisitor;
+import org.apache.cxf.jaxrs.ext.search.SearchUtils;
 
+
+public class SQLPrinterVisitor<T> implements SearchConditionVisitor<T> {
+
+    private static final Logger LOG = LogUtils.getL7dLogger(SQLPrinterVisitor.class);
+    
     private StringBuilder sb;
     private String table;
     private String[] columns;
-
+    private Map<String, String> fieldMap;
     
-    public SQLPrinterConditionVisitor(String table, String... columns) {
+    public SQLPrinterVisitor(String table, String... columns) {
+        this(null, table, columns);
+    }
+    
+    public SQLPrinterVisitor(Map<String, String> fieldMap, String table, String...
columns) {
+        this.fieldMap = fieldMap;
         this.columns = columns;
         this.table = table;
     }
     
-    
     public void visit(SearchCondition<T> sc) {
         
         if (sb == null) {
@@ -45,7 +60,17 @@ public class SQLPrinterConditionVisitor<
         if (statement != null) {
             if (statement.getProperty() != null) {
                 String rvalStr = statement.getValue().toString().replaceAll("\\*", "%");
-                sb.append(statement.getProperty()).append(" ").append(
+                String name = statement.getProperty();
+                if (fieldMap != null) {
+                    if (fieldMap.containsKey(name)) {
+                        name = fieldMap.get(name);
+                    } else {
+                        LOG.warning("Unrecognized field alias : " + name);
+                        return;
+                    }
+                }
+                
+                sb.append(name).append(" ").append(
                             SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(),
rvalStr))
                             .append(" ").append("'").append(rvalStr).append("'");
             }

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java?rev=1060874&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
Wed Jan 19 17:23:47 2011
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.sql;
+
+import java.util.Collections;
+import java.util.Date;
+
+import org.apache.cxf.jaxrs.ext.search.FiqlParseException;
+import org.apache.cxf.jaxrs.ext.search.FiqlParser;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+
+public class SQLPrinterVisitorTest extends Assert {
+    private FiqlParser<Condition> parser = new FiqlParser<Condition>(Condition.class);
+
+    @Test
+    public void testSQL1() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
+        SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        
+        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
+    public void testSQL2() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
+        SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue("SELECT * FROM table WHERE (name LIKE 'ami%') OR (level > '10')".equals(sql)
+                   || "SELECT * FROM table WHERE (level > '10') OR (name LIKE 'ami%')".equals(sql));
+    }
+    
+    @Test
+    public void testSQL3() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==foo*;(name!=*bar,level=gt=10)");
+        SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue(("SELECT * FROM table WHERE (name LIKE 'foo%') AND ((name NOT LIKE '%bar')
"
+                   + "OR (level > '10'))").equals(sql)
+                   || ("SELECT * FROM table WHERE (name LIKE 'foo%') AND "
+                   + "((level > '10') OR (name NOT LIKE '%bar'))").equals(sql));
+    }
+    
+    @Test
+    public void testSQL4() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("(name==test,level==18);(name==test1,level!=19)");
+        SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue(("SELECT * FROM table WHERE ((name = 'test') OR (level = '18'))"
+                   + " AND ((name = 'test1') OR (level <> '19'))").equals(sql)
+                   || ("SELECT * FROM table WHERE ((name = 'test1') OR (level <> '19'))"
+                   + " AND ((name = 'test') OR (level = '18'))").equals(sql));
+    }
+    
+    @Test
+    public void testSQL5() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==test");
+        SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue("SELECT * FROM table WHERE name = 'test'".equals(sql));
+    }
+    
+    @Test
+    public void testSQL5WithColumns() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==test");
+        SQLPrinterVisitor<Condition> visitor = 
+            new SQLPrinterVisitor<Condition>("table", "NAMES");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue("SELECT NAMES FROM table WHERE name = 'test'".equals(sql));
+    }
+    
+    @Test
+    public void testSQL5WithFieldMap() throws FiqlParseException {
+        SearchCondition<Condition> filter = parser.parse("name==test");
+        SQLPrinterVisitor<Condition> visitor = 
+            new SQLPrinterVisitor<Condition>(
+                Collections.singletonMap("name", "NAMES"),
+                "table", "NAMES");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue("SELECT NAMES FROM table WHERE NAMES = 'test'".equals(sql));
+    }
+    
+    @Ignore
+    public static class Condition {
+        private String name;
+        private Integer level;
+        private Date time;
+
+        public Condition() {
+        }
+
+        public Condition(String name, Integer level, Date time) {
+            this.name = name;
+            this.level = level;
+            this.time = time;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public int getLevel() {
+            return level;
+        }
+
+        public void setLevel(int level) {
+            this.level = level;
+        }
+
+        public Date getTime() {
+            return time;
+        }
+
+        public void setTime(Date time) {
+            this.time = time;
+        }
+
+        public void setException(Exception ex) {
+            // do nothing
+        }
+
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message