Skip to content

Commit 094d003

Browse files
committed
changes to make query() function return boolean for write-type queries. centralize isWriteType functionality in one place. Change tests impacted by this change
1 parent 3e55588 commit 094d003

File tree

13 files changed

+69
-44
lines changed

13 files changed

+69
-44
lines changed

system/BaseModel.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ abstract protected function doFirst();
357357
*
358358
* @param array $data Data
359359
*
360-
* @return object|integer|string|false
360+
* @return integer|string|boolean
361361
*/
362362
abstract protected function doInsert(array $data);
363363

@@ -407,7 +407,7 @@ abstract protected function doUpdateBatch(array $set = null, string $index = nul
407407
* @param integer|string|array|null $id The rows primary key(s)
408408
* @param boolean $purge Allows overriding the soft deletes setting.
409409
*
410-
* @return object|boolean
410+
* @return string|boolean
411411
*
412412
* @throws DatabaseException
413413
*/
@@ -666,11 +666,7 @@ public function save($data): bool
666666
{
667667
$response = $this->insert($data, false);
668668

669-
if ($response instanceof BaseResult)
670-
{
671-
$response = $response->resultID !== false;
672-
}
673-
elseif ($response !== false)
669+
if ($response !== false)
674670
{
675671
$response = true;
676672
}
@@ -708,7 +704,7 @@ public function getInsertID()
708704
* @param array|object|null $data Data
709705
* @param boolean $returnID Whether insert ID should be returned or not.
710706
*
711-
* @return BaseResult|object|integer|string|false
707+
* @return integer|string|boolean
712708
*
713709
* @throws ReflectionException
714710
*/

system/Database/BaseBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ public function getCompiledInsert(bool $reset = true)
22362236
*
22372237
* @throws DatabaseException
22382238
*
2239-
* @return BaseResult|Query|false
2239+
* @return Query|boolean
22402240
*/
22412241
public function insert(array $set = null, bool $escape = null)
22422242
{
@@ -2467,7 +2467,7 @@ public function update(array $set = null, $where = null, int $limit = null): boo
24672467

24682468
$result = $this->db->query($sql, $this->binds, false);
24692469

2470-
if ($result->resultID !== false)
2470+
if ($result !== false)
24712471
{
24722472
// Clear our binds so we don't eat up memory
24732473
$this->binds = [];
@@ -2816,7 +2816,7 @@ public function getCompiledDelete(bool $reset = true): string
28162816
* @param integer $limit The limit clause
28172817
* @param boolean $resetData
28182818
*
2819-
* @return mixed
2819+
* @return string|boolean
28202820
* @throws DatabaseException
28212821
*/
28222822
public function delete($where = '', int $limit = null, bool $resetData = true)

system/Database/BaseConnection.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ abstract protected function execute(string $sql);
605605
* @param boolean $setEscapeFlags
606606
* @param string $queryClass
607607
*
608-
* @return BaseResult|Query|false
608+
* @return BaseResult|Query|boolean
609609
*
610610
* @todo BC set $queryClass default as null in 4.1
611611
*/
@@ -618,7 +618,6 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
618618
$this->initialize();
619619
}
620620

621-
$resultClass = str_replace('Connection', 'Result', get_class($this));
622621
/**
623622
* @var Query $query
624623
*/
@@ -689,7 +688,20 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
689688
// If $pretend is true, then we just want to return
690689
// the actual query object here. There won't be
691690
// any results to return.
692-
return $this->pretend ? $query : new $resultClass($this->connID, $this->resultID);
691+
if ($this->pretend)
692+
{
693+
return $query;
694+
}
695+
696+
// resultID is not false, so it must be successful
697+
if (\CodeIgniter\Database\Query::sqlIsWriteType($sql)) {
698+
return true;
699+
}
700+
701+
// query is not write-type, so it must be read-type query; return QueryResult
702+
$resultClass = str_replace('Connection', 'Result', get_class($this));
703+
return new $resultClass($this->connID, $this->resultID);
704+
693705
}
694706

695707
//--------------------------------------------------------------------

system/Database/BaseUtils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function databaseExists(string $databaseName): bool
120120
* Optimize Table
121121
*
122122
* @param string $tableName
123-
* @return mixed
123+
* @return boolean
124124
* @throws DatabaseException
125125
*/
126126
public function optimizeTable(string $tableName)
@@ -135,10 +135,10 @@ public function optimizeTable(string $tableName)
135135
}
136136

137137
$query = $this->db->query(sprintf($this->optimizeTable, $this->db->escapeIdentifiers($tableName)));
138+
138139
if ($query !== false)
139140
{
140-
$query = $query->getResultArray();
141-
return current($query);
141+
return true;
142142
}
143143

144144
return false;

system/Database/ConnectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getVersion(): string;
126126
* @param string $sql
127127
* @param mixed ...$binds
128128
*
129-
* @return mixed
129+
* @return BaseResult|Query|boolean
130130
*/
131131
public function query(string $sql, $binds = null);
132132

system/Database/Query.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,27 @@ public function getErrorMessage(): string
297297

298298
//--------------------------------------------------------------------
299299

300+
/**
301+
* Determines if the statement is a write-type query or not.
302+
*
303+
* @param string $sql
304+
* @return boolean
305+
*/
306+
public static function sqlIsWriteType($sql): bool
307+
{
308+
return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|OPTIMIZE|MERGE)\s/i', $sql);
309+
}
310+
311+
//--------------------------------------------------------------------
312+
300313
/**
301314
* Determines if the statement is a write-type query or not.
302315
*
303316
* @return boolean
304317
*/
305318
public function isWriteType(): bool
306319
{
307-
return (bool) preg_match(
308-
'/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i', $this->originalQueryString);
320+
return self::sqlIsWriteType($this->originalQueryString);
309321
}
310322

