use super::common::{BEHAVIOR, INPUT_OUTPUT};
use crate::parser::{Parser, StatementType};
use crate::schema::{SchemaBuilder, SchemaGraph};
use ahash::AHashMap;
use anyhow::{bail, Result};
use clap::{Args, ValueHint};
use std::fs::File;
use std::io::{BufWriter, Read, Write};
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Args)]
pub struct OrderArgs {
#[arg(value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
file: PathBuf,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
output: Option<PathBuf>,
#[arg(short, long, help_heading = INPUT_OUTPUT)]
dialect: Option<String>,
#[arg(long, help_heading = BEHAVIOR)]
check: bool,
#[arg(long, help_heading = BEHAVIOR)]
dry_run: bool,
#[arg(long, help_heading = BEHAVIOR)]
reverse: bool,
}
pub fn run(args: OrderArgs) -> Result<ExitCode> {
let OrderArgs {
file,
output,
dialect,
check,
dry_run,
reverse,
} = args;
let output = super::common::dash_is_stdout(output);
if !file.exists() {
bail!("input file does not exist: {}", file.display());
}
let sql_dialect = super::common::resolve_dialect(&file, dialect.as_deref(), false)?;
eprintln!("Analyzing schema for topological order...");
let (graph, table_statements, other_statements) = collect_statements(&file, sql_dialect)?;
if graph.is_empty() {
eprintln!("No tables found in the file.");
return Ok(ExitCode::SUCCESS);
}
let topo_result = graph.topo_sort();
if !topo_result.cyclic_tables.is_empty() {
eprintln!("\nWarning: Circular dependencies detected!");
eprintln!("The following tables are part of cycles:");
for table_id in &topo_result.cyclic_tables {
if let Some(name) = graph.table_name(*table_id) {
eprintln!(" - {}", name);
}
}
eprintln!();
if check {
eprintln!("Check FAILED: Cannot determine valid ordering due to cycles.");
eprintln!("Use 'sql-splitter graph --cycles-only' to analyze cycles.");
return Ok(ExitCode::FAILURE);
}
}
let mut ordered_tables: Vec<String> = topo_result
.order
.iter()
.filter_map(|id| graph.table_name(*id).map(|s| s.to_string()))
.collect();
for table_id in &topo_result.cyclic_tables {
if let Some(name) = graph.table_name(*table_id) {
ordered_tables.push(name.to_string());
}
}
if reverse {
ordered_tables.reverse();
}
if check {
eprintln!("Check PASSED: Tables can be ordered topologically.");
eprintln!("\nSuggested order ({} tables):", ordered_tables.len());
for (i, table) in ordered_tables.iter().enumerate() {
eprintln!(" {}. {}", i + 1, table);
}
return Ok(ExitCode::SUCCESS);
}
if dry_run {
eprintln!("\nTopological order ({} tables):", ordered_tables.len());
for (i, table) in ordered_tables.iter().enumerate() {
eprintln!(" {}. {}", i + 1, table);
}
return Ok(ExitCode::SUCCESS);
}
let writer: Box<dyn Write> = if let Some(ref out_path) = output {
Box::new(BufWriter::new(File::create(out_path)?))
} else {
Box::new(BufWriter::new(std::io::stdout()))
};
write_ordered_output(
writer,
&ordered_tables,
&table_statements,
&other_statements,
)?;
if let Some(ref out_path) = output {
eprintln!("Ordered dump written to: {}", out_path.display());
}
eprintln!(
"\nProcessed {} tables in topological order.",
ordered_tables.len()
);
Ok(ExitCode::SUCCESS)
}
struct CollectedStatements {
create_statements: AHashMap<String, String>,
insert_statements: AHashMap<String, Vec<String>>,
other_table_statements: AHashMap<String, Vec<String>>,
}
fn collect_statements(
path: &std::path::Path,
dialect: crate::parser::SqlDialect,
) -> Result<(SchemaGraph, CollectedStatements, Vec<String>)> {
let reader: Box<dyn Read> = crate::splitter::open_input(path)?;
let mut parser = Parser::with_dialect(reader, 64 * 1024, dialect);
let mut builder = SchemaBuilder::new();
let mut collected = CollectedStatements {
create_statements: AHashMap::new(),
insert_statements: AHashMap::new(),
other_table_statements: AHashMap::new(),
};
let mut other_statements: Vec<String> = Vec::new();
while let Some(stmt) = parser.read_statement()? {
let stmt_str = String::from_utf8_lossy(&stmt).to_string();
let (stmt_type, table_name) = Parser::<&[u8]>::parse_statement_with_dialect(&stmt, dialect);
builder.ingest(stmt_type, &stmt_str);
match stmt_type {
StatementType::CreateTable => {
if !table_name.is_empty() {
collected.create_statements.insert(table_name, stmt_str);
}
}
StatementType::AlterTable => {
if !table_name.is_empty() {
collected
.other_table_statements
.entry(table_name)
.or_default()
.push(stmt_str);
}
}
StatementType::CreateIndex => {
if !table_name.is_empty() {
collected
.other_table_statements
.entry(table_name)
.or_default()
.push(stmt_str);
} else {
other_statements.push(stmt_str);
}
}
StatementType::Insert | StatementType::Copy => {
if !table_name.is_empty() {
collected
.insert_statements
.entry(table_name)
.or_default()
.push(stmt_str);
}
}
_ => {
other_statements.push(stmt_str);
}
}
}
let graph = SchemaGraph::from_schema(builder.build());
Ok((graph, collected, other_statements))
}
fn write_ordered_output(
mut writer: Box<dyn Write>,
ordered_tables: &[String],
table_statements: &CollectedStatements,
other_statements: &[String],
) -> Result<()> {
for stmt in other_statements {
writeln!(writer, "{}", stmt)?;
}
if !other_statements.is_empty() {
writeln!(writer)?;
}
for table in ordered_tables {
if let Some(create) = table_statements.create_statements.get(table) {
writeln!(writer, "{}", create)?;
writeln!(writer)?;
}
if let Some(others) = table_statements.other_table_statements.get(table) {
for stmt in others {
writeln!(writer, "{}", stmt)?;
}
if !others.is_empty() {
writeln!(writer)?;
}
}
if let Some(inserts) = table_statements.insert_statements.get(table) {
for stmt in inserts {
writeln!(writer, "{}", stmt)?;
}
if !inserts.is_empty() {
writeln!(writer)?;
}
}
}
writer.flush()?;
Ok(())
}