写一个解析sql血缘的python脚本
86浏览 · 2024-02-26 07:25:00
好的,以下是一个简单的解析SQL血缘的Python[脚本](https://geek.csdn.net/educolumn/093b3e5243538c1a64b04def9c71bb94?spm=1055.2569.3001.10083):
```python
import re
def parse_sql(sql):
"""解析SQL语句,[提取](https://geek.csdn.net/educolumn/51f874335133aa1e182153148be48d27?spm=1055.2569.3001.10083)表名和字段名"""
# 匹配[select](https://geek.csdn.net/educolumn/5d806cb20badd69e4fa54cbe0d705676?spm=1055.2569.3001.10083)语句中的表名和字段名
[select](https://geek.csdn.net/educolumn/5d806cb20badd69e4fa54cbe0d705676?spm=1055.2569.3001.10083)_pattern = r'SELECT(.*)FROM\s+(\S+)'
select_match = re.search(select_pattern, sql, re.IGNORECASE)
if select_match:
select_fields = select_match.group(1).strip()
table_name = select_match.group(2).strip()
else:
select_fields = ''
table_name = ''
# 匹配JOIN语句中的表名
join_pattern = r'JOIN\s+(\S+)\s+ON'
join_matches = re.findall(join_pattern, sql, re.IGNORECASE)
join_tables = [table.strip() for table in join_matches]
# 匹配WHERE语句中的字段名和表名
where_pattern = r'WHERE\s+(\S+)\s*(=|IN|L[ike](https://geek.csdn.net/educolumn/4654d38702ca9c29a51957a5f9fc9ed5?spm=1055.2569.3001.10083))\s*(\S+)'
where_