Completed
Pull Request — master (#4)
by Rougin
03:41
created

MySQLDriver::setColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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