Search in sources :

Example 11 with SQLExprTableSource

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

the class Demo2 method test_0.

public void test_0() throws Exception {
    String sql = "select * from user where uid = 2 and uname = ?";
    List<Object> parameters = new ArrayList<Object>();
    parameters.add(1);
    parameters.add("wenshao");
    SQLStatementParser parser = new MySqlStatementParser(sql);
    //
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement first = (SQLStatement) stmtList.get(0);
    MyVisitor visitor = new MyVisitor();
    first.accept(visitor);
    SQLExpr firstVar = visitor.getVariantList().get(0);
    int userId;
    if (firstVar instanceof SQLVariantRefExpr) {
        int varIndex = (Integer) firstVar.getAttribute("varIndex");
        userId = (Integer) parameters.get(varIndex);
    } else {
        userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
    }
    String tableName;
    if (userId == 1) {
        tableName = "user_1";
    } else {
        tableName = "user_x";
    }
    for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
        SQLExpr expr = tableSource.getExpr();
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equals("user")) {
                identExpr.setName(tableName);
            }
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
            String ident = proExpr.getName();
            if (ident.equals("user")) {
                proExpr.setName(tableName);
            }
        }
    }
    String realSql = SQLUtils.toOracleString(first);
    System.out.println(realSql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 12 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

@Override
public boolean visit(final MySqlSelectQueryBlock x) {
    stepInQuery();
    if (x.getFrom() instanceof SQLExprTableSource) {
        SQLExprTableSource tableExpr = (SQLExprTableSource) x.getFrom();
        getParseContext().setCurrentTable(tableExpr.getExpr().toString(), Optional.fromNullable(tableExpr.getAlias()));
    }
    return super.visit(x);
}
Also used : SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 13 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor 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, 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 14 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat-Server by MyCATApache.

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);
                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) 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) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) Limit(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with SQLExprTableSource

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

the class SQLDeleteBuilderImpl method from.

@Override
public SQLDeleteBuilder from(String table, String alias) {
    SQLDeleteStatement delete = getSQLDeleteStatement();
    SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(table), alias);
    delete.setTableSource(from);
    return this;
}
Also used : SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) 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)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