Repository navigation
Expand file tree
/
Copy pathPostgreSqlConnection.php
More file actions
141 lines (124 loc) · 3.73 KB
/
Copy pathPostgreSqlConnection.php
File metadata and controls
141 lines (124 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\PgSQL;
use DateTimeInterface;
use Exception;
use Hyperf\Database\Connection;
use Hyperf\Database\PgSQL\DBAL\PostgresPdoDriver;
use Hyperf\Database\PgSQL\Query\Grammars\PostgresGrammar as QueryGrammar;
use Hyperf\Database\PgSQL\Query\Processors\PostgresProcessor;
use Hyperf\Database\PgSQL\Schema\Grammars\PostgresGrammar as SchemaGrammar;
use Hyperf\Database\PgSQL\Schema\PostgresBuilder;
use Hyperf\Database\Query\Grammars\PostgresGrammar;
use PDO;
use PDOStatement;
class PostgreSqlConnection extends Connection
{
/**
* Get a schema builder instance for the connection.
*/
public function getSchemaBuilder(): PostgresBuilder
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new PostgresBuilder($this);
}
/**
* Bind values to their parameters in the given statement.
*/
public function bindValues(PDOStatement $statement, array $bindings): void
{
foreach ($bindings as $key => $value) {
$statement->bindValue(
is_string($key) ? $key : $key + 1,
$value
);
}
}
/**
* Prepare the query bindings for execution.
*
* Converts booleans to 'true'/'false' when emulated prepares are enabled,
* as PostgreSQL rejects integer literals for boolean columns.
*
* @see https://github.com/laravel/framework/issues/37261
*/
public function prepareBindings(array $bindings): array
{
if (! $this->isUsingEmulatedPrepares()) {
return parent::prepareBindings($bindings);
}
$grammar = $this->getQueryGrammar();
foreach ($bindings as $key => $value) {
if ($value instanceof DateTimeInterface) {
$bindings[$key] = $value->format($grammar->getDateFormat());
} elseif (is_bool($value)) {
$bindings[$key] = $value ? 'true' : 'false';
}
}
return $bindings;
}
/**
* Escape a boolean value for safe SQL embedding.
*/
protected function escapeBool(bool $value): string
{
return $value ? 'true' : 'false';
}
/**
* Determine if the connection is using emulated prepares.
*/
protected function isUsingEmulatedPrepares(): bool
{
return ($this->config['options'][PDO::ATTR_EMULATE_PREPARES] ?? false) === true;
}
/**
* Determine if the given database exception was caused by a unique constraint violation.
*
* @return bool
*/
protected function isUniqueConstraintError(Exception $exception)
{
return $exception->getCode() === '23505';
}
/**
* Get the default query grammar instance.
* @return PostgresGrammar
*/
protected function getDefaultQueryGrammar(): QueryGrammar
{
return $this->withTablePrefix(new QueryGrammar());
}
/**
* Get the default schema grammar instance.
*/
protected function getDefaultSchemaGrammar(): SchemaGrammar
{
return $this->withTablePrefix(new SchemaGrammar());
}
/**
* Get the default post processor instance.
*
* @return \Hyperf\Database\Query\Processors\PostgresProcessor
*/
protected function getDefaultPostProcessor(): PostgresProcessor
{
return new PostgresProcessor();
}
/**
* Get the Doctrine DBAL driver.
*/
protected function getDoctrineDriver(): PostgresPdoDriver
{
return new PostgresPdoDriver();
}
}