Metadata-Version: 2.4
Name: sqlfingerprint
Version: 0.1.0
Summary: Generate unique fingerprints for SQL queries
Project-URL: Homepage, https://github.com/yinhaox/sqlfingerprint
Author-email: yinhaox <yinhao0226@gmail.com>
License: MIT License
        
        Copyright (c) 2025 yinhaox
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Requires-Dist: sqlparse>=0.5.3
Description-Content-Type: text/markdown

# SQL Fingerprint
A Python library for generating normalized SQL fingerprints - useful for query identification, logging, and analysis.
## Features
- 🪄 **Query Normalization** - Produces consistent SQL representations
- 🔍 **Literal Replacement** - Safely replaces values with placeholders
- 📏 **Whitespace Normalization** - Removes insignificant spacing
- 🧹 **Syntax Cleaning** - Standardizes SQL keywords and formatting
- ✅ **Select Clause Preservation** - Maintains string literals in SELECT statements
## Installation
```bash
pip install sqlfingerprint
```
## Quick Start
```python
from sqlfingerprint import SQLFingerprinter

fingerprinter = SQLFingerprinter()
# Basic example
query = "SELECT * FROM users WHERE id = 123"
print(fingerprinter.fingerprint(query))
# Output: select * from users where id = ?
# Complex example
complex_sql = """
    SELECT name, 'const' AS const_val FROM users 
    WHERE email LIKE '%@example.com' 
    AND created_at > '2024-01-01' 
    GROUP BY 1 ORDER BY 1 DESC
"""
print(fingerprinter.fingerprint(complex_sql))
# Output: select name, 'const' as const_val from users where email like ? 
#         and created_at > ? group by 1 order by 1 desc
```
## Use Cases
- 🕵️ Query deduplication in database logs
- 📊 SQL performance analysis
- 🔒 Sensitive data obfuscation
- 🔄 Query pattern recognition
- 📈 Query analytics aggregation
## Normalization Rules
The fingerprinting process applies these transformations：
| Original Element       | Normalized Form       |
|-------------------------|-----------------------|
| String literals (WHERE) | ?                     |
| Numeric literals        | ?                     |
| Boolean values          | ?                     |
| IN lists                | IN (?)                |
| SQL keywords            | Lowercase             |
| Whitespace              | Single space          |
| Comments                | Removed               |
| Backticks               | Removed               |
| Parentheses spacing     | Standardized          |
*SELECT clause string literals are preserved for result set identification*
## Limitations
- Primarily tested with SELECT statements
- May require tuning for complex CTE queries
- Performance scales with query complexity
- SQL dialect support limited to standard SQL
## Contributing
Contributions are welcome! Please follow these steps:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/your-feature`)
3. Add tests for your changes
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin feature/your-feature`)
6. Open a Pull Request
Run tests with：
```bash
pytest tests/
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
---
**Project Homepage**: [https://github.com/yinhaox/sqlfingerprint](https://github.com/yinhaox/sqlfingerprint)