22
22
* Boston, MA 02110-1301 USA
23
23
*/
24
24
package org .hibernate .dialect ;
25
+
25
26
import java .sql .Types ;
26
27
27
28
import org .hibernate .dialect .function .NoArgSQLFunction ;
@@ -84,7 +85,9 @@ public int convertToFirstRowValue(int zeroBasedFirstResult) {
84
85
@ Override
85
86
public String getLimitString (String query , int offset , int limit ) {
86
87
// We transform the query to one with an offset and limit if we have an offset and limit to bind
87
- if (offset > 1 || limit > 1 ) return getLimitString (query , true );
88
+ if ( offset > 1 || limit > 1 ) {
89
+ return getLimitString ( query , true );
90
+ }
88
91
return query ;
89
92
}
90
93
@@ -101,37 +104,32 @@ public String getLimitString(String query, int offset, int limit) {
101
104
* SELECT * FROM query WHERE __hibernate_row_nr__ BEETWIN offset AND offset + last
102
105
* </pre>
103
106
*
104
- *
105
- * @param querySqlString
106
- * The SQL statement to base the limit query off of.
107
- * @param offset
108
- * Offset of the first row to be returned by the query (zero-based)
109
- * @param limit
110
- * Maximum number of rows to be returned by the query
107
+ * @param querySqlString The SQL statement to base the limit query off of.
108
+ * @param hasOffset Is the query requesting an offset?
111
109
*
112
110
* @return A new SQL statement with the LIMIT clause applied.
113
111
*/
114
112
@ Override
115
113
public String getLimitString (String querySqlString , boolean hasOffset ) {
116
- StringBuilder sb = new StringBuilder (querySqlString .trim ().toLowerCase ());
114
+ StringBuilder sb = new StringBuilder ( querySqlString .trim ().toLowerCase () );
117
115
118
- int orderByIndex = sb .indexOf ("order by" );
119
- CharSequence orderby = orderByIndex > 0 ? sb .subSequence (orderByIndex , sb .length ())
116
+ int orderByIndex = sb .indexOf ( "order by" );
117
+ CharSequence orderby = orderByIndex > 0 ? sb .subSequence ( orderByIndex , sb .length () )
120
118
: "ORDER BY CURRENT_TIMESTAMP" ;
121
119
122
120
// Delete the order by clause at the end of the query
123
- if (orderByIndex > 0 ) {
124
- sb .delete (orderByIndex , orderByIndex + orderby .length ());
121
+ if ( orderByIndex > 0 ) {
122
+ sb .delete ( orderByIndex , orderByIndex + orderby .length () );
125
123
}
126
124
127
125
// HHH-5715 bug fix
128
- replaceDistinctWithGroupBy (sb );
126
+ replaceDistinctWithGroupBy ( sb );
129
127
130
- insertRowNumberFunction (sb , orderby );
128
+ insertRowNumberFunction ( sb , orderby );
131
129
132
130
// Wrap the query within a with statement:
133
- sb .insert (0 , "WITH query AS (" ).append (") SELECT * FROM query " );
134
- sb .append ("WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?" );
131
+ sb .insert ( 0 , "WITH query AS (" ).append ( ") SELECT * FROM query " );
132
+ sb .append ( "WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?" );
135
133
136
134
return sb .toString ();
137
135
}
@@ -144,52 +142,50 @@ public String getLimitString(String querySqlString, boolean hasOffset) {
144
142
* @param sql an sql query
145
143
*/
146
144
protected static void replaceDistinctWithGroupBy (StringBuilder sql ) {
147
- int distinctIndex = sql .indexOf (DISTINCT );
148
- if (distinctIndex > 0 ) {
149
- sql .delete (distinctIndex , distinctIndex + DISTINCT .length () + 1 );
150
- sql .append (" group by" ).append (getSelectFieldsWithoutAliases (sql ) );
145
+ int distinctIndex = sql .indexOf ( DISTINCT );
146
+ if ( distinctIndex > 0 ) {
147
+ sql .delete ( distinctIndex , distinctIndex + DISTINCT .length () + 1 );
148
+ sql .append ( " group by" ).append ( getSelectFieldsWithoutAliases ( sql ) );
151
149
}
152
150
}
153
151
154
152
/**
155
153
* This utility method searches the given sql query for the fields of the select statement and returns them without
156
154
* the aliases. See {@link SQLServer2005DialectTestCase#testGetSelectFieldsWithoutAliases()}
157
155
*
158
- * @param an
159
- * sql query
156
+ * @param sql sql query
157
+ *
160
158
* @return the fields of the select statement without their alias
161
159
*/
162
160
protected static CharSequence getSelectFieldsWithoutAliases (StringBuilder sql ) {
163
- String select = sql .substring (sql .indexOf (SELECT ) + SELECT .length (), sql .indexOf (FROM ) );
161
+ String select = sql .substring ( sql .indexOf ( SELECT ) + SELECT .length (), sql .indexOf ( FROM ) );
164
162
165
163
// Strip the as clauses
166
- return stripAliases (select );
164
+ return stripAliases ( select );
167
165
}
168
166
169
167
/**
170
168
* Utility method that strips the aliases. See {@link SQLServer2005DialectTestCase#testStripAliases()}
171
169
*
172
- * @param a
173
- * string to replace the as statements
170
+ * @param str string to replace the as statements
171
+ *
174
172
* @return a string without the as statements
175
173
*/
176
174
protected static String stripAliases (String str ) {
177
- return str .replaceAll ("\\ sas[^,]+(,?)" , "$1" );
175
+ return str .replaceAll ( "\\ sas[^,]+(,?)" , "$1" );
178
176
}
179
177
180
178
/**
181
179
* Right after the select statement of a given query we must place the row_number function
182
180
*
183
- * @param sql
184
- * the initial sql query without the order by clause
185
- * @param orderby
186
- * the order by clause of the query
181
+ * @param sql the initial sql query without the order by clause
182
+ * @param orderby the order by clause of the query
187
183
*/
188
184
protected static void insertRowNumberFunction (StringBuilder sql , CharSequence orderby ) {
189
185
// Find the end of the select statement
190
- int selectEndIndex = sql .indexOf (SELECT ) + SELECT .length ();
186
+ int selectEndIndex = sql .indexOf ( SELECT ) + SELECT .length ();
191
187
192
188
// Insert after the select statement the row_number() function:
193
- sql .insert (selectEndIndex , " ROW_NUMBER() OVER (" + orderby + ") as __hibernate_row_nr__," );
189
+ sql .insert ( selectEndIndex , " ROW_NUMBER() OVER (" + orderby + ") as __hibernate_row_nr__," );
194
190
}
195
191
}
0 commit comments