class MSSQLDatabaseAdapter implements DatabaseAdapter {

    @Override
    public String getSelectStatement(String tableName, String columnNames, String whereClause, String orderByClause, Long limit, Long offset) {
        if (StringUtils.isEmpty(tableName)) {
            throw new IllegalArgumentException("Table name cannot be null or empty");
        }
        final StringBuilder query = new StringBuilder("SELECT ");

        //If this is a limit query and not a paging query then use TOP in MS SQL
        if (limit != null && offset == null){
            query.append("TOP ");
            query.append(limit);
            query.append(" ");
        }

        if (StringUtils.isEmpty(columnNames) || columnNames.trim().equals("*")) {
            query.append("*");
        } else {
            query.append(columnNames);
        }
        query.append(" FROM ");
        query.append(tableName);

        if (!StringUtils.isEmpty(whereClause)) {
            query.append(" WHERE ");
            query.append(whereClause);
        }
        if (!StringUtils.isEmpty(orderByClause)) {
            query.append(" ORDER BY ");
            query.append(orderByClause);
        }
        if (offset != null && limit != null && limit > 0) {
            if (StringUtils.isEmpty(orderByClause)) {
                throw new IllegalArgumentException("Order by clause cannot be null or empty when using row paging");
            }

            query.append(" OFFSET ");
            query.append(offset);
            query.append(" ROWS");

            query.append(" FETCH NEXT ");
            query.append(limit);
            query.append(" ROWS ONLY");
        }

        return query.toString();
    }

}