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