Completed
Pull Request — master (#2)
by Rougin
02:27
created

SQLiteDriver::prepareForeignColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Column;
6
7
/**
8
 * SQLite Driver
9
 *
10
 * A database driver extension for SQLite.
11
 *
12
 * @package  Describe
13
 * @category Driver
14
 * @author   Rougin Royce Gutib <[email protected]>
15
 */
16
class SQLiteDriver implements DriverInterface
17
{
18
    /**
19
     * @var \PDO
20
     */
21
    protected $pdo;
22
23
    /**
24
     * @param PDO $pdo
25
     */
26 18
    public function __construct(\PDO $pdo)
27
    {
28 18
        $this->pdo = $pdo;
29 18
    }
30
31
    /**
32
     * Returns the result.
33
     *
34
     * @return array
35
     */
36 12
    public function getTable($table)
37
    {
38 12
        $columns = [];
39
40
        // Gets list of columns
41 12
        $query = 'PRAGMA table_info("' . $table . '");';
42 12
        $information = $this->pdo->prepare($query);
43
44 12
        $information->execute();
45 12
        $information->setFetchMode(\PDO::FETCH_OBJ);
46
47 12
        while ($row = $information->fetch()) {
48 12
            $column = new Column;
49
50 12
            $this->setProperties($row, $column);
51
52 12
            $column->setDefaultValue($row->dflt_value);
53 12
            $column->setField($row->name);
54 12
            $column->setDataType(strtolower($row->type));
55
56 12
            array_push($columns, $column);
57 12
        }
58
59 12
        return $this->prepareForeignColumns($table, $columns);
60
    }
61
62
    /**
63
     * Shows the list of tables.
64
     *
65
     * @return array
66
     */
67 6
    public function showTables()
68
    {
69 6
        $tables = [];
70
71
        // Gets list of columns
72 6
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
73 6
        $information = $this->pdo->prepare($query);
74
75 6
        $information->execute();
76 6
        $information->setFetchMode(\PDO::FETCH_OBJ);
77
78 6
        while ($row = $information->fetch()) {
79 6
            if ($row->name != 'sqlite_sequence') {
80 6
                array_push($tables, $row->name);
81 6
            }
82 6
        }
83
84 6
        return $tables;
85
    }
86
87
    /**
88
     * Prepares the columns that have foreign keys.
89
     *
90
     * @param  string $tableName
91
     * @param  array  $columns
92
     * @return void
93
     */
94 12
    protected function prepareForeignColumns($tableName, array $columns)
95
    {
96
        // Gets list of foreign keys, if any
97 12
        $query = 'PRAGMA foreign_key_list("' . $tableName . '");';
98 12
        $table = $this->pdo->prepare($query);
99
100 12
        $table->execute();
101 12
        $table->setFetchMode(\PDO::FETCH_OBJ);
102
103 12
        $rows = [];
104
105 12
        while ($row = $table->fetch()) {
106 12
            array_push($rows, $row);
107 12
        }
108
109 12
        return $this->setForeignColumns($columns, $rows);
110
    }
111
112
    /**
113
     * Sets the properties of the specified column.
114
     *
115
     * @param  array $columns
116
     * @param  array $rows
117
     * @return array
118
     */
119 12
    protected function setForeignColumns(array $columns, array $rows)
120
    {
121 12
        foreach ($columns as $index => $column) {
122 12
            if ($column->isForeignKey() && $column->getField() == $rows[$index]->from) {
123
                $column->setForeign(true);
124
125
                $column->setReferencedField($rows[$index]->to);
126
                $column->setReferencedTable($rows[$index]->table);
127
            }
128 12
        }
129
130 12
        return $columns;
131
    }
132
133
    /**
134
     * Sets the properties of the specified column.
135
     *
136
     * @param  mixed                   $row
137
     * @param  \Rougin\Describe\Column &$column
138
     * @return void
139
     */
140 12
    protected function setProperties($row, Column &$column)
141
    {
142 12
        if (! $row->notnull) {
143 12
            $column->setNull(true);
144 12
        }
145
146 12
        if ($row->pk) {
147 12
            $column->setPrimary(true);
148 12
            $column->setAutoIncrement(true);
149 12
        }
150 12
    }
151
}
152