DatabaseCommon.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Common;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Data.Dapper
  7. {
  8. public class DatabaseCommon<T> where T : class, new()
  9. {
  10. #region 拼接
  11. /// <summary>
  12. /// 拼接 查询 SQL语句,自定义条件
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="where">条件</param>
  16. /// <param name="allFieid">是否查询所有字段</param>
  17. /// <returns></returns>
  18. public static StringBuilder SelectSql(string where, bool allFieid = false)
  19. {
  20. //表名
  21. string table = EntityAttributeHelper.GetEntityTable<T>();
  22. PropertyInfo[] props = EntityAttributeHelper.GetProperties(typeof(T));
  23. StringBuilder sbColumns = new StringBuilder();
  24. if (allFieid)
  25. {
  26. sbColumns.Append(" * ");
  27. }
  28. else
  29. {
  30. foreach (PropertyInfo prop in props)
  31. {
  32. //string propertytype = prop.PropertyType.ToString();
  33. sbColumns.Append(prop.Name + ",");
  34. }
  35. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  36. }
  37. if (string.IsNullOrWhiteSpace(where)) where = " WHERE 1 = 1";
  38. string strSql = "SELECT {0} FROM {1} {2}";
  39. strSql = string.Format(strSql, sbColumns.ToString(), table + " ", where);
  40. return new StringBuilder(strSql);
  41. }
  42. /// <summary>
  43. /// 查询条数的sql
  44. /// </summary>
  45. /// <param name="where"></param>
  46. /// <returns></returns>
  47. public static StringBuilder SelectCountSql(string where)
  48. {
  49. //表名
  50. string table = EntityAttributeHelper.GetEntityTable<T>();
  51. PropertyInfo[] props = EntityAttributeHelper.GetProperties(typeof(T));
  52. StringBuilder sbColumns = new StringBuilder();
  53. if (string.IsNullOrWhiteSpace(where)) where = " WHERE 1 = 1";
  54. string strSql = $"SELECT COUNT(*) FROM {table} {where}";
  55. return new StringBuilder(strSql);
  56. }
  57. /// <summary>
  58. /// 拼接删除语句
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="where"></param>
  62. /// <param name="allFieid"></param>
  63. /// <returns></returns>
  64. public static StringBuilder DeleteSql(string where, bool allFieid = false)
  65. {
  66. if (string.IsNullOrWhiteSpace(where)) where = " WHERE 1 = 1";
  67. //表名
  68. string table = EntityAttributeHelper.GetEntityTable<T>();
  69. var sql = $"DELETE FROM {table} {where}";
  70. return new StringBuilder(sql);
  71. }
  72. /// <summary>
  73. /// 拼接删除语句
  74. /// </summary>
  75. /// <typeparam name="T"></typeparam>
  76. /// <param name="where"></param>
  77. /// <param name="allFieid"></param>
  78. /// <returns></returns>
  79. public static StringBuilder DeleteSqlById(int id, bool allFieid = false)
  80. {
  81. //表名
  82. string table = EntityAttributeHelper.GetEntityTable<T>();
  83. var sql = $"DELETE FROM {table} where id=@{id}";
  84. return new StringBuilder(sql);
  85. }
  86. /// <summary>
  87. /// 根据实体拼接新增sql语句
  88. /// </summary>
  89. /// <returns></returns>
  90. public static StringBuilder InsertSql()
  91. {
  92. string table = EntityAttributeHelper.GetEntityTable<T>();
  93. var values = GetInsertName();
  94. var sql = $"INSERT INTO {table}({values.Replace("@", "")}) values({values}) ";
  95. return new StringBuilder(sql);
  96. }
  97. /// <summary>
  98. /// 根据实体拼接修改sql语句
  99. /// </summary>
  100. /// <returns></returns>
  101. public static StringBuilder UpdateSql(T t)
  102. {
  103. string table = EntityAttributeHelper.GetEntityTable<T>();
  104. var values = GetInsertName();
  105. var key = EntityAttributeHelper.GetEntityKey<T>();
  106. var set = GetUpdateName(t, key);
  107. if (set.IsEmpty())
  108. return new StringBuilder();
  109. var sql = $"UPDATE {table} SET {set} WHERE {key}=@{key}";
  110. return new StringBuilder(sql);
  111. }
  112. /// <summary>
  113. /// 根据实体拼接删除sql语句
  114. /// </summary>
  115. /// <returns></returns>
  116. public static StringBuilder DeleteSql(T t)
  117. {
  118. string table = EntityAttributeHelper.GetEntityTable<T>();
  119. var values = GetInsertName();
  120. var key = EntityAttributeHelper.GetEntityKey<T>();
  121. var sql = $"DELETE FROM {table} WHERE {key}=@{key}";
  122. return new StringBuilder(sql);
  123. }
  124. #endregion
  125. private static string GetInsertName()
  126. {
  127. var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
  128. if (properties.Length <= 0)
  129. {
  130. return "";
  131. }
  132. var key = EntityAttributeHelper.GetEntityKey<T>();
  133. var list = new List<string>();
  134. foreach (System.Reflection.PropertyInfo item in properties)
  135. {
  136. if (key != item.Name)
  137. {
  138. list.Add("@" + item.Name);
  139. }
  140. }
  141. return list.Join();
  142. }
  143. private static string GetUpdateName(T t, string key)
  144. {
  145. var str = "";
  146. var properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
  147. if (properties.Length <= 0)
  148. {
  149. return "";
  150. }
  151. var keyValue = "";
  152. var list = new List<string>();
  153. foreach (System.Reflection.PropertyInfo item in properties)
  154. {
  155. if (key != item.Name)
  156. {
  157. str += $"{item.Name}=@{item.Name},";
  158. }
  159. else
  160. {
  161. keyValue = item.GetValue(t, null) != null ? item.GetValue(t, null).ToString() : "";
  162. }
  163. }
  164. if (keyValue.IsEmpty() || keyValue == "0")
  165. return string.Empty;
  166. return str.Substring(0, str.Length - 1);
  167. }
  168. }
  169. }