311323
//--------------------------------------------------------------------

system/Database/SQLSRV/Connection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Database\SQLSRV;
1313

14+
use CodeIgniter\Database\Query;
1415
use CodeIgniter\Database\BaseConnection;
1516
use CodeIgniter\Database\Exceptions\DatabaseException;
1617
use Exception;
@@ -511,7 +512,7 @@ public function execute(string $sql)
511512
*/
512513
public function isWriteType($sql)
513514
{
514-
return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|MERGE)\s/i', $sql);
515+
return Query::sqlIsWriteType($sql);
515516
}
516517

517518
/**

system/Database/SQLite3/Connection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Database\SQLite3;
1313

14+
use CodeIgniter\Database\Query;
1415
use CodeIgniter\Database\BaseConnection;
1516
use CodeIgniter\Database\Exceptions\DatabaseException;
1617
use ErrorException;
@@ -497,9 +498,7 @@ protected function _transRollback(): bool
497498
*/
498499
public function isWriteType($sql): bool
499500
{
500-
return (bool) preg_match(
501-
'/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i',
502-
$sql);
501+
return Query::sqlIsWriteType($sql);
503502
}
504503

505504
//--------------------------------------------------------------------

system/Model.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Database\ConnectionInterface;
2020
use CodeIgniter\Database\Exceptions\DatabaseException;
2121
use CodeIgniter\Database\Exceptions\DataException;
22+
use CodeIgniter\Database\Query;
2223
use CodeIgniter\Exceptions\ModelException;
2324
use CodeIgniter\I18n\Time;
2425
use CodeIgniter\Validation\ValidationInterface;
@@ -253,7 +254,7 @@ protected function doFirst()
253254
*
254255
* @param array $data Data
255256
*
256-
* @return BaseResult|integer|string|false
257+
* @return Query|boolean
257258
*/
258259
protected function doInsert(array $data)
259260
{
@@ -278,7 +279,7 @@ protected function doInsert(array $data)
278279
$result = $builder->insert();
279280

280281
// If insertion succeeded then save the insert ID
281-
if ($result->resultID)
282+
if ($result)
282283
{
283284
if (! $this->useAutoIncrement)
284285
{
@@ -379,7 +380,7 @@ protected function doUpdateBatch(array $set = null, string $index = null, int $b
379380
* @param integer|string|array|null $id The rows primary key(s)
380381
* @param boolean $purge Allows overriding the soft deletes setting.
381382
*
382-
* @return BaseResult|boolean
383+
* @return string|boolean
383384
*
384385
* @throws DatabaseException
385386
*/

system/Test/Mock/MockConnection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function shouldReturn(string $method, $return)
4848
* @param boolean $setEscapeFlags
4949
* @param string $queryClass
5050
*
51-
* @return BaseResult|Query|false
51+
* @return BaseResult|Query|boolean
5252
*
5353
* @todo BC set $queryClass default as null in 4.1
5454
*/
@@ -81,8 +81,13 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
8181

8282
$query->setDuration($startTime);
8383

84-
$resultClass = str_replace('Connection', 'Result', get_class($this));
84+
// resultID is not false, so it must be successful
85+
if (Query::sqlIsWriteType($sql)) {
86+
return true;
87+
}
8588

89+
// query is not write-type, so it must be read-type query; return QueryResult
90+
$resultClass = str_replace('Connection', 'Result', get_class($this));
8691
return new $resultClass($this->connID, $this->resultID);
8792
}
8893

0 commit comments

Comments
 (0)