Search in sources :

Example 1 with PGInsertStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement 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 PGInsertStatement

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

the class PGInsertTest0 method test_1.

public void test_1() {
    String sql = "insert into test01 DEFAULT VALUES";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //		print(statementList);
    assertTrue(statemen instanceof PGInsertStatement);
    PGInsertStatement insert = (PGInsertStatement) statemen;
    assertTrue(insert.getTableName().getSimpleName().equalsIgnoreCase("test01"));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 3 with PGInsertStatement

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

the class PGSQLStatementParser method parseInsert.

public PGInsertStatement parseInsert() {
    PGInsertStatement stmt = new PGInsertStatement();
    if (lexer.token() == Token.INSERT) {
        lexer.nextToken();
        accept(Token.INTO);
        SQLName tableName = this.exprParser.name();
        stmt.setTableName(tableName);
        if (lexer.token() == Token.IDENTIFIER) {
            stmt.setAlias(lexer.stringVal());
            lexer.nextToken();
        }
    }
    if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        accept(Token.VALUES);
        stmt.setDefaultValues(true);
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == (Token.VALUES)) {
        lexer.nextToken();
        for (; ; ) {
            accept(Token.LPAREN);
            SQLInsertStatement.ValuesClause valuesCaluse = new SQLInsertStatement.ValuesClause();
            this.exprParser.exprList(valuesCaluse.getValues(), valuesCaluse);
            stmt.addValueCause(valuesCaluse);
            accept(Token.RPAREN);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == (Token.SELECT)) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        stmt.setQuery(queryExpr.getSubQuery());
    }
    if (lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        SQLExpr returning = this.exprParser.expr();
        stmt.setReturning(returning);
    }
    return stmt;
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

PGInsertStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 PGWithClause (com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause)1 PGDeleteStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)1 PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)1 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)1