use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.
the class LuceneQueryFactory method getNodeNameQuery.
protected Query getNodeNameQuery(int transform, String operator, StaticOperand right) throws RepositoryException {
if (transform != TRANSFORM_NONE || !JCR_OPERATOR_EQUAL_TO.equals(operator)) {
throw new UnsupportedRepositoryOperationException();
}
Value value = evaluator.getValue(right);
int type = value.getType();
String string = value.getString();
if (type == PropertyType.URI && string.startsWith("./")) {
string = string.substring("./".length());
} else if (type == PropertyType.DOUBLE || type == PropertyType.DECIMAL || type == PropertyType.LONG || type == PropertyType.BOOLEAN || type == PropertyType.REFERENCE || type == PropertyType.WEAKREFERENCE) {
throw new InvalidQueryException("Invalid name value: " + string);
}
try {
Name name = session.getQName(string);
Term uri = new Term(NAMESPACE_URI, name.getNamespaceURI());
Term local = new Term(LOCAL_NAME, name.getLocalName());
BooleanQuery query = new BooleanQuery();
query.add(new JackrabbitTermQuery(uri), MUST);
query.add(new JackrabbitTermQuery(local), MUST);
return query;
} catch (IllegalNameException e) {
throw new InvalidQueryException("Illegal name: " + string, e);
}
}
use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.
the class LuceneQueryBuilder method visit.
public Object visit(PathQueryNode node, Object data) throws RepositoryException {
Query context = null;
LocationStepQueryNode[] steps = node.getPathSteps();
if (steps.length > 0) {
if (node.isAbsolute() && !steps[0].getIncludeDescendants()) {
// eat up first step
Name nameTest = steps[0].getNameTest();
if (nameTest == null) {
// this is equivalent to the root node
context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
} else if (nameTest.getLocalName().length() == 0) {
// root node
context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
} else {
// then this is a node != the root node
// will never match anything!
BooleanQuery and = new BooleanQuery();
and.add(new JackrabbitTermQuery(new Term(FieldNames.PARENT, "")), Occur.MUST);
and.add(new NameQuery(nameTest, indexFormatVersion, nsMappings), Occur.MUST);
context = and;
}
// apply predicates
Object[] predicates = steps[0].acceptOperands(this, context);
BooleanQuery andQuery = new BooleanQuery();
for (Object predicate : predicates) {
andQuery.add((Query) predicate, Occur.MUST);
}
if (andQuery.clauses().size() > 0) {
andQuery.add(context, Occur.MUST);
context = andQuery;
}
LocationStepQueryNode[] tmp = new LocationStepQueryNode[steps.length - 1];
System.arraycopy(steps, 1, tmp, 0, steps.length - 1);
steps = tmp;
} else {
// path is 1) relative or 2) descendant-or-self
// use root node as context
context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
}
} else {
exceptions.add(new InvalidQueryException("Number of location steps must be > 0"));
}
// loop over steps
for (LocationStepQueryNode step : steps) {
context = (Query) step.accept(this, context);
}
if (data instanceof BooleanQuery) {
BooleanQuery constraint = (BooleanQuery) data;
if (constraint.getClauses().length > 0) {
constraint.add(context, Occur.MUST);
context = constraint;
}
}
return context;
}
use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.
the class QueryManagerImpl method getQuery.
/**
* @see QueryManager#getQuery(Node)
*/
public Query getQuery(Node node) throws InvalidQueryException, RepositoryException {
checkIsAlive();
NamePathResolver resolver = mgrProvider.getNamePathResolver();
if (!node.isNodeType(resolver.getJCRName(NameConstants.NT_QUERY))) {
throw new InvalidQueryException("Node is not of type nt:query");
}
if (node.getSession() != session) {
throw new InvalidQueryException("Node belongs to a different session.");
}
String statement = node.getProperty(resolver.getJCRName(NameConstants.JCR_STATEMENT)).getString();
String language = node.getProperty(resolver.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
if (Query.JCR_JQOM.equals(language)) {
QueryObjectModelFactory qomFactory = new QOMFactory(node, resolver);
QueryObjectModelBuilder builder = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language);
return builder.createQueryObjectModel(statement, qomFactory, valueFactory);
} else {
return new QueryImpl(session, mgrProvider, itemMgr, wspManager, statement, language, node);
}
}
use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.
the class JCRSQLQueryBuilder method createQuery.
/**
* Creates a <code>QueryNode</code> tree from a SQL <code>statement</code>
* using the passed query node <code>factory</code>.
*
* @param statement the SQL statement.
* @param resolver the namespace resolver to use.
* @return the <code>QueryNode</code> tree.
* @throws InvalidQueryException if <code>statement</code> is malformed.
*/
public static QueryRootNode createQuery(String statement, NameResolver resolver, QueryNodeFactory factory) throws InvalidQueryException {
try {
// get parser
JCRSQLParser parser;
synchronized (parsers) {
parser = (JCRSQLParser) parsers.get(resolver);
if (parser == null) {
parser = new JCRSQLParser(new StringReader(statement));
parser.setNameResolver(resolver);
parsers.put(resolver, parser);
}
}
JCRSQLQueryBuilder builder;
// guard against concurrent use within same session
synchronized (parser) {
parser.ReInit(new StringReader(statement));
builder = new JCRSQLQueryBuilder(parser.Query(), resolver, factory);
}
return builder.getRootNode();
} catch (ParseException e) {
throw new InvalidQueryException(e.getMessage());
} catch (IllegalArgumentException e) {
throw new InvalidQueryException(e.getMessage());
} catch (Throwable t) {
// javacc parser may also throw an error in some cases
throw new InvalidQueryException(t.getMessage());
}
}
use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.
the class QueryFormat method visit.
public Object visit(RelationQueryNode node, Object data) throws RepositoryException {
StringBuffer sb = (StringBuffer) data;
try {
StringBuffer propName = new StringBuffer();
PathQueryNode relPath = node.getRelativePath();
if (relPath == null) {
propName.append(".");
} else if (relPath.getPathSteps().length > 1) {
exceptions.add(new InvalidQueryException("Child axis not supported in SQL"));
return data;
} else {
visit(relPath, data);
}
// surround name with property function
node.acceptOperands(this, propName);
if (node.getOperation() == OPERATION_EQ_VALUE || node.getOperation() == OPERATION_EQ_GENERAL) {
sb.append(propName);
sb.append(" = ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_GE_VALUE || node.getOperation() == OPERATION_GE_GENERAL) {
sb.append(propName);
sb.append(" >= ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_GT_VALUE || node.getOperation() == OPERATION_GT_GENERAL) {
sb.append(propName);
sb.append(" > ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_LE_VALUE || node.getOperation() == OPERATION_LE_GENERAL) {
sb.append(propName);
sb.append(" <= ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_LIKE) {
sb.append(propName);
sb.append(" LIKE ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_LT_VALUE || node.getOperation() == OPERATION_LT_GENERAL) {
sb.append(propName);
sb.append(" < ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_NE_VALUE || node.getOperation() == OPERATION_NE_GENERAL) {
sb.append(propName);
sb.append(" <> ");
appendValue(node, sb);
} else if (node.getOperation() == OPERATION_NULL) {
sb.append(propName);
sb.append(" IS NULL");
} else if (node.getOperation() == OPERATION_NOT_NULL) {
sb.append(propName);
sb.append(" IS NOT NULL");
} else if (node.getOperation() == OPERATION_SIMILAR) {
sb.append("SIMILAR(");
sb.append(propName);
sb.append(", ");
appendValue(node, sb);
sb.append(")");
} else if (node.getOperation() == OPERATION_SPELLCHECK) {
sb.append("SPELLCHECK(");
appendValue(node, sb);
sb.append(")");
} else {
exceptions.add(new InvalidQueryException("Invalid operation: " + node.getOperation()));
}
if (node.getOperation() == OPERATION_LIKE && node.getStringValue().indexOf('\\') > -1) {
sb.append(" ESCAPE '\\'");
}
} catch (NamespaceException e) {
exceptions.add(e);
}
return sb;
}
Aggregations