use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLIdentifierExpr x) {
String currentTable = getCurrentTable();
if (containsSubQuery(currentTable)) {
return false;
}
String ident = x.toString();
if (variants.containsKey(ident)) {
return false;
}
Column column = null;
if (currentTable != null) {
column = addColumn(currentTable, ident);
if (column != null && isParentGroupBy(x)) {
this.groupByColumns.add(column);
}
x.putAttribute(ATTR_COLUMN, column);
} else {
boolean skip = false;
for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
SQLTableSource from = ((SQLSelectQueryBlock) parent).getFrom();
if (from instanceof OdpsValuesTableSource) {
skip = true;
break;
}
} else if (parent instanceof SQLSelectQuery) {
break;
}
}
if (!skip) {
column = handleUnkownColumn(ident);
}
if (column != null) {
x.putAttribute(ATTR_COLUMN, column);
}
}
if (column != null) {
SQLObject parent = x.getParent();
if (parent instanceof SQLPrimaryKey) {
column.setPrimaryKey(true);
} else if (parent instanceof SQLUnique) {
column.setUnique(true);
}
setColumn(x, column);
}
return false;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class SQLEvalVisitorUtils method eval.
public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters, boolean throwError) {
SQLEvalVisitor visitor = createEvalVisitor(dbType);
visitor.setParameters(parameters);
sqlObject.accept(visitor);
Object value = getValue(sqlObject);
if (value == null) {
if (throwError && !sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));
}
}
return value;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class SchemaStatVisitor method setColumn.
private void setColumn(SQLExpr x, Column column) {
SQLObject current = x;
for (; ; ) {
SQLObject parent = current.getParent();
if (parent == null) {
break;
}
if (parent instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock query = (SQLSelectQueryBlock) parent;
if (query.getWhere() == current) {
column.setWhere(true);
}
break;
}
if (parent instanceof SQLSelectGroupByClause) {
SQLSelectGroupByClause groupBy = (SQLSelectGroupByClause) parent;
if (current == groupBy.getHaving()) {
column.setHaving(true);
} else if (groupBy.getItems().contains(current)) {
column.setGroupBy(true);
}
break;
}
if (isParentSelectItem(parent)) {
column.setSelec(true);
break;
}
if (parent instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) parent;
if (join.getCondition() == current) {
column.setJoin(true);
}
break;
}
current = parent;
}
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class WallVisitorUtils method checkSchema.
private static boolean checkSchema(WallVisitor visitor, SQLExpr x) {
final WallTopStatementContext topStatementContext = wallTopStatementContextLocal.get();
if (topStatementContext != null && (topStatementContext.fromSysSchema || topStatementContext.fromSysTable)) {
return true;
}
if (x instanceof SQLName) {
String owner = ((SQLName) x).getSimpleName();
owner = WallVisitorUtils.form(owner);
if (isInTableSource(x) && !visitor.getProvider().checkDenySchema(owner)) {
if (!isTopStatementWithTableSource(x) && !isFirstSelectTableSource(x) && !isFirstInSubQuery(x)) {
SQLObject parent = x.getParent();
while (parent != null && !(parent instanceof SQLStatement)) {
parent = parent.getParent();
}
boolean sameToTopSelectSchema = false;
if (parent instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) parent;
SQLSelectQuery query = selectStmt.getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
SQLTableSource from = queryBlock.getFrom();
while (from instanceof SQLJoinTableSource) {
from = ((SQLJoinTableSource) from).getLeft();
}
if (from instanceof SQLExprTableSource) {
SQLExpr expr = ((SQLExprTableSource) from).getExpr();
if (expr instanceof SQLPropertyExpr) {
SQLExpr schemaExpr = ((SQLPropertyExpr) expr).getOwner();
if (schemaExpr instanceof SQLIdentifierExpr) {
String schema = ((SQLIdentifierExpr) schemaExpr).getName();
schema = form(schema);
if (schema.equalsIgnoreCase(owner)) {
sameToTopSelectSchema = true;
}
}
}
}
}
}
if (!sameToTopSelectSchema) {
addViolation(visitor, ErrorCode.SCHEMA_DENY, "deny schema : " + owner, x);
}
} else {
if (topStatementContext != null) {
topStatementContext.setFromSysSchema(Boolean.TRUE);
clearViolation(visitor);
}
}
return true;
}
if (visitor.getConfig().isDenyObjects(owner)) {
addViolation(visitor, ErrorCode.OBJECT_DENY, "deny object : " + owner, x);
return true;
}
}
// if (ownerExpr instanceof SQLPropertyExpr) {
if (x instanceof SQLPropertyExpr) {
return checkSchema(visitor, ((SQLPropertyExpr) x).getOwner());
}
return true;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class WallVisitorUtils method isFirst.
public static boolean isFirst(SQLObject x) {
if (x == null) {
return true;
}
for (; ; ) {
SQLObject parent = x.getParent();
if (!(parent instanceof SQLExpr)) {
return true;
}
if (parent instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) parent;
if (x == binaryExpr.getRight()) {
return false;
}
}
x = parent;
}
}
Aggregations