Skip to content

Commit

Permalink
Merge pull request #1902 from atishamte/BaseBuilder
Browse files Browse the repository at this point in the history
BaseBuilder Corrections
lonnieezell authored Apr 3, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 753d290 + c413ec2 commit 1c87d56
Showing 7 changed files with 216 additions and 180 deletions.
320 changes: 168 additions & 152 deletions system/Database/BaseBuilder.php

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions system/Database/Exceptions/DataException.php
Original file line number Diff line number Diff line change
@@ -51,6 +51,12 @@ public static function forTableNotFound(string $table)
return new static(lang('Database.tableNotFound', [$table]));
}


public static function forEmptyInputGiven(string $argument)
{
return new static(lang('Database.forEmptyInputGiven', [$argument]));
}

public static function forFindColumnHaveMultipleColumns()
{
return new static(lang('Database.forFindColumnHaveMultipleColumns'));
44 changes: 22 additions & 22 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
@@ -61,33 +61,33 @@ class Builder extends BaseBuilder
/**
* ORDER BY
*
* @param string $orderby
* @param string $orderBy
* @param string $direction ASC, DESC or RANDOM
* @param boolean $escape
*
* @return BaseBuilder
*/
public function orderBy($orderby, $direction = '', $escape = null)
public function orderBy(string $orderBy, string $direction = '', bool $escape = null)
{
$direction = strtoupper(trim($direction));
if ($direction === 'RANDOM')
{
if (! is_float($orderby) && ctype_digit((string) $orderby))
if (! is_float($orderBy) && ctype_digit((string) $orderBy))
{
$orderby = (float) ($orderby > 1 ? "0.{$orderby}" : $orderby);
$orderBy = (float) ($orderBy > 1 ? "0.{$orderBy}" : $orderBy);
}

if (is_float($orderby))
if (is_float($orderBy))
{
$this->db->simpleQuery("SET SEED {$orderby}");
$this->db->simpleQuery("SET SEED {$orderBy}");
}

$orderby = $this->randomKeyword[0];
$orderBy = $this->randomKeyword[0];
$direction = '';
$escape = false;
}

return parent::orderBy($orderby, $direction, $escape);
return parent::orderBy($orderBy, $direction, $escape);
}

//--------------------------------------------------------------------
@@ -145,7 +145,7 @@ public function decrement(string $column, int $value = 1)
* @throws DatabaseException
* @internal param true $bool returns the generated SQL, false executes the query.
*/
public function replace($set = null, $returnSQL = false)
public function replace(array $set = null, bool $returnSQL = false)
{
if ($set !== null)
{
@@ -200,8 +200,8 @@ public function replace($set = null, $returnSQL = false)
*
* Compiles a delete string and runs the query
*
* @param string $where
* @param null $limit
* @param mixed $where
* @param integer $limit
* @param boolean $reset_data
* @param boolean $returnSQL
*
@@ -211,7 +211,7 @@ public function replace($set = null, $returnSQL = false)
* @internal param the $mixed limit clause
* @internal param $bool
*/
public function delete($where = '', $limit = null, $reset_data = true, $returnSQL = false)
public function delete($where = '', int $limit = null, bool $reset_data = true, bool $returnSQL = false)
{
if (! empty($limit) || ! empty($this->QBLimit))
{
@@ -232,7 +232,7 @@ public function delete($where = '', $limit = null, $reset_data = true, $returnSQ
*
* @return string
*/
protected function _limit($sql)
protected function _limit(string $sql): string
{
return $sql . ' LIMIT ' . $this->QBLimit . ($this->QBOffset ? " OFFSET {$this->QBOffset}" : '');
}
@@ -252,7 +252,7 @@ protected function _limit($sql)
* @internal param the $string table name
* @internal param the $array update data
*/
protected function _update($table, $values)
protected function _update(string $table, array $values): string
{
if (! empty($this->QBLimit))
{
@@ -276,7 +276,7 @@ protected function _update($table, $values)
*
* @return string
*/
protected function _updateBatch($table, $values, $index)
protected function _updateBatch(string $table, array $values, string $index): string
{
$ids = [];
foreach ($values as $key => $val)
@@ -316,7 +316,7 @@ protected function _updateBatch($table, $values, $index)
*
* @return string
*/
protected function _delete($table)
protected function _delete(string $table): string
{
$this->QBLimit = false;
return parent::_delete($table);
@@ -336,7 +336,7 @@ protected function _delete($table)
*
* @return string
*/
protected function _truncate($table)
protected function _truncate(string $table): string
{
return 'TRUNCATE ' . $table . ' RESTART IDENTITY';
}
@@ -351,11 +351,11 @@ protected function _truncate($table)
*
* @see https://www.postgresql.org/docs/9.2/static/functions-matching.html
*
* @param string|null $prefix
* @param string $column
* @param string|null $not
* @param string $bind
* @param boolean $insensitiveSearch
* @param string $prefix
* @param string $column
* @param string $not
* @param string $bind
* @param boolean $insensitiveSearch
*
* @return string $like_statement
*/
4 changes: 2 additions & 2 deletions system/Database/SQLite3/Builder.php
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ class Builder extends BaseBuilder
*
* @return string
*/
protected function _replace($table, $keys, $values)
protected function _replace(string $table, array $keys, array $values): string
{
return 'INSERT OR ' . parent::_replace($table, $keys, $values);
}
@@ -100,7 +100,7 @@ protected function _replace($table, $keys, $values)
* @param string $table
* @return string
*/
protected function _truncate($table)
protected function _truncate(string $table): string
{
return 'DELETE FROM ' . $table;
}
1 change: 1 addition & 0 deletions system/Language/en/Database.php
Original file line number Diff line number Diff line change
@@ -26,5 +26,6 @@
'featureUnavailable' => 'This feature is not available for the database you are using.',
'tableNotFound' => 'Table `{0}` was not found in the current database.',
'noPrimaryKey' => '`{0}` model class does not specify a Primary Key.',
'forEmptyInputGiven' => 'Empty statement is given for the field `{0}`',
'forFindColumnHaveMultipleColumns' => 'Only single column allowed in Column name.',
];
3 changes: 1 addition & 2 deletions tests/system/Database/Builder/EmptyTest.php
Original file line number Diff line number Diff line change
@@ -20,8 +20,7 @@ protected function setUp()

public function testEmptyWithNoTable()
{
$builder = new BaseBuilder('jobs', $this->db);
$builder->returnDeleteSQL = true;
$builder = new BaseBuilder('jobs', $this->db);

$answer = $builder->emptyTable(true);

18 changes: 16 additions & 2 deletions tests/system/Database/Builder/SelectTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\Database\Builder;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\Exceptions\DataException;
use Tests\Support\Database\MockConnection;

class SelectTest extends \CIUnitTestCase
@@ -202,8 +203,8 @@ public function testSelectMinThrowsExceptionOnEmptyValue()
{
$builder = new BaseBuilder('invoices', $this->db);

$this->expectException('\CodeIgniter\Database\Exceptions\DatabaseException');
$this->expectExceptionMessage('The query you submitted is not valid.');
$this->expectException(DataException::class);
$this->expectExceptionMessage('Empty statement is given for the field `Select`');

$builder->selectSum('');
}
@@ -222,4 +223,17 @@ public function testSelectMaxWithDotNameAndNoAlias()
}

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

public function testSelectMinThrowsExceptionOnMultipleColumn()
{
$builder = new BaseBuilder('users', $this->db);

$this->expectException(DataException::class);
$this->expectExceptionMessage('You must provide a valid column name not separated by comma.');

$builder->selectSum('name,role');
}

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

}

0 comments on commit 1c87d56

Please sign in to comment.