no-mistakes-postgres-sql 0.109.125

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::{
    SqlAddColumnMetadata, SqlCreateIndexMetadata, SqlDropIndexMetadata, SqlForeignKeyMetadata,
    SqlNamedConstraint, SqlSettingUse, SqlStatementKind, SqlTableSchemaEvent, SqlUnnamedConstraint,
};
use std::path::PathBuf;

/// One `CREATE TABLE` after PostgreSQL dialect parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlCreateTableMetadata {
    pub table_name: String,
    pub columns: Vec<SqlColumnMetadata>,
}

/// Column facts later generated-column and lock-ordering rules can query.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlColumnMetadata {
    pub name: String,
    pub type_name: Option<String>,
    pub constraints: Vec<String>,
    pub is_primary_key: bool,
    pub is_generated: bool,
    pub generated_expression: Option<String>,
    pub generated_function: Option<String>,
    pub generated_function_arg_columns: Vec<String>,
    pub generated_source_columns: Vec<String>,
}

/// A name introduced by one migration statement, unquoted and schema-unqualified.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlDeclaredIdentifier {
    pub name: String,
    pub line: usize,
}

/// One SQL chunk that lenient parsing recovered partially or could not recover.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlUnanalyzableStatement {
    pub line: usize,
    pub column: usize,
    pub head: String,
    /// A complete, narrowly recognized PostgreSQL grammar unsupported by the AST parser.
    pub valid_unsupported: bool,
    /// Uncertainty inside an identified procedural body, including malformed body SQL.
    pub procedural_body: bool,
    /// Lexical statement ordinals, including enclosing routine bodies.
    pub source_order: Vec<usize>,
    /// INSERT relevance belongs to this failed chunk, never a valid sibling.
    pub insert_keyword_count: usize,
    /// Successfully recovered INSERTs from this same failed chunk.
    pub recovered_insert_count: usize,
    pub has_top_level_not_exists: bool,
    /// Partial AST recovery can prove schema facts while leaving DML uncertainty.
    pub schema_unanalyzable: bool,
    /// A failed routine wrapper leaves statement diagnostics to its exact lexical leaf.
    pub statement_unanalyzable: bool,
}

/// Schema facts for one SQL file read through `SourceStore`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SqlSchemaFileFacts {
    pub path: PathBuf,
    /// Schema projection that keeps executable DO blocks and leaves stored routines opaque.
    pub ddl_projection: Option<Box<SqlSchemaFileFacts>>,
    pub unanalyzable_statements: Vec<SqlUnanalyzableStatement>,
    pub tables: Vec<SqlCreateTableMetadata>,
    /// Qualified CREATE/ALTER/DROP operations in statement order for live-column catalogs.
    pub table_events: Vec<SqlTableSchemaEvent>,
    /// Distinguishes an empty execution projection from legacy hand-built facts.
    pub table_events_collected: bool,
    pub indexes: Vec<SqlCreateIndexMetadata>,
    pub dropped_indexes: Vec<SqlDropIndexMetadata>,
    pub dropped_tables: Vec<SqlDropIndexMetadata>,
    pub foreign_keys: Vec<SqlForeignKeyMetadata>,
    pub add_columns: Vec<SqlAddColumnMetadata>,
    pub unnamed_constraints: Vec<SqlUnnamedConstraint>,
    /// Lines of explicit default transactions that contain one complete DML statement.
    pub single_statement_transactions: Vec<usize>,
    pub query_marker_tokens_unavailable: bool,
    pub complex_queries: Vec<super::SqlComplexQuery>,
    pub unanalyzable_sql_lines: Vec<usize>,
    pub catalog_usage: Option<crate::usage::SqlCatalogUsage>,
    pub statement_kinds: Vec<SqlStatementKind>,
    pub setting_uses: Vec<SqlSettingUse>,
    #[doc(hidden)]
    pub clause_uses: Vec<super::SqlPolicyClauseUse>,
    pub function_calls: Vec<super::super::SqlFunctionCallFact>,
    pub not_valid_constraints: Vec<SqlNamedConstraint>,
    pub validated_constraints: Vec<SqlNamedConstraint>,
    pub declared_identifiers: Vec<SqlDeclaredIdentifier>,
}

impl SqlSchemaFileFacts {
    pub fn ddl_facts(&self) -> &Self {
        self.ddl_projection.as_deref().unwrap_or(self)
    }

    pub fn set_path(&mut self, path: PathBuf) {
        if let Some(ddl) = &mut self.ddl_projection {
            ddl.set_path(path.clone());
        }
        self.path = path;
    }
}