sqlserver2012 offset
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.dialect.pagination; import java.sql.PreparedStatement;
import java.sql.SQLException; import org.hibernate.engine.spi.RowSelection; /**
* LIMIT clause handler compatible with SQL Server 2012 and later.
*
* @author Chris Cranford
*/
public class SQLServer2012LimitHandler extends SQLServer2005LimitHandler {
// determines whether the limit handler used offset/fetch or 2005 behavior.
private boolean usedOffsetFetch; public SQLServer2012LimitHandler() { } @Override
public boolean supportsLimit() {
return true;
} @Override
public boolean supportsVariableLimit() {
return true;
} @Override
public String processSql(String sql, RowSelection selection) {
// SQLServer mandates the following rules to use OFFSET/LIMIT
// * An 'ORDER BY' is required
// * The 'OFFSET ...' clause is mandatory, cannot use 'FETCH ...' by itself.
// * The 'TOP' clause isn't permitted with LIMIT/OFFSET.
if ( hasOrderBy( sql ) ) {
if ( !LimitHelper.useLimit( this, selection ) ) {
return sql;
}
return applyOffsetFetch( selection, sql, getInsertPosition( sql ) );
}
return super.processSql( sql, selection );
} @Override
public boolean useMaxForLimit() {
// when using the offset fetch clause, the max value is passed as-is.
// SQLServer2005LimitHandler uses start + max values.
return usedOffsetFetch ? false : super.useMaxForLimit();
} @Override
public int convertToFirstRowValue(int zeroBasedFirstResult) {
// When using the offset/fetch clause, the first row is passed as-is
// SQLServer2005LimitHandler uses zeroBasedFirstResult + 1
if ( usedOffsetFetch ) {
return zeroBasedFirstResult;
}
return super.convertToFirstRowValue( zeroBasedFirstResult );
} @Override
public int bindLimitParametersAtEndOfQuery(RowSelection selection, PreparedStatement statement, int index)
throws SQLException {
if ( usedOffsetFetch && !LimitHelper.hasFirstRow( selection ) ) {
// apply just the max value when offset fetch applied
statement.setInt( index, getMaxOrLimit( selection ) );
return 1;
}
return super.bindLimitParametersAtEndOfQuery( selection, statement, index );
} private String getOffsetFetch(RowSelection selection) {
if ( !LimitHelper.hasFirstRow( selection ) ) {
return " offset 0 rows fetch next ? rows only";
}
return " offset ? rows fetch next ? rows only";
} private int getInsertPosition(String sql) {
int position = sql.length() - 1;
for ( ; position > 0; --position ) {
char ch = sql.charAt( position );
if ( ch != ';' && ch != ' ' && ch != '\r' && ch != '\n' ) {
break;
}
}
return position + 1;
} private String applyOffsetFetch(RowSelection selection, String sql, int position) {
usedOffsetFetch = true; StringBuilder sb = new StringBuilder();
sb.append( sql.substring( 0, position ) );
sb.append( getOffsetFetch( selection ) );
if ( position > sql.length() ) {
sb.append( sql.substring( position - 1 ) );
} return sb.toString();
} private boolean hasOrderBy(String sql) {
int depth = 0; String lowerCaseSQL = sql.toLowerCase(); for ( int i = lowerCaseSQL.length() - 1; i >= 0; --i ) {
char ch = lowerCaseSQL.charAt( i );
if ( ch == '(' ) {
depth++;
}
else if ( ch == ')' ) {
depth--;
}
if ( depth == 0 ) {
if ( lowerCaseSQL.startsWith( "order by ", i ) ) {
return true;
}
}
}
return false;
}
}
sqlserver2012 offset的更多相关文章
- sqlserver2012 offset分页
select ID,UserName from user order by ID OFFSET (10 * (50-1)) ROW FETCH NEXT 10 rows only
- SQLServer2012 分页语句执行分析
上一篇文章提到了,SQLServer2012在使用Offset,Fetch语句分页时,获取了大量不需要的数据,导致查询效率低的问题. 现在让我们来看看,究竟是什么导致SQLServer不能按需取数呢? ...
- SQLSERVER2012的分页新功能
SQLSERVER2012的分页新功能 简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server ...
- SQLServer2012 (非)聚集索引存储探究
SQLServer2012 (非)聚集索引存储探究 Author:zfive5(zidong) Email:zfive5@163.com 引子 因为写了前一篇文字<SQLServer2012 表 ...
- SQLSERVER2012里的扩展事件初尝试(下)
SQLSERVER2012里的扩展事件初尝试(下) SQLSERVER2012里的扩展事件初尝试(上) 我们继续文章扩展事件在Denali CTP3里的新UI(二)里的这个实验 脚本文件下载:http ...
- .NET Core使用EF分页查询数据报错:OFFSET语法错误问题
在Asp.Net Core MVC项目中使用EF分页查询数据时遇到一个比较麻烦的问题,系统会报如下错误: 分页查询代码: ) * condition.PageSize).Take(condition. ...
- SQLServer2012 表IAM存储结构探究
SQLServer2012 表IAM存储结构探究 Author:zfive5(zidong) Email: zfive5@163.com 引子 国庆节期间,一直在翻阅<程序猿的自我修养-链接.装 ...
- 虚拟数字存储表——SQLServer2012可高用
窗口函数之虚拟数字辅助表 数字辅助表是一个整数序列,可以用它来完成多种不同的查询任务.数字表有很多任务,如生成日期和时间值序列,及分裂值列表.通常,建议在数据库中保存这样一个永久表,并填充尽可能多的数 ...
- Kafka 如何读取offset topic内容 (__consumer_offsets)
众所周知,由于Zookeeper并不适合大批量的频繁写入操作,新版Kafka已推荐将consumer的位移信息保存在Kafka内部的topic中,即__consumer_offsets topic,并 ...
随机推荐
- 书写优雅的shell脚本(插曲)- /proc
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- CSS counter计数器(content目录序号自动递增)详解
一.CSS计数器三角关系 CSS计数器只能跟content属性在一起的时候才有作用,而content属性貌似专门用在before/after伪元素上的.于是,就有了,“计数器↔伪元素↔content属 ...
- springmvc不进入Controller导致404
转自:https://blog.csdn.net/qq_36769100/article/details/71746449#1.%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE ...
- A - Two Substrings
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description You ar ...
- Redis高级
Redis高级 redis数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实 ...
- POJ2105【进制转化】
直接瞎写就可以水过.我记得STL有很多好的函数,哎.水过去补多校的题. //#include <bits/stdc++.h> #include<cstdio> #include ...
- 【水水水】678A - Johny Likes Numbers
#include<stdio.h> #include<iostream> #include<cstdio> #include<queue> #inclu ...
- redis 发布订阅实现异步实时发短信
redis 中发布和订阅可以实现消息的实时传输,这里我只是用它的事件驱动,当客户端发送了消息,服务器端立马可以接收指令处理相应的业务逻辑. 客户端 client.php <?php //发布 $ ...
- IT兄弟连 JavaWeb教程 经典面试题3
1.简述什么是重定向? 服务器向浏览器发送—个302状态码及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向地址发出请求. 2.简述什么是转发?怎么实 ...
- 浅谈单调栈 By cellur925
这位dalao的单调栈文章很棒!我写的是他的题单233. http://www.cnblogs.com/COLIN-LIGHTNING/p/8474668.html 一.单调栈的一般写法 ;i< ...