|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nkey\Caribu\Type; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Concrete sqlite implementation of database type |
|
6
|
|
|
* |
|
7
|
|
|
* This class is part of Caribu package |
|
8
|
|
|
* |
|
9
|
|
|
* @author Maik Greubel <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Sqlite extends AbstractType |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* (non-PHPdoc) |
|
16
|
|
|
* |
|
17
|
|
|
* @see \Nkey\Caribu\Type\IType::getDsn() |
|
18
|
|
|
*/ |
|
19
|
23 |
|
public function getDsn(): string |
|
20
|
|
|
{ |
|
21
|
23 |
|
return "sqlite:{file}"; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* (non-PHPdoc) |
|
26
|
|
|
* |
|
27
|
|
|
* @see \Nkey\Caribu\Type\IType::getPrimaryKeyColumn() |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function getPrimaryKeyColumn(string $table, \Nkey\Caribu\Orm\Orm $orm): string |
|
30
|
|
|
{ |
|
31
|
3 |
|
$query = "PRAGMA TABLE_INFO({table})"; |
|
32
|
|
|
|
|
33
|
3 |
|
$sql = $this->interp($query, array( |
|
34
|
3 |
|
'table' => $table |
|
35
|
|
|
)); |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
3 |
|
$stmt = $orm->getConnection()->query($sql); |
|
39
|
3 |
|
$stmt->setFetchMode(\PDO::FETCH_ASSOC); |
|
40
|
3 |
|
while ($result = $stmt->fetch()) { |
|
41
|
2 |
|
$name = ''; |
|
42
|
2 |
|
foreach ($result as $identifier => $value) { |
|
43
|
2 |
|
if ($identifier == 'name') { |
|
44
|
2 |
|
$name = $value; |
|
45
|
|
|
} |
|
46
|
2 |
|
if ($identifier == 'pk' && $name) { |
|
47
|
2 |
|
return $name; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
1 |
|
$stmt->closeCursor(); |
|
52
|
|
|
} catch (\PDOException $exception) { |
|
53
|
|
|
throw \Nkey\Caribu\Orm\OrmException::fromPrevious($exception); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return ""; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* (non-PHPdoc) |
|
61
|
|
|
* |
|
62
|
|
|
* @see \Nkey\Caribu\Type\IType::getDefaultPort() |
|
63
|
|
|
*/ |
|
64
|
23 |
|
public function getDefaultPort(): int |
|
65
|
|
|
{ |
|
66
|
23 |
|
return - 1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* (non-PHPdoc) |
|
71
|
|
|
* |
|
72
|
|
|
* @see \Nkey\Caribu\Type\IType::lock() |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function lock(string $table, int $lockType, \Nkey\Caribu\Orm\Orm $orm) |
|
75
|
|
|
{ |
|
76
|
6 |
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* (non-PHPdoc) |
|
81
|
|
|
* |
|
82
|
|
|
* @see \Nkey\Caribu\Type\IType::unlock() |
|
83
|
|
|
*/ |
|
84
|
7 |
|
public function unlock(string $table, \Nkey\Caribu\Orm\Orm $orm) |
|
85
|
|
|
{ |
|
86
|
7 |
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* (non-PHPdoc) |
|
91
|
|
|
* |
|
92
|
|
|
* @see \Nkey\Caribu\Type\IType::getEscapeSign() |
|
93
|
|
|
*/ |
|
94
|
22 |
|
public function getEscapeSign(): string |
|
95
|
|
|
{ |
|
96
|
22 |
|
return ""; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* (non-PHPdoc) |
|
101
|
|
|
* |
|
102
|
|
|
* @see \Nkey\Caribu\Type\AbstractType::getTypeQuery() |
|
103
|
|
|
*/ |
|
104
|
5 |
|
protected function getTypeQuery(): string |
|
105
|
|
|
{ |
|
106
|
5 |
|
$query = "PRAGMA TABLE_INFO({table})"; |
|
107
|
|
|
|
|
108
|
5 |
|
return $query; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* (non-PHPdoc) |
|
113
|
|
|
* |
|
114
|
|
|
* @see \Nkey\Caribu\Type\AbstractType::mapType() |
|
115
|
|
|
*/ |
|
116
|
5 |
|
protected function mapType(array $result): int |
|
117
|
|
|
{ |
|
118
|
5 |
|
switch ($result['type']) { |
|
119
|
5 |
|
case 'INTEGER': |
|
120
|
3 |
|
return \Nkey\Caribu\Orm\OrmDataType::INTEGER; |
|
121
|
|
|
|
|
122
|
5 |
|
case 'REAL': |
|
123
|
|
|
return \Nkey\Caribu\Orm\OrmDataType::DECIMAL; |
|
124
|
|
|
|
|
125
|
5 |
|
case 'BLOB': |
|
126
|
|
|
return \Nkey\Caribu\Orm\OrmDataType::BLOB; |
|
127
|
|
|
|
|
128
|
5 |
|
case 'TEXT': |
|
129
|
|
|
default: |
|
130
|
5 |
|
return \Nkey\Caribu\Orm\OrmDataType::STRING; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* (non-PHPdoc) |
|
136
|
|
|
* |
|
137
|
|
|
* @see \Nkey\Caribu\Type\IType::getColumnType() |
|
138
|
|
|
*/ |
|
139
|
5 |
|
public function getColumnType(string $table, string $columnName, \Nkey\Caribu\Orm\Orm $orm): int |
|
140
|
|
|
{ |
|
141
|
5 |
|
$type = null; |
|
142
|
|
|
|
|
143
|
|
|
try { |
|
144
|
5 |
|
$stmt = $orm->getConnection()->query($this->interp($this->getTypeQuery(), array( |
|
145
|
5 |
|
'table' => $table |
|
146
|
|
|
))); |
|
147
|
5 |
|
$stmt->setFetchMode(\PDO::FETCH_ASSOC); |
|
148
|
5 |
|
while ($result = $stmt->fetch()) { |
|
149
|
5 |
|
if ($result['name'] == $columnName) { |
|
150
|
5 |
|
$type = $this->mapType($result); |
|
151
|
5 |
|
break; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
5 |
|
$stmt->closeCursor(); |
|
155
|
|
|
} catch (\PDOException $exception) { |
|
156
|
|
|
throw \Nkey\Caribu\Orm\OrmException::fromPrevious($exception); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
5 |
|
return $type; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* (non-PHPdoc) |
|
164
|
|
|
* |
|
165
|
|
|
* @see \Nkey\Caribu\Type\IType::getSequenceNameForColumn() |
|
166
|
|
|
*/ |
|
167
|
4 |
|
public function getSequenceNameForColumn(string $table, string $columnName, \Nkey\Caribu\Orm\Orm $orm): string |
|
168
|
|
|
{ |
|
169
|
4 |
|
return ""; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|