You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@rule(
key = "CreateViewWithoutOrReplaceForce",
name = "CREATE VIEW must include OR REPLACE FORCE",
description = "Enforces the inclusion of OR REPLACE FORCE in CREATE VIEW statements.",
priority = Priority.CRITICAL
)
@ActivatedByDefault
public class CreateOrReplaceForceViewCheck extends PlSqlCheck {
private static final Logger LOGGER = Logger.getLogger(CreateOrReplaceForceViewCheck.class.getName());
@Override
public void init() {
subscribeTo(PlSqlGrammar.CREATE_VIEW);
LOGGER.info("Subscribed to CREATE_VIEW nodes");
}
@Override
public void visitNode(AstNode node) {
LOGGER.info("Visiting CREATE_VIEW node to check for OR REPLACE FORCE");
boolean hasOrReplace = false;
boolean hasForce = false;
// Iterate over the children nodes to check for the OR REPLACE FORCE sequence
for (AstNode child : node.getChildren()) {
if ("OR".equals(child.getTokenValue()) && "REPLACE".equals(child.getNextSibling().getTokenValue())) {
hasOrReplace = true;
}
if ("FORCE".equals(child.getTokenValue())) {
hasForce = true;
}
}
if (!hasOrReplace || !hasForce) {
int lineNumber = node.getTokenLine();
String message = String.format("CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line %d.", lineNumber);
LOGGER.warning(message);
addIssue(node, message);
}
}
}
here is my test sql file
-- This is a compliant CREATE VIEW statement
CREATE OR REPLACE FORCE VIEW valid_view AS
SELECT employee_id, name, department
FROM employee_table;
-- This is a non-compliant CREATE VIEW statement
CREATE VIEW missing_or_replace_force AS -- Noncompliant {{CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line #7.}}
SELECT employee_id, name, department
FROM employee_table;
-- Another non-compliant CREATE VIEW statement
CREATE VIEW another_missing_clause AS -- Noncompliant {{CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line #12.}}
SELECT employee_id, name, department
FROM employee_table;
-- This is compliant again
CREATE OR REPLACE FORCE VIEW another_valid_view AS
SELECT employee_id, name, department
FROM employee_table;
please check and let me know what am I missing here.
Thanks
csrvsk
The text was updated successfully, but these errors were encountered:
It seems to be just a typo. The check register the text in the format "CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line %d." but the text expects "CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line #7" (notice the additional #).
Hi Filepe,
I am testing anew custom rule to check for CRETAE VIEW statements if it is not using OR REPLACE FORCE and report it as an issue with an error message.
Here is the error I am getting:
Here is my class,
import org.sonar.plugins.plsqlopen.api.annotations.Priority;
import org.sonar.plugins.plsqlopen.api.annotations.Rule;
import org.sonar.plugins.plsqlopen.api.annotations.ActivatedByDefault;
import org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck;
import org.sonar.plugins.plsqlopen.api.sslr.AstNode;
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar;
import java.util.logging.Logger;
@rule(
key = "CreateViewWithoutOrReplaceForce",
name = "CREATE VIEW must include OR REPLACE FORCE",
description = "Enforces the inclusion of OR REPLACE FORCE in CREATE VIEW statements.",
priority = Priority.CRITICAL
)
@ActivatedByDefault
public class CreateOrReplaceForceViewCheck extends PlSqlCheck {
}
here is my test sql file
-- This is a compliant CREATE VIEW statement
CREATE OR REPLACE FORCE VIEW valid_view AS
SELECT employee_id, name, department
FROM employee_table;
-- This is a non-compliant CREATE VIEW statement
CREATE VIEW missing_or_replace_force AS -- Noncompliant {{CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line #7.}}
SELECT employee_id, name, department
FROM employee_table;
-- Another non-compliant CREATE VIEW statement
CREATE VIEW another_missing_clause AS -- Noncompliant {{CREATE VIEW statement missing 'OR REPLACE FORCE' clause at line #12.}}
SELECT employee_id, name, department
FROM employee_table;
-- This is compliant again
CREATE OR REPLACE FORCE VIEW another_valid_view AS
SELECT employee_id, name, department
FROM employee_table;
please check and let me know what am I missing here.
Thanks
csrvsk
The text was updated successfully, but these errors were encountered: