-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpdbdoc.php
More file actions
270 lines (251 loc) · 8.38 KB
/
Copy pathphpdbdoc.php
File metadata and controls
270 lines (251 loc) · 8.38 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
error_reporting(E_ALL);
/**
* @author rafael
*
*/
class phpdbdoc {
protected $userdb = "";
protected $linkdb = "";
protected $database = "";
protected $password = "";
protected $db = "";
protected $information = "information_schema";
public function setUserdb($userdb) {
$this->userdb = $userdb;
}
public function setLinkdb($linkdb) {
$this->linkdb = $linkdb;
}
public function setDataBase($database) {
$this->database = $database;
}
public function setPassword($password) {
$this->password = $password;
}
public function getUserdb() {
return $this->userdb;
}
public function getLinkdb() {
return $this->linkdb;
}
public function getDataBase() {
return $this->database;
}
public function getPassword() {
return $this->password;
}
public function DBConnect() {
$dsn = "mysql:dbname={$this->information};host={$this->getLinkdb()}";
try {
$this->db = new PDO($dsn, $this->getUserdb(), $this->getPassword());
} catch (PDOException $ex) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function getDoc() {
$tables = $this->getTables();
$fields = $this->getFields($tables);
$constraints = $this->getConstraints($tables);
// echo "<pre>";
// print_r($constraints);
// echo "</pre>";
$this->render($tables, $fields, $constraints);
}
private function getTables() {
$tables = $this->db->prepare("select TABLE_NAME,TABLE_COMMENT,TABLE_COLLATION, CREATE_TIME from TABLES where TABLE_SCHEMA = '{$this->getDataBase()}'");
$tables->execute();
$tables->setFetchMode(PDO::FETCH_ASSOC);
$tables_list = $tables->fetchAll();
return $tables_list;
}
private function getFields($tables) {
$fields_list = array();
foreach ($tables as $table) {
$fields = $this->db->prepare("SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, COLUMN_KEY, EXTRA, COLUMN_COMMENT "
. "FROM COLUMNS "
. "WHERE TABLE_SCHEMA = '{$this->getDataBase()}' AND TABLE_NAME = '{$table['TABLE_NAME']}'");
$fields->execute();
$fields->setFetchMode(PDO::FETCH_ASSOC);
$fields_list[$table['TABLE_NAME']] = $fields->fetchAll();
}
return $fields_list;
}
private function getConstraints($tables) {
$constraints_list = array();
foreach ($tables as $table) {
$cts = $this->db->prepare("SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME "
. "FROM KEY_COLUMN_USAGE "
. "WHERE TABLE_SCHEMA = '{$this->getDataBase()}' AND TABLE_NAME = '{$table['TABLE_NAME']}'");
$cts->execute();
$cts->setFetchMode(PDO::FETCH_ASSOC);
$constraints_list[$table['TABLE_NAME']] = $cts->fetchAll();
}
return $constraints_list;
}
private function render($tables, $fields, $constraints) {
echo "<!DOCTYPE html>
<html>
<head>
<style>
html{
padding: 0;
margin: 0;
border: 0;
}
body{
min-width: 100%;
max-width: 100%;
}
table{
min-width: 100%;
border-width: 2px;
table-layout:auto;
border-style: solid;
border-collapse: collapse;
margin: 10px 0 0 0;
padding: 0;
}
thead{
background-color: #dddbdb;
border-width: 0px;
border-style: none;
padding: 0;
margin: 0;
}
tbody tr:nth-child(odd) {
background-color: #eef1f0;
}
tr td {
border-width: 0px;
border-style: none;
margin: 0;
text-align: center;
}
.table_name{
text-align: left;
background-color: #d9faea;
}
@media print {
body{
max-height: 3508px;
max-width: 2480px;
}
}
</style>
<title>DB Doc - {$this->getDataBase()}</title>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width = device-width, initial-scale = 1.0\">
</head>
<body>\n";
echo "<table>
<thead>
<tr>
<th>Table Name</th>
<th>Comment</th>
<th>Collaction</th>
<th>Create Time</th>
</tr>
</thead>\n";
echo "<tbody>\n";
foreach ($tables as $table) {
echo "<tr>\n";
echo "<td>";
print $table["TABLE_NAME"];
echo "</td>\n";
echo "<td>";
print $table["TABLE_COMMENT"];
echo "</td>\n";
echo "<td>";
print $table["TABLE_COLLATION"];
echo "</td>\n";
echo "<td>";
print $table["CREATE_TIME"];
echo "</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
foreach ($fields as $name => $field) {
echo "<table>
<thead>
<tr>
<th colspan=\"6\" class=\"table_name\">{$name}</th>
</tr>
<tr>
<th>Column Name</th>
<th>Nullable</th>
<th>Type</th>
<th>Key</th>
<th>Extra</th>
<th>Comment</th>
</tr>
</thead>\n";
echo "<tbody>\n";
foreach ($field as $row) {
echo "<tr>\n";
echo "<td>";
print $row["COLUMN_NAME"];
echo "</td>\n";
echo "<td>";
print $row["IS_NULLABLE"];
echo "</td>\n";
echo "<td>";
print $row["COLUMN_TYPE"];
echo "</td>\n";
echo "<td>";
print $row["COLUMN_KEY"];
echo "</td>\n";
echo "<td>";
print $row["EXTRA"];
echo "</td>\n";
echo "<td>";
print $row["COLUMN_COMMENT"];
echo "</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
}
foreach ($constraints as $name => $constraint) {
echo "<table>
<thead>
<tr>
<th colspan=\"6\" class=\"table_name\">{$name}</th>
</tr>
<tr>
<th>Constraint Name</th>
<th>Table</th>
<th>Column</th>
<th>Referenced Table</th>
<th>Referenced column</th>
</tr>
</thead>\n";
echo "<tbody>\n";
foreach ($constraint as $row) {
echo "<tr>\n";
echo "<td>";
print $row["CONSTRAINT_NAME"];
echo "</td>\n";
echo "<td>";
print $row["TABLE_NAME"];
echo "</td>\n";
echo "<td>";
print $row["COLUMN_NAME"];
echo "</td>\n";
echo "<td>";
print $row["REFERENCED_TABLE_NAME"];
echo "</td>\n";
echo "<td>";
print $row["REFERENCED_COLUMN_NAME"];
echo "</td>\n";
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
}
echo "</body>"
. "</html>";
}
}
?>