Search in sources :

Example 11 with SQLPropertyExpr

use of com.alibaba.druid.sql.ast.expr.SQLPropertyExpr in project druid by alibaba.

the class WallVisitorUtils method checkUpdateForMultiTenant.

private static void checkUpdateForMultiTenant(WallVisitor visitor, SQLUpdateStatement x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    if (x == null) {
        throw new IllegalStateException("x is null");
    }
    SQLTableSource tableSource = x.getTableSource();
    String alias = null;
    String matchTableName = null;
    String tenantColumn = null;
    if (tableSource instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) tableSource).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            if (tenantCallBack != null) {
                tenantColumn = tenantCallBack.getTenantColumn(StatementType.UPDATE, tableName);
            }
            if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                tenantColumn = visitor.getConfig().getTenantColumn();
            }
            if (!StringUtils.isEmpty(tenantColumn)) {
                matchTableName = tableName;
                alias = tableSource.getAlias();
            }
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLExpr item = null;
    if (alias != null) {
        item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
    } else {
        item = new SQLIdentifierExpr(tenantColumn);
    }
    SQLExpr value = generateTenantValue(visitor, alias, StatementType.UPDATE, matchTableName);
    SQLUpdateSetItem updateSetItem = new SQLUpdateSetItem();
    updateSetItem.setColumn(item);
    updateSetItem.setValue(value);
    x.addItem(updateSetItem);
    visitor.setSqlModified(true);
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 12 with SQLPropertyExpr

use of com.alibaba.druid.sql.ast.expr.SQLPropertyExpr 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 13 with SQLPropertyExpr

use of com.alibaba.druid.sql.ast.expr.SQLPropertyExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

public boolean visit(final SQLOrderBy x) {
    for (SQLSelectOrderByItem each : x.getItems()) {
        SQLExpr expr = each.getExpr();
        OrderByType orderByType = null == each.getType() ? OrderByType.ASC : OrderByType.valueOf(each.getType());
        if (expr instanceof SQLIntegerExpr) {
            getParseContext().addOrderByColumn(((SQLIntegerExpr) expr).getNumber().intValue(), orderByType);
        } else if (expr instanceof SQLIdentifierExpr) {
            getParseContext().addOrderByColumn(Optional.<String>absent(), ((SQLIdentifierExpr) expr).getName(), orderByType);
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
            getParseContext().addOrderByColumn(Optional.of(sqlPropertyExpr.getOwner().toString()), sqlPropertyExpr.getName(), orderByType);
        }
    }
    return super.visit(x);
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 14 with SQLPropertyExpr

use of com.alibaba.druid.sql.ast.expr.SQLPropertyExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

/**
     * 将GROUP BY列放入parseResult.
     * 直接返回false,防止重复解析GROUP BY表达式.
     * 
     * @param x GROUP BY 表达式
     * @return false 停止遍历AST
     */
@Override
public boolean visit(final MySqlSelectGroupByExpr x) {
    OrderByType orderByType = null == x.getType() ? OrderByType.ASC : OrderByType.valueOf(x.getType());
    if (x.getExpr() instanceof SQLPropertyExpr) {
        SQLPropertyExpr expr = (SQLPropertyExpr) x.getExpr();
        getParseContext().addGroupByColumns(Optional.of(expr.getOwner().toString()), expr.getName(), orderByType);
    } else if (x.getExpr() instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr expr = (SQLIdentifierExpr) x.getExpr();
        getParseContext().addGroupByColumns(Optional.<String>absent(), expr.getName(), orderByType);
    }
    return super.visit(x);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 15 with SQLPropertyExpr

use of com.alibaba.druid.sql.ast.expr.SQLPropertyExpr in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method getColumn.

@Override
protected Column getColumn(SQLExpr expr) {
    Map<String, String> aliasMap = getAliasMap();
    if (aliasMap == null) {
        return null;
    }
    if (expr instanceof SQLPropertyExpr) {
        SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
        String column = ((SQLPropertyExpr) expr).getName();
        if (owner instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) owner).getName();
            String table = tableName;
            if (aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null) {
                return new Column(table, column);
            }
            return handleSubQueryColumn(tableName, column);
        }
        return null;
    }
    if (expr instanceof SQLIdentifierExpr) {
        Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
        if (attrColumn != null) {
            return attrColumn;
        }
        String column = ((SQLIdentifierExpr) expr).getName();
        String table = getCurrentTable();
        if (table != null && aliasMap.containsKey(table)) {
            table = aliasMap.get(table);
            if (table == null) {
                return null;
            }
        }
        if (table != null) {
            return new Column(table, column);
        }
        if (variants.containsKey(column)) {
            return null;
        }
        return new Column("UNKNOWN", column);
    }
    if (expr instanceof SQLBetweenExpr) {
        SQLBetweenExpr betweenExpr = (SQLBetweenExpr) expr;
        if (betweenExpr.getTestExpr() != null) {
            String tableName = null;
            String column = null;
            if (betweenExpr.getTestExpr() instanceof SQLPropertyExpr) {
                //字段带别名的
                tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) betweenExpr.getTestExpr()).getOwner()).getName();
                column = ((SQLPropertyExpr) betweenExpr.getTestExpr()).getName();
                SQLObject query = this.subQueryMap.get(tableName);
                if (query == null) {
                    if (aliasMap.containsKey(tableName)) {
                        tableName = aliasMap.get(tableName);
                    }
                    return new Column(tableName, column);
                }
                return handleSubQueryColumn(tableName, column);
            } else if (betweenExpr.getTestExpr() instanceof SQLIdentifierExpr) {
                column = ((SQLIdentifierExpr) betweenExpr.getTestExpr()).getName();
                //字段不带别名的,此处如果是多表,容易出现ambiguous,
                //不知道这个字段是属于哪个表的,fdbparser用了defaultTable,即join语句的leftTable
                tableName = getOwnerTableName(betweenExpr, column);
            }
            String table = tableName;
            if (aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null && !"".equals(table)) {
                return new Column(table, column);
            }
        }
    }
    return null;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)27 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)17 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 Column (com.alibaba.druid.stat.TableStat.Column)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2