Search in sources :

Example 6 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource 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 7 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource 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 8 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.

the class PGSelectParser method parseTableSourceRest.

protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
    if (lexer.token() == Token.AS && tableSource instanceof SQLExprTableSource) {
        lexer.nextToken();
        String alias = null;
        if (lexer.token() == Token.IDENTIFIER) {
            alias = lexer.stringVal();
            lexer.nextToken();
        }
        if (lexer.token() == Token.LPAREN) {
            SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
            PGFunctionTableSource functionTableSource = new PGFunctionTableSource(exprTableSource.getExpr());
            if (alias != null) {
                functionTableSource.setAlias(alias);
            }
            lexer.nextToken();
            parserParameters(functionTableSource.getParameters());
            accept(Token.RPAREN);
            return super.parseTableSourceRest(functionTableSource);
        }
    }
    return super.parseTableSourceRest(tableSource);
}
Also used : PGFunctionTableSource(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 9 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.

the class SQLCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    SQLCreateTableStatement createTable = newCreateStatement();
    if (identifierEquals("GLOBAL")) {
        lexer.nextToken();
        if (identifierEquals("TEMPORARY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
        }
    } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error");
        }
    }
    accept(Token.TABLE);
    createTable.setName(this.exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            if (//
            lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.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(createTable);
                createTable.getTableElementList().add((SQLTableElement) constraint);
            } else if (lexer.token() == Token.TABLESPACE) {
                throw new ParserException("TODO " + lexer.token());
            } else {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    // compatible for sql server
                    break;
                }
                continue;
            }
            break;
        }
        // while
        // (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
        // parseConstaint(table.getConstraints());
        //
        // if (this.tokenList.current().equals(OracleToken.CommaToken))
        // ;
        // lexer.nextToken();
        // }
        accept(Token.RPAREN);
        if (identifierEquals("INHERITS")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            SQLName inherits = this.exprParser.name();
            createTable.setInherits(new SQLExprTableSource(inherits));
            accept(Token.RPAREN);
        }
    }
    return createTable;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 10 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.

the class SQLEvalVisitorUtils method visit.

public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) {
    if (WallVisitorUtils.isSimpleCountTableSource(null, ((SQLQueryExpr) x).getSubQuery())) {
        x.putAttribute(EVAL_VALUE, 1);
        return false;
    }
    if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery();
        boolean nullFrom = false;
        if (queryBlock.getFrom() == null) {
            nullFrom = true;
        } else if (queryBlock.getFrom() instanceof SQLExprTableSource) {
            SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr();
            if (expr instanceof SQLIdentifierExpr) {
                if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
                    nullFrom = true;
                }
            }
        }
        if (nullFrom) {
            List<Object> row = new ArrayList<Object>(queryBlock.getSelectList().size());
            for (int i = 0; i < queryBlock.getSelectList().size(); ++i) {
                SQLSelectItem item = queryBlock.getSelectList().get(i);
                item.getExpr().accept(visitor);
                Object cell = item.getExpr().getAttribute(EVAL_VALUE);
                row.add(cell);
            }
            List<List<Object>> rows = new ArrayList<List<Object>>(1);
            rows.add(row);
            Object result = rows;
            queryBlock.putAttribute(EVAL_VALUE, result);
            x.getSubQuery().putAttribute(EVAL_VALUE, result);
            x.putAttribute(EVAL_VALUE, result);
            return false;
        }
    }
    return false;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) ArrayList(java.util.ArrayList) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLObject(com.alibaba.druid.sql.ast.SQLObject) List(java.util.List) ArrayList(java.util.ArrayList) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)22 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 TableStat (com.alibaba.druid.stat.TableStat)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2