Skip to content
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

Offset support for MSSQL / SQLSRV #350

Open
henryruhs opened this issue Jun 27, 2019 · 0 comments
Open

Offset support for MSSQL / SQLSRV #350

henryruhs opened this issue Jun 27, 2019 · 0 comments

Comments

@henryruhs
Copy link

henryruhs commented Jun 27, 2019

MSSQL fails with an SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server] syntax error when using offset()...

  1. TOP and OFFSET are not allowed to be used in one SELECT
  2. OFFSET 10 ROWS with FETCH NEXT 10 ROWS ONLY should be used
  3. Therefore _build_limit() and _build_offset() need some rework

This are my approaches but I think something is missing to work properly:

protected static function _detect_limit_clause_style($connection_name) {
    switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) {
        case 'dblib':
            return ORM::LIMIT_STYLE_TOP_N;
        case 'sqlsrv':
        case 'mssql':
            return ORM::LIMIT_STYLE_FETCH;
        default:
            return ORM::LIMIT_STYLE_LIMIT;
    }
}
protected function _build_limit() {
    $fragment = '';
    if (!is_null($this->_limit)) {
        if (self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_LIMIT) {
            if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') {
                $fragment = 'ROWS';
            } else {
                $fragment = 'LIMIT';
            }
            $fragment .= " {$this->_limit}";
        } else if (self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_FETCH) {
            $fragment = 'FETCH NEXT ' . $this->_limit . ' ROWS ONLY';
        }
    }
    return $fragment;
}
protected function _build_offset() {
    if (!is_null($this->_offset)) {
        $clause = 'OFFSET';
        if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') {
            $clause = 'TO';
        }
        if (self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_FETCH) {
            return $clause . ' ' . $this->_offset . '  ROWS';
        }
        return $clause . ' ' . $this->_offset;
    }
    return '';
}

Reference:
http://www.sqlservertutorial.net/sql-server-basics/sql-server-offset-fetch/
https://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server

@henryruhs henryruhs changed the title Limit and Offset support for MSSQL Limit and Offset support for MSSQL / SQLSRV Jun 27, 2019
@henryruhs henryruhs changed the title Limit and Offset support for MSSQL / SQLSRV Offset support for MSSQL / SQLSRV Jun 27, 2019
Repository owner deleted a comment from Github743 Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants