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 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 |
View Code Duplication |
public function getTable($tableName) |
|
|
|
|
43
|
|
|
{ |
44
|
15 |
|
$this->columns = []; |
45
|
|
|
|
46
|
|
|
try { |
47
|
15 |
|
$query = $this->pdo->prepare('PRAGMA table_info("' . $tableName . '");'); |
48
|
|
|
|
49
|
15 |
|
$query->execute(); |
50
|
15 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
51
|
15 |
|
} catch (\PDOException $e) { |
52
|
|
|
// Table not found |
53
|
|
|
} |
54
|
|
|
|
55
|
15 |
|
while ($row = $query->fetch()) { |
56
|
12 |
|
$this->setColumn($tableName, $row); |
57
|
12 |
|
} |
58
|
|
|
|
59
|
15 |
|
return $this->columns; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Prepares the defined columns. |
64
|
|
|
* |
65
|
|
|
* @param string $tableName |
66
|
|
|
* @param mixed $row |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
12 |
|
protected function setColumn($tableName, $row) |
70
|
|
|
{ |
71
|
12 |
|
$column = new Column; |
72
|
|
|
|
73
|
12 |
|
$this->setProperties($row, $column); |
74
|
|
|
|
75
|
12 |
|
$column->setDefaultValue($row->dflt_value); |
76
|
12 |
|
$column->setField($row->name); |
77
|
12 |
|
$column->setDataType(strtolower($row->type)); |
78
|
|
|
|
79
|
12 |
|
$this->setForeignColumn($tableName, $row, $column); |
80
|
|
|
|
81
|
12 |
|
array_push($this->columns, $column); |
82
|
12 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Shows the list of tables. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
6 |
|
public function showTables() |
90
|
|
|
{ |
91
|
6 |
|
$tables = []; |
92
|
|
|
|
93
|
6 |
|
$query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";'); |
94
|
|
|
|
95
|
6 |
|
$query->execute(); |
96
|
6 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
97
|
|
|
|
98
|
6 |
|
while ($row = $query->fetch()) { |
99
|
6 |
|
if ($row->name != 'sqlite_sequence') { |
100
|
6 |
|
array_push($tables, $row->name); |
101
|
6 |
|
} |
102
|
6 |
|
} |
103
|
|
|
|
104
|
6 |
|
return $tables; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the properties of the specified column if it does exists. |
109
|
|
|
* |
110
|
|
|
* @param string $tableName |
111
|
|
|
* @param mixed $row |
112
|
|
|
* @param \Rougin\Describe\Column &$column |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
12 |
|
protected function setForeignColumn($tableName, $row, Column &$column) |
|
|
|
|
116
|
|
|
{ |
117
|
12 |
|
$query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");'); |
118
|
|
|
|
119
|
12 |
|
$query->execute(); |
120
|
12 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
121
|
|
|
|
122
|
12 |
|
while ($row = $query->fetch()) { |
123
|
12 |
|
if ($column->getField() == $row->from) { |
124
|
12 |
|
$column->setForeign(true); |
125
|
|
|
|
126
|
12 |
|
$column->setReferencedField($row->to); |
127
|
12 |
|
$column->setReferencedTable($row->table); |
128
|
12 |
|
} |
129
|
12 |
|
} |
130
|
12 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Prepares the query for getting the foreign columns. |
134
|
|
|
* |
135
|
|
|
* @param array $columns |
136
|
|
|
* @param string $tableName |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function prepareForeignColumns(array $columns, $tableName) |
140
|
|
|
{ |
141
|
|
|
$query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");'); |
142
|
|
|
|
143
|
|
|
$query->execute(); |
144
|
|
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
145
|
|
|
|
146
|
|
|
while ($row = $query->fetch()) { |
147
|
|
|
$this->setForeignColumn($columns, $row); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $columns; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Sets the properties of the specified column. |
155
|
|
|
* |
156
|
|
|
* @param mixed $row |
157
|
|
|
* @param \Rougin\Describe\Column &$column |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
12 |
|
protected function setProperties($row, Column &$column) |
161
|
|
|
{ |
162
|
12 |
|
if (! $row->notnull) { |
163
|
12 |
|
$column->setNull(true); |
164
|
12 |
|
} |
165
|
|
|
|
166
|
12 |
|
if ($row->pk) { |
167
|
12 |
|
$column->setPrimary(true); |
168
|
12 |
|
$column->setAutoIncrement(true); |
169
|
12 |
|
} |
170
|
12 |
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.