Search in sources :

Example 1 with SQLExprTableSource

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

the class OdpsStatementParser method parseShow.

public SQLStatement parseShow() {
    accept(Token.SHOW);
    if (identifierEquals("PARTITIONS")) {
        lexer.nextToken();
        OdpsShowPartitionsStmt stmt = new OdpsShowPartitionsStmt();
        SQLExpr expr = this.exprParser.expr();
        stmt.setTableSource(new SQLExprTableSource(expr));
        return stmt;
    }
    if (identifierEquals("STATISTIC")) {
        lexer.nextToken();
        OdpsShowStatisticStmt stmt = new OdpsShowStatisticStmt();
        SQLExpr expr = this.exprParser.expr();
        stmt.setTableSource(new SQLExprTableSource(expr));
        return stmt;
    }
    if (identifierEquals("TABLES")) {
        lexer.nextToken();
        SQLShowTablesStatement stmt = new SQLShowTablesStatement();
        if (lexer.token() == Token.FROM) {
            lexer.nextToken();
            stmt.setDatabase(this.exprParser.name());
        }
        if (lexer.token() == Token.LIKE) {
            lexer.nextToken();
            stmt.setLike(this.exprParser.expr());
        }
        return stmt;
    }
    if (identifierEquals("GRANTS")) {
        lexer.nextToken();
        OdpsShowGrantsStmt stmt = new OdpsShowGrantsStmt();
        if (lexer.token() == Token.FOR) {
            lexer.nextToken();
            stmt.setUser(this.exprParser.expr());
        }
        if (lexer.token() == Token.ON) {
            lexer.nextToken();
            acceptIdentifier("type");
            stmt.setObjectType(this.exprParser.expr());
        }
        return stmt;
    }
    throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsShowStatisticStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowStatisticStmt) SQLShowTablesStatement(com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsShowGrantsStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowGrantsStmt) OdpsShowPartitionsStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowPartitionsStmt) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 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 3 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 4 with SQLExprTableSource

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

the class DruidSelectParser method changeSql.

/**
 * 改写sql:需要加limit的加上
 */
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLNonTransientException {
    tryRoute(schema, rrs, cachePool);
    rrs.copyLimitToNodes();
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        int limitStart = 0;
        int limitSize = schema.getDefaultMaxLimit();
        // clear group having
        SQLSelectGroupByClause groupByClause = mysqlSelectQuery.getGroupBy();
        // Modified by winbill, 20160614, do NOT include having clause when routing to multiple nodes
        if (groupByClause != null && groupByClause.getHaving() != null && isRoutMultiNode(schema, rrs)) {
            groupByClause.setHaving(null);
        }
        Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
        boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
        if (isNeedAddLimit) {
            Limit limit = new Limit();
            limit.setRowCount(new SQLIntegerExpr(limitSize));
            mysqlSelectQuery.setLimit(limit);
            rrs.setLimitSize(limitSize);
            String sql = getSql(rrs, stmt, isNeedAddLimit);
            rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitSize, true);
        }
        Limit limit = mysqlSelectQuery.getLimit();
        if (limit != null && !isNeedAddLimit) {
            SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();
            SQLIntegerExpr count = (SQLIntegerExpr) limit.getRowCount();
            if (offset != null) {
                limitStart = offset.getNumber().intValue();
                rrs.setLimitStart(limitStart);
            }
            if (count != null) {
                limitSize = count.getNumber().intValue();
                rrs.setLimitSize(limitSize);
            }
            if (isNeedChangeLimit(rrs)) {
                Limit changedLimit = new Limit();
                changedLimit.setRowCount(new SQLIntegerExpr(limitStart + limitSize));
                if (offset != null) {
                    if (limitStart < 0) {
                        String msg = "You have an error in your SQL syntax; check the manual that " + "corresponds to your MySQL server version for the right syntax to use near '" + limitStart + "'";
                        throw new SQLNonTransientException(ErrorCode.ER_PARSE_ERROR + " - " + msg);
                    } else {
                        changedLimit.setOffset(new SQLIntegerExpr(0));
                    }
                }
                mysqlSelectQuery.setLimit(changedLimit);
                String sql = getSql(rrs, stmt, isNeedAddLimit);
                rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitStart + limitSize, true);
                // 设置改写后的sql
                ctx.setSql(sql);
            } else {
                rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), getCtx().getSql(), rrs.getLimitStart(), rrs.getLimitSize(), true);
            // ctx.setSql(nativeSql);
            }
        }
        if (rrs.isDistTable()) {
            SQLTableSource from = mysqlSelectQuery.getFrom();
            for (RouteResultsetNode node : rrs.getNodes()) {
                SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
                sqlIdentifierExpr.setParent(from);
                sqlIdentifierExpr.setName(node.getSubTableName());
                SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
                from2.setAlias(from.getAlias());
                mysqlSelectQuery.setFrom(from2);
                node.setStatement(stmt.toString());
            }
        }
        rrs.setCacheAble(isNeedCache(schema, rrs, mysqlSelectQuery, allConditions));
    }
}
Also used : SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) Limit(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SQLExprTableSource

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

the class DruidMycatRouteStrategy method getDisTable.

private SQLExprTableSource getDisTable(SQLTableSource tableSource, RouteResultsetNode node) throws SQLSyntaxErrorException {
    if (node.getSubTableName() == null) {
        String msg = " sub table not exists for " + node.getName() + " on " + tableSource;
        LOGGER.error("DruidMycatRouteStrategyError " + msg);
        throw new SQLSyntaxErrorException(msg);
    }
    SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
    sqlIdentifierExpr.setParent(tableSource.getParent());
    sqlIdentifierExpr.setName(node.getSubTableName());
    SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
    return from2;
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Aggregations

SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)51 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)17 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)11 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)10 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)9 TableStat (com.alibaba.druid.stat.TableStat)8 SQLNonTransientException (java.sql.SQLNonTransientException)8 SQLName (com.alibaba.druid.sql.ast.SQLName)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)7 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)5 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)5 TableConfig (com.actiontech.dble.config.model.TableConfig)4 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)4 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4