Skip to content

Commit 9210ab8

Browse files
committed
builder testMode
1 parent 1fa1e10 commit 9210ab8

File tree

11 files changed

+134
-99
lines changed

11 files changed

+134
-99
lines changed

system/Database/BaseBuilder.php

Lines changed: 59 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,23 @@ public function __construct($tableName, ConnectionInterface &$db, array $options
254261

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

264+
/**
265+
* Returns an array of bind values and their
266+
* named parameters for binding in the Query object later.
267+
*
268+
* @param boolean $mode Mode to set
269+
*
270+
* @return BaseBuilder
271+
*/
272+
public function testMode(bool $mode = true)
273+
{
274+
$this->testMode = $mode;
275+
276+
return $this;
277+
}
278+
279+
//--------------------------------------------------------------------
280+
257281
/**
258282
* Returns an array of bind values and their
259283
* named parameters for binding in the Query object later.
@@ -1738,21 +1762,20 @@ protected function compileFinalQuery(string $sql): string
17381762
* Compiles the select statement based on the other functions called
17391763
* and runs the query
17401764
*
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?
1765+
* @param integer $limit The limit clause
1766+
* @param integer $offset The offset clause
1767+
* @param boolean $reset Are we want to clear query builder values?
17451768
*
17461769
* @return ResultInterface
17471770
*/
1748-
public function get(int $limit = null, int $offset = 0, bool $returnSQL = false, bool $reset = true)
1771+
public function get(int $limit = null, int $offset = 0, bool $reset = true)
17491772
{
17501773
if (! is_null($limit))
17511774
{
17521775
$this->limit($limit, $offset);
17531776
}
17541777

1755-
$result = $returnSQL
1778+
$result = $this->testMode
17561779
? $this->getCompiledSelect($reset)
17571780
: $this->db->query($this->compileSelect(), $this->binds, false);
17581781

@@ -1776,18 +1799,17 @@ public function get(int $limit = null, int $offset = 0, bool $returnSQL = false,
17761799
* the specified database
17771800
*
17781801
* @param boolean $reset Are we want to clear query builder values?
1779-
* @param boolean $test Are we running automated tests?
17801802
*
17811803
* @return integer|string when $test = true
17821804
*/
1783-
public function countAll(bool $reset = true, bool $test = false)
1805+
public function countAll(bool $reset = true)
17841806
{
17851807
$table = $this->QBFrom[0];
17861808

17871809
$sql = $this->countString . $this->db->escapeIdentifiers('numrows') . ' FROM ' .
17881810
$this->db->protectIdentifiers($table, true, null, false);
17891811

1790-
if ($test)
1812+
if ($this->testMode)
17911813
{
17921814
return $sql;
17931815
}
@@ -1817,11 +1839,10 @@ public function countAll(bool $reset = true, bool $test = false)
18171839
* returned by an Query Builder query.
18181840
*
18191841
* @param boolean $reset
1820-
* @param boolean $test The reset clause
18211842
*
18221843
* @return integer|string when $test = true
18231844
*/
1824-
public function countAllResults(bool $reset = true, bool $test = false)
1845+
public function countAllResults(bool $reset = true)
18251846
{
18261847
// ORDER BY usage is often problematic here (most notably
18271848
// on Microsoft SQL Server) and ultimately unnecessary
@@ -1843,7 +1864,7 @@ public function countAllResults(bool $reset = true, bool $test = false)
18431864
:
18441865
$this->compileSelect($this->countString . $this->db->protectIdentifiers('numrows'));
18451866

1846-
if ($test)
1867+
if ($this->testMode)
18471868
{
18481869
return $sql;
18491870
}
@@ -1894,15 +1915,14 @@ public function getCompiledQBWhere()
18941915
*
18951916
* Allows the where clause, limit and offset to be added directly
18961917
*
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?
1918+
* @param string|array $where Where condition
1919+
* @param integer $limit Limit value
1920+
* @param integer $offset Offset value
1921+
* @param boolean $reset Are we want to clear query builder values?
19021922
*
19031923
* @return ResultInterface
19041924
*/
1905-
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
1925+
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $reset = true)
19061926
{
19071927
if ($where !== null)
19081928
{
@@ -1914,7 +1934,7 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
19141934
$this->limit($limit, $offset);
19151935
}
19161936

1917-
$result = $returnSQL
1937+
$result = $this->testMode
19181938
? $this->getCompiledSelect($reset)
19191939
: $this->db->query($this->compileSelect(), $this->binds, false);
19201940

@@ -1938,14 +1958,12 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
19381958
*
19391959
* @param array $set An associative array of insert values
19401960
* @param boolean $escape Whether to escape values and identifiers
1941-
*
1942-
* @param integer $batchSize
1943-
* @param boolean $testing
1961+
* @param integer $batchSize Batch size
19441962
*
19451963
* @return integer Number of rows inserted or FALSE on failure
19461964
* @throws DatabaseException
19471965
*/
1948-
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100, bool $testing = false)
1966+
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100)
19491967
{
19501968
if ($set === null)
19511969
{
@@ -1982,7 +2000,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
19822000
{
19832001
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, $escape, false), $this->QBKeys, array_slice($this->QBSet, $i, $batchSize));
19842002

1985-
if ($testing)
2003+
if ($this->testMode)
19862004
{
19872005
++ $affected_rows;
19882006
}
@@ -1993,7 +2011,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
19932011
}
19942012
}
19952013

1996-
if (! $testing)
2014+
if (! $this->testMode)
19972015
{
19982016
$this->resetWrite();
19992017
}
@@ -2117,11 +2135,10 @@ public function getCompiledInsert(bool $reset = true): string
21172135
*
21182136
* @param array $set An associative array of insert values
21192137
* @param boolean $escape Whether to escape values and identifiers
2120-
* @param boolean $test Used when running tests
21212138
*
21222139
* @return BaseResult|Query|false
21232140
*/
2124-
public function insert(array $set = null, bool $escape = null, bool $test = false)
2141+
public function insert(array $set = null, bool $escape = null)
21252142
{
21262143
if ($set !== null)
21272144
{
@@ -2139,7 +2156,7 @@ public function insert(array $set = null, bool $escape = null, bool $test = fals
21392156
), array_keys($this->QBSet), array_values($this->QBSet)
21402157
);
21412158

2142-
if ($test === false)
2159+
if (! $this->testMode)
21432160
{
21442161
$this->resetWrite();
21452162

@@ -2206,13 +2223,12 @@ protected function _insert(string $table, array $keys, array $unescapedKeys): st
22062223
*
22072224
* Compiles an replace into string and runs the query
22082225
*
2209-
* @param array $set An associative array of insert values
2210-
* @param boolean $returnSQL
2226+
* @param array $set An associative array of insert values
22112227
*
22122228
* @return BaseResult|Query|string|false
22132229
* @throws DatabaseException
22142230
*/
2215-
public function replace(array $set = null, bool $returnSQL = false)
2231+
public function replace(array $set = null)
22162232
{
22172233
if ($set !== null)
22182234
{
@@ -2234,7 +2250,7 @@ public function replace(array $set = null, bool $returnSQL = false)
22342250

22352251
$this->resetWrite();
22362252

2237-
return $returnSQL ? $sql : $this->db->query($sql, $this->binds, false);
2253+
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
22382254
}
22392255

22402256
//--------------------------------------------------------------------
@@ -2310,11 +2326,10 @@ public function getCompiledUpdate(bool $reset = true): string
23102326
* @param array $set An associative array of update values
23112327
* @param mixed $where
23122328
* @param integer $limit
2313-
* @param boolean $test Are we testing the code?
23142329
*
23152330
* @return boolean TRUE on success, FALSE on failure
23162331
*/
2317-
public function update(array $set = null, $where = null, int $limit = null, bool $test = false): bool
2332+
public function update(array $set = null, $where = null, int $limit = null): bool
23182333
{
23192334
if ($set !== null)
23202335
{
@@ -2343,7 +2358,7 @@ public function update(array $set = null, $where = null, int $limit = null, bool
23432358

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

2346-
if (! $test)
2361+
if (! $this->testMode)
23472362
{
23482363
$this->resetWrite();
23492364

@@ -2425,12 +2440,11 @@ protected function validateUpdate(): bool
24252440
* @param array $set An associative array of update values
24262441
* @param string $index The where key
24272442
* @param integer $batchSize The size of the batch to run
2428-
* @param boolean $returnSQL True means SQL is returned, false will execute the query
24292443
*
24302444
* @return mixed Number of rows affected, SQL string, or FALSE on failure
24312445
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
24322446
*/
2433-
public function updateBatch(array $set = null, string $index = null, int $batchSize = 100, bool $returnSQL = false)
2447+
public function updateBatch(array $set = null, string $index = null, int $batchSize = 100)
24342448
{
24352449
if ($index === null)
24362450
{
@@ -2477,7 +2491,7 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
24772491
$sql = $this->_updateBatch($table, array_slice($this->QBSet, $i, $batchSize), $this->db->protectIdentifiers($index)
24782492
);
24792493

2480-
if ($returnSQL)
2494+
if ($this->testMode)
24812495
{
24822496
$savedSQL[] = $sql;
24832497
}
@@ -2492,7 +2506,7 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
24922506

24932507
$this->resetWrite();
24942508

2495-
return $returnSQL ? $savedSQL : $affected_rows;
2509+
return $this->testMode ? $savedSQL : $affected_rows;
24962510
}
24972511

24982512
//--------------------------------------------------------------------
@@ -2595,16 +2609,15 @@ public function setUpdateBatch($key, string $index = '', bool $escape = null)
25952609
*
25962610
* Compiles a delete string and runs "DELETE FROM table"
25972611
*
2598-
* @param boolean $test
25992612
* @return boolean TRUE on success, FALSE on failure
26002613
*/
2601-
public function emptyTable(bool $test = false)
2614+
public function emptyTable()
26022615
{
26032616
$table = $this->QBFrom[0];
26042617

26052618
$sql = $this->_delete($table);
26062619

2607-
if ($test)
2620+
if ($this->testMode)
26082621
{
26092622
return $sql;
26102623
}
@@ -2623,17 +2636,15 @@ public function emptyTable(bool $test = false)
26232636
* If the database does not support the truncate() command
26242637
* This function maps to "DELETE FROM table"
26252638
*
2626-
* @param boolean $test Whether we're in test mode or not.
2627-
*
26282639
* @return boolean TRUE on success, FALSE on failure
26292640
*/
2630-
public function truncate(bool $test = false)
2641+
public function truncate()
26312642
{
26322643
$table = $this->QBFrom[0];
26332644

26342645
$sql = $this->_truncate($table);
26352646

2636-
if ($test === true)
2647+
if ($this->testMode)
26372648
{
26382649
return $sql;
26392650
}
@@ -2692,12 +2703,11 @@ public function getCompiledDelete(bool $reset = true): string
26922703
* @param mixed $where The where clause
26932704
* @param integer $limit The limit clause
26942705
* @param boolean $reset_data
2695-
* @param boolean $returnSQL
26962706
*
26972707
* @return mixed
26982708
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
26992709
*/
2700-
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
2710+
public function delete($where = '', int $limit = null, bool $reset_data = true)
27012711
{
27022712
$table = $this->db->protectIdentifiers($this->QBFrom[0], true, null, false);
27032713

@@ -2738,7 +2748,7 @@ public function delete($where = '', int $limit = null, bool $reset_data = true,
27382748
$this->resetWrite();
27392749
}
27402750

2741-
return ($returnSQL === true) ? $sql : $this->db->query($sql, $this->binds, false);
2751+
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
27422752
}
27432753

27442754
//--------------------------------------------------------------------

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)