Skip to content

Commit

Permalink
fix: wrong from is provided after reset query
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 22, 2021
1 parent de8d7f4 commit 3e5e23c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
11 changes: 8 additions & 3 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ public function __construct($tableName, ConnectionInterface &$db, array $options
/** @var BaseConnection $db */
$this->db = $db;

$this->tableName = $tableName;
if (is_string($tableName))
{
$this->tableName = $tableName;
}

$this->from($tableName);

if (! empty($options))
Expand Down Expand Up @@ -3377,6 +3381,7 @@ protected function resetSelect()
{
$this->resetRun([
'QBSelect' => [],
'QBFrom' => [],
'QBJoin' => [],
'QBWhere' => [],
'QBGroupBy' => [],
Expand All @@ -3394,9 +3399,9 @@ protected function resetSelect()
}

// Reset QBFrom part
if (! empty($this->QBFrom))
if (is_string($this->tableName))
{
$this->from(array_shift($this->QBFrom), true);
$this->from($this->tableName, true);
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/system/Database/Builder/FromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ public function testFromReset()
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

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

$builder->from('(SELECT id, name FROM test_1) t', true);

$expectedSQL = 'SELECT * FROM (SELECT id, name FROM test_1) t';

$this->assertEquals(
$expectedSQL,
str_replace(
"\n",
' ',
$builder->getCompiledSelect()
)
);

return $builder;
}

/**
* @depends testFromSelectSubQuery
*/
public function testFromAfterSubQuery($builder)
{
$expectedSQL = 'SELECT * FROM "user"';

$this->assertEquals(
$expectedSQL,
str_replace(
"\n",
' ',
$builder->getCompiledSelect()
)
);
}

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

public function testFromWithMultipleTablesAsStringWithSQLSRV()
Expand Down

0 comments on commit 3e5e23c

Please sign in to comment.