|
|
using Dapper; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; using System.Xml;
namespace Mirle.Component.Database { /// <summary>
/// 資料庫擴充方法類別
/// </summary>
public static class RelationDatabaseExtensions { /// <summary>
/// 取得資料庫語法
/// </summary>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整子節點名稱</param>
/// <returns>資料庫語法</returns>
private static string GetScriptContent(string filePath, string nodeName) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(filePath); return xmlDocument.SelectSingleNode(nodeName).InnerText.Trim(); } /// <summary>
/// 取得資料庫語法
/// </summary>
/// <param name="filePath">檔案位置</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整子節點名稱</param>
/// <returns>資料庫語法</returns>
private static string GetScriptContent(string filePath, string fileName, string nodeName) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load($"{filePath}\\{fileName}"); return xmlDocument.SelectSingleNode(nodeName).InnerText.Trim(); }
#region === [Read] ===
/// <summary>
/// 查詢資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">資料庫語法</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<dynamic> GetData(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<dynamic> GetData(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, nodeName); return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料表
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<dynamic> GetData(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, fileName, nodeName); return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">關聯式資料庫語法</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<T> GetData<T>(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<T> GetData<T>(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, nodeName); return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static IEnumerable<T> GetData<T>(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, fileName, nodeName); return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text); } /// <summary>
/// 查詢資料表
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">關聯式資料庫語法</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text); return dataset; } /// <summary>
/// 查詢資料表
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, nodeName); IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text); return dataset; } /// <summary>
/// 查詢資料表
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, fileName, nodeName); IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text); return dataset; } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">資料庫語法</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text); return dataset; } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, nodeName); IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text); return dataset; } /// <summary>
/// 查詢資料表
/// </summary>
/// <typeparam name="T">資料表類別</typeparam>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="commandTimeOut">查詢逾時</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>資料集</returns>
public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null, int commandTimeOut = 300) { string query = GetScriptContent(filePath, fileName, nodeName); IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text); return dataset; }
#endregion
#region === [Create & Update & Delete] ===
/// <summary>
/// 更新資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">資料庫語法</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>異動筆數</returns>
public static int UpdateData(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null) { return conn.Execute(query, param, trans); } /// <summary>
/// 更新資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>異動筆數</returns>
public static int UpdateData(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null) { string query = GetScriptContent(filePath, nodeName); return conn.Execute(query, param, trans); } /// <summary>
/// 更新資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>異動筆數</returns>
public static int UpdateData(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null) { string query = GetScriptContent(filePath, fileName, nodeName); return conn.Execute(query, param, trans); } /// <summary>
/// 更新資料表資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="query">資料庫語法</param>
/// <param name="param">參數</param>
/// <param name="trans">資料庫交易</param>
/// <returns>異動筆數</returns>
public static Task<int> UpdateDataAsync(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null) { return conn.ExecuteAsync(query, param, trans); } /// <summary>
/// 更新資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">完整檔案路徑</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>異動筆數</returns>
public static Task<int> UpdateDataAsync(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null) { string query = GetScriptContent(filePath, nodeName); return conn.ExecuteAsync(query, param, trans); } /// <summary>
/// 更新資料
/// </summary>
/// <param name="conn">關聯式資料庫連線</param>
/// <param name="filePath">檔案路徑</param>
/// <param name="fileName">檔案名稱</param>
/// <param name="nodeName">完整節點名稱</param>
/// <param name="param">參數</param>
/// <param name="trans">關聯式資料庫交易</param>
/// <returns>異動筆數</returns>
public static Task<int> UpdateDataAsync(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null) { string query = GetScriptContent(filePath, fileName, nodeName); return conn.ExecuteAsync(query, param, trans); }
#endregion
} }
|