Completed
Pull Request — master (#4)
by Rougin
03:41
created

SQLiteDriver::setColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
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 extends AbstractDriver implements DriverInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $columns = [];
22
23
    /**
24
     * @var \PDO
25
     */
26
    protected $pdo;
27
28
    /**
29
     * @param PDO $pdo
30
     */
31 21
    public function __construct(\PDO $pdo)
32
    {
33 21
        $this->pdo = $pdo;
34 21
    }
35
36
    /**
37
     * Returns the result.
38
     *
39
     * @param  string $tableName
40
     * @return array
41
     */
42 15
    public function getTable($tableName)
43
    {
44 15
        $this->columns = [];
45
46 15
        $this->getQuery($tableName, 'PRAGMA table_info("' . $tableName . '");');
47
48 15
        return $this->columns;
49
    }
50
51
    /**
52
     * Prepares the defined columns.
53
     *
54
     * @param  string $tableName
55
     * @param  mixed  $row
56
     * @return void
57
     */
58 12
    protected function setColumn($tableName, $row)
59
    {
60 12
        $column = new Column;
61
62 12
        $this->setProperties($row, $column);
63
64 12
        $column->setDefaultValue($row->dflt_value);
65 12
        $column->setField($row->name);
66 12
        $column->setDataType(strtolower($row->type));
67
68 12
        $this->setForeignColumn($tableName, $row, $column);
69
70 12
        array_push($this->columns, $column);
71 12
    }
72
73
    /**
74
     * Shows the list of tables.
75
     *
76
     * @return array
77
     */
78 6
    public function showTables()
79
    {
80 6
        $tables = [];
81
82 6
        $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";');
83
84 6
        $query->execute();
85 6
        $query->setFetchMode(\PDO::FETCH_OBJ);
86
87 6
        while ($row = $query->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 if it does exists.
98
     *
99
     * @param  string                  $tableName
100
     * @param  mixed                   $row
101
     * @param  \Rougin\Describe\Column &$column
102
     * @return void
103
     */
104 12
    protected function setForeignColumn($tableName, $row, Column &$column)
0 ignored issues
show
Unused Code introduced by
The parameter $row 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
        $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");');
107
108 12
        $query->execute();
109 12
        $query->setFetchMode(\PDO::FETCH_OBJ);
110
111 12
        while ($row = $query->fetch()) {
112 12
            if ($column->getField() == $row->from) {
113 12
                $column->setForeign(true);
114
115 12
                $column->setReferencedField($row->to);
116 12
                $column->setReferencedTable($row->table);
117 12
            }
118 12
        }
119 12
    }
120
121
    /**
122
     * Sets the properties of the specified column.
123
     *
124
     * @param  mixed                   $row
125
     * @param  \Rougin\Describe\Column &$column
126
     * @return void
127
     */
128 12
    protected function setProperties($row, Column &$column)
129
    {
130 12
        if (! $row->notnull) {
131 12
            $column->setNull(true);
132 12
        }
133
134 12
        if ($row->pk) {
135 12
            $column->setPrimary(true);
136 12
            $column->setAutoIncrement(true);
137 12
        }
138 12
    }
139
}
140