use of com.alibaba.druid.sql.parser.SQLStatementParser 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);
}
use of com.alibaba.druid.sql.parser.SQLStatementParser 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.parser.SQLStatementParser in project druid by alibaba.
the class OdpsSelectTest6 method test_distribute_by.
public void test_distribute_by() throws Exception {
File file = new File("/Users/wenshao/Downloads/datasafe_base_dev.udf_test.txt");
String sql = FileUtils.readFileToString(file, "UTF-8");
SQLStatementParser parser = new OdpsStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
// Assert.assertEquals("SELECT *"
// + "\nFROM t"
// + "\nWHERE ds = '20160303'"
// + "\n\tAND hour IN ('18')", SQLUtils.formatOdps(sql));
}
use of com.alibaba.druid.sql.parser.SQLStatementParser in project druid by alibaba.
the class WallVisitorUtils method check.
public static void check(WallVisitor visitor, SQLCommentHint x) {
if (!visitor.getConfig().isHintAllow()) {
addViolation(visitor, ErrorCode.EVIL_HINTS, "hint not allow", x);
return;
}
String text = x.getText();
text = text.trim();
if (text.startsWith("!")) {
text = text.substring(1);
}
if (text.length() == 0) {
return;
}
int pos = 0;
for (; pos < text.length(); pos++) {
char ch = text.charAt(pos);
if (ch >= '0' && ch <= '9') {
continue;
} else {
break;
}
}
if (pos == 5) {
text = text.substring(5);
text = text.trim();
}
text = text.toUpperCase();
boolean isWhite = false;
for (String hint : whiteHints) {
if (text.equals(hint)) {
isWhite = true;
break;
}
}
if (!isWhite) {
if (text.startsWith("FORCE INDEX") || text.startsWith("IGNORE INDEX")) {
isWhite = true;
}
}
if (!isWhite) {
if (text.startsWith("SET")) {
SQLStatementParser parser = new MySqlStatementParser(text);
List<SQLStatement> statementList = parser.parseStatementList();
if (statementList != null && statementList.size() > 0) {
SQLStatement statement = statementList.get(0);
if (statement instanceof SQLSetStatement || statement instanceof MySqlSetCharSetStatement || statement instanceof MySqlSetNamesStatement) {
isWhite = true;
}
}
}
}
if (!isWhite) {
addViolation(visitor, ErrorCode.EVIL_HINTS, "hint not allow", x);
}
}
use of com.alibaba.druid.sql.parser.SQLStatementParser in project druid by alibaba.
the class OverTest method test_over.
public void test_over() throws Exception {
String sql = //
"SELECT SalesOrderID, ProductID, OrderQty" + //
" ,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Total'" + //
" ,AVG(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Avg'" + //
" ,COUNT(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Count'" + //
" ,MIN(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Min'" + //
" ,MAX(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Max' " + //
"FROM Sales.SalesOrderDetail " + "WHERE SalesOrderID IN(43659,43664);";
SQLStatementParser parser = new SQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new SchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getTables().size());
}
Aggregations