Skip to content

Commit

Permalink
Merge pull request #2269 from michalsn/feature/qb_testMode
Browse files Browse the repository at this point in the history
testMode() method for BaseBuilder
  • Loading branch information
jim-parry authored Sep 26, 2019
2 parents 4eca6c9 + 69ecefd commit 843d46b
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 99 deletions.
107 changes: 58 additions & 49 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class BaseBuilder
*/
protected $canLimitWhereUpdates = true;

/**
* Builder testing mode status.
*
* @var boolean
*/
protected $testMode = false;

//--------------------------------------------------------------------

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

//--------------------------------------------------------------------

/**
* Sets a test mode status.
*
* @param boolean $mode Mode to set
*
* @return BaseBuilder
*/
public function testMode(bool $mode = true)
{
$this->testMode = $mode;

return $this;
}

//--------------------------------------------------------------------

/**
* Returns an array of bind values and their
* named parameters for binding in the Query object later.
Expand Down Expand Up @@ -1738,21 +1761,20 @@ protected function compileFinalQuery(string $sql): string
* Compiles the select statement based on the other functions called
* and runs the query
*
* @param integer $limit The limit clause
* @param integer $offset The offset clause
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
* @param boolean $reset Are we want to clear query builder values?
* @param integer $limit The limit clause
* @param integer $offset The offset clause
* @param boolean $reset Are we want to clear query builder values?
*
* @return ResultInterface
*/
public function get(int $limit = null, int $offset = 0, bool $returnSQL = false, bool $reset = true)
public function get(int $limit = null, int $offset = 0, bool $reset = true)
{
if (! is_null($limit))
{
$this->limit($limit, $offset);
}

$result = $returnSQL
$result = $this->testMode
? $this->getCompiledSelect($reset)
: $this->db->query($this->compileSelect(), $this->binds, false);

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

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

if ($test)
if ($this->testMode)
{
return $sql;
}
Expand Down Expand Up @@ -1817,11 +1838,10 @@ public function countAll(bool $reset = true, bool $test = false)
* returned by an Query Builder query.
*
* @param boolean $reset
* @param boolean $test The reset clause
*
* @return integer|string when $test = true
*/
public function countAllResults(bool $reset = true, bool $test = false)
public function countAllResults(bool $reset = true)
{
// ORDER BY usage is often problematic here (most notably
// on Microsoft SQL Server) and ultimately unnecessary
Expand All @@ -1843,7 +1863,7 @@ public function countAllResults(bool $reset = true, bool $test = false)
:
$this->compileSelect($this->countString . $this->db->protectIdentifiers('numrows'));

if ($test)
if ($this->testMode)
{
return $sql;
}
Expand Down Expand Up @@ -1894,15 +1914,14 @@ public function getCompiledQBWhere()
*
* Allows the where clause, limit and offset to be added directly
*
* @param string|array $where Where condition
* @param integer $limit Limit value
* @param integer $offset Offset value
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
* @param boolean $reset Are we want to clear query builder values?
* @param string|array $where Where condition
* @param integer $limit Limit value
* @param integer $offset Offset value
* @param boolean $reset Are we want to clear query builder values?
*
* @return ResultInterface
*/
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $reset = true)
{
if ($where !== null)
{
Expand All @@ -1914,7 +1933,7 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
$this->limit($limit, $offset);
}

$result = $returnSQL
$result = $this->testMode
? $this->getCompiledSelect($reset)
: $this->db->query($this->compileSelect(), $this->binds, false);

Expand All @@ -1938,14 +1957,12 @@ public function getWhere($where = null, int $limit = null, ?int $offset = 0, boo
*
* @param array $set An associative array of insert values
* @param boolean $escape Whether to escape values and identifiers
*
* @param integer $batchSize
* @param boolean $testing
* @param integer $batchSize Batch size
*
* @return integer Number of rows inserted or FALSE on failure
* @throws DatabaseException
*/
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100, bool $testing = false)
public function insertBatch(array $set = null, bool $escape = null, int $batchSize = 100)
{
if ($set === null)
{
Expand Down Expand Up @@ -1982,7 +1999,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
{
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, $escape, false), $this->QBKeys, array_slice($this->QBSet, $i, $batchSize));

if ($testing)
if ($this->testMode)
{
++ $affected_rows;
}
Expand All @@ -1993,7 +2010,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
}
}

if (! $testing)
if (! $this->testMode)
{
$this->resetWrite();
}
Expand Down Expand Up @@ -2117,11 +2134,10 @@ public function getCompiledInsert(bool $reset = true): string
*
* @param array $set An associative array of insert values
* @param boolean $escape Whether to escape values and identifiers
* @param boolean $test Used when running tests
*
* @return BaseResult|Query|false
*/
public function insert(array $set = null, bool $escape = null, bool $test = false)
public function insert(array $set = null, bool $escape = null)
{
if ($set !== null)
{
Expand All @@ -2139,7 +2155,7 @@ public function insert(array $set = null, bool $escape = null, bool $test = fals
), array_keys($this->QBSet), array_values($this->QBSet)
);

if ($test === false)
if (! $this->testMode)
{
$this->resetWrite();

Expand Down Expand Up @@ -2206,13 +2222,12 @@ protected function _insert(string $table, array $keys, array $unescapedKeys): st
*
* Compiles an replace into string and runs the query
*
* @param array $set An associative array of insert values
* @param boolean $returnSQL
* @param array $set An associative array of insert values
*
* @return BaseResult|Query|string|false
* @throws DatabaseException
*/
public function replace(array $set = null, bool $returnSQL = false)
public function replace(array $set = null)
{
if ($set !== null)
{
Expand All @@ -2234,7 +2249,7 @@ public function replace(array $set = null, bool $returnSQL = false)

$this->resetWrite();

return $returnSQL ? $sql : $this->db->query($sql, $this->binds, false);
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -2310,11 +2325,10 @@ public function getCompiledUpdate(bool $reset = true): string
* @param array $set An associative array of update values
* @param mixed $where
* @param integer $limit
* @param boolean $test Are we testing the code?
*
* @return boolean TRUE on success, FALSE on failure
*/
public function update(array $set = null, $where = null, int $limit = null, bool $test = false): bool
public function update(array $set = null, $where = null, int $limit = null): bool
{
if ($set !== null)
{
Expand Down Expand Up @@ -2343,7 +2357,7 @@ public function update(array $set = null, $where = null, int $limit = null, bool

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

if (! $test)
if (! $this->testMode)
{
$this->resetWrite();

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

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

$this->resetWrite();

return $returnSQL ? $savedSQL : $affected_rows;
return $this->testMode ? $savedSQL : $affected_rows;
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -2595,16 +2608,15 @@ public function setUpdateBatch($key, string $index = '', bool $escape = null)
*
* Compiles a delete string and runs "DELETE FROM table"
*
* @param boolean $test
* @return boolean TRUE on success, FALSE on failure
*/
public function emptyTable(bool $test = false)
public function emptyTable()
{
$table = $this->QBFrom[0];

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

if ($test)
if ($this->testMode)
{
return $sql;
}
Expand All @@ -2623,17 +2635,15 @@ public function emptyTable(bool $test = false)
* If the database does not support the truncate() command
* This function maps to "DELETE FROM table"
*
* @param boolean $test Whether we're in test mode or not.
*
* @return boolean TRUE on success, FALSE on failure
*/
public function truncate(bool $test = false)
public function truncate()
{
$table = $this->QBFrom[0];

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

if ($test === true)
if ($this->testMode)
{
return $sql;
}
Expand Down Expand Up @@ -2692,12 +2702,11 @@ public function getCompiledDelete(bool $reset = true): string
* @param mixed $where The where clause
* @param integer $limit The limit clause
* @param boolean $reset_data
* @param boolean $returnSQL
*
* @return mixed
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
*/
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
public function delete($where = '', int $limit = null, bool $reset_data = true)
{
$table = $this->db->protectIdentifiers($this->QBFrom[0], true, null, false);

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

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

//--------------------------------------------------------------------
Expand Down
10 changes: 4 additions & 6 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ public function decrement(string $column, int $value = 1)
* we simply do a DELETE and an INSERT on the first key/value
* combo, assuming that it's either the primary key or a unique key.
*
* @param array $set An associative array of insert values
* @param boolean $returnSQL
* @param array $set An associative array of insert values
*
* @return mixed
* @throws DatabaseException
* @internal param true $bool returns the generated SQL, false executes the query.
*/
public function replace(array $set = null, bool $returnSQL = false)
public function replace(array $set = null)
{
if ($set !== null)
{
Expand Down Expand Up @@ -202,22 +201,21 @@ public function replace(array $set = null, bool $returnSQL = false)
* @param mixed $where
* @param integer $limit
* @param boolean $reset_data
* @param boolean $returnSQL
*
* @return mixed
* @throws DatabaseException
* @internal param the $mixed where clause
* @internal param the $mixed limit clause
* @internal param $bool
*/
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
public function delete($where = '', int $limit = null, bool $reset_data = true)
{
if (! empty($limit) || ! empty($this->QBLimit))
{
throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.');
}

return parent::delete($where, $limit, $reset_data, $returnSQL);
return parent::delete($where, $limit, $reset_data);
}

//--------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 843d46b

Please sign in to comment.