sql-insight-cli 0.3.0

A CLI utility for SQL query analysis, formatting, and transformation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
mod executor;

use crate::executor::{
    CasingOverride, CliExecutable, ExtractExecutor, ExtractKind, FormatExecutor, NormalizeExecutor,
    OutputFormat,
};
use clap::{ArgGroup, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{generate, Shell};
use sql_insight::error::Error;
use sql_insight::formatter::FormatterOptions;
use sql_insight::normalizer::NormalizerOptions;
use sql_insight::CaseRule;
use std::io::{self, IsTerminal, Read};
use std::process::ExitCode;

#[derive(Debug, Parser)]
#[command(name = "sql-insight")]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Parser, Debug)]
#[clap(group(ArgGroup::new("source").args(& ["sql", "file", "interactive"]).required(false)))]
struct CommonOptions {
    /// The subject SQL to operate on. If omitted (and not --interactive),
    /// SQL is read from stdin when it is piped; an interactive terminal
    /// with no input is an error.
    #[clap(value_parser, group = "source")]
    sql: Option<String>,
    /// The dialect of the input SQL. Might be required for parsing dialect-specific syntax.
    /// Available dialects: ansi, bigquery, clickhouse, databricks, duckdb, generic, hive, mssql, mysql, oracle, postgres, redshift, snowflake, sqlite.
    /// Default: generic.
    #[clap(short, long)]
    dialect: Option<String>,
    /// The file containing the SQL to operate on
    #[clap(short, long, value_parser, group = "source")]
    file: Option<String>,
    /// Read statements interactively from a prompt (terminate each with `;`).
    #[clap(short, long, group = "source")]
    interactive: bool,
}

#[derive(Parser, Debug)]
struct FormatCommandOptions {
    #[clap(flatten)]
    common_options: CommonOptions,
    /// Pretty-print each statement across multiple indented lines (one item
    /// per line) instead of the default single line. For example, `SELECT a,
    /// b FROM t1` becomes a multi-line block with `a` / `b` indented under
    /// `SELECT`.
    #[clap(long)]
    pretty: bool,
}

#[derive(Parser, Debug)]
struct NormalizeCommandOptions {
    #[clap(flatten)]
    common_options: CommonOptions,
    /// Unify IN lists to a single form when all elements are literal values. For example, `IN (1, 2, 3)` becomes `IN (...)`.
    #[clap(long)]
    unify_in_list: bool,
    /// Unify VALUES lists to a single form when all elements are literal values. For example, `VALUES (1, 2, 3), (4, 5, 6)` becomes `VALUES (...)`.
    #[clap(long)]
    unify_values: bool,
    /// Alphabetize INSERT column lists so column-order-only variants normalize alike. Only takes effect together with `--unify-values`: `INSERT INTO t (c, b, a) VALUES (1, 2, 3)` becomes `INSERT INTO t (a, b, c) VALUES (...)`.
    #[clap(long)]
    alphabetize_insert_columns: bool,
}

enum ProcessType {
    Sql(String),
    File(String),
    /// Read the SQL from piped stdin (no explicit source given).
    Stdin,
    Interactive,
}

impl ProcessType {
    /// Resolve the input source. The `source` ArgGroup already makes
    /// `sql` / `--file` / `--interactive` mutually exclusive, so at most
    /// one is set. With none, fall back to stdin when it's piped; an
    /// interactive terminal with no input is an error (rather than the
    /// old surprise of dropping into the REPL).
    fn resolve(command: &Commands) -> Result<Self, Error> {
        let opts = command.common();
        if opts.interactive {
            Ok(ProcessType::Interactive)
        } else if let Some(sql) = &opts.sql {
            Ok(ProcessType::Sql(sql.clone()))
        } else if let Some(file) = &opts.file {
            Ok(ProcessType::File(file.clone()))
        } else if !io::stdin().is_terminal() {
            Ok(ProcessType::Stdin)
        } else {
            Err(Error::ArgumentError(
                "no SQL given — pass it as an argument, pipe it on stdin, \
                 use --file <path>, or --interactive"
                    .to_string(),
            ))
        }
    }
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Format SQL
    Format(FormatCommandOptions),
    /// Normalize SQL
    Normalize(NormalizeCommandOptions),
    /// Extract what a statement touches, at a chosen granularity
    Extract {
        #[command(subcommand)]
        target: ExtractTarget,
    },
    /// Print a shell completion script (bash, zsh, fish, …) to stdout
    Completions {
        /// Shell to generate the completion script for
        shell: Shell,
    },
    /// Print the man page (roff) to stdout
    Man,
}

/// Extraction granularities — thin wrappers over the library's extractors.
#[derive(Subcommand, Debug)]
enum ExtractTarget {
    /// Tables bucketed by CRUD verb (Create / Read / Update / Delete)
    Crud(ExtractArgs),
    /// Table-level reads / writes / lineage per statement
    TableOps(ExtractArgs),
    /// Column-level reads / writes / lineage per statement
    ColumnOps(ExtractArgs),
}

/// Options shared by every `extract` subcommand: the source / dialect plus
/// catalog- and casing-aware analysis controls.
#[derive(Parser, Debug)]
struct ExtractArgs {
    #[clap(flatten)]
    common: CommonOptions,
    /// Output format: human-readable text (default) or JSON.
    #[clap(long, value_enum, default_value_t = FormatArg::Text)]
    format: FormatArg,
    /// SQL DDL file (CREATE TABLE statements) to resolve against — enables
    /// catalog-aware analysis (canonicalized identities, strict columns).
    #[clap(long = "ddl-file")]
    ddl_file: Option<String>,
    /// Query-side default schema: a search-path-style fill applied to a
    /// bare query reference before matching, so it surfaces qualified
    /// (e.g. `users` -> `public.users`). Unqualified DDL tables register
    /// schema-less regardless; without this they still match bare refs by
    /// right-anchoring.
    #[clap(long)]
    default_schema: Option<String>,
    /// Query-side default catalog (search-path-style fill). Only
    /// meaningful with --ddl-file.
    #[clap(long)]
    default_catalog: Option<String>,
    /// Override identifier casing for every class (table / alias / column).
    #[clap(long, value_enum)]
    casing: Option<CasingArg>,
    /// Override casing for catalog / schema / table names only.
    #[clap(long = "casing-table", value_enum)]
    casing_table: Option<CasingArg>,
    /// Override casing for table aliases / CTE / derived names only.
    #[clap(long = "casing-table-alias", value_enum)]
    casing_table_alias: Option<CasingArg>,
    /// Override casing for column names only.
    #[clap(long = "casing-column", value_enum)]
    casing_column: Option<CasingArg>,
}

/// CLI surface of the library's `CaseRule`.
#[derive(Clone, Copy, Debug, ValueEnum)]
enum CasingArg {
    Upper,
    Lower,
    Insensitive,
    Sensitive,
}

impl From<CasingArg> for CaseRule {
    fn from(arg: CasingArg) -> Self {
        match arg {
            CasingArg::Upper => CaseRule::Upper,
            CasingArg::Lower => CaseRule::Lower,
            CasingArg::Insensitive => CaseRule::Insensitive,
            CasingArg::Sensitive => CaseRule::Sensitive,
        }
    }
}

/// CLI surface of the executor's `OutputFormat`.
#[derive(Clone, Copy, Debug, ValueEnum)]
enum FormatArg {
    Text,
    Json,
}

impl From<FormatArg> for OutputFormat {
    fn from(arg: FormatArg) -> Self {
        match arg {
            FormatArg::Text => OutputFormat::Text,
            FormatArg::Json => OutputFormat::Json,
        }
    }
}

impl ExtractTarget {
    fn args(&self) -> &ExtractArgs {
        match self {
            ExtractTarget::Crud(a) | ExtractTarget::TableOps(a) | ExtractTarget::ColumnOps(a) => a,
        }
    }

    fn common(&self) -> &CommonOptions {
        &self.args().common
    }

    fn executor(&self, sql: String) -> Box<dyn CliExecutable> {
        let args = self.args();
        let kind = match self {
            ExtractTarget::Crud(_) => ExtractKind::Crud,
            ExtractTarget::TableOps(_) => ExtractKind::TableOps,
            ExtractTarget::ColumnOps(_) => ExtractKind::ColumnOps,
        };
        Box::new(ExtractExecutor {
            kind,
            sql,
            dialect_name: args.common.dialect.clone(),
            ddl_file: args.ddl_file.clone(),
            default_schema: args.default_schema.clone(),
            default_catalog: args.default_catalog.clone(),
            casing: CasingOverride {
                all: args.casing.map(Into::into),
                table: args.casing_table.map(Into::into),
                table_alias: args.casing_table_alias.map(Into::into),
                column: args.casing_column.map(Into::into),
            },
            format: args.format.into(),
        })
    }
}

impl Commands {
    /// The source / dialect options shared by every command.
    fn common(&self) -> &CommonOptions {
        match self {
            Commands::Format(opts) => &opts.common_options,
            Commands::Normalize(opts) => &opts.common_options,
            Commands::Extract { target } => target.common(),
            // Utility commands take no SQL input; main dispatches them before
            // ever reaching the SQL path that calls this.
            Commands::Completions { .. } | Commands::Man => {
                unreachable!("completions/man are handled in main")
            }
        }
    }

    fn execute(&self) -> Result<Vec<String>, Error> {
        match ProcessType::resolve(self)? {
            ProcessType::Sql(sql) => self.execute_sql(sql),
            ProcessType::File(file) => self.execute_file(file),
            ProcessType::Stdin => self.execute_stdin(),
            ProcessType::Interactive => self.execute_interactive(),
        }
    }

    fn execute_sql(&self, sql: String) -> Result<Vec<String>, Error> {
        self.executor(sql).execute()
    }

    fn execute_stdin(&self) -> Result<Vec<String>, Error> {
        let mut sql = String::new();
        io::stdin()
            .read_to_string(&mut sql)
            .map_err(|e| Error::IOError(e.to_string()))?;
        self.executor(sql).execute()
    }

    fn execute_file(&self, file: String) -> Result<Vec<String>, Error> {
        match std::fs::read_to_string(file.clone()) {
            Ok(sql) => self.executor(sql).execute(),
            Err(e) => Err(Error::ArgumentError(format!(
                "Failed to read file {}: {}",
                file, e
            ))),
        }
    }

    fn execute_interactive(&self) -> Result<Vec<String>, Error> {
        self.entering_interactive_mode()?;
        Ok(vec![])
    }

    fn entering_interactive_mode(&self) -> Result<(), Error> {
        println!(
            "Entering interactive mode. Type sql statement end with `;` to execute. \
             Type `exit` or `quit` to exit."
        );
        let mut editor =
            rustyline::DefaultEditor::new().map_err(|e| Error::IOError(e.to_string()))?;

        let mut input_buffer = String::new();
        loop {
            let prompt = if input_buffer.is_empty() {
                "sql> "
            } else {
                "  -> "
            };
            let line = match editor.readline(prompt) {
                Ok(line) => line,
                // Ctrl-C clears the in-progress statement; Ctrl-D / EOF exits.
                Err(rustyline::error::ReadlineError::Interrupted) => {
                    input_buffer.clear();
                    continue;
                }
                Err(rustyline::error::ReadlineError::Eof) => break,
                Err(e) => return Err(Error::IOError(e.to_string())),
            };
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            let _ = editor.add_history_entry(trimmed);
            if trimmed.eq_ignore_ascii_case("exit") || trimmed.eq_ignore_ascii_case("quit") {
                break;
            }
            input_buffer.push_str(trimmed);
            input_buffer.push('\n');
            if trimmed.ends_with(';') {
                match self.executor(std::mem::take(&mut input_buffer)).execute() {
                    Ok(result) => result.iter().for_each(|r| println!("{r}")),
                    Err(e) => eprintln!("Error: {e}"),
                }
            }
        }

        println!("Bye");
        Ok(())
    }

    fn executor(&self, sql: String) -> Box<dyn CliExecutable> {
        match self {
            Commands::Format(opts) => Box::new(
                FormatExecutor::new(sql, opts.common_options.dialect.clone())
                    .with_options(FormatterOptions::new().with_pretty(opts.pretty)),
            ),
            Commands::Normalize(opts) => Box::new(
                NormalizeExecutor::new(sql, opts.common_options.dialect.clone()).with_options(
                    NormalizerOptions::new()
                        .with_unify_in_list(opts.unify_in_list)
                        .with_unify_values(opts.unify_values)
                        .with_alphabetize_insert_columns(opts.alphabetize_insert_columns),
                ),
            ),
            Commands::Extract { target } => target.executor(sql),
            Commands::Completions { .. } | Commands::Man => {
                unreachable!("completions/man are handled in main")
            }
        }
    }
}

fn main() -> ExitCode {
    let args = Cli::parse();
    // Utility commands take no SQL input — render straight to stdout.
    match &args.command {
        Commands::Completions { shell } => {
            generate(
                *shell,
                &mut Cli::command(),
                "sql-insight",
                &mut io::stdout(),
            );
            return ExitCode::SUCCESS;
        }
        Commands::Man => {
            return match clap_mangen::Man::new(Cli::command()).render(&mut io::stdout()) {
                Ok(()) => ExitCode::SUCCESS,
                Err(e) => {
                    eprintln!("Error: {}", e);
                    ExitCode::FAILURE
                }
            };
        }
        Commands::Format(_) | Commands::Normalize(_) | Commands::Extract { .. } => {}
    }
    match args.command.execute() {
        Ok(result) => {
            for r in result {
                println!("{}", r);
            }
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            ExitCode::FAILURE
        }
    }
}