Skip to content

Commit 843d46b

Browse files
authored
Merge pull request #2269 from michalsn/feature/qb_testMode
testMode() method for BaseBuilder
2 parents 4eca6c9 + 69ecefd commit 843d46b

File tree

11 files changed

+133
-99
lines changed

11 files changed

+133
-99
lines changed

system/Database/BaseBuilder.php

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ class BaseBuilder
219219
*/
220220
protected $canLimitWhereUpdates = true;
221221

222+
/**
223+
* Builder testing mode status.
224+
*
225+
* @var boolean
226+
*/
227+
protected $testMode = false;
228+
222229
//--------------------------------------------------------------------
223230

224231
/**
@@ -254,6 +261,22 @@ public function __construct($tableName, ConnectionInterface &$db, array $options
254261

255262
//--------------------------------------------------------------------
256263

264+
/**
265+
* Sets a test mode status.
266+
*
267+
* @param boolean $mode Mode to set
268+
*
269+
* @return BaseBuilder
270+
*/
271+
public function testMode(bool $mode = true)
272+
{
273+
$this->testMode = $mode;
274+
275+
return $this;
276+
}
277+
278+
//--------------------------------------------------------------------
279+
257280
/**
258281
* Returns an array of bind values and their
259282
* named parameters for binding in the Query object later.
@@ -1738,21 +1761,20 @@ protected function compileFinalQuery(string $sql): string
17381761
* Compiles the select statement based on the other functions called
17391762
* and runs the query
17401763
*
1741-
* @param integer $limit The limit clause
1742-
* @param integer $offset The offset clause
1743-
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
1744-
* @param boolean $reset Are we want to clear query builder values?
1764+
* @param integer $limit The limit clause
1765+
* @param integer $offset The offset clause
1766+
* @param boolean $reset Are we want to clear query builder values?
17451767
*
17461768
* @return ResultInterface
17471769
*/
1748-
public function get(int $limit = null, int $offset = 0, bool $returnSQL = false, bool $reset = true)
1770+
public function get(int $limit = null, int $offset = 0, bool $reset = true)
17491771
{
17501772
if (! is_null($limit))
17511773
{
17521774
$this->limit($limit, $offset);
17531775
}
17541776

1755-
$result = $returnSQL
1777+
$result = $this->testMode
17561778
? $this->getCompiledSelect($reset)
17571779
: $this->db->query($this->compileSelect(), $this->binds, false);
17581780

@@ -1776,18 +1798,17 @@ public function get(int $limit = null, int $offset = 0, bool $returnSQL = false,
17761798
* the specified database
17771799
*
17781800
* @param boolean $reset Are we want to clear query builder values?
1779-
* @param boolean $test Are we running automated tests?
17801801
*
17811802
* @return integer|string when $test = true
17821803
*/
1783-
public function countAll(bool $reset = true, bool $test = false)
1804+
public function countAll(bool $reset = true)
17841805
{
17851806
$table = $this->QBFrom[0];
17861807

17871808
$sql = $this->countString . $this->db->escapeIdentifiers('numrows') . ' FROM ' .
17881809
$this->db->protectIdentifiers($table, true, null, false);
17891810

1790-
if ($test)
1811+
if ($this->testMode)
17911812
{
17921813
return $sql;
17931814
}
@@ -1817,11 +1838,10 @@ public function countAll(bool $reset = true, bool $test = false)
18171838
* returned by an Query Builder query.
18181839
*
18191840
* @param boolean $reset
1820-
* @param boolean $test The reset clause
18211841
*
18221842
* @return integer|string when $test = true
18231843
*/
1824-
public function countAllResults(bool $reset = true, bool $test = false)
1844+
public function countAllResults(bool $reset = true)
18251845
{
18261846
// ORDER BY usage is often problematic here (most notably
18271847
// on Microsoft SQL Server) and ultimately unnecessary
@@ -1843,7 +1863,7 @@ public function countAllResults(bool $reset = true, bool $test = false)
18431863
:
18441864
$this->compileSelect($this->countString . $this->db->protectIdentifiers('numrows'));
18451865

1846-
if ($test)
1866+
if ($this->testMode)
18471867
{
18481868
return $sql;
18491869
}
@@ -1894,15 +1914,14 @@ public function getCompiledQBWhere()
18941914
*
18951915
* Allows the where clause, limit and offset to be added directly
18961916
*
1897-
* @param string|array $where Where condition
1898-
* @param integer $limit Limit value
1899-
* @param integer $offset Offset value
1900-
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
1901-
* @param boolean $reset Are we want to clear query builder values?
1917+
* @param string|array $where Where condition
1918+
* @param integer $limit Limit value
1919+
* @param integer $offset Offset value
1920+
* @param boolean $reset Are we want to clear query builder values?
19021921
*
19031922
* @return ResultInterface
19041923
*/
1905-
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
1924+
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $reset = true)
19061925
{
19071926
if ($where !== null)
19081927
{
@@ -1914,7 +1933,7 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
19141933
$this->limit($limit, $offset);
19151934
}
19161935

1917-
$result = $returnSQL
1936+
$result = $this->testMode
19181937
? $this->getCompiledSelect($reset)
19191938
: $this->db->query($this->compileSelect(), $this->binds, false);
19201939

@@ -1938,14 +1957,12 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
19381957
*
19391958
* @param array $set An associative array of insert values
19401959
* @param boolean $escape Whether to escape values and identifiers
1941-
*
1942-
* @param integer $batchSize
1943-
* @param boolean $testing
1960+
* @param integer $batchSize Batch size
19441961
*
19451962
* @return integer Number of rows inserted or FALSE on failure
19461963
* @throws DatabaseException
19471964
*/
1948-
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100, bool $testing = false)
1965+
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100)
19491966
{
19501967
if ($set === null)
19511968
{
@@ -1982,7 +1999,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
19821999
{
19832000
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, $escape, false), $this->QBKeys, array_slice($this->QBSet, $i, $batchSize));
19842001

1985-
if ($testing)
2002+
if ($this->testMode)
19862003
{
19872004
++ $affected_rows;
19882005
}
@@ -1993,7 +2010,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
19932010
}
19942011
}
19952012

1996-
if (! $testing)
2013+
if (! $this->testMode)
19972014
{
19982015
$this->resetWrite();
19992016
}
@@ -2117,11 +2134,10 @@ public function getCompiledInsert(bool $reset = true): string
21172134
*
21182135
* @param array $set An associative array of insert values
21192136
* @param boolean $escape Whether to escape values and identifiers
2120-
* @param boolean $test Used when running tests
21212137
*
21222138
* @return BaseResult|Query|false
21232139
*/
2124-
public function insert(array $set = null, bool $escape = null, bool $test = false)
2140+
public function insert(array $set = null, bool $escape = null)
21252141
{
21262142
if ($set !== null)
21272143
{
@@ -2139,7 +2155,7 @@ public function insert(array $set = null, bool $escape = null, bool $test = fals
21392155
), array_keys($this->QBSet), array_values($this->QBSet)
21402156
);
21412157

2142-
if ($test === false)
2158+
if (! $this->testMode)
21432159
{
21442160
$this->resetWrite();
21452161

@@ -2206,13 +2222,12 @@ protected function _insert(string $table, array $keys, array $unescapedKeys): st
22062222
*
22072223
* Compiles an replace into string and runs the query
22082224
*
2209-
* @param array $set An associative array of insert values
2210-
* @param boolean $returnSQL
2225+
* @param array $set An associative array of insert values
22112226
*
22122227
* @return BaseResult|Query|string|false
22132228
* @throws DatabaseException
22142229
*/
2215-
public function replace(array $set = null, bool $returnSQL = false)
2230+
public function replace(array $set = null)
22162231
{
22172232
if ($set !== null)
22182233
{
@@ -2234,7 +2249,7 @@ public function replace(array $set = null, bool $returnSQL = false)
22342249

22352250
$this->resetWrite();
22362251

2237-
return $returnSQL ? $sql : $this->db->query($sql, $this->binds, false);
2252+
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
22382253
}
22392254

22402255
//--------------------------------------------------------------------
@@ -2310,11 +2325,10 @@ public function getCompiledUpdate(bool $reset = true): string
23102325
* @param array $set An associative array of update values
23112326
* @param mixed $where
23122327
* @param integer $limit
2313-
* @param boolean $test Are we testing the code?
23142328
*
23152329
* @return boolean TRUE on success, FALSE on failure
23162330
*/
2317-
public function update(array $set = null, $where = null, int $limit = null, bool $test = false): bool
2331+
public function update(array $set = null, $where = null, int $limit = null): bool
23182332
{
23192333
if ($set !== null)
23202334
{
@@ -2343,7 +2357,7 @@ public function update(array $set = null, $where = null, int $limit = null, bool
23432357

23442358
$sql = $this->_update($this->QBFrom[0], $this->QBSet);
23452359

2346-
if (! $test)
2360+
if (! $this->testMode)
23472361
{
23482362
$this->resetWrite();
23492363

@@ -2425,12 +2439,11 @@ protected function validateUpdate(): bool
24252439
* @param array $set An associative array of update values
24262440
* @param string $index The where key
24272441
* @param integer $batchSize The size of the batch to run
2428-
* @param boolean $returnSQL True means SQL is returned, false will execute the query
24292442
*
24302443
* @return mixed Number of rows affected, SQL string, or FALSE on failure
24312444
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
24322445
*/
2433-
public function updateBatch(array $set = null, string $index = null, int $batchSize = 100, bool $returnSQL = false)
2446+
public function updateBatch(array $set = null, string $index = null, int $batchSize = 100)
24342447
{
24352448
if ($index === null)
24362449
{
@@ -2477,7 +2490,7 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
24772490
$sql = $this->_updateBatch($table, array_slice($this->QBSet, $i, $batchSize), $this->db->protectIdentifiers($index)
24782491
);
24792492

2480-
if ($returnSQL)
2493+
if ($this->testMode)
24812494
{
24822495
$savedSQL[] = $sql;
24832496
}
@@ -2492,7 +2505,7 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
24922505

24932506
$this->resetWrite();
24942507

2495-
return $returnSQL ? $savedSQL : $affected_rows;
2508+
return $this->testMode ? $savedSQL : $affected_rows;
24962509
}
24972510

24982511
//--------------------------------------------------------------------
@@ -2595,16 +2608,15 @@ public function setUpdateBatch($key, string $index = '', bool $escape = null)
25952608
*
25962609
* Compiles a delete string and runs "DELETE FROM table"
25972610
*
2598-
* @param boolean $test
25992611
* @return boolean TRUE on success, FALSE on failure
26002612
*/
2601-
public function emptyTable(bool $test = false)
2613+
public function emptyTable()
26022614
{
26032615
$table = $this->QBFrom[0];
26042616

26052617
$sql = $this->_delete($table);
26062618

2607-
if ($test)
2619+
if ($this->testMode)
26082620
{
26092621
return $sql;
26102622
}
@@ -2623,17 +2635,15 @@ public function emptyTable(bool $test = false)
26232635
* If the database does not support the truncate() command
26242636
* This function maps to "DELETE FROM table"
26252637
*
2626-
* @param boolean $test Whether we're in test mode or not.
2627-
*
26282638
* @return boolean TRUE on success, FALSE on failure
26292639
*/
2630-
public function truncate(bool $test = false)
2640+
public function truncate()
26312641
{
26322642
$table = $this->QBFrom[0];
26332643

26342644
$sql = $this->_truncate($table);
26352645

2636-
if ($test === true)
2646+
if ($this->testMode)
26372647
{
26382648
return $sql;
26392649
}
@@ -2692,12 +2702,11 @@ public function getCompiledDelete(bool $reset = true): string
26922702
* @param mixed $where The where clause
26932703
* @param integer $limit The limit clause
26942704
* @param boolean $reset_data
2695-
* @param boolean $returnSQL
26962705
*
26972706
* @return mixed
26982707
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
26992708
*/
2700-
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
2709+
public function delete($where = '', int $limit = null, bool $reset_data = true)
27012710
{
27022711
$table = $this->db->protectIdentifiers($this->QBFrom[0], true, null, false);
27032712

@@ -2738,7 +2747,7 @@ public function delete($where = '', int $limit = null, bool $reset_data = true,
27382747
$this->resetWrite();
27392748
}
27402749

2741-
return ($returnSQL === true) ? $sql : $this->db->query($sql, $this->binds, false);
2750+
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
27422751
}
27432752

27442753
//--------------------------------------------------------------------

system/Database/Postgre/Builder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ public function decrement(string $column, int $value = 1)
137137
* we simply do a DELETE and an INSERT on the first key/value
138138
* combo, assuming that it's either the primary key or a unique key.
139139
*
140-
* @param array $set An associative array of insert values
141-
* @param boolean $returnSQL
140+
* @param array $set An associative array of insert values
142141
*
143142
* @return mixed
144143
* @throws DatabaseException
145144
* @internal param true $bool returns the generated SQL, false executes the query.
146145
*/
147-
public function replace(array $set = null, bool $returnSQL = false)
146+
public function replace(array $set = null)
148147
{
149148
if ($set !== null)
150149
{
@@ -202,22 +201,21 @@ public function replace(array $set = null, bool $returnSQL = false)
202201
* @param mixed $where
203202
* @param integer $limit
204203
* @param boolean $reset_data
205-
* @param boolean $returnSQL
206204
*
207205
* @return mixed
208206
* @throws DatabaseException
209207
* @internal param the $mixed where clause
210208
* @internal param the $mixed limit clause
211209
* @internal param $bool
212210
*/
213-
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
211+
public function delete($where = '', int $limit = null, bool $reset_data = true)
214212
{
215213
if (! empty($limit) || ! empty($this->QBLimit))
216214
{
217215
throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.');
218216
}
219217

220-
return parent::delete($where, $limit, $reset_data, $returnSQL);
218+
return parent::delete($where, $limit, $reset_data);
221219
}
222220

223221
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)