Search in sources :

Example 41 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLDMLDeleteParser method delete.

/**
     * first token is {@link MySQLToken#KW_DELETE} <code><pre>
     * 'DELETE' 'LOW_PRIORITY'? 'QUICK'? 'IGNORE'? (
     *     'FROM' tid ( (',' tid)* 'USING' table_refs ('WHERE' cond)?  
     *                | ('WHERE' cond)? ('ORDER' 'BY' ids)? ('LIMIT' count)?  )  // single table
     *    | tid (',' tid)* 'FROM' table_refs ('WHERE' cond)? )
     * </pre></code>
     */
public DMLDeleteStatement delete() throws SQLSyntaxErrorException {
    match(KW_DELETE);
    boolean lowPriority = false;
    boolean quick = false;
    boolean ignore = false;
    loopOpt: for (; ; lexer.nextToken()) {
        switch(lexer.token()) {
            case KW_LOW_PRIORITY:
                lowPriority = true;
                break;
            case KW_IGNORE:
                ignore = true;
                break;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (SpecialIdentifier.QUICK == si) {
                    quick = true;
                    break;
                }
            default:
                break loopOpt;
        }
    }
    List<Identifier> tempList;
    TableReferences tempRefs;
    Expression tempWhere;
    if (lexer.token() == KW_FROM) {
        lexer.nextToken();
        Identifier id = identifier();
        tempList = new ArrayList<Identifier>(1);
        tempList.add(id);
        switch(lexer.token()) {
            case PUNC_COMMA:
                tempList = buildIdList(id);
            case KW_USING:
                lexer.nextToken();
                tempRefs = tableRefs();
                if (lexer.token() == KW_WHERE) {
                    lexer.nextToken();
                    tempWhere = exprParser.expression();
                    return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
                }
                return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
            case KW_WHERE:
            case KW_ORDER:
            case KW_LIMIT:
                break;
            default:
                return new DMLDeleteStatement(lowPriority, quick, ignore, id);
        }
        tempWhere = null;
        OrderBy orderBy = null;
        Limit limit = null;
        if (lexer.token() == KW_WHERE) {
            lexer.nextToken();
            tempWhere = exprParser.expression();
        }
        if (lexer.token() == KW_ORDER) {
            orderBy = orderBy();
        }
        if (lexer.token() == KW_LIMIT) {
            limit = limit();
        }
        return new DMLDeleteStatement(lowPriority, quick, ignore, id, tempWhere, orderBy, limit);
    }
    tempList = idList();
    match(KW_FROM);
    tempRefs = tableRefs();
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        tempWhere = exprParser.expression();
        return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
    }
    return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) DMLDeleteStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement)

Example 42 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLReplaceStatement node) {
    appendable.append("REPLACE ");
    switch(node.getMode()) {
        case DELAY:
            appendable.append("DELAYED ");
            break;
        case LOW:
            appendable.append("LOW_PRIORITY ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
    }
    appendable.append("INTO ");
    node.getTable().accept(this);
    appendable.append(' ');
    List<Identifier> cols = node.getColumnNameList();
    if (cols != null && !cols.isEmpty()) {
        appendable.append('(');
        printList(cols);
        appendable.append(") ");
    }
    QueryExpression select = node.getSelect();
    if (select == null) {
        appendable.append("VALUES ");
        List<RowExpression> rows = node.getRowList();
        if (rows != null && !rows.isEmpty()) {
            boolean isFst = true;
            for (RowExpression row : rows) {
                if (row == null || row.getRowExprList().isEmpty())
                    continue;
                if (isFst)
                    isFst = false;
                else
                    appendable.append(", ");
                appendable.append('(');
                printList(row.getRowExprList());
                appendable.append(')');
            }
        } else {
            throw new IllegalArgumentException("at least one row for REPLACE");
        }
    } else {
        select.accept(this);
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 43 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLInsertStatement node) {
    insertReplace(node);
    List<Pair<Identifier, Expression>> dup = node.getDuplicateUpdate();
    if (dup != null) {
        ASTNode[] duplist = new ASTNode[dup.size() * 2];
        int i = 0;
        for (Pair<Identifier, Expression> p : dup) {
            Identifier key = null;
            Expression value = null;
            if (p != null) {
                key = p.getKey();
                value = p.getValue();
            }
            duplist[i++] = key;
            duplist[i++] = value;
        }
        visitChild(2, false, false, duplist);
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint) Pair(com.alibaba.cobar.parser.util.Pair)

Example 44 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(TableRefFactor node) {
    Identifier table = node.getTable();
    visitChild(1, false, false, table);
    String tableName = table.getIdTextUpUnescape();
    addTable(tableName);
    String alias = node.getAliasUnescapeUppercase();
    if (alias == null) {
        tableAlias.put(null, tableName);
        tableAlias.put(tableName, tableName);
    } else {
        if (!tableAlias.containsKey(null)) {
            tableAlias.put(null, tableName);
        }
        tableAlias.put(alias, tableName);
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 45 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(InExpression node) {
    Expression left = node.getLeftOprand();
    Expression right = node.getRightOprand();
    visitChild(2, false, false, left, right);
    if (verdictColumn && !node.isNot() && left instanceof Identifier && right instanceof InExpressionList) {
        Identifier col = (Identifier) left;
        String colName = col.getIdTextUpUnescape();
        String table = tableAlias.get(col.getLevelUnescapeUpName(2));
        if (isRuledColumn(table, colName)) {
            List<Object> valList = ensureColumnValueList(ensureColumnValueByTable(table), colName);
            Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(ensureColumnValueIndexByTable(table), colName);
            InExpressionList inlist = (InExpressionList) right;
            for (Expression expr : inlist.getList()) {
                Object value = expr.evaluation(evaluationParameter);
                if (value != Expression.UNEVALUATABLE) {
                    valList.add(value);
                    addIntoColumnValueIndex(valMap, value, expr, node);
                }
            }
        }
    }
}
Also used : InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) SmallSet(com.alibaba.cobar.util.SmallSet) Set(java.util.Set) HashSet(java.util.HashSet) ShowCharaterSet(com.alibaba.cobar.parser.ast.stmt.dal.ShowCharaterSet) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Aggregations

Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)50 Expression (com.alibaba.cobar.parser.ast.expression.Expression)28 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)18 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)18 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)14 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)13 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)13 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)13 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)13 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)13 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)13 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)13 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)13 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)13 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)13 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)13 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)13 Pair (com.alibaba.cobar.parser.util.Pair)12 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)11 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)11