1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.query.process;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import org.modeshape.graph.Location;
30 import org.modeshape.graph.query.QueryContext;
31 import org.modeshape.graph.query.QueryResults.Columns;
32 import org.modeshape.graph.query.model.Column;
33 import org.modeshape.graph.query.model.Constraint;
34 import org.modeshape.graph.query.model.Limit;
35 import org.modeshape.graph.query.model.SelectorName;
36 import org.modeshape.graph.query.plan.PlanNode;
37 import org.modeshape.graph.query.plan.PlanNode.Property;
38 import org.modeshape.graph.query.plan.PlanNode.Type;
39 import org.modeshape.graph.query.validate.Schemata;
40
41
42
43
44
45 public abstract class AbstractAccessComponent extends ProcessingComponent {
46
47 protected final PlanNode accessNode;
48 protected final SelectorName sourceName;
49 protected final List<Column> projectedColumns;
50 protected final List<Constraint> andedConstraints;
51 protected final Limit limit;
52
53 protected AbstractAccessComponent( QueryContext context,
54 Columns columns,
55 PlanNode accessNode ) {
56 super(context, columns);
57 this.accessNode = accessNode;
58
59
60 PlanNode source = accessNode.findAtOrBelow(Type.SOURCE);
61 if (source != null) {
62 this.sourceName = source.getProperty(Property.SOURCE_NAME, SelectorName.class);
63
64
65
66 } else {
67 throw new IllegalArgumentException();
68 }
69
70
71 PlanNode project = accessNode.findAtOrBelow(Type.PROJECT);
72 if (project != null) {
73 List<Column> projectedColumns = project.getPropertyAsList(Property.PROJECT_COLUMNS, Column.class);
74 if (projectedColumns != null) {
75 this.projectedColumns = projectedColumns;
76 } else {
77
78 List<Schemata.Column> schemataColumns = source.getPropertyAsList(Property.SOURCE_COLUMNS, Schemata.Column.class);
79 this.projectedColumns = new ArrayList<Column>(schemataColumns.size());
80 for (Schemata.Column schemataColumn : schemataColumns) {
81 String columnName = schemataColumn.getName();
82
83 String propertyName = columnName;
84 Column column = new Column(sourceName, propertyName, columnName);
85 this.projectedColumns.add(column);
86 }
87 }
88 } else {
89 throw new IllegalArgumentException();
90 }
91
92
93 List<Constraint> andedConstraints = null;
94 for (PlanNode select : accessNode.findAllAtOrBelow(Type.SELECT)) {
95 Constraint selectConstraint = select.getProperty(Property.SELECT_CRITERIA, Constraint.class);
96 if (andedConstraints == null) andedConstraints = new ArrayList<Constraint>();
97 andedConstraints.add(selectConstraint);
98 }
99 this.andedConstraints = andedConstraints != null ? andedConstraints : Collections.<Constraint>emptyList();
100
101
102 Limit limit = Limit.NONE;
103 PlanNode limitNode = accessNode.findAtOrBelow(Type.LIMIT);
104 if (limitNode != null) {
105 Integer count = limitNode.getProperty(Property.LIMIT_COUNT, Integer.class);
106 if (count != null) limit = limit.withRowLimit(count.intValue());
107 Integer offset = limitNode.getProperty(Property.LIMIT_OFFSET, Integer.class);
108 if (offset != null) limit = limit.withOffset(offset.intValue());
109 }
110 this.limit = limit;
111 }
112
113 }