Package org.modeshape.jcr.query.parse

Examples of org.modeshape.jcr.query.parse.InvalidQueryException


        } else if (xpath instanceof PathExpression) {
            translate((PathExpression)xpath);
            return builder.query();
        }
        // unexpected component ...
        throw new InvalidQueryException(query,
                                        "Acceptable XPath queries must lead with a path expression or must be a union, intersect or except");
    }
View Full Code Here


                steps = steps.subList(1, steps.size());
            } else if (first instanceof NameTest && steps.size() > 1 && ((NameTest)first).matches("jcr", "root")) {
                // We can actually remove this first step, since relative paths are relative to the root ...
                steps = steps.subList(1, steps.size());
            } else {
                throw new InvalidQueryException(query, "An absolute path expression must start with '//' or '/jcr:root/...'");
            }
        }

        // Walk the steps along the path expression ...
        ConstraintBuilder where = builder.where();
        List<StepExpression> path = new ArrayList<StepExpression>();
        String tableName = null;
        for (StepExpression step : steps) {
            if (step instanceof AxisStep) {
                AxisStep axis = (AxisStep)step;
                NodeTest nodeTest = axis.getNodeTest();
                if (nodeTest instanceof NameTest) {
                    if (appliesToPathConstraint(axis.getPredicates())) {
                        // Everything in this axis/step applies to the path, so add it and we'll translate it below ...
                        path.add(step);
                    } else {
                        // The constraints are more complicated than can be applied to the path ...
                        // if (!nameTest.isWildcard()) {
                        // There is a non-wildcard name test that we still need to add to the path ...
                        path.add(step);
                        // }
                        // We need to define a new source/table ...
                        tableName = translateSource(tableName, path, where);
                        translatePredicates(axis.getPredicates(), tableName, where);
                        path.clear();
                    }
                } else if (nodeTest instanceof ElementTest) {
                    // We need to build a new source with the partial path we have so far ...
                    tableName = translateElementTest((ElementTest)nodeTest, path, where);
                    translatePredicates(axis.getPredicates(), tableName, where);
                    path.clear();
                } else if (nodeTest instanceof AttributeNameTest) {
                    AttributeNameTest attributeName = (AttributeNameTest)nodeTest;
                    builder.select(nameFrom(attributeName.getNameTest()));
                } else if (nodeTest instanceof TextTest) {
                    NameTest nameTest = new NameTest("jcr", "xmltext");
                    List<Component> predicates = axis.getPredicates();
                    if (predicates == null || predicates.isEmpty() || appliesToPathConstraint(predicates)) {
                        AxisStep textStep = new AxisStep(nameTest, axis.getPredicates());
                        path.add(textStep);
                    } else {
                        tableName = translateSource(tableName, path, where);
                        translatePredicates(predicates, tableName, where);
                        path.clear();
                    }
                } else {
                    throw new InvalidQueryException(query, "The '" + step + "' step is not supported");
                }
            } else if (step instanceof FilterStep) {
                FilterStep filter = (FilterStep)step;
                Component primary = filter.getPrimaryExpression();
                List<Component> predicates = filter.getPredicates();
                if (primary instanceof ContextItem) {
                    if (appliesToPathConstraint(predicates)) {
                        // Can ignore the '.' ...
                    } else {
                        // The constraints are more complicated, so we need to define a new source/table ...
                        path.add(step);
                        tableName = translateSource(tableName, path, where);
                        translatePredicates(predicates, tableName, where);
                        path.clear();
                    }
                } else if (primary instanceof Literal) {
                    throw new InvalidQueryException(query,
                                                    "A literal is not supported in the primary path expression; therefore '"
                                                    + primary + "' is not valid");
                } else if (primary instanceof FunctionCall) {
                    throw new InvalidQueryException(query,
                                                    "A function call is not supported in the primary path expression; therefore '"
                                                    + primary + "' is not valid");
                } else if (primary instanceof ParenthesizedExpression) {
                    // This can be used to define an OR-ed set of expressions defining select columns ...
                    ParenthesizedExpression paren = (ParenthesizedExpression)primary;
                    Component wrapped = paren.getWrapped().collapse();
                    if (wrapped instanceof AttributeNameTest) {
                        AttributeNameTest attributeName = (AttributeNameTest)wrapped;
                        builder.select(nameFrom(attributeName.getNameTest()));
                    } else if (wrapped instanceof BinaryComponent) {
                        for (AttributeNameTest attributeName : extractAttributeNames((BinaryComponent)wrapped)) {
                            builder.select(nameFrom(attributeName.getNameTest()));
                        }
                        path.add(filter); // in case any element names are there
                    } else {
                        throw new InvalidQueryException(query,
                                                        "A parenthesized expression of this type is not supported in the primary path expression; therefore '"
                                                        + primary + "' is not valid");
                    }
                }

            } else {
                path.add(step);
            }
        }
        if (steps.isEmpty() || !path.isEmpty()) {
            translateSource(tableName, path, where);
        }
        where.end();

        // Process the order-by clause ...
        OrderBy orderBy = pathExpression.getOrderBy();
        if (orderBy != null) {
            OrderByBuilder orderByBuilder = builder.orderBy();
            for (OrderBySpec spec : orderBy) {
                OrderByOperandBuilder operandBuilder = null;
                switch (spec.getOrder()) {
                    case ASCENDING:
                        operandBuilder = orderByBuilder.ascending();
                        break;
                    case DESCENDING:
                        operandBuilder = orderByBuilder.descending();
                        break;
                }
                assert operandBuilder != null;
                if (spec.getAttributeName() != null) {
                    // This order by is defined by an attribute ...
                    NameTest attribute = spec.getAttributeName();
                    assert !attribute.isWildcard();
                    if (attribute.matches("jcr", "path")) {
                        String pathOf = tableName;
                        if (pathOf == null) pathOf = aliases.iterator().next();
                        operandBuilder.path(pathOf);
                    } else {
                        operandBuilder.propertyValue(tableName, attribute.toString());
                        builder.select(tableName + "." + attribute.toString());
                    }
                } else if (spec.getScoreFunction() != null) {
                    // This order-by is defined by a "jcr:score" function ...
                    FunctionCall scoreFunction = spec.getScoreFunction();
                    assert scoreFunction != null;
                    List<Component> args = scoreFunction.getParameters();
                    String nameOfTableToScore = tableName;
                    if (!args.isEmpty()) {
                        if (args.size() == 1 && args.get(0) instanceof NameTest) {
                            // Just the table name ...
                            NameTest tableNameTest = (NameTest)args.get(0);
                            nameOfTableToScore = tableNameTest.toString();
                        }
                    }
                    operandBuilder.fullTextSearchScore(nameOfTableToScore);
                } else {
                    PathExpression axes = spec.getPath();
                    List<StepExpression> axesSteps = axes.getSteps();

                    if (axesSteps.size() != 2) {
                        throw new InvalidQueryException("Only child axis supported in ORDER BY clause");
                    }

                    NameTest childNode = (NameTest) ((AxisStep)axesSteps.get(0)).getNodeTest();
                    String alias = childNode.getLocalTest();
View Full Code Here

            }
        } else {
            failed = true;
        }
        if (failed) {
            throw new InvalidQueryException(query,
                                            "A parenthesized expression in a path step may only contain ORed and ANDed attribute names or element names; therefore '"
                                            + binary + "' is not valid");
        }
        return results;
    }
