- Notifications
You must be signed in to change notification settings - Fork5
Trivadis/plsql-cop-validators
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
db* CODECOP supports custom validators. A validator must implement thePLSQLCopValidator
Java interface and has to be a direct or indirect descendant of thePLSQLValidator
class. Such a class can be used in the command line utility and the SQL Developer extension.
For SonarQube aValidationConfig
is required. A config defines the validator with its rules and quality profile for SonarQube. SeeGLPValidatorConfig. The referenced XML files are generated based on the validator and the optionalsample guidelines.
You may use these validators as is or amend/extend them to suit your needs.
This project provides the following four custom validators in the packagecom.trivadis.tvdcc.validators
:
Class | Description |
---|---|
TrivadisPlsqlNaming | ChecksNaming Conventions of the Trivadis PL/SQL & SQL Coding Guidelines |
GLP | Checks naming of global and local variables and parameters |
SQLInjection | Looks for SQL injection vulnerabilities, e.g. unasserted parameters in dynamic SQL |
Hint | Looks for unknown hints and invalid table references |
OverrideTrivadisGuidelines | Extends TrivadisGuidelines3 and overrides check forG-1050. |
TrivadisGuidelines3Plus | Combines the validators TrivadisPlsqlNaming, SQLInjection and OverrideTrivadisGuidelines. |
This validator implements 15 guidelines to cover the chapter2.2 Naming Conventions of the Trivadis PL/SQL & SQL Coding Guidelines.
Guideline | Message |
---|---|
G-9101 | Always name global variables to match '^g_.+$'. |
G-9102 | Always name local variables to match '^l_.+$'. |
G-9103 | Always name cursors to match '^c_.+$'. |
G-9104 | Always name records to match '^r_.+$'. |
G-9105 | Always name collection types (arrays/tables) to match '^t_.+$'. |
G-9106 | Always name objects to match '^o_.+$'. |
G-9107 | Always name cursor parameters to match '^p_.+$'. |
G-9108 | Always name in parameters to match '^in_.+$'. |
G-9109 | Always name out parameters to match '^out_.+$'. |
G-9110 | Always name in/out parameters to match '^io_.+$'. |
G-9111 | Always name record type definitions to match '^r_.+_type$'. |
G-9112 | Always name collection type definitions (arrays/tables) to match '^t_.+_type$'. |
G-9113 | Always name exceptions to match '^e_.+$'. |
G-9114 | Always name constants to match '^co_.+$'. |
G-9115 | Always name subtypes to match '^.+_type$'. |
These regular expressions can be customized by using aTrivadisPlsqlNaming.properties
file. This file must be placed in the user's home directory ($HOME
for Linux or macOS and%HOMEDRIVE%%HOMEPATH%
for Windows). If a property is omitted it will fall back to the default value (see table above). Furthermore, you can use Java system properties to configure the naming conventions, e.g.-DREGEX_CONSTANT_NAME=^k_.+$
. However, theTrivadisPlsqlNaming.properties
file overrides system properties that where set when starting the JVM.
Here's an example of theTrivadisPlsqlNaming.properties
file content using default values for all properties:
# Override default for TrivadisPlsqlNaming validatorREGEX_GLOBAL_VARIABLE_NAME = ^g_.+$REGEX_LOCAL_VARIABLE_NAME = ^l_.+$REGEX_CURSOR_NAME = ^c_.+$REGEX_RECORD_NAME = ^r_.+$REGEX_ARRAY_NAME = ^t_.+$REGEX_OBJECT_NAME = ^o_.+$REGEX_CURSOR_PARAMETER_NAME = ^p_.+$REGEX_IN_PARAMETER_NAME = ^in_.+$REGEX_OUT_PARAMETER_NAME = ^out_.+$REGEX_IN_OUT_PARAMETER_NAME = ^io_.+$REGEX_RECORD_TYPE_NAME = ^r_.+_type$REGEX_ARRAY_TYPE_NAME = ^t_.+_type$REGEX_EXCEPTION_NAME = ^e_.+$REGEX_CONSTANT_NAME = ^co_.+$REGEX_SUBTYPE_NAME = ^.+_type$# Override defaults for TrivadisGuidelines3 validatorcop.1050.threshold = 2cop.2185.threshold = 4cop.2410.boolean.strings = true,false,t,f,0,1,2,yes,no,y,n,ja,nein,j,si,s,oui,non,o,l_true,l_false,co_true,co_false,co_numeric_true,co_numeric_falsecop.5050.threshold.from = 20000cop.5050.threshold.to = 20999cop.7210.threshold = 2000
If you are using theTrivadisGuidelines3Plus
validator, the properties for theTrivadisGuidelines3
validator can also be configured in this properties file.
This is a simple validator to check the following naming convention guidelines:
Guideline | Message |
---|---|
G-9001 | Always prefix global variables with 'g_'. |
G-9002 | Always prefix local variables with 'l_'. |
G-9003 | Always prefix parameters with 'p_'. |
This validator checks just these three guidelines. It does not extend theTrivadis PL/SQL & SQL Coding Guidelines.
This validator implements the following guideline:
Guideline | Message |
---|---|
G-9501 | Never use parameter in string expression of dynamic SQL. Use asserted local variable instead. |
It looks for unasserted parameters used inEXECUTE IMMEDIATE
statements andOPEN FOR
statements. All parameters used in these statements must be asserted with one of the subprograms provided byDBMS_ASSERT
.
Seeexample
This validator implements the following guidelines:
Guideline | Message |
---|---|
G-9600 | Never define more than one comment with hints. |
G-9601 | Never use unknown hints. |
G-9602 | Always use the alias name instead of the table name. |
G-9603 | Never reference an unknown table/alias. |
G-9604 | Never use an invalid stats method. |
G-9605 | Never use an invalid stats keyword. |
Only the first comment containing hints is considered by the optimizer, therefore all hints violatingG-9600
are treated as ordinary comments by the Oracle Database.
Using unknown hints might invalidate all subsequent hints. This happens when you use for exampleNOLOGGING
. That's expected and not a bug. See MOS note 285285.1 or bug 8432870 for details. So, do not ignoreG-9601
violations.
There are various hints that reference a table. The validator checks if the reference is valid. If an alias is defined for a table, but the table name is used in the hint then aG-9602
violation is reported. If the table reference in the hint is neither a table name nor an alias then aG-9603
violation is thrown. These violations should not be ignored either.
However, the vadiator ignores the optional query block in the hint and assumes that all table references belong to the current query block. As a result some false positives are possible. Furthermore references to a table in a query block (e.g.emp@qb1
) are not checked. This might lead to some false negatives.
The guidelinesG-9604
andG-9605
check the validity of themethod
andkeyword
in thetable_stats
hint.
This validator shows how existing guideline checks can be overridden.
The following guideline is overriden:
Guideline | Message |
---|---|
G-1050 | Avoid using literals in your code. |
Literals as part of aLogger package call are not reported (see alsoissue 8).
This validator combines the validators
This way you can deal with an unbound number of validators without comproming the maintainablity.
Download db* CODECOP Command Line
Download db* CODECOP Command Line fromhere.
Install db* CODECOP Command Line
Uncompress the distributed db* CODECOP archive file (e.g. tvdcc-4.x.x.zip) into a folder of your choice (hereinafter referred to as
TVDCC_HOME
). I use$HOME/tvdcc
forTVDCC_HOME
on my MacBook Pro.For Windows platforms only: Amend the settings for JAVA_HOME in the tvdcc.cmd file to meet your environment settings. Use at least a Java 8 runtime environment (JRE) or development kit (JDK).
Include
TVDCC_HOME
in your PATH environment variable for handy interactive usage.Optionally copy your commercial license file into the
TVDCC_HOME
directory. For simplicity name the file tvdcc.lic.
Download Custom Validators
Download
sonar-plsql-cop-custom-validators-plugin-4.x.x.jar
fromhere.Install Custom Validators
Copy the previously downloaded jar file into the
plugin
folder of yourTVDCC_HOME
folder.Run db* CODECOP with a Custom Validator
Open a terminal window, change to the
TVDCC_HOME
directory and run the following command to all files in$HOME/github/utPLSQL/source
with the custom validatorcom.trivadis.tvdcc.validators.TrivadisGuidelines3Plus
:./tvdcc.sh path=$HOME/github/utPLSQL/source validator=com.trivadis.tvdcc.validators.TrivadisGuidelines3Plus
The
tvdcc_report.html
file contain the results. Here's an excerpt:
Install db* CODECOP Command Line
As explainedabove.
Download db* CODECOP for SQL Developer
Download db* CODECOP for SQL Developer fromhere.
Install db* CODECOP for SQL Developer
- Start SQL Developer
- Select
Check for Updates…
in the help menu. - Use the
Install From Local File(s)
option to install the previously downloadedTVDCC_for_SQLDev-*.zip
file. - Restart SQL Developer
Configure Validator
Configure the validator in SQL Developer as shown in the following screenshot:
Check Code
Open the code to be checked in an editor and select
Check
from the context menu.The check result is shown by default at the bottom of your SQL Developer workspace.
Install SonarQube
Install Standalone or Secondary Plugin
Install Child Plugin (Custom Validator Plugin)
Download the
sonar-plsql-cop-custom-validators-plugin-x.x.x.jar
fromreleases. Then copy it to the extensions/plugins folder of yourSonarQube installation and restart the SonarQube server.Configure Validator Config Class
Login as Administrator in SonarQube. Go to
Administration
. SelectGeneral Settings
fromConfiguration
and click ondb* CODECOP
. Type one of the following into theValidator Config class
field:- com.trivadis.sonar.plugin.GLPValidatorConfig
- com.trivadis.sonar.plugin.TrivadisGuidelines3PlusValidatorConfig
Restart SonarQube
Select
System
in theAdministration
menu and click onRestart Server
.Important: Starting with SonarQube 9.1, rules and quality profiles are cached to improve the startup time (seeSONAR-15237). To apply changes to
Language Key
andValidator Config class
, the PL/SQL specific plugins must be uninstalled and then reinstalled. Configuration changes are not affected by this process.Run Analysis
Start an analysis from the command line as follows (seedocs for more information):
cd $HOME/github/trivadis/plsql-cop-validators/src/main/resourcessonar-scanner -Dsonar.projectKey="plsql-cop-validators"
By default the source code in the current directory is analyzed. Here's the result:
Install db* CODECOP Command Line
As explainedabove.
Install Maven
Download and install Apache Maven 3.8.6
Clone the cop-validators repository
Clone or download this repository.
Install the required db* CODECOP libraries
These libraries are not available in public Maven repositories. However, they are partof the db* CODECOP Command Line installed in step 1. You need to install these librariesinto your local Maven repository.
Open a terminal window in the cop-validators root folder and run Run the following shell script:
./install_tvdcc_libs.sh
The shell script expects to find the library
tvdcc.jar
in$HOME/tvdcc
. If it is not there, pass the path to the directory as parameter to this script. For example./install_tvdcc_libs.sh $HOME/tvdcc
Build validator jar file
Open a terminal window in the cop-validators root folder and run the Maven build by the following command
mvn clean package
The db* CODECOP Validators are licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. You may obtain a copy of the License athttps://creativecommons.org/licenses/by-nc-nd/3.0/.
About
db* CODECOP Validators
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.