python 解析sql表血缘,代码发我
时间: 2023-11-22 17:53:34
浏览: 80
下面是一个使用Python解析SQL表血缘的示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
import re
def parse_sql(sql_text):
# [提取](https://geek.csdn.net/educolumn/51f874335133aa1e182153148be48d27?spm=1055.2569.3001.10083)所有表名和列名
tables = set(re.findall(r'FROM (\w+)', sql_text, re.IGNORECASE))
tables.update(re.findall(r'JOIN (\w+)', sql_text, re.IGNORECASE))
columns = set(re.findall(r'[select](https://geek.csdn.net/educolumn/5d806cb20badd69e4fa54cbe0d705676?spm=1055.2569.3001.10083) ([\w\*,\s]+)', sql_text, re.IGNORECASE)[0].split(','))
columns.update(re.findall(r'ON [\w\.]+ = (\w+)', sql_text, re.IGNORECASE))
columns.update(re.findall(r'JOIN [\w\.]+ ON [\w\.]+ = (\w+)', sql_text, re.IGNORECASE))
# 构建表血缘[关系](https://geek.csdn.net/educolumn/583eff2c41d323861c58dd3c61f711f3?spm=1055.2569.3001.10083)
table_dependency = {}
for table in tables:
table_dependency[table] = set()
for column in columns:
for table in tables:
if column.[start](https://geek.csdn.net/educolumn/4a85bcdc6688705d0adf9b14887214ae?spm=1055.2569.3001.10083)swith(table + '.'):
for dep_table in tables:
if dep_table != table:
table_dependency[table].add(dep_table)
return table_dependency
```
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)