use of org.xwiki.query.WrappingQuery in project xwiki-platform by xwiki.
the class HqlQueryExecutor method filterQuery.
private Query filterQuery(Query query, String language) {
Query filteredQuery = query;
// If there are Query parameters of type QueryParameter then, for convenience, automatically add the
// "escapeLikeParameters" filter (if not already there)
addEscapeLikeParametersFilter(query);
if (query.getFilters() != null && !query.getFilters().isEmpty()) {
for (QueryFilter filter : query.getFilters()) {
// Step 1: For backward-compatibility reasons call #filterStatement() first
String filteredStatement = filter.filterStatement(filteredQuery.getStatement(), language);
// statement.
if (!filteredStatement.equals(filteredQuery.getStatement())) {
filteredQuery = new WrappingQuery(filteredQuery) {
@Override
public String getStatement() {
return filteredStatement;
}
};
}
// Step 2: Run #filterQuery()
filteredQuery = filter.filterQuery(filteredQuery);
}
}
return filteredQuery;
}
use of org.xwiki.query.WrappingQuery in project xwiki-platform by xwiki.
the class HqlQueryExecutor method createNamedHibernateQuery.
private org.hibernate.Query createNamedHibernateQuery(Session session, Query query) {
org.hibernate.Query hQuery = session.getNamedQuery(query.getStatement());
Query filteredQuery = query;
if (filteredQuery.getFilters() != null && !filteredQuery.getFilters().isEmpty()) {
// Since we can't modify the Hibernate query statement at this point we need to create a new one to apply
// the query filter. This comes with a performance cost, we could fix it by handling named queries ourselves
// and not delegate them to Hibernate. This way we would always get a statement that we can transform before
// the execution.
boolean isNative = hQuery instanceof SQLQuery;
String language = isNative ? "sql" : Query.HQL;
final String statement = hQuery.getQueryString();
// Run filters
filteredQuery = filterQuery(new WrappingQuery(filteredQuery) {
@Override
public String getStatement() {
return statement;
}
}, language);
if (isNative) {
hQuery = session.createSQLQuery(filteredQuery.getStatement());
// Copy the information about the return column types, if possible.
NamedSQLQueryDefinition definition = (NamedSQLQueryDefinition) this.sessionFactory.getConfiguration().getNamedSQLQueries().get(query.getStatement());
if (!StringUtils.isEmpty(definition.getResultSetRef())) {
((SQLQuery) hQuery).setResultSetMapping(definition.getResultSetRef());
}
} else {
hQuery = session.createQuery(filteredQuery.getStatement());
}
}
populateParameters(hQuery, filteredQuery);
return hQuery;
}
use of org.xwiki.query.WrappingQuery in project xwiki-platform by xwiki.
the class HqlQueryExecutorTest method createHibernateQueryWhenFilter.
@Test
public void createHibernateQueryWhenFilter() throws Exception {
Session session = mock(Session.class);
DefaultQuery query = new DefaultQuery("where doc.space='Main'", Query.HQL, this.executor);
// Add a Query Filter to verify it's called and can change the statement.
// We also verify that QueryFilter#filterStatement() is called before QueryFilter#filterQuery()
QueryFilter filter = mock(QueryFilter.class);
query.addFilter(filter);
when(filter.filterStatement("select doc.fullName from XWikiDocument doc where doc.space='Main'", Query.HQL)).thenReturn("select doc.fullName from XWikiDocument doc where doc.space='Main2'");
when(filter.filterQuery(any(Query.class))).thenReturn(new WrappingQuery(query) {
@Override
public String getStatement() {
return "select doc.fullName from XWikiDocument doc where doc.space='Main3'";
}
});
this.executor.createHibernateQuery(session, query);
// The test is here!
verify(session).createQuery("select doc.fullName from XWikiDocument doc where doc.space='Main3'");
}
use of org.xwiki.query.WrappingQuery in project xwiki-platform by xwiki.
the class HqlQueryExecutor method createHibernateQuery.
protected org.hibernate.Query createHibernateQuery(Session session, Query query) {
org.hibernate.Query hquery;
Query filteredQuery = query;
if (!filteredQuery.isNamed()) {
// For non-named queries, convert the short form into long form before we apply the filters.
filteredQuery = new WrappingQuery(filteredQuery) {
@Override
public String getStatement() {
// handle short queries
return completeShortFormStatement(getWrappedQuery().getStatement());
}
};
filteredQuery = filterQuery(filteredQuery, Query.HQL);
hquery = session.createQuery(filteredQuery.getStatement());
populateParameters(hquery, filteredQuery);
} else {
hquery = createNamedHibernateQuery(session, filteredQuery);
}
return hquery;
}
Aggregations