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 extends AbstractDriver 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 |
|
public function getTable($tableName) |
43
|
|
|
{ |
44
|
15 |
|
$this->columns = []; |
45
|
|
|
|
46
|
15 |
|
$this->getQuery($tableName, 'PRAGMA table_info("' . $tableName . '");'); |
47
|
|
|
|
48
|
15 |
|
return $this->columns; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Shows the list of tables. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
6 |
|
public function showTables() |
57
|
|
|
{ |
58
|
6 |
|
$tables = []; |
59
|
|
|
|
60
|
6 |
|
$query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";'); |
61
|
|
|
|
62
|
6 |
|
$query->execute(); |
63
|
6 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
64
|
|
|
|
65
|
6 |
|
while ($row = $query->fetch()) { |
66
|
6 |
|
if ($row->name != 'sqlite_sequence') { |
67
|
6 |
|
array_push($tables, $row->name); |
68
|
6 |
|
} |
69
|
6 |
|
} |
70
|
|
|
|
71
|
6 |
|
return $tables; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Prepares the defined columns. |
76
|
|
|
* |
77
|
|
|
* @param string $tableName |
78
|
|
|
* @param mixed $row |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
12 |
|
protected function setColumn($tableName, $row) |
82
|
|
|
{ |
83
|
12 |
|
$column = new Column; |
84
|
|
|
|
85
|
12 |
|
$this->setProperties($row, $column); |
86
|
|
|
|
87
|
12 |
|
$column->setDefaultValue($row->dflt_value); |
88
|
12 |
|
$column->setField($row->name); |
89
|
12 |
|
$column->setDataType(strtolower($row->type)); |
90
|
|
|
|
91
|
12 |
|
$this->setForeignColumn($tableName, $column); |
92
|
|
|
|
93
|
12 |
|
array_push($this->columns, $column); |
94
|
12 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the properties of the specified column if it does exists. |
98
|
|
|
* |
99
|
|
|
* @param string $tableName |
100
|
|
|
* @param \Rougin\Describe\Column &$column |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
12 |
|
protected function setForeignColumn($tableName, Column &$column) |
104
|
|
|
{ |
105
|
12 |
|
$query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");'); |
106
|
|
|
|
107
|
12 |
|
$query->execute(); |
108
|
12 |
|
$query->setFetchMode(\PDO::FETCH_OBJ); |
109
|
|
|
|
110
|
12 |
|
while ($row = $query->fetch()) { |
111
|
12 |
|
if ($column->getField() == $row->from) { |
112
|
12 |
|
$column->setForeign(true); |
113
|
|
|
|
114
|
12 |
|
$column->setReferencedField($row->to); |
115
|
12 |
|
$column->setReferencedTable($row->table); |
116
|
12 |
|
} |
117
|
12 |
|
} |
118
|
12 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets the properties of the specified column. |
122
|
|
|
* |
123
|
|
|
* @param mixed $row |
124
|
|
|
* @param \Rougin\Describe\Column &$column |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
12 |
|
protected function setProperties($row, Column &$column) |
128
|
|
|
{ |
129
|
12 |
|
if (! $row->notnull) { |
130
|
12 |
|
$column->setNull(true); |
131
|
12 |
|
} |
132
|
|
|
|
133
|
12 |
|
if ($row->pk) { |
134
|
12 |
|
$column->setPrimary(true); |
135
|
12 |
|
$column->setAutoIncrement(true); |
136
|
12 |
|
} |
137
|
12 |
|
} |
138
|
|
|
} |
139
|
|
|
|