Skip to content

Commit 54495ab

Browse files
committed
Prepare for release
1 parent da966fb commit 54495ab

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,59 @@ Please configure additional properties:
123123
- When you want SQL plugin execution ( this will disable pssql plugin):
124124
`sonar.lang.patterns.plsqlopen=na`
125125

126+
## Using command line tools
127+
With the plugin - there are additional 2 cli tools available (they are not required for sonar execution):
128+
129+
- **sql-sca-cli.jar** - allows to execute sql code analysis from the command line
130+
- **rulesHelper.jar** - command line helper tool for writing custom sql rules
131+
132+
### sql-sca-cli
133+
Usage:
134+
135+
- ```java -jar sql-sca-cli.jar --help``` - will print help
136+
- ```java -jar sql-sca-cli.jar``` - will scan current directory
137+
- ```java -jar sql-sca-cli.jar -i /home/test``` - will scan test directory
138+
139+
Full help info:
140+
```usage: sql-sca-cli
141+
-c,--custom-rules-path <arg> path to custom rules directory,
142+
defaults to current directory
143+
-csuffix,--custom-rules-suffix <arg> custom rules suffix, defaults to:
144+
.customRules
145+
-d,--dialect <arg> SQL dialect, defaults to: tsql,
146+
possible values: [TSQL, PSSQL,
147+
MYSQL, VSQL, PSSQLV2]
148+
-h,--help show help
149+
-i,--input <arg> input directory for analysis,
150+
defaults to current directory
151+
-p,--prefixes <arg> file prefixes for analysis,
152+
defaults to: .sql
153+
-warnOnly,--warnOnly flag whether tool should report
154+
warnings only
155+
```
156+
157+
### rulesHelper
158+
Usage:
159+
160+
- ```java -jar rulesHelper.jar``` - will print help
161+
- ```java -jar rulesHelper.jar print text "SELECT * FROM dbo.test;" tsql``` - will print parsed AST tree for TSQL dialect
162+
163+
Full help info:
164+
165+
```
166+
Please pass the following:
167+
action (print or verify)
168+
type (text or file)
169+
value (sql string or path to folder)
170+
dialect (tsql, pssql, mysql, pssql, pssqlv2)
171+
Example:
172+
print text "SELECT * FROM dbo.test;" tsql
173+
174+
Example:
175+
verify file "c:/tests/customRules.rules;" mysql
176+
```
177+
178+
126179
## Contributing ##
127180
Added container definitions for easy development with VSCode. Download the [remote containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) and let it figure out the maven targets.
128181
<img width="1917" alt="vscode_remote_containers_extension_maven" src="https://user-images.githubusercontent.com/3657015/125957363-653c9f6f-b5cc-4a3c-96ef-9dc18d0f8bfb.png">

src/sonar-sql-plugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
<version>4.13.2</version>
7171
<scope>test</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>commons-cli</groupId>
75+
<artifactId>commons-cli</artifactId>
76+
<version>1.5.0</version>
77+
<scope>test</scope>
78+
</dependency>
7379
<dependency>
7480
<groupId>com.fasterxml.jackson.dataformat</groupId>
7581
<artifactId>jackson-dataformat-xml</artifactId>

src/sonar-sql-plugin/src/main/java/org/antlr/sql/dialects/Dialects.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.antlr.sql.dialects;
22

3+
import java.util.Arrays;
34
import java.util.Collections;
5+
import java.util.LinkedList;
46
import java.util.List;
57

68
import org.antlr.sql.dialects.rules.CommonRules;
@@ -15,6 +17,24 @@ public AntlrContext parse(String text) {
1517
return parse(text, Collections.emptyList());
1618
}
1719

20+
public List<SqlRules> getDialectRules(SqlRules... additionalRules) {
21+
var rules = new LinkedList<SqlRules>();
22+
SQLDialectRules.INSTANCE.getRules().forEach(r -> {
23+
if (r.getDialect() == null || this.name().equalsIgnoreCase(r.getDialect())) {
24+
rules.add(r);
25+
}
26+
27+
});
28+
29+
rules.addAll(CommonRules.INSTANCE.getRules());
30+
rules.addAll(Arrays.asList(additionalRules));
31+
return rules;
32+
}
33+
34+
public AntlrContext parseInitialContext(String text) {
35+
return this.dialect.parse(text);
36+
}
37+
1838
public AntlrContext parse(String text, List<SqlRules> rules) {
1939
AntlrContext ctx = this.dialect.parse(text);
2040
ctx.initialContents = text;

src/sonar-sql-plugin/src/main/java/org/sonar/plugins/sql/issues/RuleToCheck.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public static List<RuleToCheck> createCodeList2(SqlRules... list) {
5757
return items;
5858
}
5959

60+
61+
public static List<RuleToCheck> createCodeList2(List<SqlRules> list) {
62+
List<RuleToCheck> items = new LinkedList<>();
63+
for (SqlRules item : list) {
64+
item.getRule().forEach(rule -> {
65+
if (rule.getRuleAppliesTo() == null || "code".equalsIgnoreCase(rule.getRuleAppliesTo())) {
66+
items.add(new RuleToCheck(item, rule));
67+
}
68+
});
69+
}
70+
71+
return items;
72+
}
73+
74+
75+
6076
public static List<RuleToCheck> createCommentsList(SqlRules... list) {
6177
List<RuleToCheck> items = new LinkedList<>();
6278
for (SqlRules item : list) {

src/sonar-sql-plugin/src/test/java/org/antlr/sql/tools/RulesHelperTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void main(String[] args) throws FileNotFoundException, IOException
2424
System.out.println("\taction (print or verify)");
2525
System.out.println("\ttype (text or file)");
2626
System.out.println("\tvalue (sql string or path to folder) ");
27-
System.out.println("\tdialect (tsql, pssql, mysql) ");
27+
System.out.println("\tdialect (tsql, pssql, mysql, pssql, pssqlv2) ");
2828

2929
System.out.println("Example:\r\nprint text \"SELECT * FROM dbo.test;\" tsql\r\n");
3030
System.out.println("Example:\r\nverify file \"c:/tests/customRules.rules;\" mysql");

0 commit comments

Comments
 (0)