Completed
Push — master ( 647ae2...5379df )
by Rougin
03:35
created

SQLiteDriver::reference()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
cc 3
eloc 11
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Column;
6
use Rougin\Describe\Table;
7
8
/**
9
 * SQLite Driver
10
 *
11
 * A database driver extension for SQLite.
12
 * NOTE: Should be renamed to "SqliteDriver" in v2.0.0.
13
 *
14
 * @package Describe
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class SQLiteDriver extends MySQLDriver
18
{
19
    /**
20
     * @var \PDO
21
     */
22
    protected $pdo;
23
24
    /**
25
     * Initializes the driver instance.
26
     *
27
     * @param \PDO $pdo
28
     */
29 63
    public function __construct(\PDO $pdo)
30
    {
31 63
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
32
33 63
        $this->pdo = $pdo;
34 63
    }
35
36
    /**
37
     * Returns an array of Column instances from a table.
38
     *
39
     * @param  string $table
40
     * @return \Rougin\Describe\Column[]
41
     */
42 42
    public function columns($table)
43
    {
44 42
        return $this->query($table, 'PRAGMA table_info("' . $table . '");');
45
    }
46
47
    /**
48
     * Returns an array of Column instances from a table.
49
     * NOTE: To be removed in v2.0.0. Use columns() instead.
50
     *
51
     * @param  string $table
52
     * @return \Rougin\Describe\Column[]
53
     */
54 9
    public function getColumns($table)
55
    {
56 9
        return $this->columns($table);
57
    }
58
59
    /**
60
     * Returns an array of Column instances from a table.
61
     * NOTE: To be removed in v2.0.0. Use getColumns() instead.
62
     *
63
     * @param  string $table
64
     * @return \Rougin\Describe\Column[]
65
     */
66 3
    public function getTable($table)
67
    {
68 3
        return $this->getColumns($table);
69
    }
70
71
    /**
72
     * Returns an array of table names.
73
     * NOTE: To be removed in v2.0.0. Use tables() instead.
74
     *
75
     * @return array
76
     */
77 6
    public function getTableNames()
78
    {
79 6
        return $this->items(false);
80
    }
81
82
    /**
83
     * Returns an array of table names.
84
     * NOTE: To be removed in v2.0.0. Use getTableNames() instead.
85
     *
86
     * @return array
87
     */
88 3
    public function showTables()
89
    {
90 3
        return $this->getTableNames();
91
    }
92
93
    /**
94
     * Returns an array of Table instances.
95
     *
96
     * @return \Rougin\Describe\Table[]
97
     */
98 12
    public function tables()
99
    {
100 12
        return $this->items(true);
101
    }
102
103
    /**
104
     * Prepares the defined columns.
105
     *
106
     * @param  \Rougin\Describe\Column $column
107
     * @param  string                  $table
108
     * @param  mixed                   $row
109
     * @return \Rougin\Describe\Column
110
     */
111 36
    protected function column(Column $column, $table, $row)
112
    {
113 36
        $column->setDefaultValue($row->dflt_value);
114
115 36
        $column->setField($row->name);
116
117 36
        $column->setDataType(strtolower($row->type));
118
119 36
        $column = $this->reference($table, $column);
120
121 36
        return $this->properties($row, $column);
122
    }
123
124
    /**
125
     * Returns an array of table names or Table instances.
126
     * NOTE: To be removed in v2.0.0. Move to tables() instead.
127
     *
128
     * @param  boolean $instance
129
     * @param  array   $tables
130
     * @return array|\Rougin\Describe\Table[]
131
     */
132 18
    protected function items($instance = false, $tables = array())
133
    {
134 18
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
135
136 18
        $result = $this->pdo->prepare($query);
137
138 18
        $result->execute();
139
140 18
        $result->setFetchMode(\PDO::FETCH_OBJ);
141
142 18
        while ($row = $result->fetch()) {
143 18
            $item = $name = $row->name;
144
145 18
            $row->name === 'sqlite_sequence' && $name = null;
146
147 18
            $instance && $item = new Table($name, $this);
148
149 18
            is_null($name) || $tables[] = $item;
150 18
        }
151
152 18
        return $tables;
153
    }
154
155
    /**
156
     * Sets the properties of a column.
157
     *
158
     * @param  mixed                   $row
159
     * @param  \Rougin\Describe\Column $column
160
     * @return void
161
     */
162 36
    protected function properties($row, Column $column)
163
    {
164 36
        $column->setNull(! $row->notnull);
165
166 36
        $row->pk && $column->setAutoIncrement(true);
167
168 36
        $row->pk && $column->setPrimary(true);
169
170 36
        return $column;
171
    }
172
173
    /**
174
     * Sets the properties of a column if it does exists.
175
     *
176
     * @param  string                  $table
177
     * @param  \Rougin\Describe\Column $column
178
     * @return \Rougin\Describe\Column
179
     */
180 36
    protected function reference($table, Column $column)
181
    {
182 36
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
183
184 36
        $result = $this->pdo->prepare($query);
185
186 36
        $result->execute();
187
188 36
        $result->setFetchMode(\PDO::FETCH_OBJ);
189
190 36
        while ($row = $result->fetch()) {
191 36
            if ($column->getField() === $row->from) {
192 36
                $column->setForeign(true);
193
194 36
                $column->setReferencedField($row->to);
195
196 36
                $column->setReferencedTable($row->table);
197 36
            }
198 36
        }
199
200 36
        return $column;
201
    }
202
}
203