use of org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl in project jackrabbit-oak by apache.
the class SQL2Parser method parse.
/**
* Parse the statement and return the query.
*
* @param query the query string
* @param initialise if performing the query init ({@code true}) or not ({@code false})
* @return the query
* @throws ParseException if parsing fails
*/
public Query parse(final String query, final boolean initialise) throws ParseException {
// TODO possibly support union,... as available at
// http://docs.jboss.org/modeshape/latest/manuals/reference/html/jcr-query-and-search.html
initialize(query);
selectors.clear();
expected = new ArrayList<String>();
bindVariables = new HashMap<String, BindVariableValueImpl>();
read();
boolean explain = false, measure = false;
if (readIf("EXPLAIN")) {
explain = true;
}
if (readIf("MEASURE")) {
measure = true;
}
Query q = parseSelect();
while (true) {
if (!readIf("UNION")) {
break;
}
boolean unionAll = readIf("ALL");
QueryImpl q2 = parseSelect();
q = new UnionQueryImpl(unionAll, q, q2, settings);
}
OrderingImpl[] orderings = null;
if (readIf("ORDER")) {
read("BY");
orderings = parseOrder();
}
QueryOptions options = new QueryOptions();
if (readIf("OPTION")) {
read("(");
if (readIf("TRAVERSAL")) {
String n = readName().toUpperCase(Locale.ENGLISH);
options.traversal = Traversal.valueOf(n);
}
read(")");
}
if (!currentToken.isEmpty()) {
throw getSyntaxError("<end>");
}
q.setOrderings(orderings);
q.setExplain(explain);
q.setMeasure(measure);
q.setInternal(isInternal(query));
q.setQueryOptions(options);
if (initialise) {
try {
q.init();
} catch (Exception e) {
ParseException e2 = new ParseException(statement + ": " + e.getMessage(), 0);
e2.initCause(e);
throw e2;
}
}
return q;
}
use of org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl in project jackrabbit-oak by apache.
the class QueryImpl method init.
@Override
public void init() {
final QueryImpl query = this;
if (constraint != null) {
// need to do this *before* the visitation below, as the
// simplify() method does not always keep the query reference
// passed in setQuery(). TODO: avoid that mutability concern
constraint = constraint.simplify();
}
new AstVisitorBase() {
@Override
public boolean visit(BindVariableValueImpl node) {
node.setQuery(query);
bindVariableMap.put(node.getBindVariableName(), null);
return true;
}
@Override
public boolean visit(ChildNodeImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(ChildNodeJoinConditionImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(ColumnImpl node) {
node.setQuery(query);
return true;
}
@Override
public boolean visit(DescendantNodeImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(DescendantNodeJoinConditionImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(EquiJoinConditionImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(FullTextSearchImpl node) {
node.setQuery(query);
node.bindSelector(source);
return super.visit(node);
}
@Override
public boolean visit(NativeFunctionImpl node) {
node.setQuery(query);
node.bindSelector(source);
return super.visit(node);
}
@Override
public boolean visit(SimilarImpl node) {
node.setQuery(query);
node.bindSelector(source);
return super.visit(node);
}
@Override
public boolean visit(SpellcheckImpl node) {
node.setQuery(query);
node.bindSelector(source);
return super.visit(node);
}
@Override
public boolean visit(SuggestImpl node) {
node.setQuery(query);
node.bindSelector(source);
return super.visit(node);
}
@Override
public boolean visit(FullTextSearchScoreImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(LiteralImpl node) {
node.setQuery(query);
return true;
}
@Override
public boolean visit(NodeLocalNameImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(NodeNameImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(PropertyExistenceImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(PropertyInexistenceImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(PropertyValueImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(SameNodeImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(SameNodeJoinConditionImpl node) {
node.setQuery(query);
node.bindSelector(source);
return true;
}
@Override
public boolean visit(SelectorImpl node) {
String name = node.getSelectorName();
if (selectorIndexes.put(name, selectors.size()) != null) {
throw new IllegalArgumentException("Two selectors with the same name: " + name);
}
selectors.add(node);
node.setQuery(query);
return true;
}
@Override
public boolean visit(LengthImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(UpperCaseImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(LowerCaseImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(ComparisonImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(InImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(AndImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(OrImpl node) {
node.setQuery(query);
return super.visit(node);
}
@Override
public boolean visit(NotImpl node) {
node.setQuery(query);
return super.visit(node);
}
}.visit(this);
source.setQueryConstraint(constraint);
for (ColumnImpl column : columns) {
column.bindSelector(source);
}
distinctColumns = new boolean[columns.length];
for (int i = 0; i < columns.length; i++) {
ColumnImpl c = columns[i];
boolean distinct = true;
if (JCR_SCORE.equals(c.getPropertyName())) {
distinct = false;
}
distinctColumns[i] = distinct;
}
init = true;
}
use of org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl in project jackrabbit-oak by apache.
the class SQL2Parser method parseStaticOperand.
private StaticOperandImpl parseStaticOperand() throws ParseException {
if (currentTokenType == PLUS) {
read();
if (currentTokenType != VALUE) {
throw getSyntaxError("number");
}
int valueType = currentValue.getType().tag();
switch(valueType) {
case PropertyType.LONG:
currentValue = PropertyValues.newLong(currentValue.getValue(Type.LONG));
break;
case PropertyType.DOUBLE:
currentValue = PropertyValues.newDouble(currentValue.getValue(Type.DOUBLE));
break;
case PropertyType.DECIMAL:
currentValue = PropertyValues.newDecimal(currentValue.getValue(Type.DECIMAL).negate());
break;
default:
throw getSyntaxError("Illegal operation: + " + currentValue);
}
} else if (currentTokenType == MINUS) {
read();
if (currentTokenType != VALUE) {
throw getSyntaxError("number");
}
int valueType = currentValue.getType().tag();
switch(valueType) {
case PropertyType.LONG:
currentValue = PropertyValues.newLong(-currentValue.getValue(Type.LONG));
break;
case PropertyType.DOUBLE:
currentValue = PropertyValues.newDouble(-currentValue.getValue(Type.DOUBLE));
break;
case PropertyType.BOOLEAN:
currentValue = PropertyValues.newBoolean(!currentValue.getValue(Type.BOOLEAN));
break;
case PropertyType.DECIMAL:
currentValue = PropertyValues.newDecimal(currentValue.getValue(Type.DECIMAL).negate());
break;
default:
throw getSyntaxError("Illegal operation: -" + currentValue);
}
}
if (currentTokenType == VALUE) {
LiteralImpl literal = getUncastLiteral(currentValue);
read();
return literal;
} else if (currentTokenType == PARAMETER) {
read();
String name = readName();
if (readIf(":")) {
name = name + ':' + readName();
}
BindVariableValueImpl var = bindVariables.get(name);
if (var == null) {
var = factory.bindVariable(name);
bindVariables.put(name, var);
}
return var;
} else if (readIf("TRUE")) {
LiteralImpl literal = getUncastLiteral(PropertyValues.newBoolean(true));
return literal;
} else if (readIf("FALSE")) {
LiteralImpl literal = getUncastLiteral(PropertyValues.newBoolean(false));
return literal;
} else if (readIf("CAST")) {
read("(");
StaticOperandImpl op = parseStaticOperand();
if (!(op instanceof LiteralImpl)) {
throw getSyntaxError("literal");
}
LiteralImpl literal = (LiteralImpl) op;
PropertyValue value = literal.getLiteralValue();
read("AS");
value = parseCastAs(value);
read(")");
// CastLiteral
literal = factory.literal(value);
return literal;
} else {
if (supportSQL1) {
if (readIf("TIMESTAMP")) {
StaticOperandImpl op = parseStaticOperand();
if (!(op instanceof LiteralImpl)) {
throw getSyntaxError("literal");
}
LiteralImpl literal = (LiteralImpl) op;
PropertyValue value = literal.getLiteralValue();
value = PropertyValues.newDate(value.getValue(Type.DATE));
literal = factory.literal(value);
return literal;
}
}
throw getSyntaxError("static operand");
}
}
Aggregations