我正在使用 Jenkins 和 SONARQUBE PL/SQL 插件进行 Oracle SQL 代码分析,
我需要使用 创建自定义规则XPATH 用于对通过 Jenkins 发送以进行部署的 SQL 脚本文件的质量分析。
我正在尝试创建一个自定义规则,用于检测任何 SQL 命令末尾是否缺少分号 ("; ")。
SQL 终止(“分号”)对于使用 SQLPLUS 部署 SQL 脚本很重要。
代码示例
insert into table_name values('wait','for','completion'); -- compliant with script
insert into table_name values('somename','for','good'); -- compliant with script
**insert into table_name values('someplace','for','game')** -- non compliant as semicolon missing
insert into table_name values('something','for','change'); -- compliant with script
delete from table_name ; -- compliant with script
delete from table_name ; -- compliant with script
update table_name set name='james' where id='22';
有一个插入查询是 缺少分号 ,因此 sonarqube 应该检测到这一点,并使 jenkins 构建失败或未通过 SONAR 质量测试。
请帮助创建 PLSQL 自定义规则,以通过分号检测正确的 SQL 终止。
xpath 的例子是:
/COMPILATION_UNIT/ANY_DML_EXPRESSION/following-sibling::SEMICOLON——类似这样的
最佳答案
您可以按照指南“Create a plugin with custom rules”,使用模板项目 plsql-custom-rules
.
这比 更复杂adding a rule to XPATH ,但您将拥有更多控制权。
To create a check, you can create a subclass of
org.sonar.plsqlopen.checks.AbstractBaseCheck
.
You can use theorg.sonar.check.Rule
andorg.sonar.squidbridge.annotations.SqaleConstantRemediation
annotations to configure the check metadata (name, description, key...).Very often you'll need to override just two methods:
init()
: subscribe to the desired grammar rulesvisitNode(AstNode)
: analyze the nodes that match the subscribed grammar rules
但首先,如 issue 21 中所示,请检查您的代码是否没有出现“无法解析文件”消息的错误。
I just need to know a parser like sonarqube analysis to detect compilation errors in the script file
检查您的案例是否不是可选的分号,如“Semicolon is not required in
CREATE VIEW
”。查看该源代码是检查诸如 Sonar 分析之类的解析器如何检测脚本文件中的编译错误的好方法。
关于jenkins - Sonarqube PLSQL 自定义规则,用于在 SQL 脚本文件中检测正确的 SQL 终止符分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703661/