-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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. | ||
- **Extract Table References**: Extract tables referenced in SQL queries, clarifying the data sources involved. | ||
- **Extract CRUD Table References**: 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 = "<version>" } | ||
``` | ||
|
||
Replace `<version>` with the latest version of `sql-insight`. | ||
|
||
## 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 | ||
|
||
We welcome contributions to sql-insight! Feel free to open an issue or submit a pull request. | ||
|
||
## License | ||
|
||
sql-insight is distributed under the [MIT license](https://github.com/takaebato/sql-insight/blob/master/LICENSE.txt). |