Completed
Pull Request — master (#1)
by Rougin
04:11
created

MySQLDriver::setProperties()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
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
        // if ($stripped = strpos($table, '.')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
        //     $table = substr($table, $stripped + 1);
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
        // }
59
60 45
        while ($row = $information->fetch()) {
61 45
            preg_match('/(.*?)\((.*?)\)/', $row->Type, $match);
62
63 45
            $column = new Column;
64
65 45
            $this->setProperties($row, $column);
66 45
            $this->setKey($row, $column);
67
68 45
            $column->setDataType($row->Type);
69 45
            $column->setDefaultValue($row->Default);
70 45
            $column->setField($row->Field);
71
72 45
            if (isset($match[1])) {
73 45
                $column->setDataType($match[1]);
74 45
                $column->setLength($match[2]);
75 45
            }
76
77
            $query = 'SELECT COLUMN_NAME as "column",' .
78 45
                'REFERENCED_COLUMN_NAME as "referenced_column",' .
79 45
                'CONCAT(' .
80 45
                    'REFERENCED_TABLE_SCHEMA, ".",' .
81 45
                    'REFERENCED_TABLE_NAME' .
82 45
                ') as "referenced_table"' .
83 45
                'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
84 45
                'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' .
85 45
                'AND TABLE_NAME = "' . $table . '";';
86
87 45
            $foreignTable = $this->pdo->prepare($query);
88
89 45
            $foreignTable->execute();
90 45
            $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
91
92 45
            $this->setForeignColumns($foreignTable, $row, $column);
93
94 45
            array_push($this->columns, $column);
95 45
        }
96
97 45
        return $this->columns;
98
    }
99
100
    /**
101
     * Shows the list of tables.
102
     *
103
     * @return array
104
     */
105 6
    public function showTables()
106
    {
107 6
        $tables = [];
108
109 6
        $information = $this->pdo->prepare('SHOW TABLES');
110 6
        $information->execute();
111
112 6
        while ($row = $information->fetch()) {
113 6
            array_push($tables, $row[0]);
114 6
        }
115
116 6
        return $tables;
117
    }
118
119
    /**
120
     * Sets the key of the specified column.
121
     *
122
     * @param  mixed                   $row
123
     * @param  \Rougin\Describe\Column &$column
124
     * @return void
125
     */
126 45
    protected function setKey($row, Column &$column)
127
    {
128 45
        switch ($row->Key) {
129 45
            case 'PRI':
130 45
                $column->setPrimary(true);
131
132 45
                break;
133
            
134 45
            case 'MUL':
135 45
                $column->setForeign(true);
136
137 45
                break;
138
139 45
            case 'UNI':
140 45
                $column->setUnique(true);
141
142 45
                break;
143 45
        }
144 45
    }
145
146
    /**
147
     * Sets the properties of the specified column.
148
     *
149
     * @param  \PDOStatement           $foreignTable
150
     * @param  mixed                   $row
151
     * @param  \Rougin\Describe\Column &$column
152
     * @return void
153
     */
154 45
    protected function setForeignColumns($foreignTable, $row, Column &$column)
155
    {
156 45
        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 45
    }
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 45
    protected function setProperties($row, Column &$column)
174
    {
175 45
        $null = 'Null';
176
177 45
        if ($row->Extra == 'auto_increment') {
178 45
            $column->setAutoIncrement(true);
179 45
        }
180
181 45
        if ($row->$null == 'YES') {
182 45
            $column->setNull(true);
183 45
        }
184 45
    }
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