Skip to content

Commit

Permalink
Merge pull request #104 from Sevavietl/master
Browse files Browse the repository at this point in the history
Do not reset placeholders in set writers
  • Loading branch information
nilportugues authored Feb 5, 2019
2 parents 277e87a + 2056f9e commit 8991871
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Builder/Syntax/AbstractSetWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function abstractWrite(QueryPartInterface $setClass, $setOperation, $g
$selects = [];

foreach ($setClass->$setOperation() as $select) {
$selects[] = $this->writer->write($select);
$selects[] = $this->writer->write($select, false);
}

return \implode("\n".$glue."\n", $selects);
Expand Down
39 changes: 37 additions & 2 deletions tests/Builder/Syntax/IntersectWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
Expand Down Expand Up @@ -48,7 +47,7 @@ public function tearDown()
/**
* @test
*/
public function itShouldWriteIntersects()
public function itShouldWriteIntersect()
{
$intersect = new Intersect();

Expand Down Expand Up @@ -80,4 +79,40 @@ public function itShouldWriteIntersectFromGenericBuilder()
SQL;
$this->assertEquals($expected, $this->writer->write($intersect));
}

/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();

$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();

$union = (new Intersect())
->add($select1)
->add($select2);

$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
INTERSECT
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;

$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];

$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}
39 changes: 37 additions & 2 deletions tests/Builder/Syntax/UnionAllWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
Expand Down Expand Up @@ -48,7 +47,7 @@ public function tearDown()
/**
* @test
*/
public function itShouldWriteIntersects()
public function itShouldWriteUnionAll()
{
$union = new UnionAll();

Expand Down Expand Up @@ -80,4 +79,40 @@ public function itShouldWriteUnionAllFromGenericBuilder()
SQL;
$this->assertEquals($expected, $this->writer->write($unionAll));
}

/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();

$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();

$union = (new UnionAll())
->add($select1)
->add($select2);

$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
UNION ALL
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;

$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];

$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}
41 changes: 38 additions & 3 deletions tests/Builder/Syntax/UnionWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
Expand Down Expand Up @@ -48,7 +47,7 @@ public function tearDown()
/**
* @test
*/
public function itShouldWriteIntersects()
public function itShouldWriteUnion()
{
$union = new Union();

Expand All @@ -66,7 +65,7 @@ public function itShouldWriteIntersects()
/**
* @test
*/
public function itShouldWriteUnionAllFromGenericBuilder()
public function itShouldWriteUnionFromGenericBuilder()
{
$unionAll = $this->writer->union();

Expand All @@ -80,4 +79,40 @@ public function itShouldWriteUnionAllFromGenericBuilder()
SQL;
$this->assertEquals($expected, $this->writer->write($unionAll));
}

/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();

$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();

$union = (new Union())
->add($select1)
->add($select2);

$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
UNION
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;

$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];

$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}

0 comments on commit 8991871

Please sign in to comment.