From a3f47bc9020d0ea71cd0ddfe90ff013d083f6329 Mon Sep 17 00:00:00 2001 From: Mr Percival <49914839+Lawrence72@users.noreply.github.com> Date: Thu, 15 Jan 2026 20:28:34 -0600 Subject: [PATCH] Ignore LiMIT when using RETURNING --- .gitignore | 1 + flight/database/SimplePdo.php | 2 +- tests/SimplePdoTest.php | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 90cd18f..49ff583 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ clover.xml phpcs.xml .runway-config.json .runway-creds.json +.DS_Store diff --git a/flight/database/SimplePdo.php b/flight/database/SimplePdo.php index 6e5cb57..c1bc0b7 100644 --- a/flight/database/SimplePdo.php +++ b/flight/database/SimplePdo.php @@ -68,7 +68,7 @@ class SimplePdo extends PdoWrapper public function fetchRow(string $sql, array $params = []): ?Collection { // Smart LIMIT 1 addition (avoid if already present at end or complex query) - if (!preg_match('/\sLIMIT\s+\d+(?:\s+OFFSET\s+\d+)?\s*$/i', trim($sql))) { + if (!preg_match('/\s(LIMIT\s+\d+(?:\s+OFFSET\s+\d+)?|RETURNING\s+.+)\s*$/i', trim($sql))) { $sql .= ' LIMIT 1'; } diff --git a/tests/SimplePdoTest.php b/tests/SimplePdoTest.php index de10378..a8e85b2 100644 --- a/tests/SimplePdoTest.php +++ b/tests/SimplePdoTest.php @@ -153,6 +153,17 @@ class SimplePdoTest extends TestCase $this->assertInstanceOf(Collection::class, $row); $this->assertEquals(3, $row['id']); // Should be Bob (id=3) } + + public function testFetchRowDoesNotAddLimitAfterReturningClause(): void + { + $row = $this->db->fetchRow( + 'INSERT INTO users (name, email) VALUES (?, ?) RETURNING id, name', + ['Alice', 'alice@example.com'] + ); + + $this->assertInstanceOf(Collection::class, $row); + $this->assertSame('Alice', $row['name']); + } // ========================================================================= // fetchAll Tests