package it.mice.voila.runtime.hibernate.dialect;

import org.hibernate.dialect.SQLServerDialect;

public class SQLServer2005Dialect extends SQLServerDialect {

	public boolean supportsLimitOffset() {
		return true;
	}

	public String getLimitString(String querySelect, int offset, int limit) {
		String toppedQuerySelect = super.getLimitString(querySelect, 0, limit);

		StringBuffer result = new StringBuffer();
		result.append("WITH query AS (SELECT inner_query.*, Row_number() OVER (ORDER BY CURRENT_TIMESTAMP) AS __hibernate_row_nr__ FROM (");
		result.append(toppedQuerySelect);
		result.append(") inner_query) SELECT * FROM query WHERE __hibernate_row_nr__ > " + offset);
		
		return result.toString();
	}
}