View Full Code Here

            }
        } else {
            failed = true;
        }
        if (failed) {
            throw new InvalidQueryException(query,
                                            "A parenthesized expression in a path step may only contain ORed element names; therefore '"
                                            + binary + "' is not valid");
        }
        return results;
    }
View Full Code Here

        if (typeName.isWildcard()) {
            tableName = newAlias();
            builder.fromAllNodesAs(tableName);
        } else {
            if (typeName.getLocalTest() == null) {
                throw new InvalidQueryException(
                                                query,
                                                "The '"
                                                + elementTest
                                                + "' clause uses a partial wildcard in the type name, but only a wildcard on the whole name is supported");
            }
View Full Code Here

                    propertyName = nameFrom(attribute.getNameTest());
                } else if (nodeTest instanceof NameTest) {
                    NameTest nameTest = (NameTest)left;
                    propertyName = nameFrom(nameTest);
                } else {
                    throw new InvalidQueryException(query,
                                                    "Left hand side of a comparison must be a name test or attribute name test; therefore '"
                                                    + comparison + "' is not valid");
                }
                if (right instanceof Literal) {
                    String value = ((Literal)right).getValue();
                    where.propertyValue(tableName, propertyName).is(operator, value);
                } else if (right instanceof FunctionCall) {
                    FunctionCall call = (FunctionCall)right;
                    NameTest functionName = call.getName();
                    List<Component> parameters = call.getParameters();
                    // Is this a cast ...
                    String castType = CAST_FUNCTION_NAME_TO_TYPE.get(functionName);
                    if (castType != null) {
                        if (parameters.size() == 1 && parameters.get(0).collapse() instanceof Literal) {
                            // The first parameter can be the type name (or table name) ...
                            Literal value = (Literal)parameters.get(0).collapse();
                            where.propertyValue(tableName, propertyName).is(operator).cast(value.getValue()).as(castType);
                        } else {
                            throw new InvalidQueryException(query, "A cast function requires one literal parameter; therefore '"
                                                                   + comparison + "' is not valid");
                        }
                    } else {
                        throw new InvalidQueryException(query,
                                                        "Only the 'jcr:score' function is allowed in a comparison predicate; therefore '"
                                                        + comparison + "' is not valid");
                    }
                }
            } else if (left instanceof FunctionCall && right instanceof Literal) {
                FunctionCall call = (FunctionCall)left;
                NameTest functionName = call.getName();
                List<Component> parameters = call.getParameters();
                String value = ((Literal)right).getValue();
                if (functionName.matches("jcr", "score")) {
                    String scoreTableName = tableName;
                    if (parameters.isEmpty()) {
                        scoreTableName = tableName;
                    } else if (parameters.size() == 1 && parameters.get(0) instanceof NameTest) {
                        // The first parameter can be the type name (or table name) ...
                        NameTest name = (NameTest)parameters.get(0);
                        if (!name.isWildcard()) scoreTableName = nameFrom(name);
                    } else {
                        throw new InvalidQueryException(query,
                                                        "The 'jcr:score' function may have no parameters or the type name as the only parameter.");

                    }
                    where.fullTextSearchScore(scoreTableName).is(operator, value);
                } else if (functionName.matches("fn", "name")) {
                    where.nodeName(tableName);
                } else {
                    throw new InvalidQueryException(query,
                                                    "Only the 'jcr:score' function is allowed in a comparison predicate; therefore '"
                                                    + comparison + "' is not valid");
                }
            }
        } else if (predicate instanceof FunctionCall) {
            FunctionCall call = (FunctionCall)predicate;
            NameTest functionName = call.getName();
            List<Component> parameters = call.getParameters();
            Component param1 = parameters.size() > 0 ? parameters.get(0) : null;
            Component param2 = parameters.size() > 1 ? parameters.get(1) : null;
            if (functionName.matches(null, "not")) {
                if (parameters.size() != 1) {
                    throw new InvalidQueryException(query, "The 'not' function requires one parameter; therefore '" + predicate
                                                           + "' is not valid");
                }
                where = where.not().openParen();
                translatePredicate(param1, tableName, where);
                where.closeParen();
            } else if (functionName.matches("jcr", "like")) {
                if (parameters.size() != 2) {
                    throw new InvalidQueryException(query, "The 'jcr:like' function requires two parameters; therefore '"
                                                           + predicate + "' is not valid");
                }
                if (!(param2 instanceof Literal)) {
                    throw new InvalidQueryException(query, "The second parameter of 'jcr:like' must be a literal; therefore '"
                                                           + predicate + "' is not valid");
                }
                String value = ((Literal)param2).getValue();

                if (param1 instanceof FunctionCall) {
                    where.nodeName(tableName).isLike(value);
                } else if (param1 instanceof AttributeNameTest) {
                    NameTest attributeName = ((AttributeNameTest)param1).getNameTest();
                    where.propertyValue(tableName, nameFrom(attributeName)).isLike(value);
                }
            } else if (functionName.matches("jcr", "contains")) {
                if (parameters.size() != 2) {
                    throw new InvalidQueryException(query, "The 'jcr:contains' function requires two parameters; therefore '"
                                                           + predicate + "' is not valid");
                }
                if (!(param2 instanceof Literal)) {
                    throw new InvalidQueryException(query,
                                                    "The second parameter of 'jcr:contains' must be a literal; therefore '"
                                                    + predicate + "' is not valid");
                }
                String value = ((Literal)param2).getValue();
                if (param1 instanceof ContextItem) {
                    // refers to the current node (or table) ...
                    where.search(tableName, value);
                } else if (param1 instanceof AttributeNameTest) {
                    // refers to an attribute on the current node (or table) ...
                    NameTest attributeName = ((AttributeNameTest)param1).getNameTest();
                    where.search(tableName, nameFrom(attributeName), value);
                } else if (param1 instanceof NameTest) {
                    // refers to child node, so we need to add a join ...
                    String alias = newAlias();
                    builder.joinAllNodesAs(alias).onChildNode(tableName, alias);
                    // Now add the criteria ...
                    where.search(alias, value);
                    tableName = alias;
                } else if (param1 instanceof PathExpression) {
                    // refers to a descendant node ...
                    PathExpression pathExpr = (PathExpression)param1;
                    if (pathExpr.getLastStep().collapse() instanceof AttributeNameTest) {
                        AttributeNameTest attributeName = (AttributeNameTest)pathExpr.getLastStep().collapse();
                        pathExpr = pathExpr.withoutLast();
                        String searchTable = translatePredicate(pathExpr, tableName, where);
                        if (attributeName.getNameTest().isWildcard()) {
                            where.search(searchTable, value);
                        } else {
                            where.search(searchTable, nameFrom(attributeName.getNameTest()), value);
                        }
                    } else {
                        String searchTable = translatePredicate(param1, tableName, where);
                        where.search(searchTable, value);
                    }
                } else {
                    throw new InvalidQueryException(query,
                                                    "The first parameter of 'jcr:contains' must be a relative path (e.g., '.', an attribute name, a child name, etc.); therefore '"
                                                    + predicate + "' is not valid");
                }
            } else if (functionName.matches("jcr", "deref")) {
                throw new InvalidQueryException(query,
                                                "The 'jcr:deref' function is not required by JCR and is not currently supported; therefore '"
                                                + predicate + "' is not valid");
            } else {
                throw new InvalidQueryException(query,
                                                "Only the 'jcr:like' and 'jcr:contains' functions are allowed in a predicate; therefore '"
                                                + predicate + "' is not valid");
            }
        } else if (predicate instanceof PathExpression) {
            // Requires that the descendant node with the relative path does exist ...
            PathExpression pathExpr = (PathExpression)predicate;
            List<StepExpression> steps = pathExpr.getSteps();
            OrderBy orderBy = pathExpr.getOrderBy();
            assert steps.size() > 1; // 1 or 0 would have been collapsed ...
            Component firstStep = steps.get(0).collapse();
            if (firstStep instanceof ContextItem) {
                // Remove the context and retry ...
                return translatePredicate(new PathExpression(true, steps.subList(1, steps.size()), orderBy), tableName, where);
            }
            if (firstStep instanceof NameTest) {
                // Special case where this is similar to '[a/@id]'
                NameTest childName = (NameTest)firstStep;
                String alias = newAlias();
                builder.joinAllNodesAs(alias).onChildNode(tableName, alias);
                if (!childName.isWildcard()) {
                    where.nodeName(alias).isEqualTo(nameFrom(childName));
                }
                return translatePredicate(new PathExpression(true, steps.subList(1, steps.size()), orderBy), alias, where);
            }
            if (firstStep instanceof DescendantOrSelf) {
                // Special case where this is similar to '[a/@id]'
                String alias = newAlias();
                builder.joinAllNodesAs(alias).onDescendant(tableName, alias);
                return translatePredicate(new PathExpression(true, steps.subList(1, steps.size()), orderBy), alias, where);
            }
            // Add the join ...
            String alias = newAlias();
            builder.joinAllNodesAs(alias).onDescendant(tableName, alias);
            // Now add the criteria ...
            translatePathExpressionConstraint(pathExpr, where, alias);
        } else {
            throw new InvalidQueryException(query, "Unsupported criteria '" + predicate + "'");
        }
        return tableName;
    }
