Crates.io | sql-insight |
lib.rs | sql-insight |
version | 0.2.0 |
source | src |
created_at | 2024-02-12 11:17:24.159995 |
updated_at | 2024-07-04 15:10:29.925674 |
description | A utility for SQL query analysis, formatting, and transformation. |
homepage | https://github.com/takaebato/sql-insight |
repository | https://github.com/takaebato/sql-insight |
max_upload_size | |
id | 1136758 |
size | 82,044 |
A utility for SQL query analysis, formatting, and transformation. Leveraging the comprehensive parsing capabilities of sqlparser-rs, it can handle various SQL dialects.
Add sql_insight
to your Cargo.toml
file:
[dependencies]
sql-insight = { version = "0.2.0" }
Format SQL queries according to different dialects:
use sql_insight::sqlparser::dialect::GenericDialect;
let dialect = GenericDialect {};
let formatted_sql = sql_insight::format(&dialect, "SELECT * \n from users WHERE id = 1").unwrap();
assert_eq!(formatted_sql, ["SELECT * FROM users WHERE id = 1"]);
Normalize SQL queries to abstract away literals:
use sql_insight::sqlparser::dialect::GenericDialect;
let dialect = GenericDialect {};
let normalized_sql = sql_insight::normalize(&dialect, "SELECT * \n from users WHERE id = 1").unwrap();
assert_eq!(normalized_sql, ["SELECT * FROM users WHERE id = ?"]);
Extract table references from SQL queries:
use sql_insight::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 }) }]))]
Identify CRUD operations and the tables involved in each operation within SQL queries:
use sql_insight::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: [] })]
sql-insight
supports a comprehensive range of SQL dialects through sqlparser-rs. For details on supported dialects, please refer to the sqlparser-rs documentation.
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.
sql-insight
is distributed under the MIT license.