using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Ease.Core.DataAccess; using Ease.Core.DataAccess.SQL; using HRM.BO; using Microsoft.Data.SqlClient; namespace HRM.DA { /// /// Server side of <app-virtual-grid>: SQL Server does the paging. Each call reads only one window /// of rows (OFFSET/FETCH), with the search and sort in the same SQL and the totals counted only when /// they can change. A page's service calls it with its own query: /// /// string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); /// return VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); /// /// The query is an ordinary SELECT whose column aliases are the grid's field names, with no ORDER BY /// and no WITH (it is used as a derived table). See README.md next to this file. /// /// Safety: the page query is built by code with MakeSQL; user input (search text, skip, take) reaches /// SQL only as parameters; field names are checked to be plain identifiers and bracketed. /// internal static class VirtualPagingDA { private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); private const int MaxRowsPerPage = 1000; private const int MaxSearchLength = 200; /// The page's SELECT (column aliases = grid fields, no ORDER BY). /// A unique field (e.g. employeeNo): the default sort and the tie-breaker that keeps pages from overlapping. internal static VirtualPageResult GetPage(TransactionContext tc, string query, VirtualPageRequest request, string keyField) { if (request == null) throw new ArgumentException("Invalid request."); if (!IsIdentifier(keyField)) throw new ArgumentException("Invalid key field."); List columns = GetColumns(request.Columns); string search = (request.Search ?? "").Trim(); if (search.Length > MaxSearchLength) search = search.Substring(0, MaxSearchLength); string source = " FROM (" + query + ") AS vp"; string searchCondition = search.Length > 0 ? SearchCondition(columns) : null; bool export = request.ExportExcel; // every matching row, written straight into an .xlsx bool paged = !export && request.Take > 0; bool countTotal = !export && request.CountTotal; var sql = new StringBuilder(); // 1. Totals, only on Preview / new search (scroll and sort do not change them). One scan for both. if (countTotal) { sql.Append("SELECT COUNT(*) AS TotalWithoutSearch, ") .Append(searchCondition == null ? "COUNT(*)" : "ISNULL(SUM(CASE WHEN " + searchCondition + " THEN 1 ELSE 0 END), 0)") .Append(" AS Total").Append(source).Append(";\n"); } // 2. The window of rows. sql.Append("SELECT ") .Append(columns.Count == 0 ? "*" : string.Join(", ", columns.Select(c => "[" + c.Field + "]"))) .Append(source) .Append(searchCondition == null ? "" : " WHERE " + searchCondition) .Append(" ORDER BY ").Append(OrderBy(columns, request, keyField)); if (paged) sql.Append(" OFFSET @vpSkip ROWS FETCH NEXT @vpTake ROWS ONLY"); var parameters = new List(); // SqlHelper expects a SqlParameter[] if (searchCondition != null) parameters.Add(SqlHelperExtension.CreateInParam("@vpSearch", SqlDbType.NVarChar, "%" + EscapeLike(search) + "%", 400)); if (paged) { parameters.Add(SqlHelperExtension.CreateInParam("@vpSkip", SqlDbType.Int, Math.Max(0, request.Skip))); parameters.Add(SqlHelperExtension.CreateInParam("@vpTake", SqlDbType.Int, Math.Min(request.Take, MaxRowsPerPage))); } var result = new VirtualPageResult(); using (IDataReader reader = tc.ExecuteReader(CommandType.Text, sql.ToString(), parameters.ToArray())) { // Export to Excel: rows go from SQL Server into the file one by one, never all held in memory. if (export) { result.ExcelFile = VirtualPagingExcel.Create(reader, columns); return result; } if (countTotal) { if (reader.Read()) { result.TotalWithoutSearch = Convert.ToInt32(reader["TotalWithoutSearch"]); result.Total = Convert.ToInt32(reader["Total"]); } reader.NextResult(); } while (reader.Read()) { var row = new Dictionary(reader.FieldCount); for (int i = 0; i < reader.FieldCount; i++) row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i); result.Data.Add(row); } } return result; } /// The requested columns; every field must be a plain identifier (it is put in the SQL in brackets). private static List GetColumns(List requested) { var columns = new List(); var seen = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (VirtualPageColumn c in requested ?? new List()) { if (c == null || !IsIdentifier(c.Field)) throw new ArgumentException("Invalid column: " + c?.Field); if (seen.Add(c.Field)) columns.Add(c); } return columns; } /// /// "column LIKE @vpSearch" for every column, OR-ed, on the text the grid shows: /// dates "13 Feb 2025", money "1,234.56" (or "1234.56"). /// private static string SearchCondition(List columns) { const string like = " LIKE @vpSearch ESCAPE '\\'"; var parts = new List(); foreach (VirtualPageColumn c in columns) { string f = "[" + c.Field + "]"; switch ((c.Type ?? "").ToLowerInvariant()) { case "date": parts.Add("CONVERT(varchar(11), " + f + ", 106)" + like); break; case "datetime": parts.Add("(CONVERT(varchar(11), " + f + ", 106) + ' ' + CONVERT(varchar(5), " + f + ", 108))" + like); break; case "money": parts.Add("CONVERT(varchar(30), CAST(" + f + " AS money), 1)" + like); parts.Add("CONVERT(varchar(30), CAST(" + f + " AS money), 0)" + like); break; default: parts.Add("CONVERT(nvarchar(4000), " + f + ")" + like); break; } } return parts.Count == 0 ? "1 = 0" : "(" + string.Join(" OR ", parts) + ")"; } /// The grid's sort (only by one of its columns), then the key field so the order is always the same. private static string OrderBy(List columns, VirtualPageRequest request, string keyField) { VirtualPageColumn sort = columns.FirstOrDefault(c => string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase)); if (sort == null || string.Equals(sort.Field, keyField, StringComparison.OrdinalIgnoreCase)) { bool keyDesc = sort != null && string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); return "[" + keyField + "]" + (keyDesc ? " DESC" : " ASC"); } bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); return "[" + sort.Field + "]" + (desc ? " DESC" : " ASC") + ", [" + keyField + "] ASC"; } /// Makes %, _, [ and \ in the search text match themselves. private static string EscapeLike(string text) => text.Replace("\\", "\\\\").Replace("%", "\\%").Replace("_", "\\_").Replace("[", "\\["); private static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name); } }