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