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 \PDO |
20
|
|
|
*/ |
21
|
|
|
protected $pdo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param PDO $pdo |
25
|
|
|
*/ |
26
|
18 |
|
public function __construct(\PDO $pdo) |
27
|
|
|
{ |
28
|
18 |
|
$this->pdo = $pdo; |
29
|
18 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the result. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
12 |
|
public function getTable($table) |
37
|
|
|
{ |
38
|
12 |
|
$columns = []; |
39
|
|
|
|
40
|
12 |
|
$query = $this->pdo->prepare('PRAGMA table_info("' . $table . '");'); |
41
|
|
|
|
42
|
12 |
|
$query->execute(); |
43
|
12 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
44
|
|
|
|
45
|
12 |
|
while ($row = $query->fetch()) { |
46
|
12 |
|
$column = new Column; |
47
|
|
|
|
48
|
12 |
|
$this->setProperties($row, $column); |
49
|
|
|
|
50
|
12 |
|
$column->setDefaultValue($row->dflt_value); |
51
|
12 |
|
$column->setField($row->name); |
52
|
12 |
|
$column->setDataType(strtolower($row->type)); |
53
|
|
|
|
54
|
12 |
|
array_push($columns, $column); |
55
|
12 |
|
} |
56
|
|
|
|
57
|
12 |
|
$query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $table . '");'); |
58
|
|
|
|
59
|
12 |
|
$query->execute(); |
60
|
12 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
61
|
|
|
|
62
|
12 |
|
while ($row = $query->fetch()) { |
63
|
12 |
|
$this->setForeignColumn($columns, $row); |
64
|
12 |
|
} |
65
|
|
|
|
66
|
12 |
|
return $columns; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows the list of tables. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
6 |
|
public function showTables() |
75
|
|
|
{ |
76
|
6 |
|
$tables = []; |
77
|
|
|
|
78
|
6 |
|
$query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";'); |
79
|
|
|
|
80
|
6 |
|
$query->execute(); |
81
|
6 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
82
|
|
|
|
83
|
6 |
|
while ($row = $query->fetch()) { |
84
|
6 |
|
if ($row->name != 'sqlite_sequence') { |
85
|
6 |
|
array_push($tables, $row->name); |
86
|
6 |
|
} |
87
|
6 |
|
} |
88
|
|
|
|
89
|
6 |
|
return $tables; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Prepares the columns that have foreign keys. |
94
|
|
|
* |
95
|
|
|
* @param array &$columns |
96
|
|
|
* @param object $row |
97
|
|
|
*/ |
98
|
12 |
|
protected function setForeignColumn(array &$columns, $row) |
99
|
|
|
{ |
100
|
12 |
|
foreach ($columns as $column) { |
101
|
12 |
|
if ($column->getField() == $row->from) { |
102
|
12 |
|
$column->setForeign(true); |
103
|
|
|
|
104
|
12 |
|
$column->setReferencedField($row->to); |
105
|
12 |
|
$column->setReferencedTable($row->table); |
106
|
12 |
|
} |
107
|
12 |
|
} |
108
|
|
|
|
109
|
12 |
|
return $columns; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets the properties of the specified column. |
114
|
|
|
* |
115
|
|
|
* @param mixed $row |
116
|
|
|
* @param \Rougin\Describe\Column &$column |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
12 |
|
protected function setProperties($row, Column &$column) |
120
|
|
|
{ |
121
|
12 |
|
if (! $row->notnull) { |
122
|
12 |
|
$column->setNull(true); |
123
|
12 |
|
} |
124
|
|
|
|
125
|
12 |
|
if ($row->pk) { |
126
|
12 |
|
$column->setPrimary(true); |
127
|
12 |
|
$column->setAutoIncrement(true); |
128
|
12 |
|
} |
129
|
12 |
|
} |
130
|
|
|
} |
131
|
|
|
|