View Full Code Here

                                    builder = new DualPathLikeBuilder(orig.clone().append('/').append(nameFrom(name)), builder);
                                }
                            }
                        }
                    } else {
                        throw new InvalidQueryException(query,
                                                        "A parenthesized expression of this type is not supported in the primary path expression; therefore '"
                                                        + primary + "' is not valid");
                    }
                }
            }
View Full Code Here

                        String sourceColumnName = column.getPropertyName(); // getColumnName() returns alias
                        Column sourceColumn = source.getColumn(sourceColumnName);
                        if (sourceColumn == null) {
                            sourceColumn = source.getColumn(column.getColumnName());
                            if (sourceColumn == null) {
                                throw new InvalidQueryException(Visitors.readable(command),
                                                                "The view references a non-existant column '"
                                                                + column.getColumnName() + "' in '" + source.getName() + "'");
                            }
                        }
                        Set<Operator> operators = operators(name, viewColumnName, sourceColumn.getOperators());
                        boolean orderable = orderable(name, viewColumnName, sourceColumn.isOrderable());
                        Column newColumn = new ImmutableColumn(viewColumnName, sourceColumn.getPropertyTypeName(),
                                                               sourceColumn.getRequiredType(),
                                                               sourceColumn.isFullTextSearchable(), orderable,
                                                               sourceColumn.getMinimum(), sourceColumn.getMaximum(), operators);
                        viewColumns.add(newColumn);
                        if (source.getSelectAllColumnsByName().containsKey(sourceColumnName)) {
                            viewColumnsInSelectStar.add(newColumn);
                        }
                        columnNames.add(newColumn.getName());
                    }
                    // if (viewColumns.size() != columns.size()) {
                    // // We weren't able to resolve all of the columns,
                    // // so maybe the columns were referencing yet-to-be-built views ...
                    // continue;
                    // }

                    // If we could resolve the definition ...
                    Map<String, Column> viewColumnsByName = new HashMap<String, Column>();
                    Map<String, Column> viewSelectStarColumnsByName = new HashMap<String, Column>();
                    for (Column column : viewColumns) {
                        viewColumnsByName.put(column.getName(), column);
                    }
                    for (Column column : viewColumnsInSelectStar) {
                        viewSelectStarColumnsByName.put(column.getName(), column);
                    }
                    Set<Key> keys = Collections.emptySet();
                    boolean hasExtraColumns = tablesOrViewsWithExtraColumns.contains(name);
                    ImmutableView view = new ImmutableView(name, viewColumnsByName, viewColumns, hasExtraColumns, command, keys,
                                                           viewSelectStarColumnsByName, viewColumnsInSelectStar);
                    definitions.remove(name);

                    tablesByName.put(view.getName(), view);
                    schemata = new ImmutableSchemata(tablesByName);
                    added = true;
                }
            } while (added && !definitions.isEmpty());

            if (!definitions.isEmpty()) {
                QueryCommand command = definitions.values().iterator().next();
                throw new InvalidQueryException(Visitors.readable(command), "The view definition cannot be resolved: "
                                                                            + Visitors.readable(command));
            }

            return schemata;
        }
View Full Code Here

TOP

Related Classes of org.modeshape.jcr.query.parse.InvalidQueryException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.