Completed
Pull Request — master (#1)
by Rougin
07:32
created

SQLiteDriver::setProperties()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
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 9
    public function __construct(\PDO $pdo)
27
    {
28 9
        $this->pdo = $pdo;
29 9
    }
30
31
    /**
32
     * Returns the result.
33
     *
34
     * @return array
35
     */
36 6
    public function getTable($table)
37
    {
38 6
        $columns = [];
39
40
        // Gets list of columns
41 6
        $query = 'PRAGMA table_info("' . $table . '");';
42 6
        $information = $this->pdo->prepare($query);
43
44 6
        $information->execute();
45 6
        $information->setFetchMode(\PDO::FETCH_OBJ);
46
47 6
        while ($row = $information->fetch()) {
48 6
            $column = new Column;
49
50 6
            $this->setProperties($row, $column);
51
52 6
            $column->setDefaultValue($row->dflt_value);
53 6
            $column->setField($row->name);
54 6
            $column->setDataType(strtolower($row->type));
55
56 6
            array_push($columns, $column);
57 6
        }
58
59
        // Gets list of foreign keys
60 6
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
61 6
        $foreignTable = $this->pdo->prepare($query);
62
63 6
        $foreignTable->execute();
64 6
        $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
65
66 6
        $this->setForeignColumns($foreignTable, $column);
67
68
        return $columns;
69
    }
70
71
    /**
72
     * Shows the list of tables.
73
     *
74
     * @return array
75
     */
76 3
    public function showTables()
77
    {
78 3
        $tables = [];
79
80
        // Gets list of columns
81 3
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
82 3
        $information = $this->pdo->prepare($query);
83
84 3
        $information->execute();
85 3
        $information->setFetchMode(\PDO::FETCH_OBJ);
86
87 3
        while ($row = $information->fetch()) {
88 3
            if ($row->name != 'sqlite_sequence') {
89 3
                array_push($tables, $row->name);
90 3
            }
91 3
        }
92
93 3
        return $tables;
94
    }
95
96
    /**
97
     * Sets the properties of the specified column.
98
     *
99
     * @param  \PDOStatement           $foreignTable
100
     * @param  \Rougin\Describe\Column &$column
101
     * @return void
102
     */
103 6
    protected function setForeignColumns($foreignTable, Column &$column)
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105 6
        while ($row = $foreignTable->fetch()) {
106 6
            foreach ($columns as $column) {
0 ignored issues
show
Bug introduced by
The variable $columns does not exist. Did you mean $column?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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
    }
116
117
    /**
118
     * Sets the properties of the specified column.
119
     *
120
     * @param  mixed                   $row
121
     * @param  \Rougin\Describe\Column &$column
122
     * @return void
123
     */
124 6
    protected function setProperties($row, Column &$column)
125
    {
126 6
        if (! $row->notnull) {
127 6
            $column->setNull(true);
128 6
        }
129
130 6
        if ($row->pk) {
131 6
            $column->setPrimary(true);
132 6
            $column->setAutoIncrement(true);
133 6
        }
134 6
    }
135
}
136