Skip to content

Commit 00fbc3e

Browse files
gretardgr
authored andcommitted
Update docs
1 parent 9a7117f commit 00fbc3e

File tree

11 files changed

+390
-102
lines changed

11 files changed

+390
-102
lines changed

docs/customRulesSetup.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Wiki #
22
## Custom rules ##
3-
Currently, plugin supports ability to match code against custom rules defined in xml files with specific format. Below are the details how to start using and creating custom rules.
3+
Currently, plugin supports ability to match code against custom rules defined in xml files in specific format. Below are the details how to use and create custom rules.
44

55

66
### Usage ###
7-
8-
- Create an xml file with custom rules defined, there is an [example file](https://github.com/gretard/sonar-sql-plugin/blob/master/examples/1-tsql/myExampleRepo.customRules). Each file defines a single repository.
9-
- Put custom file into your base directory of the project or you can specify absolute path to the file.
7+
To get started using custom rules:
8+
- Create an xml file with custom rules defined. There is an [example project](https://github.com/gretard/sonar-sql-plugin/blob/master/examples/6-pssql-with-custom-rules). Each xml file defines a single rule repository. You can try scanning example project first.
9+
- Put custom file into your base directory of the project or you can specify relative/absolute path to the file by setting _sonar.sql.rules.path_ property. You can check _sonar-project.properties_ file for further reference at the [example project](https://github.com/gretard/sonar-sql-plugin/blob/master/examples/6-pssql-with-custom-rules)
1010
- Run sonar scanner
1111

12+
> Please check sections below for information on how custom rules are defined.
1213
1314

1415
### Creating custom rules ###
@@ -64,10 +65,12 @@ Mandatory fields for rule:
6465
- More - checks if node's distance to parent node is more than expected value
6566
- Less - checks if node's distance to parent node is less than expected value
6667
- Equals - checks if node's distance to parent node is equal to the expected value
68+
- ruleAppliesTo - can be code or comments
69+
- ruleReportsOn - can be line or file. If file is selected - then violation will be reported once.
6770

6871
Schema can be found at [/sonar-sql-plugin/src/main/resources/schemas/customRules.xsd](https://github.com/gretard/sonar-sql-plugin/blob/master/src/sonar-sql-plugin/src/main/resources/schemas/customRules.xsd).
6972

70-
Below is an example of an xml file contents for with rules:
73+
Below is an example of an xml file contents with rules:
7174

7275
```
7376
<sql-rules repoName="Demo rules" repoKey="tsqlDemoRepo" isAdhoc="true">
@@ -313,7 +316,7 @@ How custom rule works:
313316
1. Finds all nodes of type *Select_listContext*
314317
2. As rule mode is set to *SINGLE* and *FailIfFound* - all nodes are checked if any of the nodes contain child where its text value is equal to * as text check mode is set to *Strict*. Then such nodes are reported as having issues. If mode would be set to *Contains*, then statements such as *SELECT 1 * 3* would be reported as well.
315318

316-
### Creating more complex custom rule with distance ###
319+
### Creating more complex custom rule with distance checks ###
317320

318321
For example, if you wanted to create a rule requiring that each SELECT statement ends with semicolon, you could use the following definition:
319322

docs/mysqlClasses.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# MySQL
22
Supported classes:
3+
- AddOperatorContext
34
- AdministrationStatementContext
45
- AggregateFunctionCallContext
56
- AggregateWindowedFunctionContext
@@ -13,6 +14,8 @@ Supported classes:
1314
- AlterByAddPrimaryKeyContext
1415
- AlterByAddSpecialIndexContext
1516
- AlterByAddUniqueKeyContext
17+
- AlterByAlterCheckTableConstraintContext
18+
- AlterByAlterColumnDefaultContext
1619
- AlterByAlterIndexVisibilityContext
1720
- AlterByAnalyzePartitionContext
1821
- AlterByChangeColumnContext
@@ -56,8 +59,9 @@ Supported classes:
5659
- AlterFunctionContext
5760
- AlterInstanceContext
5861
- AlterLogfileGroupContext
62+
- AlterPartitionContext
63+
- AlterPartitionSpecificationContext
5964
- AlterProcedureContext
60-
- AlterSequenceContext
6165
- AlterServerContext
6266
- AlterSimpleDatabaseContext
6367
- AlterSpecificationContext
@@ -66,12 +70,14 @@ Supported classes:
6670
- AlterUpgradeNameContext
6771
- AlterUserContext
6872
- AlterUserMysqlV56Context
69-
- AlterUserMysqlV57Context
73+
- AlterUserMysqlV80Context
7074
- AlterViewContext
7175
- AnalyzeTableContext
7276
- AssignmentFieldContext
7377
- AtomTableItemContext
78+
- AuthOptionClauseContext
7479
- AuthPluginContext
80+
- AuthenticationRuleContext
7581
- AutoIncrementColumnConstraintContext
7682
- BeginWorkContext
7783
- BetweenPredicateContext
@@ -135,7 +141,6 @@ Supported classes:
135141
- CreateLogfileGroupContext
136142
- CreateProcedureContext
137143
- CreateRoleContext
138-
- CreateSequenceContext
139144
- CreateServerContext
140145
- CreateTableContext
141146
- CreateTablespaceInnodbContext
@@ -144,12 +149,14 @@ Supported classes:
144149
- CreateUdfunctionContext
145150
- CreateUserContext
146151
- CreateUserMysqlV56Context
147-
- CreateUserMysqlV57Context
152+
- CreateUserMysqlV80Context
148153
- CreateViewContext
149154
- CteColumnNameContext
150155
- CteNameContext
151156
- CurrentSchemaPriviLevelContext
152157
- CurrentTimestampContext
158+
- CurrentUserContext
159+
- CurrentUserExpressionContext
153160
- CursorStatementContext
154161
- DataTypeBaseContext
155162
- DataTypeContext
@@ -189,7 +196,6 @@ Supported classes:
189196
- DropLogfileGroupContext
190197
- DropProcedureContext
191198
- DropRoleContext
192-
- DropSequenceContext
193199
- DropServerContext
194200
- DropTableContext
195201
- DropTablespaceContext
@@ -199,6 +205,7 @@ Supported classes:
199205
- ElifAlternativeContext
200206
- EmptyStatement_Context
201207
- EnableTypeContext
208+
- EngineNameBaseContext
202209
- EngineNameContext
203210
- ExecuteStatementContext
204211
- ExistsExpressionAtomContext
@@ -224,6 +231,7 @@ Supported classes:
224231
- FromClauseContext
225232
- FullColumnNameContext
226233
- FullColumnNameExpressionAtomContext
234+
- FullColumnNameListContext
227235
- FullDescribeStatementContext
228236
- FullIdContext
229237
- FunctionArgContext
@@ -257,6 +265,7 @@ Supported classes:
257265
- HavingClauseContext
258266
- HelpStatementContext
259267
- HexadecimalLiteralContext
268+
- HostNameContext
260269
- IfExistsContext
261270
- IfNotExistsContext
262271
- IfStatementContext
@@ -280,16 +289,24 @@ Supported classes:
280289
- IntervalScheduleContext
281290
- IntervalTypeBaseContext
282291
- IntervalTypeContext
292+
- InvisibilityColumnConstraintContext
283293
- IsExpressionContext
284294
- IsNullPredicateContext
285295
- IterateStatementContext
286296
- JoinPartContext
297+
- JoinSpecContext
298+
- JsonColumnContext
299+
- JsonColumnListContext
287300
- JsonExpressionAtomContext
288301
- JsonMemberOfPredicateContext
302+
- JsonOnEmptyContext
303+
- JsonOnErrorContext
289304
- JsonOperatorContext
305+
- JsonTableContext
290306
- JsonValueFunctionCallContext
291307
- KeywordsCanBeIdContext
292308
- KillStatementContext
309+
- LateralStatementContext
293310
- LeaveStatementContext
294311
- LengthOneDimensionContext
295312
- LengthTwoDimensionContext
@@ -307,7 +324,6 @@ Supported classes:
307324
- LoadedTableIndexesContext
308325
- LockActionContext
309326
- LockClauseContext
310-
- LockOptionContext
311327
- LockTableElementContext
312328
- LockTablesContext
313329
- LogicalExpressionContext
@@ -323,7 +339,9 @@ Supported classes:
323339
- MasterStringOptionContext
324340
- MasterUidListOptionContext
325341
- MathExpressionAtomContext
326-
- MathOperatorContext
342+
- ModuleAuthOptionContext
343+
- ModuleContext
344+
- MultOperatorContext
327345
- MultipleDeleteStatementContext
328346
- MultipleUpdateStatementContext
329347
- MysqlVariableContext
@@ -340,6 +358,7 @@ Supported classes:
340358
- NullNotnullContext
341359
- OpenCursorContext
342360
- OptimizeTableContext
361+
- OrReplaceContext
343362
- OrderByClauseContext
344363
- OrderByExpressionContext
345364
- OuterJoinContext
@@ -369,10 +388,10 @@ Supported classes:
369388
- PartitionOptionNodeGroupContext
370389
- PartitionOptionTablespaceContext
371390
- PartitionSimpleContext
372-
- PasswordAuthOptionContext
373391
- PasswordConnectionOptionContext
374392
- PasswordFunctionCallContext
375393
- PasswordFunctionClauseContext
394+
- PasswordModuleOptionContext
376395
- PluginDirConnectionOptionContext
377396
- PositionFunctionCallContext
378397
- PreciseScheduleContext
@@ -394,6 +413,7 @@ Supported classes:
394413
- QueryExpressionNointoContext
395414
- QuerySpecificationContext
396415
- QuerySpecificationNointoContext
416+
- RandomAuthOptionContext
397417
- ReferenceActionContext
398418
- ReferenceColumnConstraintContext
399419
- ReferenceControlTypeContext
@@ -449,7 +469,6 @@ Supported classes:
449469
- SelectSpecContext
450470
- SelectStarElementContext
451471
- SelectStatementContext
452-
- SequenceSpecContext
453472
- SerialDefaultColumnConstraintContext
454473
- ServerOptionContext
455474
- SetAutocommitContext
@@ -501,6 +520,7 @@ Supported classes:
501520
- SimpleIndexDeclarationContext
502521
- SimpleSelectContext
503522
- SimpleStringsContext
523+
- SimpleUserNameContext
504524
- SingleDeleteStatementContext
505525
- SingleUpdateStatementContext
506526
- SoundsLikePredicateContext
@@ -533,6 +553,7 @@ Supported classes:
533553
- TableConstraintContext
534554
- TableFlushOptionContext
535555
- TableIndexesContext
556+
- TableJsonContext
536557
- TableNameContext
537558
- TableOptionAutoIncrementContext
538559
- TableOptionAutoextendSizeContext
@@ -547,6 +568,7 @@ Supported classes:
547568
- TableOptionDataDirectoryContext
548569
- TableOptionDelayContext
549570
- TableOptionEncryptionContext
571+
- TableOptionEncryptionKeyIdContext
550572
- TableOptionEngineAttributeContext
551573
- TableOptionEngineContext
552574
- TableOptionIndexDirectoryContext
@@ -555,6 +577,8 @@ Supported classes:
555577
- TableOptionMaxRowsContext
556578
- TableOptionMinRowsContext
557579
- TableOptionPackKeysContext
580+
- TableOptionPageCompressedContext
581+
- TableOptionPageCompressionLevelContext
558582
- TableOptionPasswordContext
559583
- TableOptionPersistentContext
560584
- TableOptionRecalculationContext
@@ -564,6 +588,7 @@ Supported classes:
564588
- TableOptionStartTransactionContext
565589
- TableOptionTableTypeContext
566590
- TableOptionTablespaceContext
591+
- TableOptionTransactionalContext
567592
- TableOptionUnionContext
568593
- TablePairContext
569594
- TableSourceBaseContext
@@ -572,6 +597,7 @@ Supported classes:
572597
- TableSourceNestedContext
573598
- TableSourcesContext
574599
- TableSourcesItemContext
600+
- TableStatementContext
575601
- TableTypeContext
576602
- TablesContext
577603
- TablespaceStorageContext
@@ -613,6 +639,8 @@ Supported classes:
613639
- UtilityStatementContext
614640
- UuidSetContext
615641
- ValuesFunctionCallContext
642+
- ValuesStatementContext
643+
- VariableAssignExpressionAtomContext
616644
- VariableClauseContext
617645
- VisibilityColumnConstraintContext
618646
- WaitNowaitClauseContext
@@ -624,6 +652,8 @@ Supported classes:
624652
- WindowNameContext
625653
- WindowSpecContext
626654
- WithClauseContext
655+
- WithLateralStatementContext
656+
- WithStatementContext
627657
- XaCommitWorkContext
628658
- XaEndTransactionContext
629659
- XaPrepareStatementContext

docs/pluginRules.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
## C001 - SLEEP/WAITFOR is used.
2-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
2+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
33
SLEEP/WAITFOR is used<h2>Code examples</h2><h3>Non-compliant</h3><pre><code>WAITFOR '10:00:00';</code></pre>
44

55
## C002 - SELECT * is used
6-
<p>Supported dialects: MYSQL,TSQL</p>
6+
<p>Supported dialects: MYSQL,PSSQLV2,TSQL</p>
77
<h2>Description</h2><p>SELECT * is used. Please list names.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT t1.*, t2.* from dbo.test as t1 inner join dbo.test2 as t2 on t1.id=t2.id;</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname from dbo.test;</code></pre><pre><code>SELECT name, surname, 1 * 3 from dbo.test;</code></pre>
88

99
## C003 - INSERT statement without columns listed
10-
<p>Supported dialects: MYSQL,TSQL</p>
11-
<h2>Description</h2><p>INSERT statement does not have columns listed. Always use a column list in your INSERT statements.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>INSERT INTO dbo.test VALUES (1,2);</code></pre><h3>Compliant</h3><pre><code>INSERT INTO dbo.test (a,b) VALUES (1,2);</code></pre>
10+
<p>Supported dialects: MYSQL,PSSQLV2,TSQL</p>
11+
<h2>Description</h2><p>INSERT statement does not have columns listed. Always use a column list in your INSERT statements.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>INSERT INTO dbo.test VALUES (1,2);
12+
INSERT INTO dbo.test2 VALUES (1,2);</code></pre><h3>Compliant</h3><pre><code>INSERT INTO dbo.test (a,b) VALUES (1,2);</code></pre>
1213

1314
## C004 - ORDER BY clause contains positional references
14-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
15+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
1516
<h2>Description</h2><p>Do not use column numbers in the ORDER BY clause. Always use column names in an order by clause. Avoid positional references.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT * from dbo.test order by 1, 2;</code></pre><h3>Compliant</h3><pre><code>SELECT * from dbo.test order by name;</code></pre>
1617

1718
## C005 - EXECUTE/EXEC for dynamic query is used
@@ -23,7 +24,7 @@ SLEEP/WAITFOR is used<h2>Code examples</h2><h3>Non-compliant</h3><pre><code>WAIT
2324
<h2>Description</h2><p>Use of NOLOCK might cause data inconsistency problems.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname from dbo.test WITH (NOLOCK);</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname from dbo.test;</code></pre>
2425

2526
## C009 - Non-sargable statement used
26-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
27+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
2728
<h2>Description</h2><p>Use of non-sargeable arguments might cause performance problems.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname from dbo.test where year(date) > 2008 and month = 12;</code></pre><pre><code>SELECT name, surname from dbo.test where name like '%red' </code></pre><h3>Compliant</h3><pre><code>SELECT MAX(RateChangeDate) FROM HumanResources.EmployeePayHistory WHERE BusinessEntityID = 1</code></pre><pre><code>SELECT name, surname from dbo.test where date between 2008-10-10 and 2010-10-10;</code></pre><pre><code>SELECT max(price) from dbo.items;</code></pre>
2829

2930
## C010 - Defined primary key is not using recommended naming convention
@@ -43,26 +44,41 @@ PRIMARY KEY (Id)
4344
<h2>Description</h2><p>Defined foreign key is not using recommended naming convention to start with FK_.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>ALTER TABLE dbo.Orders ADD CONSTRAINT ClientId FOREIGN KEY (ClientId) REFERENCES dbo.Clients(Id); </code></pre><h3>Compliant</h3><pre><code>ALTER TABLE dbo.Orders ADD CONSTRAINT FK_ClientId FOREIGN KEY (ClientId) REFERENCES dbo.Clients(Id); </code></pre>
4445

4546
## C012 - Comparison operator (=, <>, !=) to check if value is null used
46-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
47+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
4748
<h2>Description</h2><p>It is not advisable to use comparison operator to check if value is null as comparison operators return UNKNOWN when either or both arguments are NULL. Please use IS NULL or IS NOT NULL instead.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT * from dbo.test where name = null and surname = 'Test' ;</code></pre><pre><code>SELECT * from dbo.test where name != null;</code></pre><pre><code>SELECT * from dbo.test where name <> null;</code></pre><h3>Compliant</h3><pre><code>SELECT * from dbo.test where name IS NULL;</code></pre><pre><code>SELECT * from dbo.test where name IS NOT NULL;</code></pre><pre><code>SELECT * from dbo.test where name = 'test';</code></pre>
4849

4950
## C013 - Defined index name is not using recommended naming convention
5051
<p>Supported dialects: TSQL</p>
5152
<h2>Description</h2><p>Defined index name is not using recommended naming convention to start with IX_.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>CREATE UNIQUE INDEX Test_Name on dbo.test (Name);</code></pre><h3>Compliant</h3><pre><code>CREATE UNIQUE INDEX IX_Test_Name on dbo.test (Name);</code></pre>
5253

5354
## C014 - OR verb is used in a WHERE clause
54-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
55+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
5556
<h2>Description</h2><p>It is advisable to consider using UNION/UNION ALL operator instead of OR verb in the WHERE clause.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname, count from dbo.test where name = 'Test' OR surname = 'Testor';</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname, count from dbo.test where name = 'or' and surname = 'TestOR';</code></pre>
5657

5758
## C015 - UNION operator is used
58-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
59+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
5960
<h2>Description</h2><p>It is advisable to consider using UNION ALL operator instead of UNION.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname, count from dbo.test union SELECT name, surname, count from dbo.test2;</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname, count from dbo.test union all SELECT name, surname, count from dbo.test2;</code></pre>
6061

6162
## C016 - IN/NOT IN is used for a subquery
62-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
63+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
6364
<h2>Description</h2><p>Consider using EXISTS/NOT EXISTS operator instead of IN for a subquery.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname, count from dbo.test where locationID in (select id from dbo.locations);</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname, count from dbo.test where locationID in (1,2,3);</code></pre><pre><code>SELECT name, surname, count from dbo.test where exists (select 1 from dbo.locations where id = locationID);</code></pre>
6465

6566
## C017 - ORDER BY clause does not contain order (ASC/DESC)
66-
<p>Supported dialects: MYSQL,PSSQL,TSQL</p>
67+
<p>Supported dialects: MYSQL,PSSQL,PSSQLV2,TSQL</p>
6768
<h2>Description</h2><p>It is advisable to specidy order how rows should be ordered.</p><h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT name, surname from dbo.test order by name, surname asc;</code></pre><h3>Compliant</h3><pre><code>SELECT name, surname from dbo.test order by name desc, surname asc;</code></pre>
6869

70+
## C020 - HINT is used
71+
<p>Supported dialects: VSQL</p>
72+
HINT is used. Consider rewriting this statement.<h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT /*+DIRECT*/ * FROM test_table1;</code></pre><pre><code>CREATE TABLE test_table2 AS SELECT /*+DIRECT*/ * FROM test_table1;</code></pre><h3>Compliant</h3><pre><code>SELECT * FROM test_table1;</code></pre>
73+
74+
## C021 - COMMIT is missing
75+
<p>Supported dialects: VSQL</p>
76+
COMMIT is missing after UPDATE/DELETE statement. If you run script without autocommit - your changes might be lost.<h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT 1; DELETE FROM temp1; SELECT 2;</code></pre><pre><code>COMMIT; DELETE FROM temp1;</code></pre><h3>Compliant</h3><pre><code>SELECT 1; DELETE FROM temp1; COMMIT; SELECT 2; </code></pre>
77+
78+
## C030 - File does not start with multiline/header comment
79+
<p>Supported dialects: null</p>
80+
File does not start with multiline/header comment.<h2>Code examples</h2><h3>Non-compliant</h3><pre><code>SELECT * FROM test_table1;</code></pre><pre><code>SELECT * FROM test_table1; /*additionalComment*/ </code></pre><h3>Compliant</h3><pre><code>/* AUTHOR: test
81+
Date: 2020-01-01
82+
*/
83+
SELECT * FROM test_table1;</code></pre>
84+

0 commit comments

Comments
 (0)