You have 3 options
- Lock laravel/framework to version 8.74. This version should work with sql server 2008
- Implement the sql code from this version yourself by overwriting the driver (I did this before we upgraded)
- Upgrade the 2008 to 2012 or newer
Hello.
I have a Laravel 9 application that must connect to 2 different sql servers, one is version 2012 and the other is 2008.
When I use the pagination in 2012 it works fine but in the 2008 version I get the following error
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'offset'.
The reason is that Eloquent uses OFFSET ... FETCH for paginating results in SQL Server which unfortunatelly does not work for 2008 (it was introduced at a later version).
How can I tackle this? Is there a way to let eloquent know how to paginate based on the server version? Maybe create my own paginator class and work from there?
What do you suggest?
@achatzi I dont know of any examples. I personally read the source code and implemented it from scratch. I can give you my old now deleted code
in a service provider
public function register()
{
Connection::resolverFor('sqlsrv', function ($connection, $database, $prefix, $config) {
return new SqlServerConnectionPolyfill($connection, $database, $prefix, $config);
});
}
First polyfill
<?php
namespace App\Database;
use Illuminate\Database\SqlServerConnection;
class SqlServerConnectionPolyfill extends SqlServerConnection
{
/**
* Get the default query grammar instance.
* @return \Illuminate\Database\Query\Grammars\SqlServerGrammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new SqlServerGrammarPolyfill);
}
}
and second
<?php
namespace App\Database;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Grammars\SqlServerGrammar;
class SqlServerGrammarPolyfill extends SqlServerGrammar
{
/**
* Compile a select query into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
*
* @return string
*/
public function compileSelect(Builder $query)
{
if (!$query->offset) {
return parent::compileSelect($query);
}
// If an offset is present on the query, we will need to wrap the query in
// a big "ANSI" offset syntax block. This is very nasty compared to the
// other database systems but is necessary for implementing features.
if (is_null($query->columns)) {
$query->columns = ['*'];
}
return $this->compileAnsiOffset(
$query, $this->compileComponents($query)
);
}
}
Please or to participate in this conversation.