-
-
Notifications
You must be signed in to change notification settings - Fork 110
Sqlsrv driver: support for limit and offset on MS SQL server. #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
What about older MSSQL? Sqlsrv driver supports MSSQL since version 2005. |
|
On 2005 and 2008 versions this SQL syntax cause an error, so it would be good solution to check database version, like this: $info = $this->connection->query('SELECT SERVERPROPERTY(\'ProductVersion\') AS version')->fetch();
if ($info->version < 11) { // 11 == SQL Server 2012
throw new Nette\NotSupportedException('Offset is not supported by this database.');
}But that doesn't work for me, I'm getting NULL. |
|
Hm, OK, when selecting server version via sql query, it's needed to be manually cast to string. So this one 43a7fc1 will preserve current behaviour on older MS SQL versions. |
0fc7ac2 to
7194522
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about checking version only once, like this:
public function applyLimit(& $sql, $limit, $offset)
{
static $ver = null;
if (is_null($ver)) {
$info = $this->connection->query("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar) AS [ver]")->fetch();
$ver = (int) $info->ver;
}
if ($ver < 11) { // 11 == SQL Server 2012I'd really like to see this merged. Perhaps squashing would help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is possible to use $connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION) like in MySqlDriver?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is.
/** @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);
}
...
public function applyLimit(& $sql, $limit, $offset)
{
if (version_compare($this->version, 11, '<')) { // < SQL Server 2012|
@jakubskrz Could you please incorporate all the mentioned changes and squash all to one commit so it could be merged? |
|
Hi, sorry for delay, I hadn't much time last week. I've updated my PR with suggested changes and also with changes from this commit b3b6e9b so there is no merge conflict with current head. Looking at #99, I've also removed checking for But I can't test it right now, as I can't access my dev SQL server (broken VPN :/). |
|
Merged, thx 15ccd8b |
Since MS SQL 2012 there is equivalent to
LIMIT OFFSETclause in T-SQL.But it requires to use
ORDER BYclause in query, see official docsTo consider:
ORDER BYinstead of exceptionNotSupportedExceptionwhen connected to older MSSQL Server version