private static void DetailPagingHelper_SqlCount(string fixedSql, string SELECTSQL, string SELECTWithoutOrder, string fromBodyString, string orderBodyString, out string sqlCount) { bool hasOrderBy = !String.IsNullOrEmpty(orderBodyString); //sqlCount if (!DealHelper.hasDistinct(SELECTSQL)) {//## no DISTINCT sqlCount = "SELECT COUNT(1) " + fromBodyString; } else {//## has DISTINCT if (hasOrderBy) { sqlCount = "SELECT COUNT(1) FROM ( " + SELECTWithoutOrder + ") as ___temp___sqlCount___"; } else { sqlCount = "SELECT COUNT(1) FROM ( " + SELECTSQL + ") as ___temp___sqlCount___"; } } //----processing fixed sql---- sqlCount = fixedSql + "\r\n" + sqlCount; //---------------------------- }
private static void DetailPagingHelper_SqlPageAndCount(SqlParameter[] paramValues, string orderBodyString, string SELECTWithoutOrder, string fixedSql, int pageIndex, int pageSize, out string FINALSQL) { StringBuilder adjustedSQL = new StringBuilder(); bool hasDistinct = DealHelper.hasDistinct(SELECTWithoutOrder); //##1 Add TOTAL column if (!hasDistinct) { int firstSelectPos = SELECTWithoutOrder.IndexOf("SELECT ", StringComparison.OrdinalIgnoreCase); adjustedSQL.Append("SELECT COUNT(*) OVER() AS ___totalCount___,"); adjustedSQL.AppendLine(SELECTWithoutOrder.Substring(firstSelectPos + 7)); } else { adjustedSQL.Append("SELECT COUNT(*) OVER() AS ___totalCount___, * FROM ( "); adjustedSQL.Append(SELECTWithoutOrder); adjustedSQL.AppendLine(" ) as ___temp___selectBody___ "); } //##2 Handle OrderBy bool HasOrderBy = !String.IsNullOrEmpty(orderBodyString); if (HasOrderBy) { adjustedSQL.Append("ORDER BY "); adjustedSQL.AppendLine(orderBodyString); } else { adjustedSQL.AppendLine("ORDER BY (select null) "); } //##3 Offset & Fetch adjustedSQL.Append("OFFSET "); adjustedSQL.Append((pageIndex - 1) * pageSize); adjustedSQL.AppendLine(" ROWS "); adjustedSQL.Append("FETCH NEXT "); adjustedSQL.Append(pageSize); adjustedSQL.AppendLine(" ROWS ONLY "); //----processing fixed sql---- FINALSQL = fixedSql + "\r\n" + adjustedSQL.ToString(); //---------------------------- }