forked from Fanstuck/sqlparse-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (53 loc) · 1.96 KB
/
main.py
File metadata and controls
74 lines (53 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Author: Shaowen He
Date: 2024-11-07 11:05:51
LastEditors: heshw@live.cn
LastEditTime: 2024-11-09 13:18:31
FilePath: /sqlparse-Python/main.py
Description:
Copyright (c) 2024 by Shaowen He, All Rights Reserved.
'''
# 设置Jupyter Notebook环境,否则echarts无法显示
# 降级notebook==6.0.1,以下内容不需要,否则无法显示
# from pyecharts.globals import CurrentConfig, NotebookType
# CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
from MainDef import *
def process_sql_visualization(sql_str: str):
"""处理SQL语句并生成可视化"""
try:
statements = analysis_statements(sql_str)
analyzer = BloodlineAnalyzer()
visualizer = BloodlineVisualizer()
for stmt in statements:
analyzer.reset()
# 分析血缘关系
table_bloodline = analyzer.analyze_table_bloodline(stmt)
if not table_bloodline:
print(f"警告: 在SQL语句中没有找到表名: {stmt}")
continue
column_bloodline = analyzer.analyze_column_bloodline(stmt)
# 创建可视化
table_viz = visualizer.create_table_tree(
analyzer.state.table_names,
stmt.get_type()
)
if column_bloodline:
column_viz = visualizer.create_column_sankey(
analyzer.state.table_names,
analyzer.state.column_names
)
# 显示结果
if table_viz:
display(table_viz)
if column_viz:
display(column_viz)
except Exception as e:
print(f"处理SQL可视化时发生错误: {e}")
if __name__ == '__main__':
# 创建分析器和可视化器实例
analyzer = BloodlineAnalyzer()
visualizer = BloodlineVisualizer()
# 读取SQL文件
sql_str = get_sqlstr('example_complex_sql.sql')
# 可视化
process_sql_visualization(sql_str)