
com.squeakysand.jcr.query.QueryRunner Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2010-2011 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squeakysand.jcr.query;
import com.squeakysand.commons.lang.StringUtils;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.InvalidQueryException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryRunner {
private static final Logger LOG = LoggerFactory.getLogger(QueryRunner.class);
public static QueryResult executeQuery(String query, QueryType queryType, Session session) throws InvalidQueryException, RepositoryException {
QueryResult result = null;
if (StringUtils.isNullOrEmpty(query)) {
throw new IllegalArgumentException("query cannot be null or empty");
}
if (queryType == null) {
throw new IllegalArgumentException("queryType cannot be null");
}
if (session == null) {
throw new IllegalArgumentException("session cannot be null");
}
Workspace workspace = session.getWorkspace();
QueryManager queryManager = workspace.getQueryManager();
Query executableQuery = queryManager.createQuery(query, queryType.getLanguage());
LOG.info("executing query: {}", query);
result = executableQuery.execute();
if (LOG.isDebugEnabled()) {
LOG.debug("query result was: {}", result);
}
return result;
}
public static NodeIterator executeNodeQuery(String sqlQuery, Session session) {
NodeIterator result = null;
try {
Workspace workspace = session.getWorkspace();
QueryManager qm = workspace.getQueryManager();
Query query = qm.createQuery(sqlQuery, Query.SQL);
QueryResult queryResult = query.execute();
result = queryResult.getNodes();
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
LOG.info("sql query: {} returned {} nodes", sqlQuery, result.getSize());
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy