You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
16 KiB

8 months ago
  1. using Dapper;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Threading.Tasks;
  5. using System.Xml;
  6. namespace Mirle.Component.Database
  7. {
  8. /// <summary>
  9. /// 資料庫擴充方法類別
  10. /// </summary>
  11. public static class RelationDatabaseExtensions
  12. {
  13. /// <summary>
  14. /// 取得資料庫語法
  15. /// </summary>
  16. /// <param name="filePath">完整檔案路徑</param>
  17. /// <param name="nodeName">完整子節點名稱</param>
  18. /// <returns>資料庫語法</returns>
  19. private static string GetScriptContent(string filePath, string nodeName)
  20. {
  21. XmlDocument xmlDocument = new XmlDocument();
  22. xmlDocument.Load(filePath);
  23. return xmlDocument.SelectSingleNode(nodeName).InnerText.Trim();
  24. }
  25. /// <summary>
  26. /// 取得資料庫語法
  27. /// </summary>
  28. /// <param name="filePath">檔案位置</param>
  29. /// <param name="fileName">檔案名稱</param>
  30. /// <param name="nodeName">完整子節點名稱</param>
  31. /// <returns>資料庫語法</returns>
  32. private static string GetScriptContent(string filePath, string fileName, string nodeName)
  33. {
  34. XmlDocument xmlDocument = new XmlDocument();
  35. xmlDocument.Load($"{filePath}\\{fileName}");
  36. return xmlDocument.SelectSingleNode(nodeName).InnerText.Trim();
  37. }
  38. #region === [Read] ===
  39. /// <summary>
  40. /// 查詢資料
  41. /// </summary>
  42. /// <param name="conn">關聯式資料庫連線</param>
  43. /// <param name="query">資料庫語法</param>
  44. /// <param name="commandTimeOut">查詢逾時</param>
  45. /// <param name="param">參數</param>
  46. /// <param name="trans">關聯式資料庫交易</param>
  47. /// <returns>資料集</returns>
  48. public static IEnumerable<dynamic> GetData(this IDbConnection conn, string query,
  49. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  50. {
  51. return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text);
  52. }
  53. /// <summary>
  54. /// 查詢資料
  55. /// </summary>
  56. /// <param name="conn">關聯式資料庫連線</param>
  57. /// <param name="filePath">完整檔案路徑</param>
  58. /// <param name="nodeName">完整節點名稱</param>
  59. /// <param name="commandTimeOut">查詢逾時</param>
  60. /// <param name="param">參數</param>
  61. /// <param name="trans">關聯式資料庫交易</param>
  62. /// <returns>資料集</returns>
  63. public static IEnumerable<dynamic> GetData(this IDbConnection conn, string filePath, string nodeName,
  64. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  65. {
  66. string query = GetScriptContent(filePath, nodeName);
  67. return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text);
  68. }
  69. /// <summary>
  70. /// 查詢資料表
  71. /// </summary>
  72. /// <param name="conn">關聯式資料庫連線</param>
  73. /// <param name="filePath">檔案路徑</param>
  74. /// <param name="fileName">檔案名稱</param>
  75. /// <param name="nodeName">完整節點名稱</param>
  76. /// <param name="commandTimeOut">查詢逾時</param>
  77. /// <param name="param">參數</param>
  78. /// <param name="trans">關聯式資料庫交易</param>
  79. /// <returns>資料集</returns>
  80. public static IEnumerable<dynamic> GetData(this IDbConnection conn, string filePath, string fileName, string nodeName,
  81. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  82. {
  83. string query = GetScriptContent(filePath, fileName, nodeName);
  84. return conn.Query(query, param, trans, true, commandTimeOut, CommandType.Text);
  85. }
  86. /// <summary>
  87. /// 查詢資料表
  88. /// </summary>
  89. /// <typeparam name="T">資料表類別</typeparam>
  90. /// <param name="conn">關聯式資料庫連線</param>
  91. /// <param name="query">關聯式資料庫語法</param>
  92. /// <param name="commandTimeOut">查詢逾時</param>
  93. /// <param name="param">參數</param>
  94. /// <param name="trans">關聯式資料庫交易</param>
  95. /// <returns>資料集</returns>
  96. public static IEnumerable<T> GetData<T>(this IDbConnection conn, string query,
  97. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  98. {
  99. return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text);
  100. }
  101. /// <summary>
  102. /// 查詢資料表
  103. /// </summary>
  104. /// <typeparam name="T">資料表類別</typeparam>
  105. /// <param name="conn">關聯式資料庫連線</param>
  106. /// <param name="filePath">完整檔案路徑</param>
  107. /// <param name="nodeName">完整節點名稱</param>
  108. /// <param name="commandTimeOut">查詢逾時</param>
  109. /// <param name="param">參數</param>
  110. /// <param name="trans">關聯式資料庫交易</param>
  111. /// <returns>資料集</returns>
  112. public static IEnumerable<T> GetData<T>(this IDbConnection conn, string filePath, string nodeName,
  113. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  114. {
  115. string query = GetScriptContent(filePath, nodeName);
  116. return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text);
  117. }
  118. /// <summary>
  119. /// 查詢資料表
  120. /// </summary>
  121. /// <typeparam name="T">資料表類別</typeparam>
  122. /// <param name="conn">關聯式資料庫連線</param>
  123. /// <param name="filePath">檔案路徑</param>
  124. /// <param name="fileName">檔案名稱</param>
  125. /// <param name="nodeName">完整節點名稱</param>
  126. /// <param name="commandTimeOut">查詢逾時</param>
  127. /// <param name="param">參數</param>
  128. /// <param name="trans">關聯式資料庫交易</param>
  129. /// <returns>資料集</returns>
  130. public static IEnumerable<T> GetData<T>(this IDbConnection conn, string filePath, string fileName, string nodeName,
  131. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  132. {
  133. string query = GetScriptContent(filePath, fileName, nodeName);
  134. return conn.Query<T>(query, param, trans, true, commandTimeOut, CommandType.Text);
  135. }
  136. /// <summary>
  137. /// 查詢資料表
  138. /// </summary>
  139. /// <param name="conn">關聯式資料庫連線</param>
  140. /// <param name="query">關聯式資料庫語法</param>
  141. /// <param name="commandTimeOut">查詢逾時</param>
  142. /// <param name="param">參數</param>
  143. /// <param name="trans">關聯式資料庫交易</param>
  144. /// <returns>資料集</returns>
  145. public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string query,
  146. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  147. {
  148. IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text);
  149. return dataset;
  150. }
  151. /// <summary>
  152. /// 查詢資料表
  153. /// </summary>
  154. /// <param name="conn">關聯式資料庫連線</param>
  155. /// <param name="filePath">完整檔案路徑</param>
  156. /// <param name="nodeName">完整節點名稱</param>
  157. /// <param name="commandTimeOut">查詢逾時</param>
  158. /// <param name="param">參數</param>
  159. /// <param name="trans">關聯式資料庫交易</param>
  160. /// <returns>資料集</returns>
  161. public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string filePath, string nodeName,
  162. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  163. {
  164. string query = GetScriptContent(filePath, nodeName);
  165. IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text);
  166. return dataset;
  167. }
  168. /// <summary>
  169. /// 查詢資料表
  170. /// </summary>
  171. /// <param name="conn">關聯式資料庫連線</param>
  172. /// <param name="filePath">檔案路徑</param>
  173. /// <param name="fileName">檔案名稱</param>
  174. /// <param name="nodeName">節點名稱</param>
  175. /// <param name="commandTimeOut">查詢逾時</param>
  176. /// <param name="param">參數</param>
  177. /// <param name="trans">關聯式資料庫交易</param>
  178. /// <returns>資料集</returns>
  179. public static async Task<IEnumerable<dynamic>> GetDataAsync(this IDbConnection conn, string filePath, string fileName, string nodeName,
  180. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  181. {
  182. string query = GetScriptContent(filePath, fileName, nodeName);
  183. IEnumerable<dynamic> dataset = await conn.QueryAsync(query, param, trans, commandTimeOut, CommandType.Text);
  184. return dataset;
  185. }
  186. /// <summary>
  187. /// 查詢資料表
  188. /// </summary>
  189. /// <typeparam name="T">資料表類別</typeparam>
  190. /// <param name="conn">關聯式資料庫連線</param>
  191. /// <param name="query">資料庫語法</param>
  192. /// <param name="commandTimeOut">查詢逾時</param>
  193. /// <param name="param">參數</param>
  194. /// <param name="trans">關聯式資料庫交易</param>
  195. /// <returns>資料集</returns>
  196. public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string query,
  197. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  198. {
  199. IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text);
  200. return dataset;
  201. }
  202. /// <summary>
  203. /// 查詢資料表
  204. /// </summary>
  205. /// <typeparam name="T">資料表類別</typeparam>
  206. /// <param name="conn">關聯式資料庫連線</param>
  207. /// <param name="filePath">完整檔案路徑</param>
  208. /// <param name="nodeName">完整節點名稱</param>
  209. /// <param name="commandTimeOut">查詢逾時</param>
  210. /// <param name="param">參數</param>
  211. /// <param name="trans">關聯式資料庫交易</param>
  212. /// <returns>資料集</returns>
  213. public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string filePath, string nodeName,
  214. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  215. {
  216. string query = GetScriptContent(filePath, nodeName);
  217. IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text);
  218. return dataset;
  219. }
  220. /// <summary>
  221. /// 查詢資料表
  222. /// </summary>
  223. /// <typeparam name="T">資料表類別</typeparam>
  224. /// <param name="conn">關聯式資料庫連線</param>
  225. /// <param name="filePath">檔案路徑</param>
  226. /// <param name="fileName">檔案名稱</param>
  227. /// <param name="nodeName">完整節點名稱</param>
  228. /// <param name="commandTimeOut">查詢逾時</param>
  229. /// <param name="param">參數</param>
  230. /// <param name="trans">關聯式資料庫交易</param>
  231. /// <returns>資料集</returns>
  232. public static async Task<IEnumerable<T>> GetDataAsync<T>(this IDbConnection conn, string filePath, string fileName, string nodeName,
  233. object param = null, IDbTransaction trans = null, int commandTimeOut = 300)
  234. {
  235. string query = GetScriptContent(filePath, fileName, nodeName);
  236. IEnumerable<T> dataset = await conn.QueryAsync<T>(query, param, trans, commandTimeOut, CommandType.Text);
  237. return dataset;
  238. }
  239. #endregion
  240. #region === [Create & Update & Delete] ===
  241. /// <summary>
  242. /// 更新資料
  243. /// </summary>
  244. /// <param name="conn">關聯式資料庫連線</param>
  245. /// <param name="query">資料庫語法</param>
  246. /// <param name="param">參數</param>
  247. /// <param name="trans">關聯式資料庫交易</param>
  248. /// <returns>異動筆數</returns>
  249. public static int UpdateData(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null)
  250. {
  251. return conn.Execute(query, param, trans);
  252. }
  253. /// <summary>
  254. /// 更新資料
  255. /// </summary>
  256. /// <param name="conn">關聯式資料庫連線</param>
  257. /// <param name="filePath">完整檔案路徑</param>
  258. /// <param name="nodeName">完整節點名稱</param>
  259. /// <param name="param">參數</param>
  260. /// <param name="trans">關聯式資料庫交易</param>
  261. /// <returns>異動筆數</returns>
  262. public static int UpdateData(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null)
  263. {
  264. string query = GetScriptContent(filePath, nodeName);
  265. return conn.Execute(query, param, trans);
  266. }
  267. /// <summary>
  268. /// 更新資料
  269. /// </summary>
  270. /// <param name="conn">關聯式資料庫連線</param>
  271. /// <param name="filePath">檔案路徑</param>
  272. /// <param name="fileName">檔案名稱</param>
  273. /// <param name="nodeName">完整節點名稱</param>
  274. /// <param name="param">參數</param>
  275. /// <param name="trans">關聯式資料庫交易</param>
  276. /// <returns>異動筆數</returns>
  277. public static int UpdateData(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null)
  278. {
  279. string query = GetScriptContent(filePath, fileName, nodeName);
  280. return conn.Execute(query, param, trans);
  281. }
  282. /// <summary>
  283. /// 更新資料表資料
  284. /// </summary>
  285. /// <param name="conn">關聯式資料庫連線</param>
  286. /// <param name="query">資料庫語法</param>
  287. /// <param name="param">參數</param>
  288. /// <param name="trans">資料庫交易</param>
  289. /// <returns>異動筆數</returns>
  290. public static Task<int> UpdateDataAsync(this IDbConnection conn, string query, object param = null, IDbTransaction trans = null)
  291. {
  292. return conn.ExecuteAsync(query, param, trans);
  293. }
  294. /// <summary>
  295. /// 更新資料
  296. /// </summary>
  297. /// <param name="conn">關聯式資料庫連線</param>
  298. /// <param name="filePath">完整檔案路徑</param>
  299. /// <param name="nodeName">完整節點名稱</param>
  300. /// <param name="param">參數</param>
  301. /// <param name="trans">關聯式資料庫交易</param>
  302. /// <returns>異動筆數</returns>
  303. public static Task<int> UpdateDataAsync(this IDbConnection conn, string filePath, string nodeName, object param = null, IDbTransaction trans = null)
  304. {
  305. string query = GetScriptContent(filePath, nodeName);
  306. return conn.ExecuteAsync(query, param, trans);
  307. }
  308. /// <summary>
  309. /// 更新資料
  310. /// </summary>
  311. /// <param name="conn">關聯式資料庫連線</param>
  312. /// <param name="filePath">檔案路徑</param>
  313. /// <param name="fileName">檔案名稱</param>
  314. /// <param name="nodeName">完整節點名稱</param>
  315. /// <param name="param">參數</param>
  316. /// <param name="trans">關聯式資料庫交易</param>
  317. /// <returns>異動筆數</returns>
  318. public static Task<int> UpdateDataAsync(this IDbConnection conn, string filePath, string fileName, string nodeName, object param = null, IDbTransaction trans = null)
  319. {
  320. string query = GetScriptContent(filePath, fileName, nodeName);
  321. return conn.ExecuteAsync(query, param, trans);
  322. }
  323. #endregion
  324. }
  325. }