Skip to content

Commit

Permalink
SQL AST DEMO
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopyhzy committed Nov 29, 2019
1 parent 464c9c5 commit 671df82
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
<version>1.6.1</version>
</dependency>

<!-- sql parser: druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>

<!-- mybatis and spring-core -->
<dependency>
<groupId>org.mybatis</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sonarsource.plugins.mybatis.sql;

import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractRule extends SQLASTVisitorAdapter {

List<Result> results;

public final void initCheckResults(List<Result> results){
this.results=results;
}

protected final boolean addCheckResult(SQLObject object){
Result result=new Result();
result.setRule(this.getClass().getSimpleName());
result.setObj(object);
return results.add(result);
}

public final List<Result> getCheckResults(){
return results;
}


}
38 changes: 38 additions & 0 deletions src/main/java/org/sonarsource/plugins/mybatis/sql/Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @Author:[email protected]
*/
package org.sonarsource.plugins.mybatis.sql;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public class Checker {

public static List<Result> doCheck(String sql, String dbType){
//USE DRUID SQL AST TO PARSER SQL
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
//USE JAVA SPI TO GET RULE DEFINE IN META-INF/services
ServiceLoader<AbstractRule> rules = ServiceLoader.load(AbstractRule.class,AbstractRule.class.getClassLoader());
//RESULT
List<Result> results=new ArrayList<>();
for (SQLStatement statement : stmtList) {
//DO CHECK
for(AbstractRule rule:rules){
rule.initCheckResults(results);
statement.accept(rule);
}
}

return results;
}

//NEED MOVE TO JUNIT
public static void main(String[] argus){
System.out.println(doCheck("select * from dual",null));
}

}
35 changes: 35 additions & 0 deletions src/main/java/org/sonarsource/plugins/mybatis/sql/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.sonarsource.plugins.mybatis.sql;

import com.alibaba.druid.sql.ast.SQLObject;

public class Result {


@Override
public String toString() {
return "Result{" +
"obj=" + obj +
", rule='" + rule + '\'' +
'}';
}

SQLObject obj;
String rule;

public SQLObject getObj() {
return obj;
}

public void setObj(SQLObject obj) {
this.obj = obj;
}

public String getRule() {
return rule;
}

public void setRule(String rule) {
this.rule = rule;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sonarsource.plugins.mybatis.sql.rules;

import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import org.sonarsource.plugins.mybatis.sql.AbstractRule;

public class NoUseSelectAllColumnsRule extends AbstractRule {
@Override
public boolean visit(SQLAllColumnExpr x) {
this.addCheckResult(x);
return super.visit(x);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.sonarsource.plugins.mybatis.sql.rules.NoUseSelectAllColumnsRule

0 comments on commit 671df82

Please sign in to comment.