Completed
Push — master ( c1d6d5...768908 )
by Rougin
23:40 queued 21:35
created

MySQLDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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