Skip to content
Open
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
32 changes: 26 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -314,10 +314,30 @@ fn inspect_schema(conn: Rc<Connection>, 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<String> =
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)?;
Expand All @@ -341,7 +361,7 @@ fn inspect_schema(conn: Rc<Connection>, 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}")?;
}
}
Expand Down
Loading