Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Sql;

use InvalidArgumentException;
use ipl\Sql\Adapter\Mssql;
use ipl\Sql\Contract\Adapter;

use function ipl\Stdlib\get_php_type;
Expand Down Expand Up @@ -106,6 +107,16 @@ public function assembleInsert(Insert $insert)
*/
public function assembleSelect(Select $select, array &$values = [])
{
$select = clone $select;

if (
$this->adapter instanceof Mssql
&& ($select->hasLimit() || $select->hasOffset())
&& ! $select->hasOrderBy()
) {
$select->orderBy(1);
}

$sql = array_filter([
$this->buildWith($select->getWith(), $values),
$this->buildSelect($select->getColumns(), $select->getDistinct(), $values),
Expand Down Expand Up @@ -622,12 +633,24 @@ public function buildLimitOffset($limit = null, $offset = null)
{
$sql = [];

if ($limit !== null) {
$sql[] = "LIMIT $limit";
}
if ($this->adapter instanceof Mssql) {
if ($offset !== null || $limit !== null) {
// If offset is null, sprintf will convert it to 0
$sql[] = sprintf('OFFSET %d ROWS', $offset);
}

if ($offset !== null) {
$sql[] = "OFFSET $offset";
if ($limit !== null) {
// FETCH FIRST n ROWS ONLY for OFFSET 0 would be an alternative here
$sql[] = "FETCH NEXT $limit ROWS ONLY";
}
} else {
if ($limit !== null) {
$sql[] = "LIMIT $limit";
}

if ($offset !== null) {
$sql[] = "OFFSET $offset";
}
}

return implode($this->separator, $sql);
Expand Down
66 changes: 66 additions & 0 deletions tests/Mssql/SelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace ipl\Tests\Sql\Mssql;

use ipl\Sql\Adapter\Mssql;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;

class SelectTest extends \PHPUnit\Framework\TestCase
{
/**
* The SELECT query to test
*
* @var Select
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;

public function setUp()
{
$this->query = new Select();
$this->queryBuilder = new QueryBuilder(new Mssql());
}

public function testLimitOffset()
{
$this->query->columns('a')->from('b')->orderBy('a')->limit(10)->offset(20);

$this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY a OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY');
}

public function testLimitWithoutOffset()
{
$this->query->columns('a')->from('b')->orderBy('a')->limit(10);

$this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY a OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY');
}

public function testOffsetWithoutLimit()
{
$this->query->columns('a')->from('b')->orderBy('a')->offset(20);

$this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY a OFFSET 20 ROWS');
}

public function testAutomaticallyFixesLimitWithoutOrder()
{
$this->query->columns('a')->from('b')->limit(10)->offset(30);

$this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY 1 OFFSET 30 ROWS FETCH NEXT 10 ROWS ONLY');
}

protected function assertCorrectStatementAndValues($statement, array $values = [])
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
27 changes: 27 additions & 0 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,33 @@ public function testOrderByWithSelect()
$this->assertCorrectStatementAndValues('ORDER BY (SELECT COUNT(*) FROM table2 WHERE active = ?) DESC', [1]);
}

public function testLimitOffset()
{
$this->query = (new Select())->columns(['a'])->from('b')->limit(4)->offset(1);
$this->assertCorrectStatementAndValues(
'SELECT a FROM b LIMIT 4 OFFSET 1',
[]
);
}

public function testLimitWithoutOffset()
{
$this->query = (new Select())->columns(['a'])->from('b')->limit(4);
$this->assertCorrectStatementAndValues(
'SELECT a FROM b LIMIT 4',
[]
);
}

public function testOffsetWithoutLimit()
{
$this->query = (new Select())->columns(['a'])->from('b')->offset(1);
$this->assertCorrectStatementAndValues(
'SELECT a FROM b OFFSET 1',
[]
);
}

public function testUnion()
{
$unionQuery = (new Select())
Expand Down