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

File XWQLQueryExecutor.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
32
4
1
142
98
13
0.41
8
4
3.25

Classes

Class Line # Actions
XWQLQueryExecutor 48 32 0% 13 12
0.7575%
 

Contributing tests

No tests hitting this source file were found.

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.xwql.internal;
21   
22    import java.util.List;
23    import java.util.Map.Entry;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.job.event.status.JobProgressManager;
34    import org.xwiki.model.EntityType;
35    import org.xwiki.model.ModelContext;
36    import org.xwiki.model.reference.EntityReference;
37    import org.xwiki.model.reference.WikiReference;
38    import org.xwiki.query.Query;
39    import org.xwiki.query.QueryException;
40    import org.xwiki.query.QueryExecutor;
41    import org.xwiki.query.QueryFilter;
42    import org.xwiki.query.QueryManager;
43    import org.xwiki.query.SecureQuery;
44   
45    @Component
46    @Named("xwql")
47    @Singleton
 
48    public class XWQLQueryExecutor implements QueryExecutor
49    {
50    @Inject
51    @Named("hql")
52    private QueryTranslator translator;
53   
54    @Inject
55    private ComponentManager componentManager;
56   
57    @Inject
58    private ModelContext context;
59   
60    @Inject
61    private JobProgressManager progress;
62   
 
63  1000 toggle public QueryManager getQueryManager() throws ComponentLookupException
64    {
65    // We can't inject QueryManager because of cyclic dependency.
66  1000 return this.componentManager.getInstance(QueryManager.class);
67    }
68   
69    /**
70    * @param statement the statement to evaluate
71    * @return true if the statement is complete, false otherwise
72    */
 
73  126 toggle public static boolean isShortFormStatement(String statement)
74    {
75  126 return StringUtils.startsWithAny(statement.trim().toLowerCase(), ",", "from", "where", "order");
76    }
77   
 
78  1000 toggle @Override
79    public <T> List<T> execute(Query query) throws QueryException
80    {
81  1000 EntityReference currentEntityReference = this.context.getCurrentEntityReference();
82   
83  1000 Query nativeQuery;
84  1000 try {
85  1000 this.progress.startStep(query, "query.xwql.progress.execute", "Execute XWQL query [{}]", query);
86   
87  1000 if (query.getWiki() != null) {
88  114 if (currentEntityReference.getType() == EntityType.WIKI) {
89  114 this.context.setCurrentEntityReference(new WikiReference(query.getWiki()));
90    } else {
91  0 this.context.setCurrentEntityReference(currentEntityReference.replaceParent(
92    currentEntityReference.extractReference(EntityType.WIKI), new WikiReference(query.getWiki())));
93    }
94    }
95   
96  1000 nativeQuery =
97    getQueryManager().createQuery(this.translator.translate(query.getStatement()),
98    this.translator.getOutputLanguage());
99  1000 nativeQuery.setLimit(query.getLimit());
100  1000 nativeQuery.setOffset(query.getOffset());
101  1000 nativeQuery.setWiki(query.getWiki());
102  1000 if (query.getFilters() != null) {
103  1000 for (QueryFilter filter : query.getFilters()) {
104  630 nativeQuery.addFilter(filter);
105    }
106    }
107  1000 for (Entry<String, Object> e : query.getNamedParameters().entrySet()) {
108  2362 nativeQuery.bindValue(e.getKey(), e.getValue());
109    }
110  1000 for (Entry<Integer, Object> e : query.getPositionalParameters().entrySet()) {
111  0 nativeQuery.bindValue(e.getKey(), e.getValue());
112    }
113   
114  1000 if (nativeQuery instanceof SecureQuery && query instanceof SecureQuery) {
115    // No need to validate the HQL query for short XWQL queries
116  1000 if (((SecureQuery) query).isCurrentAuthorChecked() && !isShortFormStatement(query.getStatement())) {
117  3 ((SecureQuery) nativeQuery).checkCurrentAuthor(true);
118    }
119   
120    // Let HQL module take care of that is supported
121  1000 ((SecureQuery) nativeQuery).checkCurrentUser(((SecureQuery) query).isCurrentUserChecked());
122    }
123   
124  1000 return nativeQuery.execute();
125    } catch (Exception e) {
126  0 if (e instanceof QueryException) {
127  0 throw (QueryException) e;
128    }
129  0 throw new QueryException("Exception while translating [" + query.getStatement() + "] XWQL query to the ["
130    + this.translator.getOutputLanguage() + "] language", query, e);
131    } finally {
132  1000 this.context.setCurrentEntityReference(currentEntityReference);
133   
134  1000 this.progress.endStep(query);
135    }
136    }
137   
 
138  0 toggle public QueryTranslator getTranslator()
139    {
140  0 return this.translator;
141    }
142    }