::: danger
注意!
这个脚本有个问题,用例中包含了失败的用例,所以提取的SQL并不都是合法的
应该通过解析对应的预期结果过滤掉非法SQL
:::
直接从 MySQL 的源码中提取测试用例中的 SQL 用于构建测试兼容性的 SQL 数据集
比如在目录 mysql-5.7.29/mysql-test/
下,执行 python 脚本
import re
import os
pattern = re.compile(r"^(select|update|insert|delete|drop|truncate|alter|set|show|audit|" \
r"comment|rename|grant|revoke|use|desc|explain).*;$")
fout = open("test.sql", "wb")
for root, dirs, files in os.walk("."):
for filename in files:
if filename.endswith(".test"):
filepath = os.path.join(root, filename)
with open(filepath, "rb") as f:
for line in f:
line = line.decode("utf-8", "ignore").lower().strip()
if pattern.match(line):
# print(line)
fout.write(line.encode("utf-8", "ignore") + b"\n")
fout.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
就可以在当前目录下生成 test.sql
了