Search in sources :

Example 1 with PGDeleteStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement in project druid by alibaba.

the class PGSQLStatementParser method parseWith.

public SQLStatement parseWith() {
    PGWithClause with = this.parseWithClause();
    if (lexer.token() == Token.INSERT) {
        PGInsertStatement stmt = this.parseInsert();
        stmt.setWith(with);
        return stmt;
    }
    if (lexer.token() == Token.SELECT) {
        PGSelectStatement stmt = this.parseSelect();
        stmt.setWith(with);
        return stmt;
    }
    if (lexer.token() == Token.DELETE) {
        PGDeleteStatement stmt = this.parseDeleteStatement();
        stmt.setWith(with);
        return stmt;
    }
    throw new ParserException("TODO");
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) PGWithClause(com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)

Example 2 with PGDeleteStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement in project druid by alibaba.

the class PGASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    PGASTVisitorAdapter adapter = new PGASTVisitorAdapter();
    new WindowClause().accept(adapter);
    new FetchClause().accept(adapter);
    new ForClause().accept(adapter);
    new PGWithQuery().accept(adapter);
    new PGWithClause().accept(adapter);
    new PGDeleteStatement().accept(adapter);
    new PGFunctionTableSource().accept(adapter);
}
Also used : PGWithQuery(com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery) PGWithClause(com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause) PGFunctionTableSource(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource) PGASTVisitorAdapter(com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitorAdapter) ForClause(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause) FetchClause(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause) WindowClause(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)

Example 3 with PGDeleteStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement in project druid by alibaba.

the class PGDeleteTest method test_1.

public void test_1() {
    String sql = "delete from test01 as a where a.id = 2";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //		print(statementList);
    assertTrue(statementList.size() == 1);
    assertTrue(statemen instanceof PGDeleteStatement);
    PGDeleteStatement delete = (PGDeleteStatement) statemen;
    assertTrue(delete.getAlias().equalsIgnoreCase("a"));
    assertTrue(delete.getTableName().getSimpleName().equals("test01"));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 4 with PGDeleteStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement in project druid by alibaba.

the class PGSQLStatementParser method parseDeleteStatement.

public PGDeleteStatement parseDeleteStatement() {
    lexer.nextToken();
    PGDeleteStatement deleteStatement = new PGDeleteStatement();
    if (lexer.token() == (Token.FROM)) {
        lexer.nextToken();
    }
    if (lexer.token() == (Token.ONLY)) {
        lexer.nextToken();
        deleteStatement.setOnly(true);
    }
    SQLName tableName = exprParser.name();
    deleteStatement.setTableName(tableName);
    if (lexer.token() == Token.AS) {
        accept(Token.AS);
    }
    if (lexer.token() == Token.IDENTIFIER) {
        deleteStatement.setAlias(lexer.stringVal());
        lexer.nextToken();
    }
    if (lexer.token() == Token.USING) {
        lexer.nextToken();
        for (; ; ) {
            SQLName name = this.exprParser.name();
            deleteStatement.getUsing().add(name);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        if (lexer.token() == Token.CURRENT) {
            lexer.nextToken();
            accept(Token.OF);
            SQLName cursorName = this.exprParser.name();
            SQLExpr where = new SQLCurrentOfCursorExpr(cursorName);
            deleteStatement.setWhere(where);
        } else {
            SQLExpr where = this.exprParser.expr();
            deleteStatement.setWhere(where);
        }
    }
    if (lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        accept(Token.STAR);
        deleteStatement.setReturning(true);
    }
    return deleteStatement;
}
Also used : SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

PGDeleteStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)4 PGWithClause (com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 PGWithQuery (com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery)1 PGFunctionTableSource (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource)1 PGInsertStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement)1 FetchClause (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause)1 ForClause (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause)1 WindowClause (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause)1 PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)1 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)1 PGASTVisitorAdapter (com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitorAdapter)1