Skip to main content

rigsql_rules/tsql/
tq01.rs

1use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
2use crate::violation::LintViolation;
3
4/// TQ01: Avoid using the `sp_` prefix on stored procedures.
5///
6/// SQL Server treats procedures prefixed with `sp_` as system procedures and
7/// searches the `master` database first, which degrades performance.
8///
9/// This rule should check CREATE PROCEDURE statements, but since our parser
10/// doesn't yet support CREATE PROCEDURE, this rule is currently a stub that
11/// produces no violations. It will be implemented when CREATE PROCEDURE
12/// parsing is added.
13#[derive(Debug, Default)]
14pub struct RuleTQ01;
15
16impl Rule for RuleTQ01 {
17    fn code(&self) -> &'static str {
18        "TQ01"
19    }
20    fn name(&self) -> &'static str {
21        "tsql.sp_prefix"
22    }
23    fn description(&self) -> &'static str {
24        "Avoid using the sp_ prefix for stored procedures."
25    }
26    fn explanation(&self) -> &'static str {
27        "Stored procedures with the sp_ prefix cause SQL Server to search the master database \
28         first before checking the current database. This lookup adds unnecessary overhead and \
29         can lead to unexpected behavior if a system procedure with the same name exists. \
30         Use a different prefix such as usp_ for user-defined stored procedures."
31    }
32    fn groups(&self) -> &[RuleGroup] {
33        &[RuleGroup::Convention]
34    }
35    fn is_fixable(&self) -> bool {
36        false
37    }
38
39    fn crawl_type(&self) -> CrawlType {
40        // Stub: needs CreateProcedureStatement support in the parser
41        CrawlType::RootOnly
42    }
43
44    fn eval(&self, _ctx: &RuleContext) -> Vec<LintViolation> {
45        // Stub: CREATE PROCEDURE parsing not yet supported
46        vec![]
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use crate::test_utils::lint_sql_with_dialect;
54
55    #[test]
56    fn test_tq01_stub_no_violations() {
57        // Currently a stub until CREATE PROCEDURE parsing is supported
58        let violations = lint_sql_with_dialect("EXEC sp_helpdb", RuleTQ01, "tsql");
59        assert_eq!(violations.len(), 0);
60    }
61
62    #[test]
63    fn test_tq01_stub_no_violations_ansi() {
64        let violations = lint_sql_with_dialect("EXEC sp_helpdb", RuleTQ01, "ansi");
65        assert_eq!(violations.len(), 0);
66    }
67}