111 lines
5.2 KiB
C#
111 lines
5.2 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
internal sealed class VirtualGridQuery
|
|
{
|
|
/// <summary>"sqlExpression AS [alias]" entries.</summary>
|
|
public List<string> Select { get; } = new List<string>();
|
|
|
|
public VirtualGridSource Source { get; set; }
|
|
|
|
/// <summary>Columns searched with LIKE, with the type that decides how each is rendered as text.</summary>
|
|
public List<KeyValuePair<string, EnumVirtualGridColumnType>> SearchColumns { get; } =
|
|
new List<KeyValuePair<string, EnumVirtualGridColumnType>>();
|
|
|
|
/// <summary>Extra OR-branches of the search (code-produced, e.g. "e.DEPARTMENTID IN (1,2)").</summary>
|
|
public List<string> SearchPredicates { get; } = new List<string>();
|
|
|
|
/// <summary>The escaped LIKE pattern ("%text%"), or null for no search.</summary>
|
|
public string SearchPattern { get; set; }
|
|
|
|
/// <summary>Whitelisted sort expression including ASC/DESC.</summary>
|
|
public string OrderBy { get; set; }
|
|
|
|
public string TieBreaker { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>One window of rows; with <paramref name="take"/> null, every row (Excel export).</summary>
|
|
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));
|
|
}
|
|
|
|
/// <summary>A column as the screen displays it, so the user can search what they see.</summary>
|
|
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<string> 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();
|
|
}
|
|
}
|
|
}
|