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.request;
25
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import org.modeshape.common.util.CheckArg;
30 import org.modeshape.common.util.HashCode;
31 import org.modeshape.graph.query.QueryResults.Columns;
32 import org.modeshape.graph.query.QueryResults.Statistics;
33 import org.modeshape.graph.query.model.Column;
34 import org.modeshape.graph.query.model.Constraint;
35 import org.modeshape.graph.query.model.Limit;
36 import org.modeshape.graph.query.model.SelectorName;
37 import org.modeshape.graph.query.model.Visitors;
38 import org.modeshape.graph.query.validate.Schemata;
39
40
41
42
43
44 public class AccessQueryRequest extends SearchRequest {
45
46 private static final Map<String, Object> EMPTY_VARIABLES = Collections.emptyMap();
47
48 private static final long serialVersionUID = 1L;
49
50 private final String workspaceName;
51 private final SelectorName tableName;
52 private final List<Constraint> andedConstraints;
53 private final Limit limit;
54 private final Map<String, Object> variables;
55 private final Schemata schemata;
56 private final int hc;
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public AccessQueryRequest( String workspace,
71 SelectorName tableName,
72 Columns resultColumns,
73 List<Constraint> andedConstraints,
74 Limit limit,
75 Schemata schemata,
76 Map<String, Object> variables ) {
77 CheckArg.isNotNull(workspace, "workspace");
78 CheckArg.isNotNull(tableName, "tableName");
79 CheckArg.isNotNull(resultColumns, "resultColumns");
80 this.workspaceName = workspace;
81 this.tableName = tableName;
82 this.andedConstraints = andedConstraints != null ? andedConstraints : Collections.<Constraint>emptyList();
83 this.variables = variables != null ? variables : EMPTY_VARIABLES;
84 this.limit = limit != null ? limit : Limit.NONE;
85 this.schemata = schemata;
86 this.doSetResults(resultColumns, null, null);
87 this.hc = HashCode.compute(workspaceName, tableName, resultColumns);
88 }
89
90
91
92
93
94
95 public String workspace() {
96 return workspaceName;
97 }
98
99
100
101
102
103
104 public SelectorName selectorName() {
105 return tableName;
106 }
107
108
109
110
111
112
113 public Columns resultColumns() {
114 return super.columns();
115 }
116
117
118
119
120
121
122
123 public List<Constraint> andedConstraints() {
124 return andedConstraints;
125 }
126
127
128
129
130
131
132 public Map<String, Object> variables() {
133 return variables;
134 }
135
136
137
138
139
140
141 public Schemata schemata() {
142 return schemata;
143 }
144
145
146
147
148
149
150
151 public Limit limit() {
152 return limit;
153 }
154
155
156
157
158
159
160
161 public void setResults( List<Object[]> tuples,
162 Statistics statistics ) {
163 super.doSetResults(columns(), tuples, statistics);
164 }
165
166
167
168
169
170
171 @Override
172 public int hashCode() {
173 return hc;
174 }
175
176
177
178
179
180
181 @Override
182 public boolean equals( Object obj ) {
183 if (obj == this) return true;
184 if (this.getClass().isInstance(obj)) {
185 AccessQueryRequest that = (AccessQueryRequest)obj;
186 if (this.hashCode() != that.hashCode()) return false;
187 if (!this.workspace().equals(that.workspace())) return false;
188 if (!this.selectorName().equals(that.selectorName())) return false;
189 if (!this.limit().equals(that.limit())) return false;
190 if (!this.andedConstraints().equals(that.andedConstraints())) return false;
191 if (!this.resultColumns().equals(that.resultColumns())) return false;
192 if (!this.variables().equals(that.variables())) return false;
193 return true;
194 }
195 return false;
196 }
197
198
199
200
201
202
203 @Override
204 public String toString() {
205 StringBuilder sb = new StringBuilder("query the \"");
206 sb.append(workspaceName).append("\" workspace: SELECT ");
207 boolean first = true;
208 for (Column column : resultColumns().getColumns()) {
209 if (first) first = false;
210 else sb.append(", ");
211 sb.append(column);
212 }
213 sb.append(" FROM ").append(selectorName().name());
214 if (!andedConstraints.isEmpty()) {
215 sb.append(" WHERE ");
216 first = true;
217 for (Constraint constraint : andedConstraints) {
218 if (first) first = false;
219 else sb.append(" AND ");
220 sb.append(Visitors.readable(constraint));
221 }
222 }
223 if (!limit.isUnlimited()) {
224 sb.append(Visitors.readable(limit));
225 }
226 if (!variables.isEmpty()) {
227 sb.append(" USING <");
228 first = true;
229 for (Map.Entry<String, Object> entry : variables.entrySet()) {
230 if (first) first = false;
231 else sb.append(", ");
232 sb.append(entry.getKey()).append('=');
233 Object value = entry.getValue();
234 if (value instanceof String) {
235 sb.append('"').append(value).append('"');
236 } else {
237 sb.append(value);
238 }
239 }
240 sb.append('>');
241 }
242 return sb.toString();
243 }
244
245 @Override
246 public RequestType getType() {
247 return RequestType.ACCESS_QUERY;
248 }
249 }