Search in sources :

Example 1 with SQLDeclareItem

use of com.alibaba.druid.sql.ast.SQLDeclareItem in project druid by alibaba.

the class MySqlStatementParser method parseDeclare.

/**
     * parse declare statement
     */
public MySqlDeclareStatement parseDeclare() {
    MySqlDeclareStatement stmt = new MySqlDeclareStatement();
    accept(Token.DECLARE);
    // lexer.nextToken();
    for (; ; ) {
        SQLDeclareItem item = new SQLDeclareItem();
        item.setName(exprParser.name());
        stmt.addVar(item);
        if (lexer.token() == Token.COMMA) {
            accept(Token.COMMA);
            continue;
        } else if (lexer.token() != Token.EOF) {
            // var type
            item.setDataType(exprParser.parseDataType());
            if (lexer.token() == Token.DEFAULT) {
                lexer.nextToken();
                SQLExpr defaultValue = this.exprParser.primary();
                item.setValue(defaultValue);
            }
            break;
        } else {
            throw new ParserException("TODO");
        }
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDeclareStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem)

Example 2 with SQLDeclareItem

use of com.alibaba.druid.sql.ast.SQLDeclareItem in project druid by alibaba.

the class SQLServerStatementParser method parseDeclare.

public SQLStatement parseDeclare() {
    this.accept(Token.DECLARE);
    SQLServerDeclareStatement declareStatement = new SQLServerDeclareStatement();
    for (; ; ) {
        SQLDeclareItem item = new SQLDeclareItem();
        declareStatement.addItem(item);
        item.setName(this.exprParser.name());
        if (lexer.token() == Token.AS) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.TABLE) {
            lexer.nextToken();
            item.setType(SQLDeclareItem.Type.TABLE);
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                for (; ; ) {
                    if (//
                    lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
                        SQLColumnDefinition column = this.exprParser.parseColumn();
                        item.getTableElementList().add(column);
                    } else if (//
                    lexer.token() == Token.PRIMARY || //
                    lexer.token() == Token.UNIQUE || //
                    lexer.token() == Token.CHECK || lexer.token() == Token.CONSTRAINT) {
                        SQLConstraint constraint = this.exprParser.parseConstaint();
                        constraint.setParent(item);
                        item.getTableElementList().add((SQLTableElement) constraint);
                    } else if (lexer.token() == Token.TABLESPACE) {
                        throw new ParserException("TODO " + lexer.token());
                    } else {
                        SQLColumnDefinition column = this.exprParser.parseColumn();
                        item.getTableElementList().add(column);
                    }
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        }
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
            }
            break;
        } else if (lexer.token() == Token.CURSOR) {
            item.setType(SQLDeclareItem.Type.CURSOR);
            lexer.nextToken();
        } else {
            item.setType(SQLDeclareItem.Type.LOCAL);
            item.setDataType(this.exprParser.parseDataType());
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
                item.setValue(this.exprParser.expr());
            }
        }
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        } else {
            break;
        }
    }
    return declareStatement;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLServerDeclareStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerDeclareStatement) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem)

Example 3 with SQLDeclareItem

use of com.alibaba.druid.sql.ast.SQLDeclareItem in project druid by alibaba.

the class SQLServerSchemaStatVisitor method visit.

@Override
public boolean visit(SQLServerDeclareStatement x) {
    for (SQLDeclareItem item : x.getItems()) {
        item.setParent(x);
        SQLExpr name = item.getName();
        this.variants.put(name.toString(), name);
    }
    return true;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem)

Example 4 with SQLDeclareItem

use of com.alibaba.druid.sql.ast.SQLDeclareItem in project druid by alibaba.

the class MySqlSchemaStatVisitor method visit.

@Override
public boolean visit(MySqlDeclareStatement x) {
    for (SQLDeclareItem item : x.getVarList()) {
        item.setParent(x);
        SQLName var = (SQLName) item.getName();
        this.variants.put(var.toString(), var);
    }
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem)

Aggregations

SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 MySqlDeclareStatement (com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement)1 SQLServerDeclareStatement (com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerDeclareStatement)1