001package io.ebean.config.dbplatform.oracle;
002
003import io.ebean.config.dbplatform.BasicSqlLimiter;
004
005/**
006 * Row limiter for Oracle 9,10,11 using rownum.
007 */
008public class OracleRownumBasicLimiter implements BasicSqlLimiter {
009
010  @Override
011  public String limit(String dbSql, int firstRow, int maxRows) {
012    if (firstRow < 1 && maxRows < 1) {
013      return dbSql;
014    }
015    StringBuilder sb = new StringBuilder(60 + dbSql.length());
016    int lastRow = maxRows;
017    if (lastRow > 0) {
018      lastRow += firstRow;
019    }
020    sb.append("select * from (select ");
021    if (maxRows > 0) {
022      sb.append("/*+ FIRST_ROWS(").append(maxRows).append(") */ ");
023    }
024    sb.append("a.*, rownum rn_ from (");
025    sb.append(dbSql).append(") a ");
026    if (lastRow > 0) {
027      sb.append(" where rownum <= ").append(lastRow);
028    }
029    sb.append(") ");
030    if (firstRow > 0) {
031      sb.append(" where rn_ > ").append(firstRow);
032    }
033    return sb.toString();
034  }
035}