Completed
Pull Request — master (#3)
by Rougin
02:34
created

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