Completed
Push — master ( ec2154...751b89 )
by Rougin
04:27
created

MySQLDriver::showTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use PDO;
6
use Rougin\Describe\Column;
7
8
/**
9
 * MySQL Driver
10
 *
11
 * A database driver extension for MySQL.
12
 *
13
 * @package  Describe
14
 * @category Driver
15
 * @author   Rougin Royce Gutib <[email protected]>
16
 */
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 42
    public function __construct(PDO $pdo, $database)
28
    {
29 42
        $this->database = $database;
30 42
        $this->pdo = $pdo;
31 42
    }
32
33
    /**
34
     * Returns the result.
35
     *
36
     * @return array
37
     */
38 39
    public function getTable($table)
39
    {
40 39
        $this->columns = [];
41
42 39
        $information = $this->pdo->prepare('DESCRIBE ' . $table);
43 39
        $information->execute();
44 39
        $information->setFetchMode(PDO::FETCH_OBJ);
45
46 39
        if ($stripped = strpos($table, '.')) {
47 3
            $table = substr($table, $stripped + 1);
48 3
        }
49
50 39
        while ($row = $information->fetch()) {
51 39
            preg_match('/(.*?)\((.*?)\)/', $row->Type, $match);
52
53 39
            $column = new Column;
54 39
            $null   = 'Null';
55
56 39
            if ($row->Extra == 'auto_increment') {
57 39
                $column->setAutoIncrement(true);
58 39
            }
59
60 39
            if ($row->$null == 'YES') {
61 39
                $column->setNull(true);
62 39
            }
63
64 39
            switch ($row->Key) {
65 39
                case 'PRI':
66 39
                    $column->setPrimary(true);
67
68 39
                    break;
69
                
70 39
                case 'MUL':
71 39
                    $column->setForeign(true);
72
73 39
                    break;
74
75 39
                case 'UNI':
76 39
                    $column->setUnique(true);
77
78 39
                    break;
79 39
            }
80
81 39
            $column->setDataType($row->Type);
82 39
            $column->setDefaultValue($row->Default);
83 39
            $column->setField($row->Field);
84
85 39
            if (isset($match[1])) {
86 39
                $column->setDataType($match[1]);
87 39
                $column->setLength($match[2]);
88 39
            }
89
90
            $query = 'SELECT COLUMN_NAME as "column",' .
91 39
                'REFERENCED_COLUMN_NAME as "referenced_column",' .
92 39
                'CONCAT(' .
93 39
                    'REFERENCED_TABLE_SCHEMA, ".",' .
94 39
                    'REFERENCED_TABLE_NAME' .
95 39
                ') as "referenced_table"' .
96 39
                'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
97 39
                'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' .
98 39
                'AND TABLE_NAME = "' . $table . '";';
99
100 39
            $foreignTable = $this->pdo->prepare($query);
101 39
            $foreignTable->execute();
102 39
            $foreignTable->setFetchMode(PDO::FETCH_OBJ);
103
104 39
            while ($foreignRow = $foreignTable->fetch()) {
105 39
                if ($foreignRow->column == $row->Field) {
106 39
                    $column->setReferencedField($foreignRow->referenced_column);
107 39
                    $column->setReferencedTable($foreignRow->referenced_table);
108 39
                }
109 39
            }
110
111 39
            array_push($this->columns, $column);
112 39
        }
113
114 39
        return $this->columns;
115
    }
116
117
    /**
118
     * Shows the list of tables.
119
     *
120
     * @return array
121
     */
122 3
    public function showTables()
123
    {
124 3
        $tables = [];
125
126 3
        $information = $this->pdo->prepare('SHOW TABLES');
127 3
        $information->execute();
128
129 3
        while ($row = $information->fetch()) {
130 3
            array_push($tables, $row[0]);
131 3
        }
132
133 3
        return $tables;
134
    }
135
}
136