Skip to content

Commit feb7f81

Browse files
Remove isWriteType override for Postgre;
Omit RETURNING from isWriteType for all database types; Modify tests for uniform handling for all database types
1 parent b0dd905 commit feb7f81

File tree

3 files changed

+16
-106
lines changed

3 files changed

+16
-106
lines changed

system/Database/BaseConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ public function resetDataCache()
16571657
*/
16581658
public function isWriteType($sql): bool
16591659
{
1660-
return (bool) preg_match('/^\s*(WITH\s.+(\s|[)]))?"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|MERGE)\s/is', $sql);
1660+
return (bool) preg_match('/^\s*(WITH\s.+(\s|[)]))?"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|MERGE)\s(?!.*\sRETURNING\s)/is', $sql);
16611661
}
16621662

16631663
/**

system/Database/Postgre/Connection.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -567,20 +567,4 @@ protected function _transRollback(): bool
567567
{
568568
return (bool) pg_query($this->connID, 'ROLLBACK');
569569
}
570-
571-
/**
572-
* Determines if a query is a "write" type.
573-
*
574-
* Overrides BaseConnection::isWriteType, adding additional read query types.
575-
*
576-
* @param string $sql
577-
*/
578-
public function isWriteType($sql): bool
579-
{
580-
if (preg_match('#^\s*(WITH\s.+(\s|[)]))?(INSERT|UPDATE|DELETE).*RETURNING\s.+(\,\s?.+)*$#is', $sql)) {
581-
return false;
582-
}
583-
584-
return parent::isWriteType($sql);
585-
}
586570
}

tests/system/Database/Live/WriteTypeQueryTest.php

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ final class WriteTypeQueryTest extends CIUnitTestCase
2828
protected $refresh = true;
2929
protected $seed = CITestSeeder::class;
3030

31-
/**
32-
* Whether CodeIgniter considers RETURNING in isWriteType.
33-
*
34-
* Currently, only Postgre is supported by CodeIgniter.
35-
* This method should be updated if support for RETURNING
36-
* is expanded for other databases.
37-
*
38-
* @param string $dbDriver
39-
*/
40-
private function testReturning($dbDriver): bool
41-
{
42-
return $dbDriver === 'Postgre';
43-
}
44-
4531
public function testSet(): void
4632
{
4733
$sql = 'SET FOREIGN_KEY_CHECKS=0';
@@ -110,11 +96,7 @@ public function testInsertOneReturning(): void
11096
{
11197
$sql = "INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool') RETURNING id;";
11298

113-
if ($this->testReturning($this->db->DBDriver)) {
114-
$this->assertFalse($this->db->isWriteType($sql));
115-
} else {
116-
$this->assertTrue($this->db->isWriteType($sql));
117-
}
99+
$this->assertFalse($this->db->isWriteType($sql));
118100
}
119101

120102
public function testInsertMultiReturning(): void
@@ -125,33 +107,21 @@ public function testInsertMultiReturning(): void
125107
RETURNING id;
126108
SQL;
127109

128-
if ($this->testReturning($this->db->DBDriver)) {
129-
$this->assertFalse($this->db->isWriteType($sql));
130-
} else {
131-
$this->assertTrue($this->db->isWriteType($sql));
132-
}
110+
$this->assertFalse($this->db->isWriteType($sql));
133111
}
134112

135113
public function testInsertWithOneReturning(): void
136114
{
137115
$sql = "WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;";
138116

139-
if ($this->testReturning($this->db->DBDriver)) {
140-
$this->assertFalse($this->db->isWriteType($sql));
141-
} else {
142-
$this->assertTrue($this->db->isWriteType($sql));
143-
}
117+
$this->assertFalse($this->db->isWriteType($sql));
144118
}
145119

146120
public function testInsertWithOneReturningNoSpace(): void
147121
{
148122
$sql = "WITH seqvals AS (SELECT '3' AS seqval)INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;";
149123

150-
if ($this->testReturning($this->db->DBDriver)) {
151-
$this->assertFalse($this->db->isWriteType($sql));
152-
} else {
153-
$this->assertTrue($this->db->isWriteType($sql));
154-
}
124+
$this->assertFalse($this->db->isWriteType($sql));
155125
}
156126

157127
public function testInsertWithMultiReturning(): void
@@ -164,11 +134,7 @@ public function testInsertWithMultiReturning(): void
164134
RETURNING id;
165135
SQL;
166136

167-
if ($this->testReturning($this->db->DBDriver)) {
168-
$this->assertFalse($this->db->isWriteType($sql));
169-
} else {
170-
$this->assertTrue($this->db->isWriteType($sql));
171-
}
137+
$this->assertFalse($this->db->isWriteType($sql));
172138
}
173139

