1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.internal

File DefaultQuery.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
44
25
1
303
174
28
0.64
1.76
25
1.12

Classes

Class Line # Actions
DefaultQuery 41 44 0% 28 2
0.9733333697.3%
 

Contributing tests

This file is covered by 29 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.query.internal;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import org.slf4j.Logger;
28    import org.slf4j.LoggerFactory;
29    import org.xwiki.query.Query;
30    import org.xwiki.query.QueryException;
31    import org.xwiki.query.QueryExecutor;
32    import org.xwiki.query.QueryFilter;
33    import org.xwiki.query.SecureQuery;
34   
35    /**
36    * Stores all information needed for execute a query.
37    *
38    * @version $Id: bd101da64537e006f15071d6d74b0e398a5e0d46 $
39    * @since 1.6M1
40    */
 
41    public class DefaultQuery implements SecureQuery
42    {
43    /**
44    * Used to log possible warnings.
45    */
46    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultQuery.class);
47   
48    /**
49    * field for {@link #isNamed()}.
50    */
51    protected boolean isNamed;
52   
53    /**
54    * field for {@link Query#getStatement()}.
55    */
56    private String statement;
57   
58    /**
59    * field for {@link Query#getLanguage()}.
60    */
61    private String language;
62   
63    /**
64    * virtual wiki to run the query.
65    */
66    private String wiki;
67   
68    /**
69    * map from query parameters to values.
70    */
71    private Map<String, Object> namedParameters = new HashMap<String, Object>();
72   
73    /**
74    * map from index to positional parameter value.
75    */
76    private Map<Integer, Object> positionalParameters = new HashMap<Integer, Object>();
77   
78    /**
79    * field for {@link Query#setLimit(int)}.
80    */
81    private int limit;
82   
83    /**
84    * field for {@link Query#setOffset(int)}.
85    */
86    private int offset;
87   
88    /**
89    * @see #isCurrentAuthorChecked()
90    */
91    private boolean checkCurrentAuthor;
92   
93    /**
94    * @see #isCurrentUserChecked()
95    */
96    private boolean checkCurrentUser;
97   
98    /**
99    * field for {@link #getFilters()}.
100    */
101    private List<QueryFilter> filters = new ArrayList<QueryFilter>();
102   
103    /**
104    * field for {@link #getExecuter()}.
105    */
106    private transient QueryExecutor executer;
107   
108    /**
109    * Create a Query.
110    *
111    * @param statement query statement
112    * @param language query language
113    * @param executor QueryExecutor component for execute the query.
114    */
 
115  12104 toggle public DefaultQuery(String statement, String language, QueryExecutor executor)
116    {
117  12104 this.statement = statement;
118  12104 this.language = language;
119  12104 this.executer = executor;
120  12104 this.isNamed = false;
121    }
122   
123    /**
124    * Create a named Query.
125    *
126    * @param queryName the name of the query.
127    * @param executor QueryExecutor component for execute the query.
128    */
 
129  292 toggle public DefaultQuery(String queryName, QueryExecutor executor)
130    {
131  292 this.statement = queryName;
132  291 this.executer = executor;
133  292 this.isNamed = true;
134    }
135   
 
136  12860 toggle @Override
137    public String getStatement()
138    {
139  12861 return this.statement;
140    }
141   
 
142  12217 toggle @Override
143    public String getLanguage()
144    {
145  12217 return this.language;
146    }
147   
 
148  23425 toggle @Override
149    public boolean isNamed()
150    {
151  23426 return this.isNamed;
152    }
153   
 
154  6926 toggle @Override
155    public Query setWiki(String wiki)
156    {
157  6926 this.wiki = wiki;
158  6926 return this;
159    }
160   
 
161  19169 toggle @Override
162    public String getWiki()
163    {
164  19170 return this.wiki;
165    }
166   
 
167  16882 toggle @Override
168    public Query bindValue(String var, Object val)
169    {
170  16883 this.namedParameters.put(var, val);
171  16880 return this;
172    }
173   
 
174  343 toggle @Override
175    public Query bindValue(int index, Object val)
176    {
177  343 this.positionalParameters.put(index, val);
178  343 return this;
179    }
180   
 
181  280 toggle @Override
182    public Query bindValues(List<Object> values)
183    {
184  484 for (int i = 0; i < values.size(); i++) {
185    // There's a difference in the way positional parameters are handled:
186    // - HQL (jdbc-like), the index of positional parameters must start at 0
187    // - XWQL (jpql-like), the index of positional parameters must start at 1
188    //
189    // This difference is also hardcoded in HqlQueryExecutor#populateParameters(), a better solution could
190    // be to replace the current DefaultQuery with distinct implementations: XwqlQuery, HqlQuery, NamedQuery.
191  204 if (Query.HQL.equals(getLanguage())) {
192  204 bindValue(i, values.get(i));
193    } else {
194  0 bindValue(i + 1, values.get(i));
195    }
196    }
197  280 return this;
198    }
199   
 
200  12658 toggle @Override
201    public int getLimit()
202    {
203  12657 return this.limit;
204    }
205   
 
206  12296 toggle @Override
207    public int getOffset()
208    {
209  12294 return this.offset;
210    }
211   
 
212  2079 toggle @Override
213    public Query setLimit(int limit)
214    {
215  2082 this.limit = limit;
216  2081 return this;
217    }
218   
 
219  2087 toggle @Override
220    public Query setOffset(int offset)
221    {
222  2089 this.offset = offset;
223  2087 return this;
224    }
225   
 
226  12233 toggle @Override
227    public boolean isCurrentAuthorChecked()
228    {
229  12233 return this.checkCurrentAuthor;
230    }
231   
 
232  773 toggle @Override
233    public SecureQuery checkCurrentAuthor(boolean checkCurrentAuthor)
234    {
235  773 this.checkCurrentAuthor = checkCurrentAuthor;
236   
237  773 return this;
238    }
239   
 
240  1052 toggle @Override
241    public boolean isCurrentUserChecked()
242    {
243  1052 return this.checkCurrentUser;
244    }
245   
 
246  1366 toggle @Override
247    public SecureQuery checkCurrentUser(boolean checkUser)
248    {
249  1366 this.checkCurrentUser = checkUser;
250   
251  1366 return this;
252    }
253   
 
254  12307 toggle @Override
255    public Map<String, Object> getNamedParameters()
256    {
257  12304 return this.namedParameters;
258    }
259   
 
260  12553 toggle @Override
261    public Map<Integer, Object> getPositionalParameters()
262    {
263  12554 return this.positionalParameters;
264    }
265   
 
266  47919 toggle @Override
267    public List<QueryFilter> getFilters()
268    {
269  47921 return this.filters;
270    }
271   
 
272  1956 toggle @Override
273    public Query addFilter(QueryFilter filter)
274    {
275  1956 if (!this.filters.contains(filter)) {
276  1955 this.filters.add(filter);
277    } else {
278  1 LOGGER.warn("QueryFilter [{}] already added to the query [{}]", filter.toString(), this.getStatement());
279    }
280   
281  1956 return this;
282    }
283   
 
284  12221 toggle @Override
285    public <T> List<T> execute() throws QueryException
286    {
287  12221 return getExecuter().execute(this);
288    }
289   
290    /**
291    * @return QueryExecutor interface for execute the query.
292    */
 
293  12219 toggle protected QueryExecutor getExecuter()
294    {
295  12220 return this.executer;
296    }
297   
 
298  384 toggle @Override
299    public String toString()
300    {
301  384 return getStatement();
302    }
303    }