001package io.ebean.config.dbplatform.sqlanywhere;
002
003import io.ebean.config.dbplatform.SqlLimitRequest;
004import io.ebean.config.dbplatform.SqlLimitResponse;
005import io.ebean.config.dbplatform.SqlLimiter;
006
007/**
008 * Use top xx and start at xx to limit sql results.
009 */
010public class SqlAnywhereLimiter implements SqlLimiter {
011
012  @Override
013  public SqlLimitResponse limit(SqlLimitRequest request) {
014
015    String dbSql = request.getDbSql();
016
017    StringBuilder sb = new StringBuilder(60 + dbSql.length());
018
019    int firstRow = request.getFirstRow();
020    int maxRows = request.getMaxRows();
021
022    /*
023     * SELECT TOP xx START AT xx ... FROM ...
024     */
025    sb.append("select ");
026    if (request.isDistinct()) {
027      sb.append("distinct ");
028    }
029    if (maxRows > 0) {
030      sb.append("top ").append(maxRows).append(" ");
031    }
032    if (firstRow > 0) {
033      sb.append("start at ").append(firstRow + 1).append(" ");
034    }
035    sb.append(dbSql);
036
037    String sql = request.getDbPlatform().completeSql(sb.toString(), request.getOrmQuery());
038    return new SqlLimitResponse(sql);
039  }
040
041}