Skip to content

Commit 9e51de6

Browse files
Fixed bug that PGSqlSwooleConnection::affectingStatement() can't work when the sql is wrong. (#5221)
* Return value must be of type int, bool returned `TypeError: Hyperf\Database\PgSQL\PostgreSqlSwooleExtConnection::affectingStatement(): Return value must be of type int, bool returned` The hyperf database method has type hint 'int' but Swoole\Coroutine\PostgreSQLStatement::affectedRows() can return bool https://github.com/swoole/swoole-src/blob/master/ext-src/swoole_postgresql_coro.cc#L1242 * Initial fix didn't solve the exception Seems maybe the $this->run() method might also return bool which mismatches the function return type hint Co-authored-by: 李铭昕 <[email protected]>
1 parent 6906bc3 commit 9e51de6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/PostgreSqlSwooleExtConnection.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ public function affectingStatement(string $query, array $bindings = []): int
8282
$statement = $this->prepare($query);
8383
$this->recordsHaveBeenModified();
8484

85-
$result = $statement->execute($this->prepareBindings($bindings));
85+
$statement->execute($this->prepareBindings($bindings));
86+
87+
$count = $statement->affectedRows();
88+
if ($count === false) {
89+
throw new QueryException($query, $bindings, new Exception($statement->error, $statement->errCode));
90+
}
8691

87-
$this->recordsHaveBeenModified(
88-
($count = $statement->affectedRows($result)) > 0
89-
);
92+
$this->recordsHaveBeenModified($count > 0);
9093

9194
return $count;
9295
});

tests/Cases/PostgreSqlSwooleExtConnectionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,24 @@ public function testSelectMethodDuplicateKeyValueException()
6969
$this->assertIsNumeric($id);
7070
$this->assertIsNumeric($id2);
7171
}
72+
73+
public function testAffectingStatementWithWrongSql()
74+
{
75+
if (SWOOLE_MAJOR_VERSION < 5) {
76+
$this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0');
77+
}
78+
79+
$connection = $this->connectionFactory->make([
80+
'driver' => 'pgsql-swoole',
81+
'host' => '127.0.0.1',
82+
'port' => 5432,
83+
'database' => 'postgres',
84+
'username' => 'postgres',
85+
'password' => 'postgres',
86+
]);
87+
88+
$this->expectException(QueryException::class);
89+
90+
$connection->affectingStatement('UPDATE xx SET x = 1 WHERE id = 1');
91+
}
7292
}

0 commit comments

Comments
 (0)