001package io.ebean.config.dbplatform.sqlserver;
002
003import io.ebean.config.dbplatform.BasicSqlLimiter;
004
005/**
006 * SQL Server 2012 style limiter for raw sql.
007 */
008public class SqlServerBasicSqlLimiter implements BasicSqlLimiter {
009
010  @Override
011  public String limit(String dbSql, int firstRow, int maxRows) {
012
013    StringBuilder sb = new StringBuilder(50 + dbSql.length());
014    sb.append(dbSql);
015    if (!dbSql.toLowerCase().contains("order by")) {
016      sb.append(" order by 1");
017    }
018    sb.append(" ").append("offset");
019    sb.append(" ").append(firstRow).append(" rows");
020    if (maxRows > 0) {
021      sb.append(" fetch next ").append(maxRows).append(" rows only");
022    }
023    return sb.toString();
024  }
025
026}