using System; using System.Collections.Generic; using System.Data; using System.Linq; using Ease.Core.DataAccess; using Ease.Core.DataAccess.SQL; using HRM.BO; using Microsoft.Data.SqlClient; namespace HRM.DA { /// /// Everything VirtualGridDA needs to build a grid's SQL. Built by VirtualGridService from a grid /// definition; all SQL fragments in it are code-produced (definition expressions, whitelisted sort, /// integer lists), and all user values are parameters. /// internal sealed class VirtualGridQuery { /// "sqlExpression AS [alias]" entries. public List Select { get; } = new List(); public VirtualGridSource Source { get; set; } /// Columns searched with LIKE, with the type that decides how each is rendered as text. public List> SearchColumns { get; } = new List>(); /// Extra OR-branches of the search (code-produced, e.g. "e.DEPARTMENTID IN (1,2)"). public List SearchPredicates { get; } = new List(); /// The escaped LIKE pattern ("%text%"), or null for no search. public string SearchPattern { get; set; } /// Whitelisted sort expression including ASC/DESC. public string OrderBy { get; set; } public string TieBreaker { get; set; } } /// /// The one data-access class behind every virtual grid: a COUNT and an OFFSET/FETCH window (or every /// row, for Excel) over the grid's source, with search and sort done by SQL Server. /// internal static class VirtualGridDA { internal static int Count(TransactionContext tc, VirtualGridQuery query, bool withSearch) { string sql = "SELECT COUNT(*) " + query.Source.FromWhere + SearchClause(query, withSearch); object value = tc.ExecuteScalar(CommandType.Text, sql, Parameters(query, withSearch, null, null)); return value == null || value == DBNull.Value ? 0 : Convert.ToInt32(value); } /// One window of rows; with null, every row (Excel export). internal static IDataReader GetRows(TransactionContext tc, VirtualGridQuery query, int skip, int? take) { string sql = "SELECT " + string.Join(", ", query.Select) + " " + query.Source.FromWhere + SearchClause(query, true) + " ORDER BY " + query.OrderBy + ", " + query.TieBreaker + " ASC"; if (take.HasValue) sql += " OFFSET @__Skip ROWS FETCH NEXT @__Take ROWS ONLY"; return tc.ExecuteReader(CommandType.Text, sql, Parameters(query, true, skip, take)); } /// A column as the screen displays it, so the user can search what they see. private static string AsDisplayText(string sql, EnumVirtualGridColumnType type) { switch (type) { case EnumVirtualGridColumnType.Date: return "CONVERT(varchar(11), " + sql + ", 106)"; // 13 Feb 2025 case EnumVirtualGridColumnType.DateTime: return "(CONVERT(varchar(11), " + sql + ", 106) + ' ' + CONVERT(varchar(5), " + sql + ", 108))"; case EnumVirtualGridColumnType.Money: return "CONVERT(varchar(30), CAST(" + sql + " AS money), 1)"; // 1,234.56 case EnumVirtualGridColumnType.Number: return "CONVERT(varchar(40), " + sql + ")"; default: return sql; } } private static string SearchClause(VirtualGridQuery query, bool withSearch) { if (!withSearch || string.IsNullOrEmpty(query.SearchPattern)) return string.Empty; List parts = query.SearchColumns .Select(c => AsDisplayText(c.Key, c.Value) + @" LIKE @__Search ESCAPE '\'") .Concat(query.SearchPredicates) .ToList(); return parts.Count == 0 ? " AND 1 = 0" : " AND (" + string.Join(" OR ", parts) + ")"; } private static IDataParameter[] Parameters(VirtualGridQuery query, bool withSearch, int? skip, int? take) { // Clones: a SqlParameter cannot belong to two commands, and one query runs several. var list = query.Source.Parameters .Select(p => (SqlParameter)((ICloneable)p).Clone()) .ToList(); if (withSearch && !string.IsNullOrEmpty(query.SearchPattern)) list.Add(SqlHelperExtension.CreateInParam("@__Search", SqlDbType.NVarChar, query.SearchPattern, 400)); if (take.HasValue) { list.Add(SqlHelperExtension.CreateInParam("@__Skip", SqlDbType.Int, skip ?? 0)); list.Add(SqlHelperExtension.CreateInParam("@__Take", SqlDbType.Int, take.Value)); } return list.ToArray(); } } }