<!-- https://mvnrepository.com/artifact/cn.zidot/sprite-utils -->
<dependency>
<groupId>cn.zidot</groupId>
<artifactId>sprite-utils</artifactId>
<version>1.2.4</version>
</dependency>
public static String DATABASE_TYPE_SQLSERVER = "sqlserver";
public static String DATABASE_TYPE_SQLSERVER2012 = "sqlserver2012";
public static String DATABASE_TYPE_MYSQL = "mysql";
public static String DATABASE_TYPE_ORACLE = "oracle";
public static String DATABASE_TYPE_PGSQL = "pgsql";
public static String makeOffset(String sql, int offset, int limit, String dbtype, String orderBy) {
if(DATABASE_TYPE_MYSQL.equals(dbtype)) {
String temp = "{} LIMIT {},{}";
return UtilString.place(temp, sql, offset, limit);
}else if(DATABASE_TYPE_SQLSERVER2012.equals(dbtype)) {
String temp = "{} OFFSET {} ROW FETCH NEXT {} ROWS only ";
return UtilString.place(temp, sql, offset, limit);
}else if(DATABASE_TYPE_PGSQL.equals(dbtype)) {
String temp = "{} LIMIT {} OFFSET {} ";
return UtilString.place(temp, sql, limit, offset);
}
if(DATABASE_TYPE_SQLSERVER.equals(dbtype)) {
if(UtilString.isBlank(orderBy)){
throw new IllegalArgumentException(" orderBy must, if dbtype is : "+dbtype);
}
String temp = "SELECT TOP {} _rn.* from (SELECT row_number() over({}) as _rnum,* from({}) as _rf) as _rn WHERE _rnum>{}";
return UtilString.place(temp, offset,orderBy, sql, offset);
}else if(DATABASE_TYPE_ORACLE.equals(dbtype)) {
String temp = "SELECT * FROM(SELECT a.*,ROWNUM _rnum FROM({}) _rn WHERE ROWNUM<=({})) WHERE _rnum>{}";
return UtilString.place(temp,sql, offset+limit, offset);
}
throw new IllegalArgumentException(" un support dbtype : "+dbtype);
}