jcrsqlquerybuilder.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 947 行 · 第 1/3 页

JAVA
947 行
字号
                    value[0] = node;                    return data;                }                public Object visit(ASTLowerFunction node, Object data) {                    getIdentifier(node);                    return data;                }                public Object visit(ASTUpperFunction node, Object data) {                    getIdentifier(node);                    return data;                }                private void getIdentifier(SimpleNode node) {                    if (node.jjtGetNumChildren() > 0) {                        Node n = node.jjtGetChild(0);                        if (n instanceof ASTIdentifier) {                            ASTIdentifier identifier = (ASTIdentifier) n;                            if (tmp[0] == null) {                                tmp[0] = identifier.getName();                            } else if (tmp[1] == null) {                                tmp[1] = identifier.getName();                            }                        }                    }                }            }, data);            QName identifier = tmp[0];            if (identifier.equals(QName.JCR_PATH)) {                if (tmp[1] != null) {                    // simply ignore, this is a join of a mixin node type                } else {                    createPathQuery(value[0].getValue(), parent.getType());                }                // done                return data;            }            if (type == QueryConstants.OPERATION_BETWEEN) {                AndQueryNode between = new AndQueryNode(parent);                RelationQueryNode rel = createRelationQueryNode(between,                        identifier, QueryConstants.OPERATION_GE_GENERAL, (ASTLiteral) node.children[1]);                node.childrenAccept(this, rel);                between.addOperand(rel);                rel = createRelationQueryNode(between,                        identifier, QueryConstants.OPERATION_LE_GENERAL, (ASTLiteral) node.children[2]);                node.childrenAccept(this, rel);                between.addOperand(rel);                predicateNode = between;            } else if (type == QueryConstants.OPERATION_GE_GENERAL                    || type == QueryConstants.OPERATION_GT_GENERAL                    || type == QueryConstants.OPERATION_LE_GENERAL                    || type == QueryConstants.OPERATION_LT_GENERAL                    || type == QueryConstants.OPERATION_NE_GENERAL                    || type == QueryConstants.OPERATION_EQ_GENERAL) {                predicateNode = createRelationQueryNode(parent,                        identifier, type, value[0]);                node.childrenAccept(this, predicateNode);            } else if (type == QueryConstants.OPERATION_LIKE) {                ASTLiteral pattern = value[0];                if (node.getEscapeString() != null) {                    if (node.getEscapeString().length() == 1) {                        // backslash is the escape character we use internally                        pattern.setValue(translateEscaping(pattern.getValue(), node.getEscapeString().charAt(0), '\\'));                    } else {                        throw new IllegalArgumentException("ESCAPE string value must have length 1: '" + node.getEscapeString() + "'");                    }                } else {                    // no escape character specified.                    // if the pattern contains any backslash characters we need                    // to escape them.                    pattern.setValue(pattern.getValue().replaceAll("\\\\", "\\\\\\\\"));                }                predicateNode = createRelationQueryNode(parent,                        identifier, type, pattern);                node.childrenAccept(this, predicateNode);            } else if (type == QueryConstants.OPERATION_IN) {                OrQueryNode in = new OrQueryNode(parent);                for (int i = 1; i < node.children.length; i++) {                    RelationQueryNode rel = createRelationQueryNode(in,                            identifier, QueryConstants.OPERATION_EQ_VALUE, (ASTLiteral) node.children[i]);                    node.childrenAccept(this, rel);                    in.addOperand(rel);                }                predicateNode = in;            } else if (type == QueryConstants.OPERATION_NULL                    || type == QueryConstants.OPERATION_NOT_NULL) {                // create a dummy literal                ASTLiteral star = new ASTLiteral(JCRSQLParserTreeConstants.JJTLITERAL);                star.setType(QueryConstants.TYPE_STRING);                star.setValue("%");                predicateNode = createRelationQueryNode(parent,                        identifier, type, star);            } else {                throw new IllegalArgumentException("Unknown operation type: " + type);            }        } catch (ArrayIndexOutOfBoundsException e) {            throw new IllegalArgumentException("Too few arguments in predicate");        }        if (predicateNode != null) {            parent.addOperand(predicateNode);        }        return data;    }    public Object visit(ASTOrExpression node, Object data) {        NAryQueryNode parent = (NAryQueryNode) data;        OrQueryNode orQuery = new OrQueryNode(parent);        // pass to operands        node.childrenAccept(this, orQuery);        if (orQuery.getNumOperands() > 0) {            parent.addOperand(orQuery);        }        return parent;    }    public Object visit(ASTAndExpression node, Object data) {        NAryQueryNode parent = (NAryQueryNode) data;        AndQueryNode andQuery = new AndQueryNode(parent);        // pass to operands        node.childrenAccept(this, andQuery);        parent.addOperand(andQuery);        return parent;    }    public Object visit(ASTNotExpression node, Object data) {        NAryQueryNode parent = (NAryQueryNode) data;        NotQueryNode notQuery = new NotQueryNode(parent);        // pass to operand        node.childrenAccept(this, notQuery);        parent.addOperand(notQuery);        return parent;    }    public Object visit(ASTBracketExpression node, Object data) {        // bracket expression only has influence on how the syntax tree        // is created.        // simply pass on to children        return node.childrenAccept(this, data);    }    public Object visit(ASTLiteral node, Object data) {        // do nothing        return data;    }    public Object visit(ASTIdentifier node, Object data) {        // do nothing        return data;    }    public Object visit(ASTOrderByClause node, Object data) {        QueryRootNode root = (QueryRootNode) data;        OrderQueryNode order = new OrderQueryNode(root);        root.setOrderNode(order);        node.childrenAccept(this, order);        return root;    }    public Object visit(ASTOrderSpec node, Object data) {        OrderQueryNode order = (OrderQueryNode) data;        final QName[] identifier = new QName[1];        // collect identifier        node.childrenAccept(new DefaultParserVisitor() {            public Object visit(ASTIdentifier node, Object data) {                identifier[0] = node.getName();                return data;            }        }, data);        OrderQueryNode.OrderSpec spec = new OrderQueryNode.OrderSpec(identifier[0], true);        order.addOrderSpec(spec);        node.childrenAccept(this, spec);        return data;    }    public Object visit(ASTAscendingOrderSpec node, Object data) {        // do nothing ascending is default anyway        return data;    }    public Object visit(ASTDescendingOrderSpec node, Object data) {        OrderQueryNode.OrderSpec spec = (OrderQueryNode.OrderSpec) data;        spec.setAscending(false);        return data;    }    public Object visit(ASTContainsExpression node, Object data) {        NAryQueryNode parent = (NAryQueryNode) data;        try {            Path relPath = null;            if (node.getPropertyName() != null) {                Path.PathBuilder builder = new Path.PathBuilder();                builder.addLast(node.getPropertyName());                relPath = builder.getPath();            }            parent.addOperand(new TextsearchQueryNode(parent, node.getQuery(), relPath, true));        } catch (MalformedPathException e) {            // path is always valid        }        return parent;    }    public Object visit(ASTLowerFunction node, Object data) {        RelationQueryNode parent = (RelationQueryNode) data;        if (parent.getValueType() != QueryConstants.TYPE_STRING) {            String msg = "LOWER() function is only supported for String literal";            throw new IllegalArgumentException(msg);        }        parent.addOperand(new PropertyFunctionQueryNode(parent, PropertyFunctionQueryNode.LOWER_CASE));        return parent;    }    public Object visit(ASTUpperFunction node, Object data) {        RelationQueryNode parent = (RelationQueryNode) data;        if (parent.getValueType() != QueryConstants.TYPE_STRING) {            String msg = "UPPER() function is only supported for String literal";            throw new IllegalArgumentException(msg);        }        parent.addOperand(new PropertyFunctionQueryNode(parent, PropertyFunctionQueryNode.UPPER_CASE));        return parent;    }    public Object visit(ASTExcerptFunction node, Object data) {        // do nothing        return data;    }    //------------------------< internal >--------------------------------------    /**     * Creates a new {@link org.apache.jackrabbit.core.query.RelationQueryNode}.     *     * @param parent        the parent node for the created <code>RelationQueryNode</code>.     * @param propertyName  the property name for the relation.     * @param operationType the operation type.     * @param literal       the literal value for the relation.     * @return a <code>RelationQueryNode</code>.     * @throws IllegalArgumentException if the literal value does not conform     *                                  to its type. E.g. a malformed String representation of a date.     */    private RelationQueryNode createRelationQueryNode(QueryNode parent,                                                      QName propertyName,                                                      int operationType,                                                      ASTLiteral literal)            throws IllegalArgumentException {        String stringValue = literal.getValue();        RelationQueryNode node = null;        try {            Path.PathBuilder builder = new Path.PathBuilder();            builder.addLast(propertyName);            Path relPath = builder.getPath();            if (literal.getType() == QueryConstants.TYPE_DATE) {                SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);                Date date = format.parse(stringValue);                node = new RelationQueryNode(parent, relPath, date, operationType);            } else if (literal.getType() == QueryConstants.TYPE_DOUBLE) {                double d = Double.parseDouble(stringValue);                node = new RelationQueryNode(parent, relPath, d, operationType);            } else if (literal.getType() == QueryConstants.TYPE_LONG) {                long l = Long.parseLong(stringValue);                node = new RelationQueryNode(parent, relPath, l, operationType);            } else if (literal.getType() == QueryConstants.TYPE_STRING) {                node = new RelationQueryNode(parent, relPath, stringValue, operationType);            } else if (literal.getType() == QueryConstants.TYPE_TIMESTAMP) {                Calendar c = ISO8601.parse(stringValue);                node = new RelationQueryNode(parent, relPath, c.getTime(), operationType);            }        } catch (java.text.ParseException e) {            throw new IllegalArgumentException(e.toString());        } catch (NumberFormatException e) {            throw new IllegalArgumentException(e.toString());        } catch (MalformedPathException e) {            // path is always valid, but throw anyway            throw new IllegalArgumentException(e.getMessage());        }        if (node == null) {            throw new IllegalArgumentException("Unknown type for literal: " + literal.getType());        }        return node;    }    /**     * Creates <code>LocationStepQueryNode</code>s from a <code>path</code>.     *     * @param path      the path pattern     * @param operation the type of the parent node     */    private void createPathQuery(String path, int operation) {        MergingPathQueryNode pathNode = new MergingPathQueryNode(operation);        pathNode.setAbsolute(true);        if (path.equals("/")) {            pathNode.addPathStep(new LocationStepQueryNode(pathNode));            pathConstraints.add(pathNode);            return;        }        String[] names = path.split("/");        for (int i = 0; i < names.length; i++) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?