← 返回命令列表

Linux command

py-sql-cleaner 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

List

py-sql-cleaner list jobs/load_users.py

Format

py-sql-cleaner format jobs/load_users.py

Preview

py-sql-cleaner format jobs/load_users.py --dry-run

Format

py-sql-cleaner format jobs/load_users.py -d postgres

Extract

py-sql-cleaner extract jobs/load_users.py --out-dir sql

Check

py-sql-cleaner check jobs/load_users.py

List

py-sql-cleaner dialects

说明

py-sql-cleaner is a Python CLI that finds triple-quoted SQL strings inside Python files and reformats or extracts them using SQLGlot. It is intended for ETL and data-engineering projects where SQL is embedded in `.py` files rather than kept in separate `.sql` files. The tool is conservative by default. f-strings, Jinja templates and other runtime placeholders are detected but skipped instead of being rewritten, so formatting cannot silently change a query that depends on interpolation. py-sql-cleaner never connects to a database and never executes SQL. The supported dialects (`generic`, `mysql`, `postgres`, `redshift`) only select SQLGlot's parser and formatter mode, not full database validation. The `check` subcommand exits non-zero when formatting would change a file, making it suitable as a pre-commit hook or CI gate next to `black`, `ruff` and similar formatters.

参数

list _file_
Show every embedded SQL block found in the file.
format _file_
Reformat embedded SQL in place.
extract _file_
Write each SQL block to a separate `.sql` file.
check _file_
Exit non-zero if formatting would change the file.
dialects
Print supported SQL dialects.
-d, --dialect _NAME_
Select dialect: `generic`, `mysql`, `postgres`, `redshift`.
--dry-run
Print the formatted result instead of writing it.
--out-dir _DIR_
Output directory for `extract`.
--version
Print the installed version.

FAQ

What is the py-sql-cleaner command used for?

py-sql-cleaner is a Python CLI that finds triple-quoted SQL strings inside Python files and reformats or extracts them using SQLGlot. It is intended for ETL and data-engineering projects where SQL is embedded in `.py` files rather than kept in separate `.sql` files. The tool is conservative by default. f-strings, Jinja templates and other runtime placeholders are detected but skipped instead of being rewritten, so formatting cannot silently change a query that depends on interpolation. py-sql-cleaner never connects to a database and never executes SQL. The supported dialects (`generic`, `mysql`, `postgres`, `redshift`) only select SQLGlot's parser and formatter mode, not full database validation. The `check` subcommand exits non-zero when formatting would change a file, making it suitable as a pre-commit hook or CI gate next to `black`, `ruff` and similar formatters.

How do I run a basic py-sql-cleaner example?

Run `py-sql-cleaner list jobs/load_users.py` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does list _file_ do in py-sql-cleaner?

Show every embedded SQL block found in the file.