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
|
60 |
|
public function __construct(\PDO $pdo, $database) |
38
|
|
|
{ |
39
|
60 |
|
$this->database = $database; |
40
|
60 |
|
$this->pdo = $pdo; |
41
|
60 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the result. |
45
|
|
|
* |
46
|
|
|
* @param string $tableName |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
51 |
|
public function getTable($tableName) |
50
|
|
|
{ |
51
|
51 |
|
$this->columns = []; |
52
|
|
|
|
53
|
|
|
try { |
54
|
51 |
|
$information = $this->pdo->prepare('DESCRIBE ' . $tableName); |
55
|
|
|
|
56
|
51 |
|
$information->execute(); |
57
|
51 |
|
$information->setFetchMode(\PDO::FETCH_OBJ); |
58
|
51 |
|
} catch (\PDOException $e) { |
59
|
|
|
// Table not found |
60
|
|
|
} |
61
|
|
|
|
62
|
51 |
|
while ($row = $information->fetch()) { |
63
|
48 |
|
$this->setColumns($tableName, $row); |
64
|
48 |
|
} |
65
|
|
|
|
66
|
51 |
|
return $this->columns; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows the list of tables. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
9 |
|
public function showTables() |
75
|
|
|
{ |
76
|
9 |
|
$tables = []; |
77
|
|
|
|
78
|
9 |
|
$information = $this->pdo->prepare('SHOW TABLES'); |
79
|
9 |
|
$information->execute(); |
80
|
|
|
|
81
|
9 |
|
while ($row = $information->fetch()) { |
82
|
9 |
|
array_push($tables, $row[0]); |
83
|
9 |
|
} |
84
|
|
|
|
85
|
9 |
|
return $tables; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Prepares the defined columns. |
90
|
|
|
* |
91
|
|
|
* @param string $tableName |
92
|
|
|
* @param mixed $row |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
48 |
|
protected function setColumns($tableName, $row) |
96
|
|
|
{ |
97
|
48 |
|
preg_match('/(.*?)\((.*?)\)/', $row->Type, $match); |
98
|
|
|
|
99
|
48 |
|
$column = new Column; |
100
|
|
|
|
101
|
48 |
|
$this->setProperties($row, $column); |
102
|
48 |
|
$this->setKey($row, $column); |
103
|
|
|
|
104
|
48 |
|
$column->setDataType($row->Type); |
105
|
48 |
|
$column->setDefaultValue($row->Default); |
106
|
48 |
|
$column->setField($row->Field); |
107
|
|
|
|
108
|
48 |
|
if (isset($match[1])) { |
109
|
48 |
|
$column->setDataType($match[1]); |
110
|
48 |
|
$column->setLength($match[2]); |
111
|
48 |
|
} |
112
|
|
|
|
113
|
|
|
$query = 'SELECT COLUMN_NAME as "column",' . |
114
|
48 |
|
'REFERENCED_COLUMN_NAME as "referenced_column",' . |
115
|
48 |
|
'CONCAT(' . |
116
|
48 |
|
'REFERENCED_TABLE_SCHEMA, ".",' . |
117
|
48 |
|
'REFERENCED_TABLE_NAME' . |
118
|
48 |
|
') as "referenced_table"' . |
119
|
48 |
|
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' . |
120
|
48 |
|
'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' . |
121
|
48 |
|
'AND TABLE_NAME = "' . $tableName . '";'; |
122
|
|
|
|
123
|
48 |
|
$foreignTable = $this->pdo->prepare($query); |
124
|
|
|
|
125
|
48 |
|
$foreignTable->execute(); |
126
|
48 |
|
$foreignTable->setFetchMode(\PDO::FETCH_OBJ); |
127
|
|
|
|
128
|
48 |
|
$this->setForeignColumns($foreignTable, $row, $column); |
129
|
|
|
|
130
|
48 |
|
array_push($this->columns, $column); |
131
|
48 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets the key of the specified column. |
135
|
|
|
* |
136
|
|
|
* @param mixed $row |
137
|
|
|
* @param \Rougin\Describe\Column &$column |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
48 |
|
protected function setKey($row, Column &$column) |
141
|
|
|
{ |
142
|
48 |
|
switch ($row->Key) { |
143
|
48 |
|
case 'PRI': |
144
|
48 |
|
$column->setPrimary(true); |
145
|
|
|
|
146
|
48 |
|
break; |
147
|
|
|
|
148
|
48 |
|
case 'MUL': |
149
|
48 |
|
$column->setForeign(true); |
150
|
|
|
|
151
|
48 |
|
break; |
152
|
|
|
|
153
|
48 |
|
case 'UNI': |
154
|
48 |
|
$column->setUnique(true); |
155
|
|
|
|
156
|
48 |
|
break; |
157
|
48 |
|
} |
158
|
48 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sets the properties of the specified column. |
162
|
|
|
* |
163
|
|
|
* @param \PDOStatement $foreignTable |
164
|
|
|
* @param mixed $row |
165
|
|
|
* @param \Rougin\Describe\Column &$column |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
48 |
|
protected function setForeignColumns($foreignTable, $row, Column &$column) |
169
|
|
|
{ |
170
|
48 |
|
while ($foreignRow = $foreignTable->fetch()) { |
171
|
45 |
|
if ($foreignRow->column == $row->Field) { |
172
|
45 |
|
$referencedTable = $this->stripTableSchema($foreignRow->referenced_table); |
173
|
|
|
|
174
|
45 |
|
$column->setReferencedField($foreignRow->referenced_column); |
175
|
45 |
|
$column->setReferencedTable($referencedTable); |
176
|
45 |
|
} |
177
|
45 |
|
} |
178
|
48 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Sets the properties of the specified column. |
182
|
|
|
* |
183
|
|
|
* @param mixed $row |
184
|
|
|
* @param \Rougin\Describe\Column &$column |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
48 |
|
protected function setProperties($row, Column &$column) |
188
|
|
|
{ |
189
|
48 |
|
$null = 'Null'; |
190
|
|
|
|
191
|
48 |
|
if ($row->Extra == 'auto_increment') { |
192
|
48 |
|
$column->setAutoIncrement(true); |
193
|
48 |
|
} |
194
|
|
|
|
195
|
48 |
|
if ($row->$null == 'YES') { |
196
|
48 |
|
$column->setNull(true); |
197
|
48 |
|
} |
198
|
48 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Strips the table schema from the table name. |
202
|
|
|
* |
203
|
|
|
* @param string $table |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
45 |
|
protected function stripTableSchema($table) |
207
|
|
|
{ |
208
|
45 |
|
return (strpos($table, '.') !== false) ? substr($table, strpos($table, '.') + 1) : $table; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|