use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class OracleSchemaStatVisitor method visit.
@Override
public boolean visit(OracleSelectJoin x) {
super.visit(x);
for (SQLExpr item : x.getUsing()) {
if (item instanceof SQLIdentifierExpr) {
String columnName = ((SQLIdentifierExpr) item).getName();
String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
if (leftTable != null && rightTable != null) {
Relationship relationship = new Relationship();
relationship.setLeft(new Column(leftTable, columnName));
relationship.setRight(new Column(rightTable, columnName));
relationship.setOperator("USING");
relationships.add(relationship);
}
if (leftTable != null) {
addColumn(leftTable, columnName);
}
if (rightTable != null) {
addColumn(rightTable, columnName);
}
}
}
return false;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method merge.
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
SQLExpr left = x.getLeft();
SQLExpr right = x.getRight();
SQLObject parent = x.getParent();
if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
if (//
x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
left.putAttribute(ATTR_PARAMS_SKIP, true);
}
if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
} else {
left.putAttribute(ATTR_PARAMS_SKIP, true);
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
}
return x;
}
for (; ; ) {
if (x.getRight() instanceof SQLBinaryOpExpr) {
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
if (leftBinaryExpr.getRight().equals(x.getRight())) {
x = leftBinaryExpr;
v.incrementReplaceCunt();
continue;
}
}
SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
if (mergedRight != x.getRight()) {
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
v.incrementReplaceCunt();
}
x.setParent(parent);
}
break;
}
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
if (mergedLeft != x.getLeft()) {
SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
tmp.setParent(parent);
x = tmp;
v.incrementReplaceCunt();
}
}
// ID = ? OR ID = ? => ID = ?
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
if (mergeEqual(leftBinary, rightBinary)) {
v.incrementReplaceCunt();
leftBinary.setParent(x.getParent());
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
if (//
isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
if (mergeEqual(leftBinary.getRight(), x.getRight())) {
v.incrementReplaceCunt();
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
}
}
}
return x;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class SchemaStatVisitor method handleSubQueryColumn.
protected Column handleSubQueryColumn(String alias, SQLObject query) {
if (query instanceof SQLSelect) {
query = ((SQLSelect) query).getQuery();
}
SQLSelectQueryBlock queryBlock = null;
List<SQLSelectItem> selectList = null;
if (query instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) query;
if (queryBlock.getGroupBy() != null) {
return null;
}
selectList = queryBlock.getSelectList();
}
if (selectList != null) {
boolean allColumn = false;
String allColumnOwner = null;
for (SQLSelectItem item : selectList) {
if (!item.getClass().equals(SQLSelectItem.class)) {
continue;
}
String itemAlias = item.getAlias();
SQLExpr itemExpr = item.getExpr();
String ident = itemAlias;
if (itemAlias == null) {
if (itemExpr instanceof SQLIdentifierExpr) {
ident = itemAlias = itemExpr.toString();
} else if (itemExpr instanceof SQLPropertyExpr) {
itemAlias = ((SQLPropertyExpr) itemExpr).getName();
ident = itemExpr.toString();
}
}
if (alias.equalsIgnoreCase(itemAlias)) {
Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN);
if (column != null) {
return column;
} else {
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLSubqueryTableSource) {
SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
Column subQueryColumn = handleSubQueryColumn(ident, select);
return subQueryColumn;
}
}
}
if (itemExpr instanceof SQLAllColumnExpr) {
allColumn = true;
} else if (itemExpr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
if (propertyExpr.getName().equals("*")) {
SQLExpr owner = propertyExpr.getOwner();
if (owner instanceof SQLIdentifierExpr) {
allColumnOwner = ((SQLIdentifierExpr) owner).getName();
allColumn = true;
}
}
}
}
if (allColumn) {
SQLTableSource from = queryBlock.getFrom();
String tableName = getTable(from, allColumnOwner);
if (tableName != null) {
return new Column(tableName, alias);
}
}
}
return null;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLJoinTableSource x) {
x.getLeft().accept(this);
x.getRight().accept(this);
SQLExpr condition = x.getCondition();
if (condition != null) {
condition.accept(this);
}
return false;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class WallVisitorUtils method checkSelelct.
public static void checkSelelct(WallVisitor visitor, SQLSelectQueryBlock x) {
for (SQLSelectItem item : x.getSelectList()) {
item.setParent(x);
}
if (x.getInto() != null) {
checkReadOnly(visitor, x.getInto());
}
if (!visitor.getConfig().isSelectIntoAllow() && x.getInto() != null) {
addViolation(visitor, ErrorCode.SELECT_INTO_NOT_ALLOW, "select into not allow", x);
return;
}
if (x.getFrom() != null) {
x.getFrom().setParent(x);
}
SQLExpr where = x.getWhere();
if (where != null) {
where.setParent(x);
checkCondition(visitor, x.getWhere());
Object whereValue = getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck());
if (Boolean.TRUE == whereValue) {
if (visitor.getConfig().isSelectWhereAlwayTrueCheck() && visitor.isSqlEndOfComment() && !isSimpleConstExpr(where)) {
// 简单表达式
addViolation(visitor, ErrorCode.ALWAYS_TRUE, "select alway true condition not allow", x);
}
}
}
checkSelectForMultiTenant(visitor, x);
// checkConditionForMultiTenant(visitor, x.getWhere(), x);
}
Aggregations