1 | <?php |
||
17 | class MySQLDriver implements DriverInterface |
||
18 | { |
||
19 | protected $database; |
||
20 | protected $columns = []; |
||
21 | protected $pdo; |
||
22 | |||
23 | /** |
||
24 | * @param PDO $pdo |
||
25 | * @param string $database |
||
26 | */ |
||
27 | 51 | public function __construct(PDO $pdo, $database) |
|
32 | |||
33 | /** |
||
34 | * Returns the result. |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | 45 | public function getTable($table) |
|
39 | { |
||
40 | 45 | $this->columns = []; |
|
41 | |||
42 | 45 | $information = $this->pdo->prepare('DESCRIBE ' . $table); |
|
43 | 45 | $information->execute(); |
|
44 | 45 | $information->setFetchMode(PDO::FETCH_OBJ); |
|
45 | |||
46 | 45 | if ($stripped = strpos($table, '.')) { |
|
47 | 3 | $table = substr($table, $stripped + 1); |
|
48 | 3 | } |
|
49 | |||
50 | 45 | while ($row = $information->fetch()) { |
|
51 | 45 | preg_match('/(.*?)\((.*?)\)/', $row->Type, $match); |
|
52 | |||
53 | 45 | $column = new Column; |
|
54 | |||
55 | 45 | if ($row->Extra == 'auto_increment') { |
|
56 | 45 | $column->setAutoIncrement(true); |
|
57 | 45 | } |
|
58 | |||
59 | 45 | if ($row->Null == 'YES') { |
|
60 | 45 | $column->setNull(true); |
|
61 | 45 | } |
|
62 | |||
63 | 45 | switch ($row->Key) { |
|
64 | 45 | case 'PRI': |
|
65 | 45 | $column->setPrimary(true); |
|
66 | |||
67 | 45 | break; |
|
68 | |||
69 | 45 | case 'MUL': |
|
70 | 45 | $column->setForeign(true); |
|
71 | |||
72 | 45 | break; |
|
73 | |||
74 | 45 | case 'UNI': |
|
75 | 45 | $column->setUnique(true); |
|
76 | |||
77 | 45 | break; |
|
78 | 45 | } |
|
79 | |||
80 | 45 | $column->setDataType($row->Type); |
|
81 | 45 | $column->setDefaultValue($row->Default); |
|
82 | 45 | $column->setField($row->Field); |
|
83 | |||
84 | 45 | if (isset($match[1])) { |
|
85 | 45 | $column->setDataType($match[1]); |
|
86 | 45 | $column->setLength($match[2]); |
|
87 | 45 | } |
|
88 | |||
89 | $query = 'SELECT COLUMN_NAME as "column",' . |
||
90 | 45 | 'REFERENCED_COLUMN_NAME as "referenced_column",' . |
|
91 | 45 | 'CONCAT(' . |
|
92 | 45 | 'REFERENCED_TABLE_SCHEMA, ".",' . |
|
93 | 45 | 'REFERENCED_TABLE_NAME' . |
|
94 | 45 | ') as "referenced_table"' . |
|
95 | 45 | 'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' . |
|
96 | 45 | 'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' . |
|
97 | 45 | 'AND TABLE_NAME = "' . $table . '";'; |
|
98 | |||
99 | 45 | $foreignTable = $this->pdo->prepare($query); |
|
100 | 45 | $foreignTable->execute(); |
|
101 | 45 | $foreignTable->setFetchMode(PDO::FETCH_OBJ); |
|
102 | |||
103 | 45 | while ($foreignRow = $foreignTable->fetch()) { |
|
104 | 45 | if ($foreignRow->column == $row->Field) { |
|
105 | 45 | $column->setReferencedField($foreignRow->referenced_column); |
|
106 | 45 | $column->setReferencedTable($foreignRow->referenced_table); |
|
107 | 45 | } |
|
108 | 45 | } |
|
109 | |||
110 | 45 | array_push($this->columns, $column); |
|
111 | 45 | } |
|
112 | |||
113 | 45 | return $this->columns; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Shows the list of tables. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 6 | public function showTables() |
|
134 | } |
||
135 |