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

SQLiteDriver::setForeignColumns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
crap 4
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
        $query = $this->pdo->prepare('PRAGMA table_info("' . $table . '");');
41 12
42 12
        $query->setFetchMode(\PDO::FETCH_OBJ)->execute();
0 ignored issues
show
Bug introduced by
The method execute cannot be called on $query->setFetchMode(\PDO::FETCH_OBJ) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
43
44 12
        while ($row = $query->fetch()) {
45 12
            $column = new Column;
46
47 12
            $this->setProperties($row, $column);
48 12
49
            $column->setDefaultValue($row->dflt_value);
50 12
            $column->setField($row->name);
51
            $column->setDataType(strtolower($row->type));
52 12
53 12
            array_push($columns, $column);
54 12
        }
55
56 12
        $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $table . '");');
57 12
58
        $query->setFetchMode(\PDO::FETCH_OBJ)->execute();
0 ignored issues
show
Bug introduced by
The method execute cannot be called on $query->setFetchMode(\PDO::FETCH_OBJ) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
59
60 12
        while ($row = $query->fetch()) {
61 12
            $this->setForeignColumn($columns, $row);
62
        }
63 12
64 12
        return $columns;
65
    }
66 12
67
    /**
68
     * Shows the list of tables.
69
     *
70
     * @return array
71
     */
72
    public function showTables()
73
    {
74 6
        $tables = [];
75
76 6
        $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";');
77
78
        $query->setFetchMode(\PDO::FETCH_OBJ)->execute();
0 ignored issues
show
Bug introduced by
The method execute cannot be called on $query->setFetchMode(\PDO::FETCH_OBJ) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79 6
80 6
        while ($row = $query->fetch()) {
81
            if ($row->name != 'sqlite_sequence') {
82 6
                array_push($tables, $row->name);
83 6
            }
84
        }
85 6
86 6
        return $tables;
87 6
    }
88 6
89 6
    /**
90
     * Prepares the columns that have foreign keys.
91 6
     *
92
     * @param array  &$columns
93
     * @param object $row
94
     */
95
    protected function setForeignColumn(array &$columns, $row)
96
    {
97
        foreach ($columns as $column) {
98
            if ($column->getField() == $row->from) {
99
                $column->setForeign(true);
100
101 12
                $column->setReferencedField($row->to);
102
                $column->setReferencedTable($row->table);
103 12
            }
104 12
        }
105 12
106 12
        return $columns;
107
    }
108 12
109 12
    /**
110 12
     * Sets the properties of the specified column.
111 12
     *
112 12
     * @param  mixed                   $row
113
     * @param  \Rougin\Describe\Column &$column
114 12
     * @return void
115
     */
116
    protected function setProperties($row, Column &$column)
117
    {
118
        if (! $row->notnull) {
119
            $column->setNull(true);
120
        }
121
122
        if ($row->pk) {
123
            $column->setPrimary(true);
124 12
            $column->setAutoIncrement(true);
125
        }
126 12
    }
127
}
128