From 67551ae1880ef3bc69484ef82de1081385ca5e0e Mon Sep 17 00:00:00 2001 From: Takahiro Ebato Date: Mon, 12 Feb 2024 12:10:58 +0900 Subject: [PATCH] Create README.md --- README.md | 112 +++++++++++++++++++++++++++++++++++++ sql-insight-cli/Cargo.toml | 1 + sql-insight-cli/README.md | 101 +++++++++++++++++++++++++++++++++ sql-insight/README.md | 1 + 4 files changed, 215 insertions(+) create mode 100644 README.md create mode 100644 sql-insight-cli/README.md create mode 120000 sql-insight/README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab68ac2 --- /dev/null +++ b/README.md @@ -0,0 +1,112 @@ +# sql-insight + +`sql-insight` provides intuitive tools for crafting and extracting insights from SQL queries, enhancing query analysis and processing. +Leveraging the comprehensive parsing capabilities of [sqlparser-rs](https://github.com/sqlparser-rs/sqlparser-rs), it ensures robust handling of various SQL dialects. + +[![Crates.io](https://img.shields.io/crates/v/sql-insight.svg)](https://crates.io/crates/sql-insight) +[![Docs.rs](https://docs.rs/sql-insight/badge.svg)](https://docs.rs/sql-insight) +[![Rust](https://github.com/takaebato/sql-insight/actions/workflows/rust.yaml/badge.svg?branch=master)](https://github.com/takaebato/sql-insight/actions/workflows/rust.yaml) +[![codecov](https://codecov.io/gh/takaebato/sql-insight/graph/badge.svg?token=Z1KYAWA3HY)](https://codecov.io/gh/takaebato/sql-insight) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) + +## Features + +- **SQL Formatting**: Format SQL queries to standardized form, improving readability and maintainability. +- **SQL Normalization**: Convert SQL queries into a normalized form, making them easier to analyze and process. +- **Table References Extraction**: Extract tables referenced in SQL queries, clarifying the data sources involved. +- **CRUD Table References Extraction**: Identify the create, read, update, and delete operations, along with the tables involved in each operation within SQL queries. + +## Installation + +Add `sql_insight` to your `Cargo.toml` file: + +```toml +[dependencies] +sql-insight = { version = "0.1.0" } +``` + +## Usage + +### SQL Formatting + +Format SQL queries according to different dialects: + +```rust +use sqlparser::dialect::GenericDialect; + +let formatted_sql = sql_insight::format(GenericDialect {}, "SELECT * \n from users WHERE id = 1").unwrap(); +println!("{}", formatted_sql); +``` + +This outputs: + +```sql +SELECT * FROM users WHERE id = 1 +``` + +### SQL Normalization + +Normalize SQL queries to abstract away literals: + +```rust +use sqlparser::dialect::GenericDialect; + +let dialect = GenericDialect {}; +let normalized_sql = sql_insight::normalize(&dialect, "SELECT * \n from users WHERE id = 1").unwrap(); +println!("{}", normalized_sql); +``` + +This outputs: + +```sql +SELECT * FROM users WHERE id = ? +``` + + +### Extract Table References + +Extract table references from SQL queries: + +```rust +use sqlparser::dialect::GenericDialect; + +let dialect = GenericDialect {}; +let tables = sql_insight::extract_tables(&dialect, "SELECT * FROM catalog.schema.`users` as users_alias").unwrap(); +println!("{:?}", tables); +``` + +This outputs: + +``` +[Ok(Tables([TableReference { catalog: Some(Ident { value: "catalog", quote_style: None }), schema: Some(Ident { value: "schema", quote_style: None }), name: Ident { value: "users", quote_style: Some('`') }, alias: Some(Ident { value: "users_alias", quote_style: None }) }]))] +``` + +### Extract CRUD Table References + +Identify CRUD operations and the tables involved in each operation within SQL queries: + +```rust +use sqlparser::dialect::GenericDialect; + +let dialect = GenericDialect {}; +let crud_tables = sql_insight::extract_crud_tables(&dialect, "INSERT INTO users (name) SELECT name FROM employees").unwrap(); +println!("{:?}", crud_tables); +``` + +This outputs: + +``` +[Ok(CrudTables { create_tables: [TableReference { catalog: None, schema: None, name: Ident { value: "users", quote_style: None }, alias: None }], read_tables: [TableReference { catalog: None, schema: None, name: Ident { value: "employees", quote_style: None }, alias: None }], update_tables: [], delete_tables: [] })] +``` + +## Supported SQL Dialects + +`sql-insight` supports a comprehensive range of SQL dialects through [sqlparser-rs](https://github.com/sqlparser-rs/sqlparser-rs). For details on supported dialects, please refer to the documentation. + +## 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 + +`sql-insight` is distributed under the [MIT license](https://github.com/takaebato/sql-insight/blob/master/LICENSE.txt). diff --git a/sql-insight-cli/Cargo.toml b/sql-insight-cli/Cargo.toml index ad435c4..db8dba8 100644 --- a/sql-insight-cli/Cargo.toml +++ b/sql-insight-cli/Cargo.toml @@ -20,6 +20,7 @@ authors = { workspace = true } [[bin]] name = "sql-insight" path = "src/main.rs" +doc = false [dependencies] sql-insight = { path = "../sql-insight", version = "0.1.0" } diff --git a/sql-insight-cli/README.md b/sql-insight-cli/README.md new file mode 100644 index 0000000..d970162 --- /dev/null +++ b/sql-insight-cli/README.md @@ -0,0 +1,101 @@ +# sql-insight-cli + +`sql-insight-cli` is a command-line interface built on top of the sql-insight library. It provides a set of commands that `sql-insight` supports. + +[![Crates.io](https://img.shields.io/crates/v/sql-insight-cli.svg)](https://crates.io/crates/sql-insight-cli) +[![Rust](https://github.com/takaebato/sql-insight/actions/workflows/rust.yaml/badge.svg?branch=master)](https://github.com/takaebato/sql-insight/actions/workflows/rust.yaml) +[![codecov](https://codecov.io/gh/takaebato/sql-insight/graph/badge.svg?token=Z1KYAWA3HY)](https://codecov.io/gh/takaebato/sql-insight) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) + +## Features + +- **SQL Formatting**: Format SQL queries to standardized form, improving readability and maintainability. +- **SQL Normalization**: Convert SQL queries into a normalized form, making them easier to analyze and process. +- **Table References Extraction**: Extract tables referenced in SQL queries, clarifying the data sources involved. +- **CRUD Table References Extraction**: Identify the create, read, update, and delete operations, along with the tables involved in each operation within SQL queries. + +Additional Features: + +- **File and Interactive Mode Support**: Process SQL queries directly from files or via an interactive CLI session. + +## Installation + +Install `sql-insight-cli` using Cargo: + +```bash +cargo install sql-insight-cli +``` + +## Usage + +`sql-insight-cli` supports the following commands. Commands can process input directly from the command line, from a file using the --file option, or interactively. + +### General Options + +- `--file `: Read SQL queries from the specified file instead of command line arguments. +- interactive mode: Launch an interactive CLI session to input SQL queries. Enter this mode by running the command without a SQL argument nor --file option. To exit, type `exit`, `quit` or press `Ctrl + C`. + +### Formatting SQL + +Format SQL queries to a standardized style: + +```bash +sql-insight format "SELECT * \n FROM users WHERE id = 1;" +``` + +This outputs: + +```sql +SELECT * FROM users WHERE id = 1 +``` + +### Normalizing SQL + +Normalize SQL queries, abstracting values to placeholders: + +```bash +sql-insight normalize "SELECT * \n FROM users WHERE id = 1;" +``` + +This outputs: + +```sql +SELECT * FROM users WHERE id = ? +``` + +### Extracting Table References + +Identify tables involved in SQL queries: + +```bash +sql-insight extract-tables "SELECT * FROM catalog.schema.users as users_alias" +``` + +This outputs: + +``` +catalog.schema.users AS users_alias +``` + +### Extracting CRUD Operations + +Extract and identify CRUD operations and involved tables: + +```bash +sql-insight extract-crud "INSERT INTO users (name) SELECT name FROM employees" +``` + +This outputs: + +``` +Create: [users], Read: [employees], Update: [], Delete: [] +``` + +## Supported SQL Dialects +`sql-insight-cli` leverages sqlparser-rs for parsing, supporting a wide range of SQL dialects. For a detailed list, please refer to the sqlparser-rs documentation. + +## Contributing +Contributions to `sql-insight-cli` 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 +`sql-insight-cli` is licensed under the [MIT License](https://github.com/takaebato/sql-insight/blob/master/sql-insight-cli/LICENSE.txt). diff --git a/sql-insight/README.md b/sql-insight/README.md new file mode 120000 index 0000000..32d46ee --- /dev/null +++ b/sql-insight/README.md @@ -0,0 +1 @@ +../README.md \ No newline at end of file