use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.
the class OracleExprParser method methodRest.
@Override
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
if (acceptLPAREN) {
accept(Token.LPAREN);
}
if (lexer.token() == Token.PLUS) {
lexer.nextToken();
accept(Token.RPAREN);
return new OracleOuterExpr(expr);
}
if (expr instanceof SQLIdentifierExpr) {
String methodName = ((SQLIdentifierExpr) expr).getName();
SQLMethodInvokeExpr methodExpr = new SQLMethodInvokeExpr(methodName);
if ("trim".equalsIgnoreCase(methodName)) {
if (//
identifierEquals("LEADING") || //
identifierEquals("TRAILING") || identifierEquals("BOTH")) {
methodExpr.putAttribute("trim_option", lexer.stringVal());
lexer.nextToken();
}
if (lexer.token() != Token.FROM) {
SQLExpr trim_character = this.primary();
trim_character.setParent(methodExpr);
if (trim_character instanceof SQLCharExpr) {
methodExpr.putAttribute("trim_character", trim_character);
} else {
methodExpr.getParameters().add(trim_character);
}
}
if (lexer.token() == Token.FROM) {
lexer.nextToken();
SQLExpr trim_source = this.expr();
methodExpr.addParameter(trim_source);
}
accept(Token.RPAREN);
return primaryRest(methodExpr);
}
}
return super.methodRest(expr, false);
}
use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.
the class Unhex method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if (x.getParameters().size() != 1) {
return SQLEvalVisitor.EVAL_ERROR;
}
SQLExpr param0 = x.getParameters().get(0);
if (param0 instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr paramMethodExpr = (SQLMethodInvokeExpr) param0;
if (paramMethodExpr.getMethodName().equalsIgnoreCase("hex")) {
SQLExpr subParamExpr = paramMethodExpr.getParameters().get(0);
subParamExpr.accept(visitor);
Object param0Value = subParamExpr.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
x.putAttribute(EVAL_EXPR, subParamExpr);
return SQLEvalVisitor.EVAL_ERROR;
}
return param0Value;
}
}
param0.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
if (param0Value instanceof String) {
byte[] bytes = HexBin.decode((String) param0Value);
if (bytes == null) {
return SQLEvalVisitor.EVAL_VALUE_NULL;
}
String result;
try {
result = new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return result;
}
return SQLEvalVisitor.EVAL_ERROR;
}
use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.
the class WallVisitorUtils method check.
public static boolean check(WallVisitor visitor, SQLBinaryOpExpr x) {
if (x.getOperator() == SQLBinaryOperator.BooleanOr || x.getOperator() == SQLBinaryOperator.BooleanAnd) {
List<SQLExpr> groupList = SQLUtils.split(x);
for (SQLExpr item : groupList) {
item.accept(visitor);
}
return false;
}
if (x.getOperator() == SQLBinaryOperator.Add || x.getOperator() == SQLBinaryOperator.Concat) {
List<SQLExpr> groupList = SQLUtils.split(x);
if (groupList.size() >= 4) {
int chrCount = 0;
for (int i = 0; i < groupList.size(); ++i) {
SQLExpr item = groupList.get(i);
if (item instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) item;
String methodName = methodExpr.getMethodName().toLowerCase();
if ("chr".equals(methodName) || "char".equals(methodName)) {
if (methodExpr.getParameters().get(0) instanceof SQLLiteralExpr) {
chrCount++;
}
}
} else if (item instanceof SQLCharExpr) {
if (((SQLCharExpr) item).getText().length() > 5) {
chrCount = 0;
continue;
}
}
if (chrCount >= 4) {
addViolation(visitor, ErrorCode.EVIL_CONCAT, "evil concat", x);
break;
}
}
}
}
return true;
}
use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.
the class SQLASTVisitorAdapterTest method test_adapter.
public void test_adapter() throws Exception {
SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
new SQLBinaryOpExpr().accept(adapter);
new SQLInListExpr().accept(adapter);
new SQLSelectQueryBlock().accept(adapter);
new SQLDropTableStatement().accept(adapter);
new SQLCreateTableStatement().accept(adapter);
new SQLDeleteStatement().accept(adapter);
new SQLCurrentOfCursorExpr().accept(adapter);
new SQLInsertStatement().accept(adapter);
new SQLUpdateStatement().accept(adapter);
new SQLNotNullConstraint().accept(adapter);
new SQLMethodInvokeExpr().accept(adapter);
new SQLCallStatement().accept(adapter);
new SQLSomeExpr().accept(adapter);
new SQLAnyExpr().accept(adapter);
new SQLAllExpr().accept(adapter);
new SQLDefaultExpr().accept(adapter);
new SQLCommentStatement().accept(adapter);
new SQLDropViewStatement().accept(adapter);
new SQLSavePointStatement().accept(adapter);
new SQLReleaseSavePointStatement().accept(adapter);
new SQLCreateDatabaseStatement().accept(adapter);
new SQLAlterTableDropIndex().accept(adapter);
new SQLOver().accept(adapter);
new SQLWithSubqueryClause().accept(adapter);
new SQLAlterTableAlterColumn().accept(adapter);
new SQLAlterTableStatement().accept(adapter);
new SQLAlterTableDisableConstraint().accept(adapter);
new SQLAlterTableEnableConstraint().accept(adapter);
new SQLColumnCheck().accept(adapter);
new SQLExprHint().accept(adapter);
new SQLAlterTableDropConstraint().accept(adapter);
}
Aggregations