private LimitType limitQuery(
     String limitPattern,
     LimitType limitType,
     String queryString,
     StringBuilder queryResult,
     Integer firstResult,
     Integer maxResults) {
   if (limitPattern == null) return null;
   int ix = limitPattern.indexOf("$S");
   if (ix >= 0) {
     limitType.afterSql = limitPattern.indexOf("$", ix + 1) > 0;
     queryResult.append(limitPattern.substring(0, ix));
     queryResult.append(queryString);
     queryResult.append(limitPattern.substring(ix + 2));
   } else {
     ix = limitPattern.indexOf("$s");
     if (ix >= 0) {
       limitType.afterSql = limitPattern.indexOf("$", ix + 1) > 0;
       int ix2 = queryString.toLowerCase().indexOf("select");
       if (ix2 < 0) return null;
       queryResult.append(limitPattern.substring(0, ix));
       queryResult.append(queryString.substring(ix2 + 6));
       queryResult.append(limitPattern.substring(ix + 2));
     } else {
       return null;
     }
   }
   if (limitType.alsoFirst) {
     ix = queryResult.indexOf("$F");
     if (ix >= 0) {
       if (queryResult.indexOf("$m", ix) < 0 && queryResult.indexOf("$M", ix) < 0)
         limitType.maxBeforeFirst = true;
       queryResult.replace(ix, ix + 2, "?");
     } else {
       ix = queryResult.indexOf("$f");
       if (ix >= 0) {
         limitType.zeroBasedFirst = true;
         if (queryResult.indexOf("$m", ix) < 0 && queryResult.indexOf("$M", ix) < 0)
           limitType.maxBeforeFirst = true;
         queryResult.replace(ix, ix + 2, "?");
       } else {
         return null;
       }
     }
   }
   ix = queryResult.indexOf("$M");
   if (ix >= 0) {
     queryResult.replace(ix, ix + 2, "?");
   } else {
     ix = queryResult.indexOf("$m");
     if (ix >= 0) {
       limitType.rowidBasedMax = true;
       queryResult.replace(ix, ix + 2, "?");
     } else {
       return null;
     }
   }
   return limitType;
 }