@@ -110,7 +110,8 @@ public abstract class QueryUtils {
110110
111111 private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s" ;
112112 private static final Pattern ORDER_BY = Pattern .compile ("(order\\ s+by\\ s+)" , CASE_INSENSITIVE );
113- private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern .compile ("(\\ (\\ s*[a-z0-9 ,.*]*order\\ s+by\\ s+[a-z0-9 ,.]*\\ s*\\ ))" , CASE_INSENSITIVE );
113+ private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
114+ .compile ("(\\ (\\ s*[a-z0-9 ,.*]*order\\ s+by\\ s+[a-z0-9 ,.]*\\ s*\\ ))" , CASE_INSENSITIVE );
114115
115116 private static final Pattern NAMED_PARAMETER = Pattern .compile (COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER ,
116117 CASE_INSENSITIVE );
@@ -144,6 +145,7 @@ public abstract class QueryUtils {
144145 ALIAS_MATCH = compile (builder .toString (), CASE_INSENSITIVE );
145146
146147 builder = new StringBuilder ();
148+ builder .append ("\\ s*" );
147149 builder .append ("(select\\ s+((distinct)?((?s).+?)?)\\ s+)?(from\\ s+" );
148150 builder .append (IDENTIFIER );
149151 builder .append ("(?:\\ s+as)?\\ s+)" );
@@ -279,8 +281,8 @@ public static String applySorting(String query, Sort sort, @Nullable String alia
279281 }
280282
281283 /**
282- * Returns {@code true} if the query has {@code order by} clause.
283- * The query has {@code order by} clause if there is an {@code order by} which is not part of window clause.
284+ * Returns {@code true} if the query has {@code order by} clause. The query has {@code order by} clause if there is an
285+ * {@code order by} which is not part of window clause.
284286 *
285287 * @param query the analysed query string
286288 * @return {@code true} if the query has {@code order by} clause, {@code false} otherwise
@@ -297,9 +299,13 @@ private static boolean hasOrderByClause(String query) {
297299 * @return the number of occurences of the pattern in the string
298300 */
299301 private static int countOccurences (Pattern pattern , String string ) {
302+
300303 Matcher matcher = pattern .matcher (string );
304+
301305 int occurences = 0 ;
302- while (matcher .find ()) occurences ++;
306+ while (matcher .find ()) {
307+ occurences ++;
308+ }
303309 return occurences ;
304310 }
305311
0 commit comments