1. 程式人生 > >資料庫資料字典生成指令碼 (Database Dictionary Generator)

資料庫資料字典生成指令碼 (Database Dictionary Generator)

<?php
/**  
 * 生成mysql資料字典  
*/
//資料庫配置
$config = [
'host'     => '127.0.0.1',
'user'     => 'root',
'password' => 'root',
];
function export_dict($dbname, $config) {
$title = $dbname.' 資料字典';
$dsn = 'mysql:dbname='.$dbname.';host='.$config['host'];
//資料庫連線
try {
$con = new PDO($dsn, $config['user'
], $config['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); } catch (PDOException $e) { die('Connection failed: ' . $e->getMessage()); } $tables = $con->query('SHOW tables')->fetchAll(PDO::FETCH_COLUMN); //取得所有的表名 foreach ($tables as $table) { $_tables[]['TABLE_NAME'] =
$table; } //迴圈取得所有表的備註及表中列訊息 foreach ($_tables as $k => $v) { $sql = 'SELECT * FROM '; $sql .= 'INFORMATION_SCHEMA.TABLES '; $sql .= 'WHERE '; $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$dbname}'"; $tr = $con->query($sql)->fetch(PDO::FETCH_ASSOC); $_tables
[$k]['TABLE_COMMENT'] = $tr['TABLE_COMMENT']; $sql = 'SELECT * FROM '; $sql .= 'INFORMATION_SCHEMA.COLUMNS '; $sql .= 'WHERE '; $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$dbname}'"; $fields = []; $field_result = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC); foreach ($field_result as $fr) { $fields[] = $fr; } $_tables[$k]['COLUMN'] = $fields; } unset($con); $mark = ''; //迴圈所有表 foreach ($_tables as $k => $v) { $mark .= '## '.$v['TABLE_NAME'].' '.$v['TABLE_COMMENT'].PHP_EOL; $mark .= ''.PHP_EOL; $mark .= '| 欄位名 | 資料型別 | 預設值 | 允許非空 | 自動遞增 | 備註 |'.PHP_EOL; $mark .= '| ------ | ------ | ------ | ------ | ------ | ------ |'.PHP_EOL; foreach ($v['COLUMN'] as $f) { $mark .= '| '.$f['COLUMN_NAME'].' | '.$f['COLUMN_TYPE'].' | '.$f['COLUMN_DEFAULT'].' | '.$f['IS_NULLABLE'].' | '.($f['EXTRA'] == 'auto_increment' ? '' : '').' | '.(empty($f['COLUMN_COMMENT']) ? '-' : str_replace('|', '/', $f['COLUMN_COMMENT'])).' |'.PHP_EOL; } $mark .= ''.PHP_EOL; } //markdown輸出 $md_tplt = <<<EOT # {$title}> 本資料字典由PHP指令碼自動匯出,字典的備註來自資料庫表及其欄位的註釋(`comment`).開發者在增改庫表及其欄位時,請在 `migration` 時寫明註釋,以備後來者查閱. {$mark}EOT; //html輸出 $marked_text = htmlentities($md_tplt); $html_tplt = <<<EOT <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>{$title} - Powered By Markdown Viewer</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" type="text/css" href="http://s1.ystatic.cn/41345695beaa9b2e/css/github-markdown.css"> <script src="http://s1.ystatic.cn/lib/marked/marked.js"></script> <script src="http://s1.ystatic.cn/lib/highlight.js/highlight.pack.js?v=9.6.0"></script> <link href="http://s1.ystatic.cn/lib/highlight.js/styles/github.css?v=9.6.0" rel="stylesheet"> </head> <body> <div class="markdown-body" id="content" style="margin:auto; width: 1024px;"> </div> <div id="marked_text" style="display:none;"> {$marked_text}</div> <script> var marked_text = document.getElementById('marked_text').innerText; var renderer = new marked.Renderer(); renderer.table = function(header, body) { return '<table class="table table-bordered table-striped">\\n' + '<thead>\\n' + header + '</thead>\\n' + '<tbody>\\n' + body + '</tbody>\\n' + '</table>\\n'; }; marked.setOptions({ renderer: renderer, gfm: true, tables: true, breaks: false, pedantic: false, sanitize: true, smartLists: true, smartypants: false, langPrefix: 'language-', //這裡使用了highlight對程式碼進行高亮顯示 highlight: function (code) { return hljs.highlightAuto(code).value; } }); document.getElementById('content').innerHTML = marked(marked_text); </script> </body> </html>EOT; file_put_contents($dbname.'.md', $md_tplt); file_put_contents($dbname.'.html', $html_tplt); } $dbs = ['yascmf_app', 'test']; foreach ($dbs as $db) { export_dict($db, $config); } ?>