diff --git a/src/main.rs b/src/main.rs index 052d728..a248dd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use crossterm::tty::IsTty; use rusqlite; use rusqlite::types::Value; use rusqlite::{Connection, OpenFlags}; -use sqlparser::ast::Statement; +use sqlparser::ast::{ConditionalStatements, CreateView, Statement, TriggerEvent, TriggerPeriod}; use sqlparser::dialect::SQLiteDialect; use sqlparser::parser::Parser; use yansi::{Condition, Paint}; @@ -314,10 +314,30 @@ fn inspect_schema(conn: Rc, filename: &Path, inc_hidden: &bool) -> a let triggers = table.triggers_info()?; if !triggers.is_empty() { writeln!(output, "Triggers:")?; - for (name, _) in triggers { - writeln!(output, " {}", name.bright_magenta())?; - // More trigger info needs support for parsing CREATE TRIGGER - // https://github.com/apache/datafusion-sqlparser-rs/issues/1388 + for (name, create_sql) in triggers { + write!(output, " {}", name.bright_magenta())?; + let ast = Parser::parse_sql(&SQLiteDialect {}, &create_sql)?; + if let Some(Statement::CreateTrigger(ct)) = ast.first() { + if ct.period == Some(TriggerPeriod::After) { + write!(output, " AFTER")?; + } + if let Some(te) = ct.events.first() { + match te { + TriggerEvent::Update(cols) => { + let col_names: Vec = + cols.into_iter().map(|i| i.to_string()).collect(); + write!(output, " UPDATE OF {}", fmt_col_names(&col_names))?; + } + _ => write!(output, " {te}")?, + }; + } + writeln!(output, "")?; + if let Some(ConditionalStatements::BeginEnd(bes)) = &ct.statements { + for stmt in &bes.statements { + writeln!(output, " {stmt};")?; + } + } + } } } writeln!(output)?; @@ -341,7 +361,7 @@ fn inspect_schema(conn: Rc, filename: &Path, inc_hidden: &bool) -> a // Find the 'AS SELECT' clause for this view let ast = Parser::parse_sql(&SQLiteDialect {}, &view.create_sql()?)?; - if let Some(Statement::CreateView { query: q, .. }) = ast.first() { + if let Some(Statement::CreateView(CreateView { query: q, .. })) = ast.first() { writeln!(output, "AS {q}")?; } }