use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.
the class ExportParameterVisitorUtils method exportParameter.
public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
Object value = null;
boolean replace = false;
if (param instanceof SQLCharExpr) {
value = ((SQLCharExpr) param).getText();
replace = true;
}
if (param instanceof SQLBooleanExpr) {
value = ((SQLBooleanExpr) param).getValue();
replace = true;
}
if (param instanceof SQLNumericLiteralExpr) {
value = ((SQLNumericLiteralExpr) param).getNumber();
replace = true;
}
if (replace) {
SQLObject parent = param.getParent();
if (parent != null) {
List<SQLObject> mergedList = (List<SQLObject>) parent.getAttribute(ParameterizedOutputVisitorUtils.ATTR_MERGED);
if (mergedList != null) {
List<Object> mergedListParams = new ArrayList<Object>(mergedList.size() + 1);
for (int i = 0; i < mergedList.size(); ++i) {
SQLObject item = mergedList.get(i);
if (item instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpItem = (SQLBinaryOpExpr) item;
exportParameter(mergedListParams, binaryOpItem.getRight());
}
}
if (mergedListParams.size() > 0) {
mergedListParams.add(0, value);
value = mergedListParams;
}
}
}
parameters.add(value);
return new SQLVariantRefExpr("?");
}
return param;
}
use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method mergeEqual.
private static boolean mergeEqual(SQLExpr a, SQLExpr b) {
if (!(a instanceof SQLBinaryOpExpr)) {
return false;
}
if (!(b instanceof SQLBinaryOpExpr)) {
return false;
}
SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.
the class SQLStatementParser method parseCall.
public SQLCallStatement parseCall() {
boolean brace = false;
if (lexer.token() == Token.LBRACE) {
lexer.nextToken();
brace = true;
}
SQLCallStatement stmt = new SQLCallStatement(getDbType());
if (lexer.token() == Token.QUES) {
lexer.nextToken();
accept(Token.EQ);
stmt.setOutParameter(new SQLVariantRefExpr("?"));
}
acceptIdentifier("CALL");
stmt.setProcedureName(exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
exprParser.exprList(stmt.getParameters(), stmt);
accept(Token.RPAREN);
}
if (brace) {
accept(Token.RBRACE);
stmt.setBrace(true);
}
return stmt;
}
use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.
the class WallVisitorUtils method check.
public static void check(WallVisitor visitor, SQLSelectItem x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLVariantRefExpr) {
if (!isTopSelectItem(expr) && "@".equals(((SQLVariantRefExpr) expr).getName())) {
addViolation(visitor, ErrorCode.EVIL_NAME, "@ not allow", x);
}
}
if (visitor.getConfig().isSelectAllColumnAllow()) {
return;
}
if (//
expr instanceof SQLAllColumnExpr && x.getParent() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getParent();
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLExprTableSource) {
addViolation(visitor, ErrorCode.SELECT_NOT_ALLOW, "'SELECT *' not allow", x);
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.
the class Demo1 method test_0.
public void test_0() throws Exception {
String sql = "select * from user where uid = ? and uname = ?";
List<Object> parameters = new ArrayList<Object>();
parameters.add(1);
parameters.add("wenshao");
SQLStatementParser parser = new OracleStatementParser(sql);
//
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement first = (SQLStatement) stmtList.get(0);
GetVariantVisitor variantVisitor = new GetVariantVisitor();
first.accept(variantVisitor);
SQLVariantRefExpr firstVar = variantVisitor.getVariantList().get(0);
int varIndex = (Integer) firstVar.getAttribute("varIndex");
Integer param = (Integer) parameters.get(varIndex);
String tableName;
if (param.intValue() == 1) {
tableName = "user_1";
} else {
tableName = "user_x";
}
MyOracleVisitor visitor = new MyOracleVisitor(tableName);
first.accept(visitor);
String realSql = SQLUtils.toOracleString(first);
System.out.println(realSql);
}
Aggregations