Skip to content
Closed
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
24 changes: 17 additions & 7 deletions src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class SqlsrvDriver extends Nette\Object implements Nette\Database\ISupplementalD
/** @var Nette\Database\Connection */
private $connection;

/** @var string */
private $version;

public function __construct(Nette\Database\Connection $connection, array $options)
{
$this->connection = $connection;
$this->version = $connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
}


Expand Down Expand Up @@ -88,15 +91,22 @@ public function formatLike($value, $pos)
*/
public function applyLimit(& $sql, $limit, $offset)
{
if ($limit >= 0) {
$sql = preg_replace('#^\s*(SELECT(\s+DISTINCT|\s+ALL)?|UPDATE|DELETE)#i', '$0 TOP ' . (int) $limit, $sql, 1, $count);
if (!$count) {
throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
if (version_compare($this->version, 11, '<')) { // 11 == SQL Server 2012
if ($limit >= 0) {
$sql = preg_replace('#^\s*(SELECT(\s+DISTINCT|\s+ALL)?|UPDATE|DELETE)#i', '$0 TOP ' . (int) $limit, $sql, 1, $count);
if (!$count) {
throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
}
}
}

if ($offset > 0) {
throw new Nette\NotSupportedException('Offset is not supported by this database.');
if ($offset > 0) {
throw new Nette\NotSupportedException('Offset is not supported by this database.');
}
} else {
if ($limit > 0 || $offset >= 0) {
$sql .= ' OFFSET ' . (int) $offset . ' ROWS '
. 'FETCH NEXT ' . (int) $limit . ' ROWS ONLY';
}
}
}

Expand Down