174140
public function testUpdateBuilder(): void
@@ -229,11 +195,7 @@ public function testUpdateOneReturning(): void
229195
{
230196
$sql = "UPDATE my_table SET col1 = 'foo' WHERE id = 2 RETURNING *;";
231197

232-
if ($this->testReturning($this->db->DBDriver)) {
233-
$this->assertFalse($this->db->isWriteType($sql));
234-
} else {
235-
$this->assertTrue($this->db->isWriteType($sql));
236-
}
198+
$this->assertFalse($this->db->isWriteType($sql));
237199
}
238200

239201
public function testUpdateMultiReturning(): void
@@ -245,33 +207,21 @@ public function testUpdateMultiReturning(): void
245207
RETURNING *;
246208
SQL;
247209

248-
if ($this->testReturning($this->db->DBDriver)) {
249-
$this->assertFalse($this->db->isWriteType($sql));
250-
} else {
251-
$this->assertTrue($this->db->isWriteType($sql));
252-
}
210+
$this->assertFalse($this->db->isWriteType($sql));
253211
}
254212

255213
public function testUpdateWithOneReturning(): void
256214
{
257215
$sql = "WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;";
258216

259-
if ($this->testReturning($this->db->DBDriver)) {
260-
$this->assertFalse($this->db->isWriteType($sql));
261-
} else {
262-
$this->assertTrue($this->db->isWriteType($sql));
263-
}
217+
$this->assertFalse($this->db->isWriteType($sql));
264218
}
265219

266220
public function testUpdateWithOneReturningNoSpace(): void
267221
{
268222
$sql = "WITH seqvals AS (SELECT '3' AS seqval)UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;";
269223

270-
if ($this->testReturning($this->db->DBDriver)) {
271-
$this->assertFalse($this->db->isWriteType($sql));
272-
} else {
273-
$this->assertTrue($this->db->isWriteType($sql));
274-
}
224+
$this->assertFalse($this->db->isWriteType($sql));
275225
}
276226

277227
public function testUpdateWithMultiReturning(): void
@@ -285,11 +235,7 @@ public function testUpdateWithMultiReturning(): void
285235
RETURNING *;
286236
SQL;
287237

288-
if ($this->testReturning($this->db->DBDriver)) {
289-
$this->assertFalse($this->db->isWriteType($sql));
290-
} else {
291-
$this->assertTrue($this->db->isWriteType($sql));
292-
}
238+
$this->assertFalse($this->db->isWriteType($sql));
293239
}
294240

295241
public function testDeleteBuilder(): void
@@ -348,11 +294,7 @@ public function testDeleteOneReturning(): void
348294
{
349295
$sql = 'DELETE FROM my_table WHERE id = 2 RETURNING *;';
350296

351-
if ($this->testReturning($this->db->DBDriver)) {
352-
$this->assertFalse($this->db->isWriteType($sql));
353-
} else {
354-
$this->assertTrue($this->db->isWriteType($sql));
355-
}
297+
$this->assertFalse($this->db->isWriteType($sql));
356298
}
357299

358300
public function testDeleteMultiReturning(): void
@@ -363,33 +305,21 @@ public function testDeleteMultiReturning(): void
363305
RETURNING *;
364306
SQL;
365307

366-
if ($this->testReturning($this->db->DBDriver)) {
367-
$this->assertFalse($this->db->isWriteType($sql));
368-
} else {
369-
$this->assertTrue($this->db->isWriteType($sql));
370-
}
308+
$this->assertFalse($this->db->isWriteType($sql));
371309
}
372310

373311
public function testDeleteWithOneReturning(): void
374312
{
375313
$sql = "WITH seqvals AS (SELECT '3' AS seqval) DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;";
376314

377-
if ($this->testReturning($this->db->DBDriver)) {
378-
$this->assertFalse($this->db->isWriteType($sql));
379-
} else {
380-
$this->assertTrue($this->db->isWriteType($sql));
381-
}
315+
$this->assertFalse($this->db->isWriteType($sql));
382316
}
383317

384318
public function testDeleteWithOneReturningNoSpace(): void
385319
{
386320
$sql = "WITH seqvals AS (SELECT '3' AS seqval)DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;";
387321

388-
if ($this->testReturning($this->db->DBDriver)) {
389-
$this->assertFalse($this->db->isWriteType($sql));
390-
} else {
391-
$this->assertTrue($this->db->isWriteType($sql));
392-
}
322+
$this->assertFalse($this->db->isWriteType($sql));
393323
}
394324

395325
public function testDeleteWithMultiReturning(): void
@@ -403,11 +333,7 @@ public function testDeleteWithMultiReturning(): void
403333
RETURNING *;
404334
SQL;
405335

406-
if ($this->testReturning($this->db->DBDriver)) {
407-
$this->assertFalse($this->db->isWriteType($sql));
408-
} else {
409-
$this->assertTrue($this->db->isWriteType($sql));
410-
}
336+
$this->assertFalse($this->db->isWriteType($sql));
411337
}
412338

413339
public function testReplace(): void

0 commit comments

Comments
 (0)