Completed
Pull Request — master (#1)
by Rougin
06:18
created

SQLiteDriver::setForeignColumns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.2
c 1
b 0
f 0
cc 4
eloc 7
nc 4
nop 3
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
        // 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
60 12
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
61 12
        $foreignTable = $this->pdo->prepare($query);
62
63 12
        $foreignTable->execute();
64 12
        $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
65
66 12
        $this->setForeignColumns($foreignTable, $columns, $column);
67
68 12
        return $columns;
69
    }
70
71
    /**
72
     * Shows the list of tables.
73
     *
74
     * @return array
75
     */
76 6
    public function showTables()
77
    {
78 6
        $tables = [];
79
80
        // Gets list of columns
81 6
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
82 6
        $information = $this->pdo->prepare($query);
83
84 6
        $information->execute();
85 6
        $information->setFetchMode(\PDO::FETCH_OBJ);
86
87 6
        while ($row = $information->fetch()) {
88 6
            if ($row->name != 'sqlite_sequence') {
89 6
                array_push($tables, $row->name);
90 6
            }
91 6
        }
92
93 6
        return $tables;
94
    }
95
96
    /**
97
     * Sets the properties of the specified column.
98
     *
99
     * @param  \PDOStatement           $foreignTable
100
     * @param  array                   $columns
101
     * @param  \Rougin\Describe\Column &$column
102
     * @return void
103
     */
104 12
    protected function setForeignColumns($foreignTable, array $columns, 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...
105
    {
106 12
        while ($row = $foreignTable->fetch()) {
107 12
            foreach ($columns as $column) {
108 12
                if ($column->getField() == $row->from) {
109 12
                    $column->setForeign(true);
110
111 12
                    $column->setReferencedField($row->to);
112 12
                    $column->setReferencedTable($row->table);
113 12
                }
114 12
            }
115 12
        }
116 12
    }
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