Skip to content

Commit

Permalink
修复批量执行SQL在PHP8下运行报“SQLSTATE[HY000]: General error”的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 12, 2020
1 parent 581303b commit 305ae47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Db/Drivers/PdoMysql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,18 @@ public function batchExec(string $sql): array
$queryResult = $this->query($sql);
$result = [];
do {
$result[] = $queryResult->fetchAll();
try {
$result[] = $queryResult->fetchAll();
} catch(\PDOException $pe) {
if('SQLSTATE[HY000]: General error' === $pe->getMessage())
{
$result[] = [];
}
else
{
throw $pe;
}
}
} while($queryResult->nextRowset());
return $result;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Component/Tests/Db/DbBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public function testExec()
public function testBatchExec()
{
$db = Db::getInstance($this->poolName);
$result = $db->batchExec('select 1 as a;select 2 as b;');
$result = $db->batchExec('select 1 as a;update tb_article set id = 1 where id = 1;select 2 as b;');
$this->assertEquals([
[['a' => 1]],
[],
[['b' => 2]],
], $result);
}
Expand Down

0 comments on commit 305ae47

Please sign in to comment.