128 lines
4.3 KiB
C#
128 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
|
|
namespace Lskj.Business.Impl
|
|
{
|
|
/// <summary>
|
|
/// 单次模块初始化期间复用的参数缓存。
|
|
/// </summary>
|
|
public static class InitialParamCache
|
|
{
|
|
private static readonly object SyncRoot = new object();
|
|
private static readonly Dictionary<string, bool> TableExistsCache =
|
|
new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
|
|
private static readonly Dictionary<string, bool> ColumnExistsCache =
|
|
new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
|
|
private static readonly Dictionary<string, DataTable> TableColumnsCache =
|
|
new Dictionary<string, DataTable>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>
|
|
/// 获取表是否存在。没有缓存时执行查询,并记录查询结果。
|
|
/// </summary>
|
|
public static bool GetTableExists(string tableName, Func<bool> query)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(tableName))
|
|
{
|
|
throw new ArgumentException("表名不能为空。", "tableName");
|
|
}
|
|
if (query == null)
|
|
{
|
|
throw new ArgumentNullException("query");
|
|
}
|
|
|
|
lock (SyncRoot)
|
|
{
|
|
bool tableExists;
|
|
if (TableExistsCache.TryGetValue(tableName, out tableExists))
|
|
{
|
|
return tableExists;
|
|
}
|
|
|
|
tableExists = query();
|
|
TableExistsCache[tableName] = tableExists;
|
|
return tableExists;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字段是否存在。没有缓存时执行查询,并记录查询结果。
|
|
/// </summary>
|
|
public static bool GetColumnExists(string tableName, string columnName, string columnType, Func<bool> query)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(tableName))
|
|
{
|
|
throw new ArgumentException("表名不能为空。", "tableName");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(columnName))
|
|
{
|
|
throw new ArgumentException("字段名不能为空。", "columnName");
|
|
}
|
|
if (query == null)
|
|
{
|
|
throw new ArgumentNullException("query");
|
|
}
|
|
|
|
string cacheKey = tableName + "|" + columnName + "|" + (columnType ?? string.Empty);
|
|
lock (SyncRoot)
|
|
{
|
|
bool columnExists;
|
|
if (ColumnExistsCache.TryGetValue(cacheKey, out columnExists))
|
|
{
|
|
return columnExists;
|
|
}
|
|
|
|
columnExists = query();
|
|
ColumnExistsCache[cacheKey] = columnExists;
|
|
return columnExists;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取表结构。没有缓存时执行查询,并缓存不包含数据的结构模板。
|
|
/// </summary>
|
|
public static DataTable GetTableColumns(string tableName, Func<DataTable> query)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(tableName))
|
|
{
|
|
throw new ArgumentException("表名不能为空。", "tableName");
|
|
}
|
|
if (query == null)
|
|
{
|
|
throw new ArgumentNullException("query");
|
|
}
|
|
|
|
lock (SyncRoot)
|
|
{
|
|
DataTable tableColumns;
|
|
if (!TableColumnsCache.TryGetValue(tableName, out tableColumns))
|
|
{
|
|
DataTable queryResult = query();
|
|
if (queryResult == null)
|
|
{
|
|
throw new InvalidOperationException("获取表结构失败。" + tableName);
|
|
}
|
|
|
|
tableColumns = queryResult.Clone();
|
|
TableColumnsCache[tableName] = tableColumns;
|
|
}
|
|
|
|
return tableColumns.Clone();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空上一次模块初始化记录的参数。
|
|
/// </summary>
|
|
public static void Clear()
|
|
{
|
|
lock (SyncRoot)
|
|
{
|
|
TableExistsCache.Clear();
|
|
ColumnExistsCache.Clear();
|
|
TableColumnsCache.Clear();
|
|
}
|
|
}
|
|
}
|
|
}
|