1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.query.internal

File DefaultQuery.java

 

Coverage histogram

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

Code metrics

6
47
26
1
312
182
29
0.62
1.81
26
1.12

Classes

Class Line # Actions
DefaultQuery 42 47 0% 29 2
0.974683597.5%
 

Contributing tests

This file is covered by 42 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.QueryParameter;
34    import org.xwiki.query.SecureQuery;
35   
36    /**
37    * Stores all information needed for execute a query.
38    *
39    * @version $Id: d6de62b339a70b136f955f8587e7703df527bd44 $
40    * @since 1.6M1
41    */
 
42    public class DefaultQuery implements SecureQuery
43    {
44    /**
45    * Used to log possible warnings.
46    */
47    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultQuery.class);
48   
49    /**
50    * field for {@link #isNamed()}.
51    */
52    protected boolean isNamed;
53   
54    /**
55    * field for {@link Query#getStatement()}.
56    */
57    private String statement;
58   
59    /**
60    * field for {@link Query#getLanguage()}.
61    */
62    private String language;
63   
64    /**
65    * virtual wiki to run the query.
66    */
67    private String wiki;
68   
69    /**
70    * map from query parameters to values.
71    */
72    private Map<String, Object> namedParameters = new HashMap<String, Object>();
73   
74    /**
75    * map from index to positional parameter value.
76    */
77    private Map<Integer, Object> positionalParameters = new HashMap<Integer, Object>();
78   
79    /**
80    * field for {@link Query#setLimit(int)}.
81    */
82    private int limit;
83   
84    /**
85    * field for {@link Query#setOffset(int)}.
86    */
87    private int offset;
88   
89    /**
90    * @see #isCurrentAuthorChecked()
91    */
92    private boolean checkCurrentAuthor;
93   
94    /**
95    * @see #isCurrentUserChecked()
96    */
97    private boolean checkCurrentUser;
98   
99    /**
100    * field for {@link #getFilters()}.
101    */
102    private List<QueryFilter> filters = new ArrayList<QueryFilter>();
103   
104    /**
105    * field for {@link #getExecuter()}.
106    */
107    private transient QueryExecutor executer;
108   
109    /**
110    * Create a Query.
111    *
112    * @param statement query statement
113    * @param language query language
114    * @param executor QueryExecutor component for execute the query.
115    */
 
116  152694 toggle public DefaultQuery(String statement, String language, QueryExecutor executor)
117    {
118  152691 this.statement = statement;
119  152695 this.language = language;
120  152694 this.executer = executor;
121  152693 this.isNamed = false;
122    }
123   
124    /**
125    * Create a named Query.
126    *
127    * @param queryName the name of the query.
128    * @param executor QueryExecutor component for execute the query.
129    */
 
130  3245 toggle public DefaultQuery(String queryName, QueryExecutor executor)
131    {
132  3244 this.statement = queryName;
133  3245 this.executer = executor;
134  3245 this.isNamed = true;
135    }
136   
 
137  207245 toggle @Override
138    public String getStatement()
139    {
140  207248 return this.statement;
141    }
142   
 
143  155337 toggle @Override
144    public String getLanguage()
145    {
146  155337 return this.language;
147    }
148   
 
149  292752 toggle @Override
150    public boolean isNamed()
151    {
152  292757 return this.isNamed;
153    }
154   
 
155  119862 toggle @Override
156    public Query setWiki(String wiki)
157    {
158  119862 this.wiki = wiki;
159  119863 return this;
160    }
161   
 
162  272406 toggle @Override
163    public String getWiki()
164    {
165  272408 return this.wiki;
166    }
167   
 
168  224176 toggle @Override
169    public Query bindValue(String var, Object val)
170    {
171  224174 this.namedParameters.put(var, val);
172  224174 return this;
173    }
174   
 
175  12492 toggle @Override
176    public Query bindValue(int index, Object val)
177    {
178  12492 this.positionalParameters.put(index, val);
179  12492 return this;
180    }
181   
 
182  1121 toggle @Override
183    public Query bindValues(List<Object> values)
184    {
185  3630 for (int i = 0; i < values.size(); i++) {
186    // There's a difference in the way positional parameters are handled:
187    // - HQL (jdbc-like), the index of positional parameters must start at 0
188    // - XWQL (jpql-like), the index of positional parameters must start at 1
189    //
190    // This difference is also hardcoded in HqlQueryExecutor#populateParameters(), a better solution could
191    // be to replace the current DefaultQuery with distinct implementations: XwqlQuery, HqlQuery, NamedQuery.
192  2509 if (Query.HQL.equals(getLanguage())) {
193  2509 bindValue(i, values.get(i));
194    } else {
195  0 bindValue(i + 1, values.get(i));
196    }
197    }
198  1121 return this;
199    }
200   
 
201  64 toggle @Override
202    public QueryParameter bindValue(String var)
203    {
204  64 QueryParameter parameter = new DefaultQueryParameter(this);
205  64 bindValue(var, parameter);
206  64 return parameter;
207    }
208   
 
209  174508 toggle @Override
210    public int getLimit()
211    {
212  174516 return this.limit;
213    }
214   
 
215  156579 toggle @Override
216    public int getOffset()
217    {
218  156582 return this.offset;
219    }
220   
 
221  35097 toggle @Override
222    public Query setLimit(int limit)
223    {
224  35096 this.limit = limit;
225  35097 return this;
226    }
227   
 
228  20326 toggle @Override
229    public Query setOffset(int offset)
230    {
231  20324 this.offset = offset;
232  20326 return this;
233    }
234   
 
235  155456 toggle @Override
236    public boolean isCurrentAuthorChecked()
237    {
238  155457 return this.checkCurrentAuthor;
239    }
240   
 
241  57567 toggle @Override
242    public SecureQuery checkCurrentAuthor(boolean checkCurrentAuthor)
243    {
244  57567 this.checkCurrentAuthor = checkCurrentAuthor;
245   
246  57567 return this;
247    }
248   
 
249  18495 toggle @Override
250    public boolean isCurrentUserChecked()
251    {
252  18495 return this.checkCurrentUser;
253    }
254   
 
255  38472 toggle @Override
256    public SecureQuery checkCurrentUser(boolean checkUser)
257    {
258  38472 this.checkCurrentUser = checkUser;
259   
260  38472 return this;
261    }
262   
 
263  291271 toggle @Override
264    public Map<String, Object> getNamedParameters()
265    {
266  291283 return this.namedParameters;
267    }
268   
 
269  287881 toggle @Override
270    public Map<Integer, Object> getPositionalParameters()
271    {
272  287884 return this.positionalParameters;
273    }
274   
 
275  652035 toggle @Override
276    public List<QueryFilter> getFilters()
277    {
278  652046 return this.filters;
279    }
280   
 
281  88475 toggle @Override
282    public Query addFilter(QueryFilter filter)
283    {
284  88478 if (!this.filters.contains(filter)) {
285  88477 this.filters.add(filter);
286    } else {
287  1 LOGGER.warn("QueryFilter [{}] already added to the query [{}]", filter.toString(), this.getStatement());
288    }
289   
290  88475 return this;
291    }
292   
 
293  155441 toggle @Override
294    public <T> List<T> execute() throws QueryException
295    {
296  155437 return getExecuter().execute(this);
297    }
298   
299    /**
300    * @return QueryExecutor interface for execute the query.
301    */
 
302  155438 toggle protected QueryExecutor getExecuter()
303    {
304  155433 return this.executer;
305    }
306   
 
307  2440 toggle @Override
308    public String toString()
309    {
310  2440 return getStatement();
311    }
312    }