Search in sources :

Example 1 with WrappingQuery

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;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) WrappingQuery(org.xwiki.query.WrappingQuery)

Example 2 with WrappingQuery

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;
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SQLQuery(org.hibernate.SQLQuery)

Example 3 with WrappingQuery

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'");
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) WrappingQuery(org.xwiki.query.WrappingQuery) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with WrappingQuery

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;
}
Also used : Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) WrappingQuery(org.xwiki.query.WrappingQuery)

Aggregations

SQLQuery (org.hibernate.SQLQuery)4 Query (org.xwiki.query.Query)4 WrappingQuery (org.xwiki.query.WrappingQuery)4 SecureQuery (org.xwiki.query.SecureQuery)3 QueryFilter (org.xwiki.query.QueryFilter)2 Session (org.hibernate.Session)1 NamedSQLQueryDefinition (org.hibernate.engine.NamedSQLQueryDefinition)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 DefaultQuery (org.xwiki.query.internal.DefaultQuery)1