Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
358 changes: 83 additions & 275 deletions crates/pgls_analyser/src/lib.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/pgls_analyser/src/lint/style/require_table_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ fn dropped_names(tokens: StatementTokens) -> Vec<TextRange> {

/// The unqualified names of the temporary tables created by earlier statements of the file.
/// An unqualified name refers to them, since `pg_temp` comes first in the search path.
fn temporary_tables(previous: &[NodeEnum]) -> Vec<&str> {
fn temporary_tables<'a>(previous: &[&'a NodeEnum]) -> Vec<&'a str> {
let mut tables = Vec::new();
for stmt in previous {
let created = match stmt {
let created = match *stmt {
NodeEnum::CreateStmt(stmt) => stmt.relation.as_ref(),
NodeEnum::CreateTableAsStmt(stmt) => {
stmt.into.as_ref().and_then(|into| into.rel.as_ref())
Expand Down
21 changes: 12 additions & 9 deletions crates/pgls_analyser/src/linter_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a> StatementContext<'a> {
///
/// In a file with migration sections, the file context is that of the current section.
pub struct AnalysedFileContext<'a> {
pub stmts: &'a [pgls_query::NodeEnum],
pub stmts: &'a [&'a pgls_query::NodeEnum],
pos: usize,
stmt_count: usize,
catalog: Catalog,
Expand All @@ -219,7 +219,7 @@ pub struct AnalysedFileContext<'a> {
}

impl<'a> AnalysedFileContext<'a> {
pub fn new(stmts: &'a [pgls_query::NodeEnum], catalog: Catalog, session: Session) -> Self {
pub fn new(stmts: &'a [&'a pgls_query::NodeEnum], catalog: Catalog, session: Session) -> Self {
Self {
stmts,
pos: 0,
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'a> AnalysedFileContext<'a> {
self
}

pub fn previous_stmts(&self) -> &[pgls_query::NodeEnum] {
pub fn previous_stmts(&self) -> &[&'a pgls_query::NodeEnum] {
&self.stmts[0..self.pos]
}

Expand Down Expand Up @@ -302,12 +302,15 @@ impl<'a> AnalysedFileContext<'a> {

/// Moves to the next statement.
///
/// Executed statements (`applies_effects`) update the catalog and the session. Statements
/// in the bodies of SQL functions are only analysed, not executed.
pub fn next(&mut self, applies_effects: bool) {
if applies_effects && self.pos < self.stmts.len() {
let stmt = &self.stmts[self.pos];
self.catalog.apply(stmt, self.session.search_path());
/// A statement that `runs` updates the session, and the catalog too if `updates_catalog`.
/// Statements in the bodies of SQL functions are only analysed, not executed. A statement on
/// placeholder names, like psql's `:name`, still takes its locks and changes the transaction,
/// but the catalog can't follow it.
pub fn next(&mut self, runs: bool, updates_catalog: bool) {
if runs && let Some(stmt) = self.stmts.get(self.pos) {
if updates_catalog {
self.catalog.apply(stmt, self.session.search_path());
}
self.session.apply(stmt);
}
self.pos += 1;
Expand Down
2 changes: 1 addition & 1 deletion crates/pgls_analyser/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct MigrationSection {
/// The parts of the section that the migration tool sends to Postgres as one query, like a
/// whole dbmate section or a goose `StatementBegin` block. Postgres runs a query with more
/// than one statement in a transaction block, also in a section without a transaction. The
/// other statements of the section are sent one by one.
/// other statements of the section are sent one by one. In order, and they don't overlap.
pub queries: Vec<TextRange>,
}

Expand Down
Loading
Loading