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.jcr.query;
25
26 import java.util.Map;
27 import java.util.Set;
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Value;
30 import javax.jcr.query.Query;
31 import javax.jcr.query.QueryResult;
32 import net.jcip.annotations.NotThreadSafe;
33 import org.modeshape.common.util.CheckArg;
34 import org.modeshape.common.util.StringUtil;
35 import org.modeshape.graph.property.Path;
36 import org.modeshape.graph.query.QueryResults;
37 import org.modeshape.graph.query.model.QueryCommand;
38 import org.modeshape.graph.query.parse.QueryParser;
39 import org.modeshape.graph.query.plan.PlanHints;
40 import org.modeshape.graph.query.validate.Schemata;
41 import org.modeshape.jcr.query.qom.JcrAbstractQuery;
42 import org.modeshape.jcr.query.qom.JcrLiteral;
43
44
45
46
47 @NotThreadSafe
48 public class JcrQuery extends JcrAbstractQuery {
49
50 private QueryCommand query;
51 private final PlanHints hints;
52 private final Map<String, Object> variables;
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public JcrQuery( JcrQueryContext context,
67 String statement,
68 String language,
69 QueryCommand query,
70 PlanHints hints,
71 Path storedAtPath ) {
72 super(context, statement, language, storedAtPath);
73 assert query != null;
74 this.query = query;
75 this.hints = hints;
76 this.variables = null;
77 }
78
79 protected QueryCommand query() {
80 return query;
81 }
82
83
84
85
86
87
88 public QueryCommand getAbstractQueryModel() {
89 return query;
90 }
91
92
93
94
95
96
97 @SuppressWarnings( "deprecation" )
98 public QueryResult execute() throws RepositoryException {
99 context.isLive();
100
101 Schemata schemata = context.getSchemata();
102 QueryResults result = context.execute(query, hints, variables);
103 checkForProblems(result.getProblems());
104 if (Query.XPATH.equals(language)) {
105 return new XPathQueryResult(context, statement, result, schemata);
106 } else if (Query.SQL.equals(language)) {
107 return new JcrSqlQueryResult(context, statement, result, schemata);
108 }
109 return new JcrQueryResult(context, statement, result, schemata);
110 }
111
112
113
114
115
116
117 @Override
118 public String toString() {
119 return language + " -> " + statement + "\n" + StringUtil.createString(' ', Math.min(language.length() - 3, 0))
120 + "AQM -> " + query;
121 }
122
123
124
125
126
127
128 @Override
129 public void bindValue( String varName,
130 Value value ) throws IllegalArgumentException, RepositoryException {
131 CheckArg.isNotNull(varName, "varName");
132 CheckArg.isNotNull(value, "value");
133 variables.put(varName, JcrLiteral.rawValue(value));
134 }
135
136
137
138
139
140
141 @Override
142 public String[] getBindVariableNames() {
143 Set<String> keys = variables.keySet();
144 return keys.toArray(new String[keys.size()]);
145 }
146
147
148
149
150
151
152 @Override
153 public void setLimit( long limit ) {
154 if (limit > Integer.MAX_VALUE) limit = Integer.MAX_VALUE;
155 query = query.withLimit((int)limit);
156 }
157
158
159
160
161
162
163 @Override
164 public void setOffset( long offset ) {
165 if (offset > Integer.MAX_VALUE) offset = Integer.MAX_VALUE;
166 query = query.withOffset((int)offset);
167 }
168 }