1
|
|
|
<?php |
2
|
|
|
namespace Nkey\Caribu\Type; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Concrete postgresql implementation of database type |
6
|
|
|
* |
7
|
|
|
* This class is part of Caribu package |
8
|
|
|
* |
9
|
|
|
* @author Maik Greubel <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Postgres extends AbstractType |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* (non-PHPdoc) |
16
|
|
|
* |
17
|
|
|
* @see \Nkey\Caribu\Type\IType::getDsn() |
18
|
|
|
*/ |
19
|
3 |
|
public function getDsn(): string |
20
|
|
|
{ |
21
|
|
|
// From the docs: |
22
|
|
|
// return "pgsql:host={host};port={port};dbname={schema};user={user};password={password}"; |
23
|
3 |
|
return "pgsql:host={host};port={port};dbname={schema}"; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* (non-PHPdoc) |
28
|
|
|
* |
29
|
|
|
* @see \Nkey\Caribu\Type\IType::getDefaultPort() |
30
|
|
|
*/ |
31
|
3 |
|
public function getDefaultPort(): int |
32
|
|
|
{ |
33
|
3 |
|
return 5432; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* (non-PHPdoc) |
38
|
|
|
* |
39
|
|
|
* @see \Nkey\Caribu\Type\IType::getPrimaryKeyColumn() |
40
|
|
|
*/ |
41
|
1 |
|
public function getPrimaryKeyColumn(string $table, \Nkey\Caribu\Orm\Orm $orm): string |
42
|
|
|
{ |
43
|
1 |
|
$query = "select ccu.column_name as column_name from information_schema.constraint_column_usage ccu " . "inner join information_schema.table_constraints tc on ccu.constraint_name = tc.constraint_name " . "where tc.table_catalog = '{schema}' AND tc.table_name = '{table}'"; |
44
|
|
|
|
45
|
1 |
|
return $this->getPrimaryColumnViaSql($orm, $table, $query); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* (non-PHPdoc) |
50
|
|
|
* |
51
|
|
|
* @see \Nkey\Caribu\Type\IType::lock() |
52
|
|
|
*/ |
53
|
3 |
|
public function lock(string $table, int $lockType, \Nkey\Caribu\Orm\Orm $orm) |
54
|
|
|
{ |
55
|
3 |
|
$mode = "ACCESS SHARE"; |
56
|
3 |
|
if ($lockType == IType::LOCK_TYPE_WRITE) { |
57
|
3 |
|
$mode = "ROW EXCLUSIVE"; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
$lockStatement = sprintf("LOCK TABLE %s%s%s IN %s MODE NOWAIT", $this->getEscapeSign(), $table, $this->getEscapeSign(), $mode); |
61
|
|
|
|
62
|
3 |
|
$this->changeLockViaSql($orm, $table, $lockStatement); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* (non-PHPdoc) |
67
|
|
|
* |
68
|
|
|
* @see \Nkey\Caribu\Type\IType::unlock() |
69
|
|
|
*/ |
70
|
3 |
|
public function unlock(string $table, \Nkey\Caribu\Orm\Orm $orm) |
71
|
|
|
{ |
72
|
|
|
// No unlock command; locks are released upon transaction end via commit or rollback |
73
|
3 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* (non-PHPdoc) |
77
|
|
|
* |
78
|
|
|
* @see \Nkey\Caribu\Type\IType::getEscapeSign() |
79
|
|
|
*/ |
80
|
3 |
|
public function getEscapeSign(): string |
81
|
|
|
{ |
82
|
3 |
|
return '"'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* (non-PHPdoc) |
87
|
|
|
* |
88
|
|
|
* @see \Nkey\Caribu\Type\AbstractType::getTypeQuery() |
89
|
|
|
*/ |
90
|
2 |
|
protected function getTypeQuery(): string |
91
|
|
|
{ |
92
|
2 |
|
$query = "select data_type from information_schema.columns where table_catalog = '{schema}' " . "and table_name = '{table}' and column_name = '{column}'"; |
93
|
|
|
|
94
|
2 |
|
return $query; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* (non-PHPdoc) |
99
|
|
|
* |
100
|
|
|
* @see \Nkey\Caribu\Type\AbstractType::mapType() |
101
|
|
|
*/ |
102
|
2 |
|
protected function mapType(array $result): int |
103
|
|
|
{ |
104
|
2 |
|
switch (strtoupper($result['data_type'])) { |
105
|
2 |
|
case 'SMALLINT': |
106
|
2 |
|
case 'INTEGER': |
107
|
2 |
|
case 'BIGINT': |
108
|
1 |
|
return \Nkey\Caribu\Orm\OrmDataType::INTEGER; |
109
|
|
|
|
110
|
2 |
|
case 'NUMERIC': |
111
|
2 |
|
case 'MONEY': |
112
|
|
|
return \Nkey\Caribu\Orm\OrmDataType::DECIMAL; |
113
|
|
|
|
114
|
2 |
|
case 'CHARACTER VARYING': |
115
|
2 |
|
case 'CHARACTER': |
116
|
2 |
|
case 'TEXT': |
117
|
1 |
|
case 'TIME': |
118
|
2 |
|
return \Nkey\Caribu\Orm\OrmDataType::STRING; |
119
|
|
|
|
120
|
1 |
|
case 'BYTEA': |
121
|
|
|
return \Nkey\Caribu\Orm\OrmDataType::BLOB; |
122
|
|
|
|
123
|
1 |
|
case 'TIMESTAMP': |
124
|
1 |
|
case 'DATE': |
125
|
1 |
|
case 'TIMESTAMP WITHOUT TIME ZONE': |
126
|
1 |
|
return \Nkey\Caribu\Orm\OrmDataType::DATETIME; |
127
|
|
|
|
128
|
|
|
default: |
129
|
|
|
return \Nkey\Caribu\Orm\OrmDataType::STRING; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* (non-PHPdoc) |
135
|
|
|
* |
136
|
|
|
* @see \Nkey\Caribu\Type\IType::getSequenceNameForColumn() |
137
|
|
|
*/ |
138
|
2 |
|
public function getSequenceNameForColumn(string $table, string $columnName, \Nkey\Caribu\Orm\Orm $orm): string |
139
|
|
|
{ |
140
|
2 |
|
$sequenceName = null; |
141
|
|
|
|
142
|
2 |
|
$query = "select column_default from information_schema.columns where table_catalog = '{schema}' " . "AND table_name = '{table}' and column_name = '{column}'"; |
143
|
|
|
|
144
|
2 |
|
$sql = $this->interp($query, array( |
145
|
2 |
|
'schema' => $orm->getSchema(), |
146
|
2 |
|
'table' => $table, |
147
|
2 |
|
'column' => $columnName |
148
|
|
|
)); |
149
|
|
|
|
150
|
2 |
|
$stmt = null; |
151
|
|
|
|
152
|
|
|
try { |
153
|
2 |
|
$stmt = $orm->getConnection()->query($sql); |
154
|
2 |
|
$stmt->setFetchMode(\PDO::FETCH_ASSOC); |
155
|
|
|
|
156
|
2 |
|
$result = $stmt->fetch(); |
157
|
|
|
|
158
|
2 |
|
$this->handleNoColumn($table, $columnName, $orm, $stmt, $result); |
159
|
|
|
|
160
|
2 |
|
$matches = array(); |
161
|
2 |
|
if (preg_match("/nextval\('([^']+)'.*\)/", $result['column_default'], $matches)) { |
162
|
2 |
|
$sequenceName = $matches[1]; |
163
|
|
|
} |
164
|
2 |
|
$stmt->closeCursor(); |
165
|
|
|
} catch (\PDOException $exception) { |
166
|
|
|
if ($stmt) { |
167
|
|
|
$stmt->closeCursor(); |
168
|
|
|
} |
169
|
|
|
throw \Nkey\Caribu\Orm\OrmException::fromPrevious($exception); |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
return $sequenceName; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|