|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Describe\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Describe\Column; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* MySQL Driver |
|
9
|
|
|
* |
|
10
|
|
|
* A database driver extension for MySQL. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Describe |
|
13
|
|
|
* @category Driver |
|
14
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class MySQLDriver implements DriverInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $columns = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $database; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \PDO |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $pdo; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param PDO $pdo |
|
35
|
|
|
* @param string $database |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(\PDO $pdo, $database) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->database = $database; |
|
40
|
|
|
$this->pdo = $pdo; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the result. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getTable($table) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->columns = []; |
|
51
|
|
|
|
|
52
|
|
|
$information = $this->pdo->prepare('DESCRIBE ' . $table); |
|
53
|
|
|
$information->execute(); |
|
54
|
|
|
$information->setFetchMode(\PDO::FETCH_OBJ); |
|
55
|
|
|
|
|
56
|
|
|
// if ($stripped = strpos($table, '.')) { |
|
|
|
|
|
|
57
|
|
|
// $table = substr($table, $stripped + 1); |
|
|
|
|
|
|
58
|
|
|
// } |
|
59
|
|
|
|
|
60
|
|
|
while ($row = $information->fetch()) { |
|
61
|
|
|
preg_match('/(.*?)\((.*?)\)/', $row->Type, $match); |
|
62
|
|
|
|
|
63
|
|
|
$column = new Column; |
|
64
|
|
|
$null = 'Null'; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->setProperties($row, $column); |
|
67
|
|
|
$this->setKey($row, $column); |
|
68
|
|
|
|
|
69
|
|
|
$column->setDataType($row->Type); |
|
70
|
|
|
$column->setDefaultValue($row->Default); |
|
71
|
|
|
$column->setField($row->Field); |
|
72
|
|
|
|
|
73
|
|
|
if (isset($match[1])) { |
|
74
|
|
|
$column->setDataType($match[1]); |
|
75
|
|
|
$column->setLength($match[2]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$query = 'SELECT COLUMN_NAME as "column",' . |
|
79
|
|
|
'REFERENCED_COLUMN_NAME as "referenced_column",' . |
|
80
|
|
|
'CONCAT(' . |
|
81
|
|
|
'REFERENCED_TABLE_SCHEMA, ".",' . |
|
82
|
|
|
'REFERENCED_TABLE_NAME' . |
|
83
|
|
|
') as "referenced_table"' . |
|
84
|
|
|
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' . |
|
85
|
|
|
'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' . |
|
86
|
|
|
'AND TABLE_NAME = "' . $table . '";'; |
|
87
|
|
|
|
|
88
|
|
|
$foreignTable = $this->pdo->prepare($query); |
|
89
|
|
|
|
|
90
|
|
|
$foreignTable->execute(); |
|
91
|
|
|
$foreignTable->setFetchMode(\PDO::FETCH_OBJ); |
|
92
|
|
|
|
|
93
|
|
|
$this->setForeignColumns($foreignTable, $row, $column); |
|
94
|
|
|
|
|
95
|
|
|
array_push($this->columns, $column); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->columns; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Shows the list of tables. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function showTables() |
|
107
|
|
|
{ |
|
108
|
|
|
$tables = []; |
|
109
|
|
|
|
|
110
|
|
|
$information = $this->pdo->prepare('SHOW TABLES'); |
|
111
|
|
|
$information->execute(); |
|
112
|
|
|
|
|
113
|
|
|
while ($row = $information->fetch()) { |
|
114
|
|
|
array_push($tables, $row[0]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $tables; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Sets the key of the specified column. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $row |
|
124
|
|
|
* @param \Rougin\Describe\Column &$column |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function setKey($row, Column &$column) |
|
128
|
|
|
{ |
|
129
|
|
|
switch ($row->Key) { |
|
130
|
|
|
case 'PRI': |
|
131
|
|
|
$column->setPrimary(true); |
|
132
|
|
|
|
|
133
|
|
|
break; |
|
134
|
|
|
|
|
135
|
|
|
case 'MUL': |
|
136
|
|
|
$column->setForeign(true); |
|
137
|
|
|
|
|
138
|
|
|
break; |
|
139
|
|
|
|
|
140
|
|
|
case 'UNI': |
|
141
|
|
|
$column->setUnique(true); |
|
142
|
|
|
|
|
143
|
|
|
break; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Sets the properties of the specified column. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \PDOStatement $foreignTable |
|
151
|
|
|
* @param mixed $row |
|
152
|
|
|
* @param \Rougin\Describe\Column &$column |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function setForeignColumns($foreignTable, $row, Column &$column) |
|
156
|
|
|
{ |
|
157
|
|
|
while ($foreignRow = $foreignTable->fetch()) { |
|
158
|
|
|
if ($foreignRow->column == $row->Field) { |
|
159
|
|
|
$referencedTable = $this->stripTableSchema($foreignRow->referenced_table); |
|
160
|
|
|
|
|
161
|
|
|
$column->setReferencedField($foreignRow->referenced_column); |
|
162
|
|
|
$column->setReferencedTable($referencedTable); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Sets the properties of the specified column. |
|
169
|
|
|
* |
|
170
|
|
|
* @param mixed $row |
|
171
|
|
|
* @param \Rougin\Describe\Column &$column |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function setProperties($row, Column &$column) |
|
175
|
|
|
{ |
|
176
|
|
|
if ($row->Extra == 'auto_increment') { |
|
177
|
|
|
$column->setAutoIncrement(true); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ($row->$null == 'YES') { |
|
|
|
|
|
|
181
|
|
|
$column->setNull(true); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Strips the table schema from the table name. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $table |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function stripTableSchema($table) |
|
192
|
|
|
{ |
|
193
|
|
|
return (strpos($table, '.') !== false) ? substr($table, strpos($table, '.') + 1) : $table; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.