php脚本导出Markdown数据字典。
仅支持mysql。
执行完毕会在脚本所在目录下创建markdown文件夹,md文件存放在这个文件夹中。
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Created by PhpStorm.
* User: heimo
* Date: 2017/10/24
* Time: 下午2:31
*/
/**
* mysql配置
*/
$mysql_conf = array(
'host' => 'localhost:3306',
'db' => 'db',
'db_user' => 'root',
'db_pwd' => 'password',
);
/**
* 创建mysqli对象
*/
$mysqli = new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if (!$mysqli) {
//诊断连接错误
die("could not connect to the database:\n" . $mysqli->error);
} else {
echo "mysql connected\n";
}
//选择数据库
$select_db = $mysqli->select_db($mysql_conf['db']);
if (!$select_db) {
die("could not connect to the db:\n" . $mysqli->error);
} else {
echo "database selected\n";
}
//当前脚本地址
$path = dirname(__FILE__);
//Markdown文件保存地址,防止中文目录乱码
$markdownFileName = "markdown";
$markdownDir = iconv("UTF-8", "GBK", $path . '/' . $markdownFileName);
if (!file_exists($markdownDir)) {
//创建目录
if (mkdir($markdownDir, 0777, true)) {
echo "create file " . $markdownFile . " success\n";
} else {
die("create file " . $markdownFile . " failed\n");
}
} else {
echo "file " . $markdownFile . " is exist\n";
}
//数据库概览
$markdownFile = $mysql_conf['db'] . ".md";
//创建md文件
$file = fopen($markdownDir . '/' . $markdownFile, "w+") or die("create file " . $markdownFile . " failed\n");
//文本内容
$content = <<<markdown
# {$mysql_conf['db']}\n
-------------------\n
[TOC]
### overview
| TABLE_NAME | TABLE_COMMENT | ENGINE | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH| DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME | UPDATE_TIME| TABLE_COLLATION|
| :-------- | :-------- | :-------- | :-------- | --------: | --------: | --------: | --------: | --------: | --------: | --------: | --------: | --------: | --------: |\n
markdown;
$result = $mysqli->query("SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = '{$mysql_conf['db']}'");
while ($overview = $result->fetch_array()) {
$content .= <<<markdown
|{$overview['TABLE_NAME']}|{$overview['TABLE_COMMENT']}|{$overview['ENGINE']}|{$overview['ROW_FORMAT']}|{$overview['TABLE_ROWS']}|{$overview['AVG_ROW_LENGTH']}|{$overview['DATA_LENGTH']}|{$overview['MAX_DATA_LENGTH']}|{$overview['INDEX_LENGTH']}|{$overview['DATA_FREE']}|{$overview['AUTO_INCREMENT']}|{$overview['CREATE_TIME']}|{$overview['UPDATE_TIME']}|{$overview['TABLE_COLLATION']}|\n
markdown;
}
if (fwrite($file, $content)) {
echo $markdownFile . " success\n";
} else {
echo $markdownFile . " failed\n";
}
//获取所有表,循环获取列信息
//$tableResult = $mysqli->query("SHOW TABLES");
//while ($table = $tableResult->fetch_row()) {
$tableResult = $mysqli->query("SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = '{$mysql_conf['db']}'");
while ($tableInfo = $tableResult->fetch_array()) {
//表名
$table = $tableInfo['TABLE_NAME'];
$content = '';
//文本内容
$content .= <<<markdown
\n\n
-------------------\n
### {$table} {$tableInfo['TABLE_COMMENT']}\n
| COLUMN_NAME | COLUMN_DEFAULT| IS_NULLABLE| COLUMN_TYPE | COLUMN_KEY | EXTRA | COLUMN_COMMENT|
| :-------- | :-------- | :-------- | :-------- | :-------- | :-------- | :-------- |\n
markdown;
//当前表的列
$clumnResult = $mysqli->query("SELECT * FROM information_schema.COLUMNS WHERE table_name = '{$table}' AND table_schema = '{$mysql_conf['db']}';");
while ($column = $clumnResult->fetch_array()) {
if ($column['COLUMN_DEFAULT'] === null){
$column['COLUMN_DEFAULT'] = $column['IS_NULLABLE'] == 'YES' ? 'null' : '';
}elseif ($column['COLUMN_DEFAULT'] === ''){
$column['COLUMN_DEFAULT'] = "''";
}
$content .= <<<markdown
|{$column['COLUMN_NAME']}|{$column['COLUMN_DEFAULT']}|{$column['IS_NULLABLE']}|{$column['COLUMN_TYPE']}|{$column['COLUMN_KEY']}|{$column['EXTRA']}|{$column['COLUMN_COMMENT']}|\n
markdown;
}
//写入文件
if (fwrite($file, $content)) {
echo $table . " success\n";
} else {
echo $table . " failed\n";
}
}
fclose($file);
$mysqli->close();
转载使用注明出处。原文链接 https://heimo-he.github.io/php/2018/03/30/php-mmd/