1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Umbrellio\Postgres; |
6
|
|
|
|
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Illuminate\Database\PostgresConnection as BasePostgresConnection; |
10
|
|
|
use Illuminate\Support\Traits\Macroable; |
11
|
|
|
use PDO; |
12
|
|
|
use Umbrellio\Postgres\Extensions\AbstractExtension; |
13
|
|
|
use Umbrellio\Postgres\Extensions\Exceptions\ExtensionInvalidException; |
14
|
|
|
use Umbrellio\Postgres\Schema\Builder; |
15
|
|
|
use Umbrellio\Postgres\Schema\Grammars\PostgresGrammar; |
16
|
|
|
use Umbrellio\Postgres\Schema\Types\NumericType; |
17
|
|
|
use Umbrellio\Postgres\Schema\Types\TsRangeType; |
18
|
|
|
use Umbrellio\Postgres\Schema\Types\TsTzRangeType; |
19
|
|
|
|
20
|
|
|
class PostgresConnection extends BasePostgresConnection |
21
|
|
|
{ |
22
|
|
|
use Macroable; |
23
|
|
|
|
24
|
|
|
public $name; |
25
|
|
|
|
26
|
|
|
private static $extensions = []; |
27
|
|
|
|
28
|
|
|
private $initialTypes = [ |
29
|
|
|
TsRangeType::TYPE_NAME => TsRangeType::class, |
30
|
|
|
TsTzRangeType::TYPE_NAME => TsTzRangeType::class, |
31
|
|
|
NumericType::TYPE_NAME => NumericType::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param AbstractExtension|string $extension |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
final public static function registerExtension(string $extension): void |
39
|
|
|
{ |
40
|
|
|
if (! is_subclass_of($extension, AbstractExtension::class)) { |
41
|
|
|
throw new ExtensionInvalidException(sprintf( |
42
|
|
|
'Class %s must be implemented from %s', |
43
|
|
|
$extension, |
44
|
|
|
AbstractExtension::class |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
self::$extensions[$extension::getName()] = $extension; |
48
|
|
|
} |
49
|
|
|
|
50
|
27 |
|
public function getSchemaBuilder() |
51
|
|
|
{ |
52
|
27 |
|
if ($this->schemaGrammar === null) { |
53
|
27 |
|
$this->useDefaultSchemaGrammar(); |
54
|
|
|
} |
55
|
27 |
|
return new Builder($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
29 |
|
public function useDefaultPostProcessor(): void |
59
|
|
|
{ |
60
|
29 |
|
parent::useDefaultPostProcessor(); |
61
|
|
|
|
62
|
29 |
|
$this->registerExtensions(); |
63
|
29 |
|
$this->registerInitialTypes(); |
64
|
|
|
} |
65
|
|
|
|
66
|
27 |
|
public function bindValues($statement, $bindings) |
67
|
|
|
{ |
68
|
27 |
|
if ($this->getPdo()->getAttribute(PDO::ATTR_EMULATE_PREPARES)) { |
69
|
8 |
|
foreach ($bindings as $key => $value) { |
70
|
8 |
|
$parameter = is_string($key) ? $key : $key + 1; |
71
|
|
|
|
72
|
|
|
switch (true) { |
73
|
8 |
|
case is_bool($value): |
74
|
2 |
|
$dataType = PDO::PARAM_BOOL; |
75
|
2 |
|
break; |
76
|
|
|
|
77
|
6 |
|
case $value === null: |
78
|
1 |
|
$dataType = PDO::PARAM_NULL; |
79
|
1 |
|
break; |
80
|
|
|
|
81
|
|
|
default: |
82
|
5 |
|
$dataType = PDO::PARAM_STR; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
$statement->bindValue($parameter, $value, $dataType); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
19 |
|
parent::bindValues($statement, $bindings); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
27 |
|
public function prepareBindings(array $bindings) |
93
|
|
|
{ |
94
|
27 |
|
if ($this->getPdo()->getAttribute(PDO::ATTR_EMULATE_PREPARES)) { |
95
|
8 |
|
$grammar = $this->getQueryGrammar(); |
96
|
|
|
|
97
|
8 |
|
foreach ($bindings as $key => $value) { |
98
|
8 |
|
if ($value instanceof DateTimeInterface) { |
99
|
1 |
|
$bindings[$key] = $value->format($grammar->getDateFormat()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
return $bindings; |
104
|
|
|
} |
105
|
|
|
|
106
|
19 |
|
return parent::prepareBindings($bindings); |
107
|
|
|
} |
108
|
|
|
|
109
|
27 |
|
protected function getDefaultSchemaGrammar() |
110
|
|
|
{ |
111
|
27 |
|
return $this->withTablePrefix(new PostgresGrammar()); |
112
|
|
|
} |
113
|
|
|
|
114
|
29 |
|
private function registerInitialTypes(): void |
115
|
|
|
{ |
116
|
29 |
|
foreach ($this->initialTypes as $type => $typeClass) { |
117
|
29 |
|
if (! Type::hasType($type)) { |
118
|
1 |
|
Type::addType($type, $typeClass); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @codeCoverageIgnore |
125
|
|
|
*/ |
126
|
|
|
private function registerExtensions(): void |
127
|
|
|
{ |
128
|
|
|
collect(self::$extensions)->each(function ($extension) { |
|
|
|
|
129
|
|
|
/** @var AbstractExtension $extension */ |
130
|
|
|
$extension::register(); |
131
|
|
|
foreach ($extension::getTypes() as $type => $typeClass) { |
132
|
|
|
if (! Type::hasType($type)) { |
133
|
|
|
Type::addType($type, $typeClass); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|