Skip to content

Commit 8ffd6b0

Browse files
authored
Add error checking for PostgreSQLStatement::execute method. (#5121)
1 parent 91c7e3a commit 8ffd6b0

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/PostgreSqlSwooleExtConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function select(string $query, array $bindings = [], bool $useReadPdo = t
106106

107107
$result = $statement->execute($this->prepareBindings($bindings));
108108

109-
if ($result === false) {
109+
if ($result === false || ! empty($this->pdo->error)) {
110110
throw new QueryException($query, [], new \Exception($this->pdo->error));
111111
}
112112

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Database\PgSQL\Cases;
13+
14+
use Hyperf\Database\Connection;
15+
use Hyperf\Database\Connectors\ConnectionFactory;
16+
use Hyperf\Database\Exception\QueryException;
17+
use Hyperf\Database\PgSQL\Connectors\PostgresSqlSwooleExtConnector;
18+
use Hyperf\Database\PgSQL\PostgreSqlSwooleExtConnection;
19+
use Hyperf\Database\Query\Builder;
20+
use PHPUnit\Framework\TestCase;
21+
use Psr\Container\ContainerInterface;
22+
23+
/**
24+
* @internal
25+
* @coversNothing
26+
*/
27+
class PostgreSqlSwooleExtConnectionTest extends TestCase
28+
{
29+
protected ConnectionFactory $connectionFactory;
30+
31+
public function setUp(): void
32+
{
33+
$container = \Mockery::mock(ContainerInterface::class);
34+
$container->shouldReceive('has')->andReturn(true);
35+
$container->shouldReceive('get')->with('db.connector.pgsql-swoole')->andReturn(new PostgresSqlSwooleExtConnector());
36+
37+
$this->connectionFactory = new ConnectionFactory($container);
38+
39+
Connection::resolverFor('pgsql-swoole', static function ($connection, $database, $prefix, $config) {
40+
return new PostgreSqlSwooleExtConnection($connection, $database, $prefix, $config);
41+
});
42+
}
43+
44+
public function testSelectMethodDuplicateKeyValueException()
45+
{
46+
if (SWOOLE_MAJOR_VERSION < 5) {
47+
$this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0');
48+
}
49+
50+
$connection = $this->connectionFactory->make([
51+
'driver' => 'pgsql-swoole',
52+
'host' => '127.0.0.1',
53+
'port' => 5432,
54+
'database' => 'postgres',
55+
'username' => 'postgres',
56+
'password' => 'postgres',
57+
]);
58+
59+
$builder = new Builder($connection);
60+
61+
$this->expectException(QueryException::class);
62+
$this->expectExceptionMessage('ERROR: duplicate key value violates unique constraint "users_email"');
63+
64+
$id = $builder->from('users')->insertGetId(['email' => '[email protected]', 'name' => 'hyperf'], 'id');
65+
$id2 = $builder->from('users')->insertGetId(['email' => '[email protected]', 'name' => 'hyperf'], 'id');
66+
67+
// Never here
68+
$this->assertIsNumeric($id);
69+
$this->assertIsNumeric($id2);
70+
}
71+
}

0 commit comments

Comments
 (0)