Completed
Pull Request — master (#5)
by Rougin
02:15
created

SQLiteDriver::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 24
    public function __construct(\PDO $pdo)
32
    {
33 24
        $this->pdo = $pdo;
34 24
    }
35
36
    /**
37
     * Returns a listing of columns from the specified table.
38
     *
39
     * @param  string $tableName
40
     * @return array
41
     * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException
42
     */
43 18
    public function getColumns($tableName)
44
    {
45 18
        return $this->getColumnsFromQuery($tableName, 'PRAGMA table_info("' . $tableName . '");');
46
    }
47
48
    /**
49
     * Returns a listing of columns from the specified table.
50
     * NOTE: To be removed in v2.0.0.
51
     *
52
     * @param  string $tableName
53
     * @return array
54
     * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException
55
     */
56 18
    public function getTable($tableName)
57
    {
58 18
        return $this->getColumns($tableName);
59
    }
60
61
    /**
62
     * Returns a listing of tables from the specified database.
63
     *
64
     * @return array
65
     */
66 6
    public function getTableNames()
67
    {
68 6
        return $this->showTables();
69
    }
70
71
    /**
72
     * Shows the list of tables.
73
     * NOTE: To be removed in v2.0.0.
74
     *
75
     * @return array
76
     */
77 6
    public function showTables()
78
    {
79 6
        $tables = [];
80
81 6
        $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";');
82
83 6
        $query->execute();
84 6
        $query->setFetchMode(\PDO::FETCH_OBJ);
85
86 6
        while ($row = $query->fetch()) {
87 6
            if ($row->name != 'sqlite_sequence') {
88 6
                array_push($tables, $row->name);
89 6
            }
90 6
        }
91
92 6
        return $tables;
93
    }
94
95
    /**
96
     * Prepares the defined columns.
97
     *
98
     * @param  string $tableName
99
     * @param  mixed  $row
100
     * @return \Rougin\Describe\Column
101
     */
102 15
    protected function setColumn($tableName, $row)
103
    {
104 15
        $column = new Column;
105
106 15
        $column->setDefaultValue($row->dflt_value);
107 15
        $column->setField($row->name);
108 15
        $column->setDataType(strtolower($row->type));
109
110 15
        $column = $this->setProperties($row, $column);
111 15
        $column = $this->setForeignColumn($tableName, $column);
112
113 15
        return $column;
114
    }
115
116
    /**
117
     * Sets the properties of the specified column if it does exists.
118
     *
119
     * @param  string                  $tableName
120
     * @param  \Rougin\Describe\Column $column
121
     * @return \Rougin\Describe\Column
122
     */
123 15
    protected function setForeignColumn($tableName, Column $column)
124
    {
125 15
        $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");');
126
127 15
        $query->execute();
128 15
        $query->setFetchMode(\PDO::FETCH_OBJ);
129
130 15
        while ($row = $query->fetch()) {
131 15
            if ($column->getField() == $row->from) {
132 15
                $column->setForeign(true);
133
134 15
                $column->setReferencedField($row->to);
135 15
                $column->setReferencedTable($row->table);
136 15
            }
137 15
        }
138
139 15
        return $column;
140
    }
141
142
    /**
143
     * Sets the properties of the specified column.
144
     *
145
     * @param  mixed                   $row
146
     * @param  \Rougin\Describe\Column $column
147
     * @return void
148
     */
149 15
    protected function setProperties($row, Column $column)
150
    {
151 15
        if (! $row->notnull) {
152 15
            $column->setNull(true);
153 15
        }
154
155 15
        if ($row->pk) {
156 15
            $column->setPrimary(true);
157 15
            $column->setAutoIncrement(true);
158 15
        }
159
160 15
        return $column;
161
    }
162
}
163