Completed
Pull Request — master (#1)
by Rougin
03:27
created

MySQLDriver::stripTableSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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