Ignore LiMIT when using RETURNING

pull/679/head
Mr Percival 4 days ago
parent 88d7032928
commit a3f47bc902

1
.gitignore vendored

@ -10,3 +10,4 @@ clover.xml
phpcs.xml phpcs.xml
.runway-config.json .runway-config.json
.runway-creds.json .runway-creds.json
.DS_Store

@ -68,7 +68,7 @@ class SimplePdo extends PdoWrapper
public function fetchRow(string $sql, array $params = []): ?Collection public function fetchRow(string $sql, array $params = []): ?Collection
{ {
// Smart LIMIT 1 addition (avoid if already present at end or complex query) // 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'; $sql .= ' LIMIT 1';
} }

@ -154,6 +154,17 @@ class SimplePdoTest extends TestCase
$this->assertEquals(3, $row['id']); // Should be Bob (id=3) $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 // fetchAll Tests
// ========================================================================= // =========================================================================

Loading…
Cancel
Save