Skip to content

Commit 10c28cb

Browse files
authored
Optimized the code for pgsql.
1 parent 639f8a1 commit 10c28cb

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/PgSQL/PgSQLConnection.php

+21-17
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222

2323
class PgSQLConnection extends AbstractConnection
2424
{
25-
/**
26-
* @var PostgreSQL
27-
*/
28-
protected $connection;
25+
protected PostgreSQL $connection;
2926

3027
protected array $config = [
3128
'driver' => 'pgsql',
@@ -104,7 +101,7 @@ public function execute(string $query, array $bindings = []): int
104101

105102
public function exec(string $sql): int
106103
{
107-
return $this->execute($sql, []);
104+
return $this->execute($sql);
108105
}
109106

110107
public function query(string $query, array $bindings = []): array
@@ -128,24 +125,31 @@ public function fetch(string $query, array $bindings = [])
128125

129126
public function call(string $method, array $argument = [])
130127
{
131-
switch ($method) {
132-
case 'beginTransaction':
133-
return $this->connection->query('BEGIN');
134-
case 'rollBack':
135-
return $this->connection->query('ROLLBACK');
136-
case 'commit':
137-
return $this->connection->query('COMMIT');
138-
}
139-
140-
return $this->connection->{$method}(...$argument);
128+
return match ($method) {
129+
'beginTransaction' => $this->connection->query('BEGIN'),
130+
'rollBack' => $this->connection->query('ROLLBACK'),
131+
'commit' => $this->connection->query('COMMIT'),
132+
default => $this->connection->{$method}(...$argument),
133+
};
141134
}
142135

143136
public function run(Closure $closure)
144137
{
145138
return $closure->call($this, $this->connection);
146139
}
147140

148-
public function str_replace_once($needle, $replace, $haystack)
141+
/**
142+
* @param string $needle
143+
* @param string $replace
144+
* @param string $haystack
145+
* @deprecated ,using `strReplaceOnce` instead
146+
*/
147+
public function str_replace_once($needle, $replace, $haystack): array|string
148+
{
149+
return $this->strReplaceOnce($needle, $replace, $haystack);
150+
}
151+
152+
public function strReplaceOnce(string $needle, string $replace, string $haystack): array|string
149153
{
150154
// Looks for the first occurence of $needle in $haystack
151155
// and replaces it with $replace.
@@ -162,7 +166,7 @@ protected function prepare(string $query): PostgreSQLStatement
162166
{
163167
$num = 1;
164168
while (strpos($query, '?')) {
165-
$query = $this->str_replace_once('?', '$' . $num++, $query);
169+
$query = $this->strReplaceOnce('?', '$' . $num++, $query);
166170
}
167171

168172
$statement = $this->connection->prepare($query);

0 commit comments

Comments
 (0)