All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jn.sqlhelper.dialect.internal.limit.SkipLimitHandler Maven / Gradle / Ivy

package com.jn.sqlhelper.dialect.internal.limit;

import com.jn.sqlhelper.dialect.pagination.RowSelection;

import java.util.Locale;

public class SkipLimitHandler extends AbstractLimitHandler {
    private String firstNKeyword = "FIRST"; // may be: "LIMIT", "FIRST" or "TOP"

    public SkipLimitHandler() {
        this("FIRST");
    }

    public SkipLimitHandler(String firstNKeyword) {
        this.firstNKeyword = firstNKeyword;
    }

    @Override
    public String processSql(String sql,boolean isSubquery, boolean useLimitVariable,RowSelection selection) {
        final boolean hasOffset = LimitHelper.hasFirstRow(selection);
        String sqlLimit = "";
        if (hasOffset) {
            if (useLimitVariable && getDialect().isUseLimitInVariableMode(isSubquery)) {
                sqlLimit = " SKIP ? ";
            } else {
                sqlLimit = " SKIP " + selection.getOffset();
            }
        }
        if (useLimitVariable && getDialect().isUseLimitInVariableMode(isSubquery)) {
            sqlLimit = sqlLimit + " " + firstNKeyword + " ? ";
        } else {
            sqlLimit = sqlLimit + " " + firstNKeyword + " " + getMaxOrLimit(selection) + " ";
        }

        return new StringBuilder(sql.length() + 10)
                .append(sql)
                .insert(sql.toLowerCase(Locale.ROOT).indexOf("select") + 6, sqlLimit).toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy