Search in sources :

Example 6 with Limit

use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLDeleteStatement node) {
    appendable.append("DELETE ");
    if (node.isLowPriority())
        appendable.append("LOW_PRIORITY ");
    if (node.isQuick())
        appendable.append("QUICK ");
    if (node.isIgnore())
        appendable.append("IGNORE ");
    TableReferences tableRefs = node.getTableRefs();
    if (tableRefs == null) {
        appendable.append("FROM ");
        node.getTableNames().get(0).accept(this);
    } else {
        printList(node.getTableNames());
        appendable.append(" FROM ");
        node.getTableRefs().accept(this);
    }
    Expression where = node.getWhereCondition();
    if (where != null) {
        appendable.append(" WHERE ");
        where.accept(this);
    }
    OrderBy orderBy = node.getOrderBy();
    if (orderBy != null) {
        appendable.append(' ');
        orderBy.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) 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) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) 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) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) 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) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)

Example 7 with Limit

use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.

the class MySQLDMLParserTest method testLimit.

public void testLimit() throws SQLSyntaxErrorException {
    String sql = "limit 1,2";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    Limit limit = parser.limit();
    String output = output2MySQL(limit, sql);
    Assert.assertEquals(1, limit.getOffset());
    Assert.assertEquals(2, limit.getSize());
    Assert.assertEquals("LIMIT 1, 2", output);
    sql = "limit 1,?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(1, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 1, ?", output);
    sql = "limit ?,9";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT ?, 9", output);
    sql = "limit ?,?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(new ParamMarker(2), limit.getSize());
    Assert.assertEquals("LIMIT ?, ?", output);
    sql = "limit ? d";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 0, ?", output);
    sql = "limit 9 f";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT 0, 9", output);
    sql = "limit 9 ofFset 0";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT 0, 9", output);
    sql = "limit ? offset 0";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 0, ?", output);
    sql = "limit ? offset ?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(2), limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT ?, ?", output);
    sql = "limit 9 offset ?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT ?, 9", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) ParamMarker(com.alibaba.cobar.parser.ast.expression.primary.ParamMarker) Limit(com.alibaba.cobar.parser.ast.fragment.Limit)

Example 8 with Limit

use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.

the class MySQLDMLSelectParser method select.

@Override
public DMLSelectStatement select() throws SQLSyntaxErrorException {
    match(KW_SELECT);
    DMLSelectStatement.SelectOption option = selectOption();
    List<Pair<Expression, String>> exprList = selectExprList();
    TableReferences tables = null;
    Expression where = null;
    GroupBy group = null;
    Expression having = null;
    OrderBy order = null;
    Limit limit = null;
    boolean dual = false;
    if (lexer.token() == KW_FROM) {
        if (lexer.nextToken() == KW_DUAL) {
            lexer.nextToken();
            dual = true;
            List<TableReference> trs = new ArrayList<TableReference>(1);
            trs.add(new Dual());
            tables = new TableReferences(trs);
        } else {
            tables = tableRefs();
        }
    }
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    if (!dual) {
        group = groupBy();
        if (lexer.token() == KW_HAVING) {
            lexer.nextToken();
            having = exprParser.expression();
        }
        order = orderBy();
    }
    limit = limit();
    if (!dual) {
        switch(lexer.token()) {
            case KW_FOR:
                lexer.nextToken();
                match(KW_UPDATE);
                option.lockMode = DMLSelectStatement.LockMode.FOR_UPDATE;
                break;
            case KW_LOCK:
                lexer.nextToken();
                match(KW_IN);
                matchIdentifier("SHARE");
                matchIdentifier("MODE");
                option.lockMode = DMLSelectStatement.LockMode.LOCK_IN_SHARE_MODE;
                break;
        }
    }
    return new DMLSelectStatement(option, exprList, tables, where, group, having, order, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) ArrayList(java.util.ArrayList) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Dual(com.alibaba.cobar.parser.ast.fragment.tableref.Dual) Pair(com.alibaba.cobar.parser.util.Pair)

Example 9 with Limit

use of com.alibaba.cobar.parser.ast.fragment.Limit 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 10 with Limit

use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLSelectUnionStatement node) {
    List<DMLSelectStatement> list = node.getSelectStmtList();
    if (list == null || list.isEmpty()) {
        throw new IllegalArgumentException("SELECT UNION must have at least one SELECT");
    }
    final int fstDist = node.getFirstDistinctIndex();
    int i = 0;
    for (DMLSelectStatement select : list) {
        if (i > 0) {
            appendable.append(" UNION ");
            if (i > fstDist) {
                appendable.append("ALL ");
            }
        }
        appendable.append('(');
        select.accept(this);
        appendable.append(')');
        ++i;
    }
    OrderBy order = node.getOrderBy();
    if (order != null) {
        appendable.append(' ');
        order.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint)

Aggregations

Limit (com.alibaba.cobar.parser.ast.fragment.Limit)14 Expression (com.alibaba.cobar.parser.ast.expression.Expression)10 VariableExpression (com.alibaba.cobar.parser.ast.expression.primary.VariableExpression)7 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)7 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)5 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)5 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)5 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)5 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)5 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)5 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)5 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)5 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)5 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)5 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)5 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)5 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)5 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)5 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)5 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)5