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 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 |
|
$column->setNull(! $row->notnull); |
122
|
|
|
|
123
|
36 |
|
$row->pk && $column->setAutoIncrement(true); |
124
|
|
|
|
125
|
36 |
|
$row->pk && $column->setPrimary(true); |
126
|
|
|
|
127
|
36 |
|
return $column; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns an array of table names or Table instances. |
132
|
|
|
* NOTE: To be removed in v2.0.0. Move to tables() instead. |
133
|
|
|
* |
134
|
|
|
* @param boolean $instance |
135
|
|
|
* @param array $tables |
136
|
|
|
* @return array|\Rougin\Describe\Table[] |
137
|
|
|
*/ |
138
|
18 |
|
protected function items($instance = false, $tables = array()) |
139
|
|
|
{ |
140
|
18 |
|
$query = 'SELECT name FROM sqlite_master WHERE type = "table";'; |
141
|
|
|
|
142
|
18 |
|
$result = $this->pdo->prepare($query); |
143
|
|
|
|
144
|
18 |
|
$result->execute(); |
145
|
|
|
|
146
|
18 |
|
$result->setFetchMode(\PDO::FETCH_OBJ); |
147
|
|
|
|
148
|
18 |
|
while ($row = $result->fetch()) { |
149
|
18 |
|
if ($row->name !== 'sqlite_sequence') { |
150
|
18 |
|
$name = $row->name; |
151
|
|
|
|
152
|
18 |
|
$tables[] = new Table($name, $this); |
153
|
18 |
|
} |
154
|
18 |
|
} |
155
|
|
|
|
156
|
18 |
|
return $tables; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets the properties of a column if it does exists. |
161
|
|
|
* |
162
|
|
|
* @param string $table |
163
|
|
|
* @param \Rougin\Describe\Column $column |
164
|
|
|
* @return \Rougin\Describe\Column |
165
|
|
|
*/ |
166
|
36 |
|
protected function reference($table, Column $column) |
167
|
|
|
{ |
168
|
36 |
|
$query = 'PRAGMA foreign_key_list("' . $table . '");'; |
169
|
|
|
|
170
|
36 |
|
$result = $this->pdo->prepare($query); |
171
|
|
|
|
172
|
36 |
|
$result->execute(); |
173
|
|
|
|
174
|
36 |
|
$result->setFetchMode(\PDO::FETCH_OBJ); |
175
|
|
|
|
176
|
36 |
|
while ($row = $result->fetch()) { |
177
|
36 |
|
if ($column->getField() === $row->from) { |
178
|
36 |
|
$column->setReferencedTable($row->table); |
179
|
|
|
|
180
|
36 |
|
$column->setForeign(true); |
181
|
|
|
|
182
|
36 |
|
$column->setReferencedField($row->to); |
183
|
36 |
|
} |
184
|
36 |
|
} |
185
|
|
|
|
186
|
36 |
|
return $column; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|