forked from donhui/sonar-mybatis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/org/sonarsource/plugins/mybatis/sql/AbstractRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/main/java/org/sonarsource/plugins/mybatis/sql/Checker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/main/java/org/sonarsource/plugins/mybatis/sql/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/sonarsource/plugins/mybatis/sql/rules/NoUseSelectAllColumnsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.sonarsource.plugins.mybatis.sql.AbstractRule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.sonarsource.plugins.mybatis.sql.rules.NoUseSelectAllColumnsRule |