cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7436] Experimenting with Joins
Date Tue, 04 Jul 2017 13:46:55 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes d0df127c3 -> 2f62e85eb


[CXF-7436] Experimenting with Joins


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

Branch: refs/heads/3.1.x-fixes
Commit: 2f62e85eb391c5c018006416343a4ce83170b7ec
Parents: d0df127
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue Jul 4 14:39:57 2017 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue Jul 4 14:46:37 2017 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/ext/search/SearchUtils.java       |  1 -
 .../jaxrs/ext/search/sql/SQLPrinterVisitor.java | 58 ++++++++++++---
 .../search/sql/SQLHierarchicalQueryTest.java    | 74 ++++++++++++++++++++
 3 files changed, 122 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2f62e85e/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 b8e889f..8109c07 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
@@ -159,7 +159,6 @@ public final class SearchUtils {
         if (tableAlias != null) {
             sb.append(" " + tableAlias);
         }
-        sb.append(" WHERE ");
     }
     
     public static String conditionTypeToSqlOperator(ConditionType ct, String value, String
originalValue) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/2f62e85e/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
index 4ce60d8..f28b3ac 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
@@ -25,16 +25,19 @@ import java.util.Map;
 
 import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchParseException;
 import org.apache.cxf.jaxrs.ext.search.SearchUtils;
 import org.apache.cxf.jaxrs.ext.search.visitor.AbstractUntypedSearchConditionVisitor;
 
 
 public class SQLPrinterVisitor<T> extends AbstractUntypedSearchConditionVisitor<T,
String> {
 
-    private String table;
+    private String primaryTable;
     private String tableAlias;
     private List<String> columns;
-    
+    private StringBuilder topBuilder = new StringBuilder();
+    private volatile boolean joinDone;
+
     // Can be useful when some other code will build Select and From clauses.
     public SQLPrinterVisitor() {
         this(null, null, Collections.<String>emptyList());
@@ -57,8 +60,9 @@ public class SQLPrinterVisitor<T> extends AbstractUntypedSearchConditionVisitor<
         super(fieldMap);
         
         this.columns = columns;
-        this.table = table;
+        this.primaryTable = table;
         this.tableAlias = tableAlias;
+        prepareTopStringBuilder();
     }
     
     public void visit(SearchCondition<T> sc) {
@@ -67,7 +71,33 @@ public class SQLPrinterVisitor<T> extends AbstractUntypedSearchConditionVisitor<
         PrimitiveStatement statement = sc.getStatement();
         if (statement != null) {
             if (statement.getProperty() != null) {
-                String name = getRealPropertyName(statement.getProperty());
+                
+                String property = statement.getProperty();
+                String[] properties =  property.split("\\.");
+                if (properties.length > 2) {
+                    throw new SearchParseException("SQL Visitor supports only a single JOIN");
+                } else if (properties.length == 2) {
+                    if (joinDone) {
+                        throw new SearchParseException("SQL Visitor has already created JOIN");
+                    } else {
+                        joinDone = true;
+                    }
+                    String joinTable = getRealPropertyName(properties[0]);
+                    // Joining key can be pre-configured
+                    String joiningKey = primaryTable;
+                    if (joiningKey.endsWith("s")) {
+                        joiningKey = joiningKey.substring(0, joiningKey.length() - 1);
+                    }
+                    joiningKey += "_id";
+                    
+                    topBuilder.append(" left join " + joinTable);
+                    topBuilder.append(" on " + primaryTable + ".id" + " = " + joinTable
+                                      + "." + joiningKey);
+                    
+                    property = joinTable + "." + getRealPropertyName(properties[1]);
+                }
+                
+                String name = getRealPropertyName(property);
                 String originalValue = getPropertyValue(name, statement.getValue());
                 validatePropertyValue(name, originalValue);
                 
@@ -106,13 +136,21 @@ public class SQLPrinterVisitor<T> extends AbstractUntypedSearchConditionVisitor<
         StringBuilder sb = super.getStringBuilder();
         if (sb == null) {
             sb = new StringBuilder();
-            if (table != null) {
-                SearchUtils.startSqlQuery(sb, table, tableAlias, columns);
-            }
         }
         return sb;
     }
-    
-    
-    
+
+
+    @Override
+    public String getQuery() {
+        StringBuilder sb = removeStringBuilder();
+        return sb == null ? null : topBuilder.toString() + " WHERE " + sb.toString();
+    }
+
+    private void prepareTopStringBuilder() {
+        if (primaryTable != null) {
+            SearchUtils.startSqlQuery(topBuilder, primaryTable, tableAlias, columns);
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2f62e85e/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLHierarchicalQueryTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLHierarchicalQueryTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLHierarchicalQueryTest.java
new file mode 100644
index 0000000..731d6d3
--- /dev/null
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLHierarchicalQueryTest.java
@@ -0,0 +1,74 @@
+/**
+ * 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 org.apache.cxf.jaxrs.ext.search.SearchBean;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchParseException;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+
+import org.junit.Assert;
+import org.junit.Test;
+//userName eq "admin@amarkevich.talend.com" and entitlements sw "TDP_"
+
+public class SQLHierarchicalQueryTest extends Assert {
+    @Test
+    public void testSimpleHierarchicalQuery() throws SearchParseException {
+        FiqlParser<SearchBean> parser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = parser.parse("cartridges.colour==blue");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("printers");
+        filter.accept(visitor.visitor());
+        String sql = visitor.getQuery();
+
+        assertEquals("SELECT * FROM printers left join cartridges"
+                     + " on printers.id = cartridges.printer_id"
+                     + " WHERE cartridges.colour = 'blue'",
+                     sql);
+    }
+    
+    @Test
+    public void testAndHierarchicalQuery() throws SearchParseException {
+        FiqlParser<SearchBean> parser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = parser.parse("name==Epson;cartridges.colour==blue");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("printers");
+        filter.accept(visitor.visitor());
+        String sql = visitor.getQuery();
+
+        assertEquals("SELECT * FROM printers left join cartridges"
+                     + " on printers.id = cartridges.printer_id"
+                     + " WHERE (name = 'Epson') AND (cartridges.colour = 'blue')",
+                     sql);
+    }
+    
+    @Test(expected = SearchParseException.class)
+    public void testLongHierarchicalQuery() {
+        FiqlParser<SearchBean> parser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = parser.parse("cartridges.producer.location==Japan");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("printers");
+        filter.accept(visitor.visitor());
+    }
+    
+    @Test(expected = SearchParseException.class)
+    public void testTooManyJoins() {
+        FiqlParser<SearchBean> parser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = parser.parse("cartridges.colour==blue;cartridges.location==Japan");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("printers");
+        filter.accept(visitor.visitor());
+    }
+}


Mime
View raw message