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