Search in sources :

Example 6 with DMLSelectStatement

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

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

the class MySQLDDLParser method createTable.

/**
     * <code>TABLE</code> has been consumed
     */
private DDLCreateTableStatement createTable(boolean temp) throws SQLSyntaxErrorException {
    boolean ifNotExists = false;
    if (lexer.token() == KW_IF) {
        lexer.nextToken();
        match(KW_NOT);
        match(KW_EXISTS);
        ifNotExists = true;
    }
    Identifier table = identifier();
    DDLCreateTableStatement stmt = new DDLCreateTableStatement(temp, ifNotExists, table);
    createTableDefs(stmt);
    TableOptions options = new TableOptions();
    stmt.setTableOptions(options);
    tableOptions(options);
    DDLCreateTableStatement.SelectOption selectOpt = null;
    switch(lexer.token()) {
        case KW_IGNORE:
            selectOpt = DDLCreateTableStatement.SelectOption.IGNORED;
            if (lexer.nextToken() == KW_AS) {
                lexer.nextToken();
            }
            break;
        case KW_REPLACE:
            selectOpt = DDLCreateTableStatement.SelectOption.REPLACE;
            if (lexer.nextToken() == KW_AS) {
                lexer.nextToken();
            }
            break;
        case KW_AS:
            lexer.nextToken();
        case KW_SELECT:
            break;
        case EOF:
            return stmt;
        default:
            throw new SQLSyntaxErrorException("DDL CREATE TABLE statement not end properly");
    }
    DMLSelectStatement select = new MySQLDMLSelectParser(lexer, exprParser).select();
    stmt.setSelect(selectOpt, select);
    match(EOF);
    return stmt;
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) TableOptions(com.alibaba.cobar.parser.ast.fragment.ddl.TableOptions) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) DDLCreateTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLCreateTableStatement)

Example 8 with DMLSelectStatement

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

Example 9 with DMLSelectStatement

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

the class MySQLDMLSelectParserTest method testSelectChinese.

public void testSelectChinese() throws SQLSyntaxErrorException {
    String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id='中''‘文' and t1.id=test.t2.id";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    DMLSelectStatement select = parser.select();
    Assert.assertNotNull(select);
    String output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT t1.id, t2.* FROM t1, test.t2 WHERE test.t1.id = '中\\'‘文' AND t1.id = test.t2.id", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)

Aggregations

DMLSelectStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)9 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)3 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)3 TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)3 Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)2 GroupBy (com.alibaba.cobar.parser.ast.fragment.GroupBy)2 TableReference (com.alibaba.cobar.parser.ast.fragment.tableref.TableReference)2 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)2 Pair (com.alibaba.cobar.parser.util.Pair)2 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)1 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)1 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)1 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)1 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)1 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)1 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)1 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)1 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)1 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)1