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

SQLiteDriver::setForeignColumn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 12
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
        // Gets list of foreign keys, if any
60 12
        $query = 'PRAGMA foreign_key_list("' . $tableName . '");';
0 ignored issues
show
Bug introduced by
The variable $tableName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
        $table = $this->pdo->prepare($query);
62
63
        $table->execute();
64
        $table->setFetchMode(\PDO::FETCH_OBJ);
65
66
        while ($row = $table->fetch()) {
67
            $this->setForeignColumn($columns, $row);
68
        }
69
70
        return $columns;
71
    }
72
73
    /**
74
     * Shows the list of tables.
75
     *
76
     * @return array
77
     */
78 6
    public function showTables()
79
    {
80 6
        $tables = [];
81
82
        // Gets list of columns
83 6
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
84 6
        $information = $this->pdo->prepare($query);
85
86 6
        $information->execute();
87 6
        $information->setFetchMode(\PDO::FETCH_OBJ);
88
89 6
        while ($row = $information->fetch()) {
90 6
            if ($row->name != 'sqlite_sequence') {
91 6
                array_push($tables, $row->name);
92 6
            }
93 6
        }
94
95 6
        return $tables;
96
    }
97
98
    /**
99
     * Prepares the columns that have foreign keys.
100
     *
101
     * @param array  &$columns
102
     * @param object $row
103
     */
104
    protected function setForeignColumn(array &$columns, $row)
105
    {
106
        foreach ($columns as $column) {
107
            if ($column->getField() == $row->from) {
108
                $column->setForeign(true);
109
110
                $column->setReferencedField($row->to);
111
                $column->setReferencedTable($row->table);
112
            }
113
        }
114
115
        return $columns;
116
    }
117
118
    /**
119
     * Sets the properties of the specified column.
120
     *
121
     * @param  mixed                   $row
122
     * @param  \Rougin\Describe\Column &$column
123
     * @return void
124
     */
125 12
    protected function setProperties($row, Column &$column)
126
    {
127 12
        if (! $row->notnull) {
128 12
            $column->setNull(true);
129 12
        }
130
131 12
        if ($row->pk) {
132 12
            $column->setPrimary(true);
133 12
            $column->setAutoIncrement(true);
134 12
        }
135 12
    }
136
}
137