|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.dialect.pagination; |
| 8 | + |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +import org.hibernate.LockMode; |
| 12 | +import org.hibernate.LockOptions; |
| 13 | +import org.hibernate.engine.spi.QueryParameters; |
| 14 | +import org.hibernate.engine.spi.RowSelection; |
| 15 | + |
| 16 | +/** |
| 17 | + * A {@link LimitHandler} for databases which support the |
| 18 | + * ANSI SQL standard syntax {@code FETCH FIRST m ROWS ONLY} |
| 19 | + * and {@code OFFSET n ROWS FETCH NEXT m ROWS ONLY}. |
| 20 | + * |
| 21 | + * @author Gavin King |
| 22 | + */ |
| 23 | +public class Oracle12LimitHandler extends AbstractLimitHandler { |
| 24 | + |
| 25 | + public boolean bindLimitParametersInReverseOrder; |
| 26 | + public boolean useMaxForLimit; |
| 27 | + |
| 28 | + public static final Oracle12LimitHandler INSTANCE = new Oracle12LimitHandler(); |
| 29 | + |
| 30 | + Oracle12LimitHandler() { |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String processSql(String sql, RowSelection selection) { |
| 35 | + final boolean hasFirstRow = LimitHelper.hasFirstRow( selection ); |
| 36 | + final boolean hasMaxRows = LimitHelper.hasMaxRows( selection ); |
| 37 | + |
| 38 | + if ( !hasMaxRows ) { |
| 39 | + return sql; |
| 40 | + } |
| 41 | + |
| 42 | + return processSql( sql, getForUpdateIndex( sql ), hasFirstRow ); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String processSql(String sql, QueryParameters queryParameters) { |
| 47 | + final RowSelection selection = queryParameters.getRowSelection(); |
| 48 | + |
| 49 | + final boolean hasFirstRow = LimitHelper.hasFirstRow( selection ); |
| 50 | + final boolean hasMaxRows = LimitHelper.hasMaxRows( selection ); |
| 51 | + |
| 52 | + if ( !hasMaxRows ) { |
| 53 | + return sql; |
| 54 | + } |
| 55 | + |
| 56 | + final LockOptions lockOptions = queryParameters.getLockOptions(); |
| 57 | + if ( lockOptions != null ) { |
| 58 | + final LockMode lockMode = lockOptions.getLockMode(); |
| 59 | + switch ( lockMode ) { |
| 60 | + case UPGRADE: |
| 61 | + case PESSIMISTIC_READ: |
| 62 | + case PESSIMISTIC_WRITE: |
| 63 | + case UPGRADE_NOWAIT: |
| 64 | + case FORCE: |
| 65 | + case PESSIMISTIC_FORCE_INCREMENT: |
| 66 | + case UPGRADE_SKIPLOCKED: |
| 67 | + return processSql( sql, selection ); |
| 68 | + default: |
| 69 | + return processSqlOffsetFetch( sql, hasFirstRow ); |
| 70 | + } |
| 71 | + } |
| 72 | + return processSqlOffsetFetch( sql, hasFirstRow ); |
| 73 | + } |
| 74 | + |
| 75 | + private String processSqlOffsetFetch(String sql, boolean hasFirstRow) { |
| 76 | + |
| 77 | + final int forUpdateLastIndex = getForUpdateIndex( sql ); |
| 78 | + |
| 79 | + if ( forUpdateLastIndex > -1 ) { |
| 80 | + return processSql( sql, forUpdateLastIndex, hasFirstRow ); |
| 81 | + } |
| 82 | + |
| 83 | + bindLimitParametersInReverseOrder = false; |
| 84 | + useMaxForLimit = false; |
| 85 | + |
| 86 | + sql = normalizeStatement( sql ); |
| 87 | + final int offsetFetchLength; |
| 88 | + final String offsetFetchString; |
| 89 | + if ( hasFirstRow ) { |
| 90 | + offsetFetchString = " offset ? rows fetch next ? rows only"; |
| 91 | + } |
| 92 | + else { |
| 93 | + offsetFetchString = " fetch first ? rows only"; |
| 94 | + } |
| 95 | + offsetFetchLength = sql.length() + offsetFetchString.length(); |
| 96 | + |
| 97 | + return new StringBuilder( offsetFetchLength ).append( sql ).append( offsetFetchString ).toString(); |
| 98 | + } |
| 99 | + |
| 100 | + private String processSql(String sql, int forUpdateIndex, boolean hasFirstRow) { |
| 101 | + bindLimitParametersInReverseOrder = true; |
| 102 | + useMaxForLimit = true; |
| 103 | + sql = normalizeStatement( sql ); |
| 104 | + |
| 105 | + String forUpdateClause = null; |
| 106 | + boolean isForUpdate = false; |
| 107 | + if ( forUpdateIndex > -1 ) { |
| 108 | + // save 'for update ...' and then remove it |
| 109 | + forUpdateClause = sql.substring( forUpdateIndex ); |
| 110 | + sql = sql.substring( 0, forUpdateIndex - 1 ); |
| 111 | + isForUpdate = true; |
| 112 | + } |
| 113 | + |
| 114 | + final StringBuilder pagingSelect; |
| 115 | + |
| 116 | + final int forUpdateClauseLength; |
| 117 | + if ( forUpdateClause == null ) { |
| 118 | + forUpdateClauseLength = 0; |
| 119 | + } |
| 120 | + else { |
| 121 | + forUpdateClauseLength = forUpdateClause.length() + 1; |
| 122 | + } |
| 123 | + |
| 124 | + if ( hasFirstRow ) { |
| 125 | + pagingSelect = new StringBuilder( sql.length() + forUpdateClauseLength + 98 ); |
| 126 | + pagingSelect.append( "select * from ( select row_.*, rownum rownum_ from ( " ); |
| 127 | + pagingSelect.append( sql ); |
| 128 | + pagingSelect.append( " ) row_ where rownum <= ?) where rownum_ > ?" ); |
| 129 | + } |
| 130 | + else { |
| 131 | + pagingSelect = new StringBuilder( sql.length() + forUpdateClauseLength + 37 ); |
| 132 | + pagingSelect.append( "select * from ( " ); |
| 133 | + pagingSelect.append( sql ); |
| 134 | + pagingSelect.append( " ) where rownum <= ?" ); |
| 135 | + } |
| 136 | + |
| 137 | + if ( isForUpdate ) { |
| 138 | + pagingSelect.append( " " ); |
| 139 | + pagingSelect.append( forUpdateClause ); |
| 140 | + } |
| 141 | + |
| 142 | + return pagingSelect.toString(); |
| 143 | + } |
| 144 | + |
| 145 | + private String normalizeStatement(String sql) { |
| 146 | + return sql.trim().replaceAll( "\\s+", " " ); |
| 147 | + } |
| 148 | + |
| 149 | + private int getForUpdateIndex(String sql) { |
| 150 | + final int forUpdateLastIndex = sql.toLowerCase( Locale.ROOT ).lastIndexOf( "for update" ); |
| 151 | + // We need to recognize cases like : select a from t where b = 'for update'; |
| 152 | + final int lastIndexOfQuote = sql.lastIndexOf( "'" ); |
| 153 | + if ( forUpdateLastIndex > -1 ) { |
| 154 | + if ( lastIndexOfQuote == -1 ) { |
| 155 | + return forUpdateLastIndex; |
| 156 | + } |
| 157 | + if ( lastIndexOfQuote > forUpdateLastIndex ) { |
| 158 | + return -1; |
| 159 | + } |
| 160 | + return forUpdateLastIndex; |
| 161 | + } |
| 162 | + return forUpdateLastIndex; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public final boolean supportsLimit() { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean bindLimitParametersInReverseOrder() { |
| 172 | + return bindLimitParametersInReverseOrder; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean useMaxForLimit() { |
| 177 | + return useMaxForLimit; |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | +} |
0 commit comments