数据库数据字典生成脚本 (Database Dictionary Generator)
来源:互联网 发布:历代windows 编辑:程序博客网 时间:2024/06/11 00:52
<?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);}?>
阅读全文
0 0
- 数据库数据字典生成脚本 (Database Dictionary Generator)
- 数据字典(Data Dictionary)
- 数据字典(Data Dictionary)
- 生成MySQL数据字典的PHP脚本
- SqlServer2005(2000)数据库字典,将表数据生成SQL脚本的存储过程 (转)
- Oracle 数据字典(Data Dictionary)
- 框架笔记----数据字典Dictionary
- 数据库生成脚本,架构+数据
- 数据库生成数据字典工具(PDMREAD)图解
- PowerDesigner根据数据库生成数据字典
- 数据库生成数据字典工具(PDMREAD)图解
- 数据库脚本:Database.sql
- 用友时空CCERP/KSOA/CDM数据字典自生成脚本
- SQLserver数据库生成数据字典,数据库字段说明
- oracle 数据字典视图之 DICT / DICTIONARY
- asp中Dictionary(数据字典)的使用
- 线程安全的数据字典Dictionary
- 关于数据库数据导出、生成脚本方法
- HDU 6127-Hard challenge(计算几何)
- 现代操作系统面试总结
- 栈应用之任意进制转换
- 大家好!今日起,我的技术博客开通了。
- mysql 让id重新从1开始
- 数据库数据字典生成脚本 (Database Dictionary Generator)
- OpenGL error 0x0502
- 最大连续子序列乘积
- oracle回滚表数据到指定时间
- vue实现a标签点击高亮
- html5的一些小常识
- 关于spring boot的一些注解
- jQuery基础知识
- 【Python】copy()与deepcopy()的区别