use sqlparser::ast::{Expr, Query, Select};
use crate::ast::SqltStatement;
use crate::lint::ctx::LintCtx;
use crate::lint::diagnostic::Diagnostic;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RuleId(pub &'static str);
impl RuleId {
pub fn as_str(&self) -> &'static str {
self.0
}
}
impl std::fmt::Display for RuleId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
Error,
Warning,
Info,
}
impl Severity {
pub fn as_str(self) -> &'static str {
match self {
Severity::Error => "error",
Severity::Warning => "warning",
Severity::Info => "info",
}
}
pub fn sarif_level(self) -> &'static str {
match self {
Severity::Error => "error",
Severity::Warning => "warning",
Severity::Info => "note",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Category {
Raw,
DialectXc,
PreFlight,
Joins,
Subquery,
Perf,
Correctness,
Style,
Ddl,
Schema,
}
impl Category {
pub fn as_str(self) -> &'static str {
match self {
Category::Raw => "raw",
Category::DialectXc => "dialect-xc",
Category::PreFlight => "pre-flight",
Category::Joins => "joins",
Category::Subquery => "subquery",
Category::Perf => "perf",
Category::Correctness => "correctness",
Category::Style => "style",
Category::Ddl => "ddl",
Category::Schema => "schema",
}
}
}
pub struct RuleMeta {
pub id: RuleId,
pub name: &'static str,
pub category: Category,
pub default_severity: Severity,
pub default_enabled: bool,
pub summary: &'static str,
pub explanation: &'static str,
}
pub trait Rule: Send + Sync {
fn meta(&self) -> &'static RuleMeta;
fn check_statement(&self, _stmt: &SqltStatement, _ctx: &LintCtx, _out: &mut Vec<Diagnostic>) {}
fn check_query(
&self,
_query: &Query,
_depth: usize,
_ctx: &LintCtx,
_out: &mut Vec<Diagnostic>,
) {
}
fn check_select(&self, _select: &Select, _ctx: &LintCtx, _out: &mut Vec<Diagnostic>) {}
fn check_expr(&self, _expr: &Expr, _ctx: &LintCtx, _out: &mut Vec<Diagnostic>) {}
}