Search in sources :

Example 26 with SQLName

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

the class OdpsStatementParser method parseSet.

public SQLStatement parseSet() {
    List<String> comments = null;
    if (lexer.isKeepComments() && lexer.hasComment()) {
        comments = lexer.readAndResetComments();
    }
    accept(Token.SET);
    if (identifierEquals("LABEL")) {
        OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        lexer.nextToken();
        stmt.setLabel(lexer.stringVal());
        lexer.nextToken();
        accept(Token.TO);
        if (lexer.token() == Token.USER) {
            lexer.nextToken();
            SQLName name = this.exprParser.name();
            stmt.setUser(name);
            return stmt;
        }
        accept(Token.TABLE);
        SQLExpr expr = this.exprParser.name();
        stmt.setTable(new SQLExprTableSource(expr));
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            this.exprParser.names(stmt.getColumns(), stmt);
            accept(Token.RPAREN);
        }
        return stmt;
    } else {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        parseAssignItems(stmt.getItems(), stmt);
        return stmt;
    }
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsSetLabelStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsSetLabelStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 27 with SQLName

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

the class OdpsSchemaStatVisitor method visit.

@Override
public boolean visit(OdpsInsert x) {
    setMode(x, TableStat.Mode.Insert);
    setAliasMap();
    SQLExprTableSource tableSource = x.getTableSource();
    SQLExpr tableName = tableSource.getExpr();
    if (tableName instanceof SQLName) {
        String ident = ((SQLName) tableName).toString();
        setCurrentTable(ident);
        TableStat stat = getTableStat(ident);
        stat.incrementInsertCount();
        Map<String, String> aliasMap = getAliasMap();
        putAliasMap(aliasMap, tableSource.getAlias(), ident);
        putAliasMap(aliasMap, ident, ident);
    }
    for (SQLAssignItem partition : x.getPartitions()) {
        partition.accept(this);
    }
    accept(x.getQuery());
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 28 with SQLName

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

the class MySqlSchemaStatVisitor method visit.

// DUAL
public boolean visit(MySqlDeleteStatement x) {
    setAliasMap();
    setMode(x, Mode.Delete);
    accept(x.getFrom());
    accept(x.getUsing());
    x.getTableSource().accept(this);
    if (x.getTableSource() instanceof SQLExprTableSource) {
        SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
        String ident = tableName.toString();
        setCurrentTable(x, ident);
        TableStat stat = this.getTableStat(ident);
        stat.incrementDeleteCount();
    }
    accept(x.getWhere());
    accept(x.getOrderBy());
    accept(x.getLimit());
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 29 with SQLName

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

the class PhoenixStatementParser method parseInsertColumns.

@Override
protected void parseInsertColumns(SQLInsertInto insert) {
    if (lexer.token() == Token.RPAREN) {
        return;
    }
    for (; ; ) {
        SQLName expr = this.exprParser.name();
        expr.setParent(insert);
        insert.getColumns().add(expr);
        if (lexer.token() == Token.IDENTIFIER) {
            String text = lexer.stringVal();
            if (text.equalsIgnoreCase("TINYINT") || text.equalsIgnoreCase("BIGINT") || text.equalsIgnoreCase("INTEGER") || text.equalsIgnoreCase("DOUBLE") || text.equalsIgnoreCase("DATE") || text.equalsIgnoreCase("VARCHAR")) {
                expr.getAttributes().put("dataType", text);
                lexer.nextToken();
            } else if (text.equalsIgnoreCase("CHAR")) {
                String dataType = text;
                lexer.nextToken();
                accept(Token.LPAREN);
                SQLExpr char_len = this.exprParser.primary();
                accept(Token.RPAREN);
                dataType += ("(" + char_len.toString() + ")");
                expr.getAttributes().put("dataType", dataType);
            }
        }
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        }
        break;
    }
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 30 with SQLName

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

the class OracleSchemaStatVisitor method visit.

public boolean visit(OracleSelectTableReference x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) expr;
        if ("TABLE".equalsIgnoreCase(methodInvoke.getMethodName()) && methodInvoke.getParameters().size() == 1) {
            expr = methodInvoke.getParameters().get(0);
        }
    }
    Map<String, String> aliasMap = getAliasMap();
    if (expr instanceof SQLName) {
        String ident;
        if (expr instanceof SQLPropertyExpr) {
            String owner = ((SQLPropertyExpr) expr).getOwner().toString();
            String name = ((SQLPropertyExpr) expr).getName();
            if (aliasMap.containsKey(owner)) {
                owner = aliasMap.get(owner);
            }
            ident = owner + "." + name;
        } else {
            ident = expr.toString();
        }
        if (containsSubQuery(ident)) {
            return false;
        }
        if ("DUAL".equalsIgnoreCase(ident)) {
            return false;
        }
        x.putAttribute(ATTR_TABLE, ident);
        TableStat stat = getTableStat(ident);
        Mode mode = getMode();
        switch(mode) {
            case Delete:
                stat.incrementDeleteCount();
                break;
            case Insert:
                stat.incrementInsertCount();
                break;
            case Update:
                stat.incrementUpdateCount();
                break;
            case Select:
                stat.incrementSelectCount();
                break;
            case Merge:
                stat.incrementMergeCount();
                break;
            default:
                break;
        }
        putAliasMap(aliasMap, x.getAlias(), ident);
        putAliasMap(aliasMap, ident, ident);
        return false;
    }
    accept(x.getExpr());
    return false;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Mode(com.alibaba.druid.stat.TableStat.Mode) SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLName (com.alibaba.druid.sql.ast.SQLName)102 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)33 TableStat (com.alibaba.druid.stat.TableStat)20 ParserException (com.alibaba.druid.sql.parser.ParserException)17 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLObject (com.alibaba.druid.sql.ast.SQLObject)6 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)6 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)6 WallContext (com.alibaba.druid.wall.WallContext)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 WallSqlTableStat (com.alibaba.druid.wall.WallSqlTableStat)5 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLSubPartition (com.alibaba.druid.sql.ast.SQLSubPartition)3 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3