Search in sources :

Example 1 with DMLDeleteStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement in project cobar by alibaba.

the class MySQLDMLDeleteParserTest method testDelete1.

public void testDelete1() throws SQLSyntaxErrorException {
    String sql = "deLetE LOW_PRIORITY from id1.id , id using t1 a where col1 =? ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLDeleteParser parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    DMLDeleteStatement delete = parser.delete();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE LOW_PRIORITY id1.id, id FROM t1 AS A WHERE col1 = ?", output);
    sql = "deLetE from id1.id  using t1  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id1.id FROM t1", output);
    sql = "delete from offer.*,wp_image.* using offer a,wp_image b where a.member_id=b.member_id and a.member_id='abc' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE offer.*, wp_image.* FROM offer AS A, wp_image AS B WHERE " + "a.member_id = b.member_id AND a.member_id = 'abc'", output);
    sql = "deLetE from id1.id where col1='adf' limit 1,?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM id1.id WHERE col1 = 'adf' LIMIT 1, ?", output);
    sql = "deLetE from id where col1='adf' ordEr by d liMit ? offset 2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM id WHERE col1 = 'adf' ORDER BY d LIMIT 2, ?", output);
    sql = "deLetE id.* from t1,t2 where col1='adf'            and col2=1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id.* FROM t1, t2 WHERE col1 = 'adf' AND col2 = 1", output);
    sql = "deLetE id,id.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id, id.t FROM t1", output);
    sql = "deLetE from t1 where t1.id1='abc' order by a limit 5";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM t1 WHERE t1.id1 = 'abc' ORDER BY a LIMIT 0, 5", output);
    sql = "deLetE from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM t1", output);
    sql = "deLetE ignore tb1.*,id1.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE IGNORE tb1.*, id1.t FROM t1", output);
    sql = "deLetE quick tb1.*,id1.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE QUICK tb1.*, id1.t FROM t1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLDeleteStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement)

Example 2 with DMLDeleteStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement 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)

Aggregations

DMLDeleteStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement)2 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)1 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)1 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)1 TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1