SQL file processing package for extracting table names and WHERE conditions
Project description
sql_scan
sql_scan is a Python library designed for processing SQL files. It provides functionalities for extracting table names and WHERE conditions, formatting SQL queries, and generating structured reports. This package is particularly useful for data engineers and developers working with SQL scripts, enabling them to streamline their workflow and enhance code readability.
Table of Contents
Features
- Extract table names and types (FROM, JOIN, etc.) from SQL queries.
- Identify and extract WHERE clauses associated with table queries.
- Format SQL queries for improved readability using
sqlparse. - Generate reports summarizing extracted information in a user-friendly format.
Installation
To install sql_scan, you can use pip. The package requires Python 3.6 or higher. Run the following command in your terminal:
pip install sql_scan
Usage
To use sql_scan in your project, simply import the relevant modules and call the functions as needed. Below is a basic usage example.
Example
from sql_scan.file_writer import extract_data
# Replace 'your_sql_file.sql' with the path to your SQL file.
extract_data('your_sql_file.sql', save_formatted_file=True)
This function will process the specified SQL file, extract the relevant information, and generate a report file summarizing the results.
Modules
sql_scan is composed of several modules, each responsible for specific functionalities:
- name_cleaner.py: Contains functions for cleaning SQL table names.
- table_extractor.py: Handles the extraction of table names from SQL statements.
- where_extractor.py: Extracts WHERE clauses from SQL queries.
- formatter.py: Formats SQL queries for better readability.
- file_writer.py: Integrates all modules to extract data and write it to output files.
Detailed Functionality
1. name_cleaner.py
This module provides functions for cleaning and validating SQL table names to ensure they adhere to proper naming conventions.
Function:
name_cleaner(name: str) -> str: Cleans up table names by removing unwanted characters.
2. table_extractor.py
This module extracts table names from SQL lines based on specified keywords.
Function:
extract_table_name(current_line: str, prev_line: str) -> tuple: Returns the keyword type and cleaned table name.
3. where_extractor.py
This module extracts WHERE conditions from SQL queries, returning them as lists of strings.
Function:
extract_where_condition(lines: list, bookmark: int) -> list: Extracts WHERE clauses based on a starting bookmark.
4. formatter.py
Formats SQL files for improved readability using the sqlparse library.
Function:
format_file_first(file_name: str): Reads and formats an SQL file, saving it as a new file.
5. file_writer.py
This module consolidates the functionality of other modules to extract and write information to a report.
Function:
extract_data(sql_file_name: str, save_formatted_file: bool): Main entry point for extracting data from SQL files and generating reports.
Contributing
Contributions are welcome! If you would like to contribute to sql_scan, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes and commit them (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Open a pull request describing your changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Thanks to the contributors of the
sqlparselibrary, which greatly enhances the formatting capabilities of sql_scan. - Special thanks to the open-source community for their continuous support and inspiration.
For more information, feel free to reach out via meghshamofficial@gmail.com.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_scan-0.5.tar.gz.
File metadata
- Download URL: sql_scan-0.5.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73288759de6ce5a2144ad99dc3fdcc3a7d82b73b21db2f32847ef1ef3bf3a68a
|
|
| MD5 |
07e0c7b814ced555fc975d870c578227
|
|
| BLAKE2b-256 |
0903a510ad8d06684a7eeee541f3e0c6e67372732d98df5a75c0ecc9c0012558
|
File details
Details for the file sql_scan-0.5-py3-none-any.whl.
File metadata
- Download URL: sql_scan-0.5-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eee746452973d56006fcd0ad0993b5daa3a6affd6e107f97bb741026cde23817
|
|
| MD5 |
fffb96f6079fb9d57402cf117ed99b84
|
|
| BLAKE2b-256 |
d0563b8c804b772d08cf5f72793297c14ce6af82d2cdc9c80af52eeeecd88c3d
|