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 | $column->setNull(true); |
||
61 | } |
||
62 | |||
63 | switch ($row->Key) { |
||
64 | case 'PRI': |
||
65 | $column->setPrimary(true); |
||
66 | |||
67 | break; |
||
68 | |||
69 | case 'MUL': |
||
70 | $column->setForeign(true); |
||
71 | |||
72 | break; |
||
73 | |||
74 | case 'UNI': |
||
75 | $column->setUnique(true); |
||
76 | |||
77 | break; |
||
78 | } |
||
79 | |||
80 | $column->setDataType($row->Type); |
||
81 | $column->setDefaultValue($row->Default); |
||
82 | $column->setField($row->Field); |
||
83 | |||
84 | if (isset($match[1])) { |
||
85 | $column->setDataType($match[1]); |
||
86 | $column->setLength($match[2]); |
||
87 | } |
||
88 | |||
89 | $query = 'SELECT COLUMN_NAME as "column",' . |
||
90 | 'REFERENCED_COLUMN_NAME as "referenced_column",' . |
||
91 | 'CONCAT(' . |
||
92 | 'REFERENCED_TABLE_SCHEMA, ".",' . |
||
93 | 'REFERENCED_TABLE_NAME' . |
||
94 | ') as "referenced_table"' . |
||
95 | 'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' . |
||
96 | 'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' . |
||
97 | 'AND TABLE_NAME = "' . $table . '";'; |
||
98 | |||
99 | $foreignTable = $this->pdo->prepare($query); |
||
100 | $foreignTable->execute(); |
||
101 | $foreignTable->setFetchMode(PDO::FETCH_OBJ); |
||
102 | |||
103 | while ($foreignRow = $foreignTable->fetch()) { |
||
104 | if ($foreignRow->column == $row->Field) { |
||
105 | $column->setReferencedField($foreignRow->referenced_column); |
||
106 | $column->setReferencedTable($foreignRow->referenced_table); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | array_push($this->columns, $column); |
||
111 | } |
||
112 | |||
113 | return $this->columns; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Shows the list of tables. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 6 | public function showTables() |
|
134 | } |
||
135 |