use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.
the class QueryObjectModelFactoryTest method testFullTextSearchWithBindVariableValue.
/**
* Test case for {@link QueryObjectModelFactory#fullTextSearch(String, String, StaticOperand)}
*/
public void testFullTextSearchWithBindVariableValue() throws RepositoryException {
FullTextSearch ftSearch = qf.fullTextSearch(SELECTOR_NAME1, propertyName1, qf.bindVariable(VARIABLE_NAME));
assertEquals("Wrong selector name", SELECTOR_NAME1, ftSearch.getSelectorName());
assertEquals("Wrong propertyName", propertyName1, ftSearch.getPropertyName());
StaticOperand op = ftSearch.getFullTextSearchExpression();
assertNotNull(op);
assertTrue("not a BindVariableValue", op instanceof BindVariableValue);
BindVariableValue value = (BindVariableValue) op;
assertEquals(VARIABLE_NAME, value.getBindVariableName());
}
use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.
the class Parser method parseStaticOperand.
private StaticOperand parseStaticOperand() throws RepositoryException {
if (currentTokenType == PLUS) {
read();
} else if (currentTokenType == MINUS) {
read();
if (currentTokenType != VALUE) {
throw getSyntaxError("number");
}
int valueType = currentValue.getType();
switch(valueType) {
case PropertyType.LONG:
currentValue = valueFactory.createValue(-currentValue.getLong());
break;
case PropertyType.DOUBLE:
currentValue = valueFactory.createValue(-currentValue.getDouble());
break;
case PropertyType.BOOLEAN:
currentValue = valueFactory.createValue(!currentValue.getBoolean());
break;
case PropertyType.DECIMAL:
currentValue = valueFactory.createValue(currentValue.getDecimal().negate());
break;
default:
throw getSyntaxError("Illegal operation: -" + currentValue);
}
}
if (currentTokenType == VALUE) {
Literal literal = getUncastLiteral(currentValue);
read();
return literal;
} else if (currentTokenType == PARAMETER) {
read();
String name = readName();
if (readIf(":")) {
name = name + ":" + readName();
}
BindVariableValue var = bindVariables.get(name);
if (var == null) {
var = factory.bindVariable(name);
bindVariables.put(name, var);
}
return var;
} else if (readIf("TRUE")) {
Literal literal = getUncastLiteral(valueFactory.createValue(true));
return literal;
} else if (readIf("FALSE")) {
Literal literal = getUncastLiteral(valueFactory.createValue(false));
return literal;
} else if (readIf("CAST")) {
read("(");
StaticOperand op = parseStaticOperand();
if (!(op instanceof Literal)) {
throw getSyntaxError("literal");
}
Literal literal = (Literal) op;
Value value = literal.getLiteralValue();
read("AS");
value = parseCastAs(value);
read(")");
// CastLiteral
literal = factory.literal(value);
return literal;
} else {
throw getSyntaxError("static operand");
}
}
use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.
the class LuceneQueryFactory method mapConstraintToQueryAndFilter.
protected Predicate mapConstraintToQueryAndFilter(QueryPair query, Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher, IndexReader reader) throws RepositoryException, IOException {
Predicate filter = Predicate.TRUE;
if (constraint instanceof And) {
And and = (And) constraint;
filter = mapConstraintToQueryAndFilter(query, and.getConstraint1(), selectorMap, searcher, reader);
Predicate other = mapConstraintToQueryAndFilter(query, and.getConstraint2(), selectorMap, searcher, reader);
if (filter == Predicate.TRUE) {
filter = other;
} else if (other != Predicate.TRUE) {
filter = Predicates.and(filter, other);
}
} else if (constraint instanceof Comparison) {
Comparison c = (Comparison) constraint;
Transform transform = new Transform(c.getOperand1());
DynamicOperand left = transform.operand;
final String operator = c.getOperator();
StaticOperand right = c.getOperand2();
if (left instanceof Length || left instanceof FullTextSearchScore || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE.equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
try {
int type = PropertyType.UNDEFINED;
if (left instanceof Length) {
type = PropertyType.LONG;
} else if (left instanceof FullTextSearchScore) {
type = PropertyType.DOUBLE;
}
final DynamicOperand operand = c.getOperand1();
final Value value = evaluator.getValue(right, type);
filter = new RowPredicate() {
@Override
protected boolean evaluate(Row row) throws RepositoryException {
return new ValueComparator().evaluate(operator, evaluator.getValue(operand, row), value);
}
};
} catch (ValueFormatException e) {
throw new InvalidQueryException(e);
}
} else {
Query cq = getComparisonQuery(left, transform.transform, operator, right, selectorMap);
query.subQuery.add(cq, MUST);
}
} else if (constraint instanceof DescendantNode) {
final DescendantNode descendantNode = (DescendantNode) constraint;
Query context = getNodeIdQuery(UUID, descendantNode.getAncestorPath());
query.mainQuery = new DescendantSelfAxisQuery(context, query.subQuery, false);
} else {
query.subQuery.add(create(constraint, selectorMap, searcher), MUST);
}
return filter;
}
use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.
the class LuceneQueryFactory method getFullTextSearchQuery.
protected Query getFullTextSearchQuery(FullTextSearch fts) throws RepositoryException {
String field = FieldNames.FULLTEXT;
String property = fts.getPropertyName();
if (property != null) {
Name name = session.getQName(property);
field = nsMappings.getPrefix(name.getNamespaceURI()) + ":" + FieldNames.FULLTEXT_PREFIX + name.getLocalName();
}
StaticOperand expression = fts.getFullTextSearchExpression();
String query = evaluator.getValue(expression).getString();
try {
QueryParser parser = new JackrabbitQueryParser(field, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
return parser.parse(query);
} catch (ParseException e) {
throw new RepositoryException("Invalid full text search expression: " + query, e);
}
}
use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.
the class LuceneQueryFactory method create.
/**
* Creates a lucene query for the given QOM full text search.
*
* @param fts the full text search constraint.
* @return the lucene query for the given constraint.
* @throws RepositoryException if an error occurs while creating the query.
*/
public Query create(FullTextSearchImpl fts) throws RepositoryException {
String fieldname;
if (fts.getPropertyName() == null) {
// fulltext on node
fieldname = FieldNames.FULLTEXT;
} else {
// final path element is a property name
Name propName = fts.getPropertyQName();
StringBuffer tmp = new StringBuffer();
tmp.append(nsMappings.getPrefix(propName.getNamespaceURI()));
tmp.append(":").append(FieldNames.FULLTEXT_PREFIX);
tmp.append(propName.getLocalName());
fieldname = tmp.toString();
}
QueryParser parser = new JackrabbitQueryParser(fieldname, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
try {
StaticOperand expr = fts.getFullTextSearchExpression();
return parser.parse(evaluator.getValue(expr).getString());
} catch (ParseException e) {
throw new RepositoryException(e);
}
}
Aggregations