HRM-10/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs
2026-11-09 17:22:45 +06:00

238 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
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
{
/// <summary>
/// Server side of &lt;app-virtual-grid&gt;: 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,employeeId");
///
/// 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, ID lists)
/// reaches SQL only as parameters; field names are checked to be plain identifiers and bracketed.
/// </summary>
internal static class VirtualPagingDA
{
private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled);
private static readonly Regex TempTableName = new Regex("^#[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled);
private const int MaxRowsPerPage = 1000;
private const int MaxSearchLength = 200;
/// <param name="query">The page's SELECT (column aliases = grid fields, no ORDER BY).</param>
/// <param name="keyFields">
/// Comma-separated fields that together are unique, e.g. "employeeNo,employeeId". The first is the
/// default sort; all of them end every ORDER BY, so pages never overlap or skip a row.
/// </param>
/// <param name="idLists">ID lists the query reads as temp tables, e.g. IdList("#empIds", ids). Optional.</param>
internal static VirtualPageResult GetPage(TransactionContext tc, string query, VirtualPageRequest request, string keyFields,
params VirtualPagingIdList[] idLists)
{
if (request == null)
throw new ArgumentException("Invalid request.");
List<string> keys = (keyFields ?? "").Split(',').Select(k => k.Trim()).ToList();
if (keys.Count == 0 || !keys.All(IsIdentifier))
throw new ArgumentException("Invalid key fields.");
idLists = idLists ?? new VirtualPagingIdList[0];
List<VirtualPageColumn> 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("SET NOCOUNT ON;\n");
var parameters = new List<SqlParameter>(); // SqlHelper expects a SqlParameter[]
// 0. ID lists → temp tables with a primary key: SQL Server then knows how many rows there are and
// picks a good plan (a STRING_SPLIT inside the query is guessed at 50 rows and can be 10x slower).
for (int i = 0; i < idLists.Length; i++)
{
string table = idLists[i].TableName, name = "@vpIds" + i;
sql.Append("IF OBJECT_ID('tempdb..").Append(table).Append("') IS NOT NULL DROP TABLE ").Append(table).Append(";\n")
.Append("CREATE TABLE ").Append(table).Append(" (ID int NOT NULL PRIMARY KEY);\n")
.Append("INSERT INTO ").Append(table).Append(" (ID) SELECT DISTINCT CONVERT(int, value) FROM STRING_SPLIT(")
.Append(name).Append(", ',');\n");
parameters.Add(SqlHelperExtension.CreateInParam(name, SqlDbType.VarChar, idLists[i].ToText(), -1)); // varchar(max)
}
// 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, keys));
if (paged)
sql.Append(" OFFSET @vpSkip ROWS FETCH NEXT @vpTake ROWS ONLY");
sql.Append(";\n");
foreach (VirtualPagingIdList list in idLists)
sql.Append("DROP TABLE ").Append(list.TableName).Append(";\n");
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<string, object>(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;
}
/// <summary>
/// A list of IDs (e.g. the employees selected on the screen), however many there are. GetPage loads it
/// into the temp table <paramref name="tableName"/> (one parameter, then a primary key) and the query
/// reads it with: e.EMPLOYEEID IN (SELECT ID FROM #empIds). Needs SQL Server 2016+ (STRING_SPLIT).
/// </summary>
internal static VirtualPagingIdList IdList(string tableName, IEnumerable<int> ids)
{
if (tableName == null || !TempTableName.IsMatch(tableName))
throw new ArgumentException("Invalid temp table name.");
return new VirtualPagingIdList(tableName, (ids ?? Enumerable.Empty<int>()).ToList());
}
/// <summary>The requested columns; every field must be a plain identifier (it is put in the SQL in brackets).</summary>
private static List<VirtualPageColumn> GetColumns(List<VirtualPageColumn> requested)
{
var columns = new List<VirtualPageColumn>();
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (VirtualPageColumn c in requested ?? new List<VirtualPageColumn>())
{
if (c == null || !IsIdentifier(c.Field))
throw new ArgumentException("Invalid column: " + c?.Field);
if (seen.Add(c.Field))
columns.Add(c);
}
return columns;
}
/// <summary>
/// "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").
/// </summary>
private static string SearchCondition(List<VirtualPageColumn> columns)
{
const string like = " LIKE @vpSearch ESCAPE '\\'";
var parts = new List<string>();
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) + ")";
}
/// <summary>
/// The grid's sort (only by one of its columns), then the key fields so the order is always the same.
/// No sort = the key fields (the first one is the default sort).
/// </summary>
private static string OrderBy(List<VirtualPageColumn> columns, VirtualPageRequest request, List<string> keys)
{
VirtualPageColumn sort = columns.FirstOrDefault(c => string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase));
string dir = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase) ? " DESC" : " ASC";
var parts = new List<string>();
if (sort != null)
parts.Add("[" + sort.Field + "]" + dir);
foreach (string key in keys)
{
if (sort == null || !string.Equals(key, sort.Field, StringComparison.OrdinalIgnoreCase))
parts.Add("[" + key + "] ASC");
}
return string.Join(", ", parts);
}
/// <summary>Makes %, _, [ and \ in the search text match themselves.</summary>
private static string EscapeLike(string text) =>
text.Replace("\\", "\\\\").Replace("%", "\\%").Replace("_", "\\_").Replace("[", "\\[");
private static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name);
}
/// <summary>An ID list for a virtual-paging query (made by VirtualPagingDA.IdList).</summary>
internal sealed class VirtualPagingIdList
{
internal VirtualPagingIdList(string tableName, List<int> ids)
{
TableName = tableName;
Ids = ids;
}
internal string TableName { get; }
internal List<int> Ids { get; }
internal string ToText() => string.Join(",", Ids.Select(id => id.ToString(CultureInfo.InvariantCulture)));
}
}