Help with writing rules

I’ve never works with XPath and never wrote rules in sonar. I don’t know where put on the non-compliant or compliant code in XPath. I need check if a procedure is calling inside of exception code block.

//*Non-compliant*/
BEGIN
 P_EXEC_ROLLBACK_TRATIVA('Parameter1','Parameter2');
END;

//*compliant*/
BEGIN
 EXCEPTION
  P_EXEC_ROLLBACK_TRATIVA('Parameter1','Parameter2');
END;

I have no idea where to start.

Hi Welton.

I think the basics are covered by https://docs.sonarqube.org/display/DEV/Adding+Coding+Rules+using+XPath

About your specific situation, your PL/SQL code isn’t valid, so I fixed it to test:

BEGIN
 P_EXEC_ROLLBACK_TRATIVA('PARAMETER1','PARAMETER2');
END;

BEGIN
 NULL;
EXCEPTION
 WHEN OTHERS THEN
   P_EXEC_ROLLBACK_TRATIVA('PARAMETER1','PARAMETER2');
END;

I think you could use this XPATH expression: //*[not(ancestor::EXCEPTION_CLAUSE)]/BIN_IDENTIFIER[@tokenValue='P_EXEC_ROLLBACK_TRATIVA']

It seems to work fine in the PL/SQL toolkit:

1 Like

Thanks, I’m so limited to understand the documentation but your explanation helped me a see a little light