Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MODE-1680: Extend query processor to allow use aliased joinings

  • Loading branch information...
commit b4198c1ed174e4b71150312a0b2fb8dc301c11b7 1 parent 7c4bee8
Oleg Kulikov authored March 11, 2013 rhauch committed March 11, 2013
18  modeshape-jcr/src/main/java/org/modeshape/jcr/query/process/QueryProcessor.java
@@ -320,6 +320,22 @@ protected ProcessingComponent createComponent( QueryCommand originalQuery,
320 320
             case SORT:
321 321
                 // Create the component under the SORT ...
322 322
                 assert node.getChildCount() == 1;
  323
+
  324
+                List<PlanNode> project = node.findAllAtOrBelow(Type.PROJECT);
  325
+                assert project != null;
  326
+
  327
+                ArrayList<Column> allColumns = new ArrayList();
  328
+                for (PlanNode p : project) {
  329
+                    allColumns.addAll(p.getPropertyAsList(Property.PROJECT_COLUMNS, Column.class));
  330
+                }
  331
+
  332
+                ArrayList<String> ctypes = new ArrayList();
  333
+                for (PlanNode p : project) {
  334
+                    ctypes.addAll(p.getPropertyAsList(Property.PROJECT_COLUMN_TYPES, String.class));
  335
+                }
  336
+
  337
+                QueryResultColumns orderColumns = new QueryResultColumns(allColumns, ctypes, context.getHints().hasFullTextSearch);
  338
+
323 339
                 ProcessingComponent sortDelegate = createComponent(originalQuery,
324 340
                                                                    context,
325 341
                                                                    node.getFirstChild(),
@@ -343,7 +359,7 @@ protected ProcessingComponent createComponent( QueryCommand originalQuery,
343 359
                             if (alias != null) sourceNamesByAlias.put(alias, name);
344 360
                         }
345 361
                         // Now create the sorting component ...
346  
-                        component = new SortValuesComponent(sortDelegate, orderings, sourceNamesByAlias);
  362
+                        component = new SortValuesComponent(sortDelegate, orderings, orderColumns, sourceNamesByAlias);
347 363
                     } else {
348 364
                         // Order by the location(s) because it's before a merge-join ...
349 365
                         component = new SortLocationsComponent(sortDelegate);
9  modeshape-jcr/src/main/java/org/modeshape/jcr/query/process/SortValuesComponent.java
@@ -47,12 +47,19 @@
47 47
     private final Comparator<Object[]> sortingComparator;
48 48
 
49 49
     public SortValuesComponent( ProcessingComponent delegate,
50  
-                                List<Ordering> orderings,
  50
+                                List<Ordering> orderings, 
51 51
                                 Map<SelectorName, SelectorName> sourceNamesByAlias ) {
52 52
         super(delegate);
53 53
         this.sortingComparator = createSortComparator(delegate.getContext(), delegate.getColumns(), orderings, sourceNamesByAlias);
54 54
     }
55 55
 
  56
+    public SortValuesComponent( ProcessingComponent delegate,
  57
+                                List<Ordering> orderings, QueryResultColumns columns,
  58
+                                Map<SelectorName, SelectorName> sourceNamesByAlias ) {
  59
+        super(delegate);
  60
+        this.sortingComparator = createSortComparator(delegate.getContext(), columns, orderings, sourceNamesByAlias);
  61
+    }
  62
+
56 63
     /**
57 64
      * @return sortingComparator
58 65
      */
58  modeshape-jcr/src/test/java/org/modeshape/jcr/JcrQueryManagerTest.java
@@ -40,6 +40,7 @@
40 40
 import java.io.File;
41 41
 import java.io.IOException;
42 42
 import java.io.InputStream;
  43
+import java.math.BigDecimal;
43 44
 import java.net.URI;
44 45
 import java.net.URISyntaxException;
45 46
 import java.net.URL;
@@ -2972,6 +2973,63 @@ public void shouldFindChildrenOfRootUsingIsChildNodeCriteria() throws Exception
2972 2973
         assertNodesAreFound(queryString, Query.JCR_SQL2, "/jcr:system", "/Cars", "/Other", "/NodeB", "/node1", "/node2");
2973 2974
     }
2974 2975
 
  2976
+    @Test
  2977
+    public void testOrderByWithAliases() throws Exception {
  2978
+        //fill the repository with test data
  2979
+        Node src = session.getRootNode().addNode("src", "nt:folder");
  2980
+
  2981
+        //add node f1 with child jcr:content
  2982
+        Node f1 = src.addNode("f1", "nt:file");
  2983
+        f1.addMixin("mix:simpleVersionable");
  2984
+        Node content1 = f1.addNode("jcr:content", "nt:resource");
  2985
+        content1.setProperty("jcr:data", session.getValueFactory().createBinary("Node f1".getBytes()));
  2986
+
  2987
+        //save and slip a bit to have difference in time of node creation.
  2988
+        session.save();
  2989
+        Thread.currentThread().sleep(1000);
  2990
+        
  2991
+        //add node f2 with child jcr:content
  2992
+        Node f2 = src.addNode("f2", "nt:file");
  2993
+        f2.addMixin("mix:simpleVersionable");
  2994
+        Node content2 = f2.addNode("jcr:content", "nt:resource");
  2995
+        content2.setProperty("jcr:data", session.getValueFactory().createBinary("Node f2".getBytes()));
  2996
+
  2997
+        session.save();
  2998
+        
  2999
+        System.out.println("-------------------- MyQueryTest---------------------");
  3000
+
  3001
+        String descOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:created] DESC";
  3002
+        String ascOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:created] ASC";
  3003
+
  3004
+        QueryManager queryManager = session.getWorkspace().getQueryManager();
  3005
+        Query query = queryManager.createQuery(descOrder, Query.JCR_SQL2);
  3006
+        QueryResult result = query.execute();
  3007
+
  3008
+        //checking first query
  3009
+        RowIterator it = result.getRows();
  3010
+        assertEquals(2, it.getSize());
  3011
+
  3012
+        Node n1 = it.nextRow().getNode();
  3013
+        Node n2 = it.nextRow().getNode();
  3014
+
  3015
+        assertEquals("f2", n1.getName());
  3016
+        assertEquals("f1", n2.getName());
  3017
+
  3018
+        //the same request with other order
  3019
+        query = queryManager.createQuery(ascOrder, Query.JCR_SQL2);
  3020
+        result = query.execute();
  3021
+
  3022
+        //checking second query
  3023
+        it = result.getRows();
  3024
+        assertEquals(2, it.getSize());
  3025
+
  3026
+        n1 = it.nextRow().getNode();
  3027
+        n2 = it.nextRow().getNode();
  3028
+
  3029
+        assertEquals("f1", n1.getName());
  3030
+        assertEquals("f2", n2.getName());
  3031
+    }
  3032
+
2975 3033
     private void assertNodesAreFound( String queryString,
2976 3034
                                       String queryType,
2977 3035
                                       String... expectedNodesPaths ) throws RepositoryException {

0 notes on commit b4198c1

Please sign in to comment.
Something went wrong with that request. Please try again.