package com.github.pagehelper.parser.impl;

import com.github.pagehelper.Page;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

import java.util.Map;

/**
 * Take note that at least one column
 * needs to be defined for ORDER BY
 * in oder for OFFSET .. ROWS to work
 */
public class SqlServer2012Dialect extends AbstractParser {

    @Override
    public String getPageSql(String sql) {
        StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
        sqlBuilder.append(sql);
        sqlBuilder.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
        return sqlBuilder.toString();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map setPageParameter(MappedStatement ms, Object parameterObject, BoundSql boundSql, Page page) {

        Map paramMap = super.setPageParameter(ms, parameterObject, boundSql, page);

        // OFFSET (@PageNumber-1)*@RowsPerPage ROWS
        /**
        log.debug( "page.getPageNum() = {}, page.getEndRow() = {}, page.getStartRow = {}",
            page.getPageNum(),
            page.getEndRow(),
            page.getStartRow()
        );
        **/

        paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow() );
        paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize() );
        return paramMap;
    }

}

Support OFFSET ... ROWS in Sql Server 2012
Support OFFSET ... ROWS in Sql Server 2012
