本文主要是介绍PHP 针对人大金仓KingbaseES自动生成数据字典,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
针对国产数据库 人大金仓KingbaseES 其实php 连接采用pdo方式
必须:需要去人大数据金仓官方网站 下载对应版本的pdo_kdb 扩展驱动
其连接方法与pgsql 数据库连接方法大致相同 不解释 直接上代码:
<?php
/*** 生成人大金仓数据字典*/
header("Content-type:text/html;charset=utf-8");// 配置数据库
$database = array();
$database['DB_HOST'] = '127.0.0.1';
$database['DB_NAME'] = 'demo';
$database['DB_USER'] = 'demo';
$database['DB_PWD'] = 'demo';
$database['PORT'] = 54321;
try {$conn = new PDO("kdb:host={$database['DB_HOST']};dbname={$database['DB_NAME']};port={$database['PORT']}", $database['DB_USER'], $database['DB_PWD']);$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {die("连接失败: " . $e->getMessage());
}$result = $conn->query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'");
$tables = $result->fetchAll(PDO::FETCH_ASSOC);$html = '';$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<tbody><tr><th>序号</th><th>表名</th><th>功能说明</th></tr>';foreach ($tables as $k => $v) {$sort = $k + 1;$tableName = $v['table_name'];// Get table comment$tableComment = $conn->query("SELECT obj_description('public.{$tableName}'::regclass, 'pg_class') AS table_comment")->fetchColumn();$html .= '<tr>';$html .= '<td class="c1">' . $sort . '</td>';$html .= '<td class="c2">' . $tableName . '</td>';$html .= '<td class="c3">' . $tableComment . '</td>';$html .= '</tr>';
}
$html .= '</tbody></table></p>';$html .= "<br /><br /><br />";foreach ($tables as $v) {$tableName = $v['table_name'];$fields = $conn->query("SELECT column_name,data_type,column_default,is_nullable,character_maximum_length, -- Add this line if you want to get the character maximum lengthnumeric_precision, -- Add this line if you want to get the numeric precisionnumeric_scale, -- Add this line if you want to get the numeric scale(SELECT description FROM pg_description WHERE objoid = (SELECT oid FROM pg_class WHERE relname = '{$tableName}' AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public'))AND objsubid = (SELECT ordinal_position FROM information_schema.columns WHERE table_name = '{$tableName}' AND table_schema = 'public' AND column_name = c.column_name)) AS column_commentFROM information_schema.columns cWHERE table_name = '{$tableName}' AND table_schema = 'public'")->fetchAll(PDO::FETCH_ASSOC);$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';$html .= '<caption>表名:' . $tableName . ' ' . '</caption>';$html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>备注</th></tr>';foreach ($fields as $f) {$html .= '<tr>';$html .= '<td class="c1">' . $f['column_name'] . '</td>';$html .= '<td class="c2">' . $f['data_type'] . '</td>';$html .= '<td class="c3">' . $f['column_default'] . '</td>';$html .= '<td class="c4">' . ($f['is_nullable'] == 'YES' ? '是' : '否') . '</td>';$html .= '<td class="c5">' . $f['column_comment'] . '</td>';$html .= '</tr>';}$html .= '</tbody></table></p>';
}echo '<html><meta charset="utf-8"><title>自动生成数据字典</title><style>body,td,th {font-family:"宋体"; font-size:12px;}table,h1,p{width:960px;margin:0px auto;}table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;padding-left:5px;}table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}.c1{ width: 150px;}.c2{ width: 150px;}.c3{ width: 80px;}.c4{ width: 100px;}.c5{ width: 300px;}</style><body>';
echo '<h1 style="text-align:center;">' . $database['DB_NAME'] . '数据字典</h1>';
echo '<p style="text-align:center;margin:20px auto;">生成时间:' . date('Y-m-d H:i:s', time()) . '</p>';
echo $html;
echo '<p style="text-align:left;margin:20px auto;">总共:' . count($tables) . '个数据表</p>';
echo '</body></html>';
?>
这篇关于PHP 针对人大金仓KingbaseES自动生成数据字典的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!