# sql-insight-cli
A command-line interface to [sql-insight](https://github.com/takaebato/sql-insight/tree/master/sql-insight) — format, normalize, and extract tables / operations / lineage from SQL.
[](https://crates.io/crates/sql-insight-cli)
[](https://github.com/takaebato/sql-insight/actions/workflows/rust.yaml)
[](https://codecov.io/gh/takaebato/sql-insight)
[](https://opensource.org/licenses/MIT)
## Installation
Build from source:
```bash
cargo install sql-insight-cli
```
Skip the compile — fetch the prebuilt `sql-insight` binary attached to
the GitHub Release ([cargo-binstall](https://github.com/cargo-bins/cargo-binstall)
required):
```bash
cargo binstall sql-insight-cli
```
Or download the archive for your platform directly from
[Releases](https://github.com/takaebato/sql-insight/releases) (Linux musl
x86_64/aarch64, macOS x86_64/aarch64, Windows x86_64). Each archive is
signed with GitHub Artifact Attestations — verify the download:
```bash
gh attestation verify sql-insight-<target>.tar.gz --repo takaebato/sql-insight
```
## Commands
- `format` — re-emit SQL in a standardized layout: single-line by default,
or multi-line indented with `--pretty` (comments are not preserved — the
parser does not retain them in the AST).
- `normalize` — abstract literals to placeholders (`--unify-in-list` /
`--unify-values` collapse repetitive shapes; `--alphabetize-insert-columns`
sorts INSERT column lists, only with `--unify-values`).
- `extract crud` — tables bucketed by Create / Read / Update / Delete.
- `extract table-ops` — table-level reads / writes / lineage per statement.
- `extract column-ops` — the same at column granularity, with lineage kinds.
## Input
Every command takes the SQL one of four ways (mutually exclusive):
- an inline argument — `sql-insight format "SELECT 1"`
- `--file <path>` — read it from a file
- piped stdin — `echo "SELECT 1" | sql-insight format`
- `--interactive` / `-i` — a REPL with line editing and history (↑/↓,
Ctrl-R); enter statements terminated by `;`, exit with `exit` / `quit` /
Ctrl-D (Ctrl-C clears the in-progress statement)
With no input on an interactive terminal, the command errors rather than
guessing.
## Examples
```bash
$ sql-insight format "SELECT * FROM users WHERE id = 1;"
SELECT * FROM users WHERE id = 1
$ sql-insight format --pretty "SELECT a, b FROM users WHERE id = 1"
SELECT
a,
b
FROM
users
WHERE
id = 1
$ sql-insight normalize "SELECT * FROM users WHERE id = 1"
SELECT * FROM users WHERE id = ?
$ sql-insight extract crud "INSERT INTO users (name) SELECT name FROM employees"
Create: [users], Read: [employees], Update: [], Delete: []
$ sql-insight extract column-ops "INSERT INTO users (name) SELECT LOWER(name) FROM employees"
[1] Insert
reads: employees.name
writes: users.name
lineage: employees.name -> users.name [transform]
```
## Shell completions and man page
The CLI can print a static shell-completion script or its man page to
stdout — the same subcommands packagers call at install time.
Shell completions:
```bash
# bash
sql-insight completions bash > ~/.local/share/bash-completion/completions/sql-insight
# zsh (needs a dir on $fpath; add e.g. `fpath=(~/.zsh/completions $fpath)` to ~/.zshrc)
sql-insight completions zsh > ~/.zsh/completions/_sql-insight
# fish
sql-insight completions fish > ~/.config/fish/completions/sql-insight.fish
```
Supported shells: `bash`, `zsh`, `fish`, `powershell`, `elvish`.
Man page:
```bash
# read it directly
sql-insight man | man -l -
# or install per-user (add ~/.local/share/man to MANPATH if needed)
mkdir -p ~/.local/share/man/man1
sql-insight man > ~/.local/share/man/man1/sql-insight.1
```
## Options (extract commands)
`--dialect <name>` is available on **every** command (`format` / `normalize` /
`extract`) — parse under a specific dialect (default `generic`). The options
below are specific to `extract`:
- `--format <text|json>` — `json` emits one array of per-statement results.
- `--ddl-file <path>` — a DDL file (`CREATE TABLE`s) to resolve against;
enables catalog-aware analysis (canonicalized identities, strict columns).
- `--default-schema` / `--default-catalog` — search-path-style fill for bare
query references.
- `--casing <upper|lower|insensitive|sensitive>` (and per-class
`--casing-table` / `--casing-table-alias` / `--casing-column`) — override
the dialect's identifier casing.
## Supported SQL Dialects
Via [sqlparser-rs](https://github.com/sqlparser-rs/sqlparser-rs): Generic,
MySQL, PostgreSQL, Hive, SQLite, Snowflake, Redshift, Microsoft SQL Server,
ClickHouse, BigQuery, ANSI, DuckDB, Databricks, Oracle. See the
[sqlparser-rs docs](https://docs.rs/sqlparser/latest/sqlparser/dialect/index.html#structs)
for details.
## Contributing
Contributions to `sql-insight` are welcome! Whether it's adding new
features, fixing bugs, or improving documentation, feel free to fork
the repository and submit a pull request.
## License
MIT — see [LICENSE.txt](https://github.com/takaebato/sql-insight/blob/master/sql-insight-cli/LICENSE.txt).