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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations