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.

289 lines
11 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. namespace SqlSugar
  7. {
  8. public class UpdateBuilder : IDMLBuilder
  9. {
  10. public UpdateBuilder()
  11. {
  12. this.sql = new StringBuilder();
  13. this.DbColumnInfoList = new List<DbColumnInfo>();
  14. this.SetValues = new List<KeyValuePair<string, string>>();
  15. this.WhereValues = new List<string>();
  16. this.Parameters = new List<SugarParameter>();
  17. }
  18. public SqlSugarClient Context { get; set; }
  19. public ILambdaExpressions LambdaExpressions { get; set; }
  20. public ISqlBuilder Builder { get; set; }
  21. public StringBuilder sql { get; set; }
  22. public List<SugarParameter> Parameters { get; set; }
  23. public string TableName { get; set; }
  24. public string TableWithString { get; set; }
  25. public List<DbColumnInfo> DbColumnInfoList { get; set; }
  26. public List<string> WhereValues { get; set; }
  27. public List<KeyValuePair<string, string>> SetValues { get; set; }
  28. public bool IsNoUpdateNull { get; set; }
  29. public List<string> PrimaryKeys { get; set; }
  30. public bool IsOffIdentity { get; set; }
  31. public virtual string SqlTemplate
  32. {
  33. get
  34. {
  35. return @"UPDATE {0} SET
  36. {1} {2}";
  37. }
  38. }
  39. public virtual string SqlTemplateBatch
  40. {
  41. get
  42. {
  43. return @"UPDATE S SET {0} FROM {1} S {2} INNER JOIN ";
  44. }
  45. }
  46. public virtual string SqlTemplateJoin
  47. {
  48. get
  49. {
  50. return @" (
  51. {0}
  52. ) T ON {1}
  53. ; ";
  54. }
  55. }
  56. public virtual string SqlTemplateBatchSet
  57. {
  58. get
  59. {
  60. return "{0} AS {1}";
  61. }
  62. }
  63. public virtual string SqlTemplateBatchSelect
  64. {
  65. get
  66. {
  67. return "{0} AS {1}";
  68. }
  69. }
  70. public virtual string SqlTemplateBatchUnion
  71. {
  72. get
  73. {
  74. return "\t\t\r\nUNION ALL ";
  75. }
  76. }
  77. public virtual void Clear()
  78. {
  79. }
  80. public virtual string GetTableNameString
  81. {
  82. get
  83. {
  84. var result = Builder.GetTranslationTableName(TableName);
  85. result += UtilConstants.Space;
  86. if (this.TableWithString.HasValue())
  87. {
  88. result += TableWithString + UtilConstants.Space;
  89. }
  90. return result;
  91. }
  92. }
  93. public virtual string GetTableNameStringNoWith
  94. {
  95. get
  96. {
  97. var result = Builder.GetTranslationTableName(TableName);
  98. return result;
  99. }
  100. }
  101. public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType, bool isMapping = true)
  102. {
  103. var resolveExpress = this.LambdaExpressions;
  104. this.LambdaExpressions.Clear();
  105. if (isMapping)
  106. {
  107. resolveExpress.MappingColumns = Context.MappingColumns;
  108. resolveExpress.MappingTables = Context.MappingTables;
  109. resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
  110. resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices?.SqlFuncServices;
  111. }
  112. resolveExpress.Resolve(expression, resolveType);
  113. this.Parameters.AddRange(resolveExpress.Parameters);
  114. var result = resolveExpress.Result;
  115. return result;
  116. }
  117. public virtual string ToSqlString()
  118. {
  119. if (IsNoUpdateNull)
  120. {
  121. DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
  122. }
  123. var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
  124. var isSingle = groupList.Count() == 1;
  125. if (isSingle)
  126. {
  127. return ToSingleSqlString(groupList);
  128. }
  129. else
  130. {
  131. return TomultipleSqlString(groupList);
  132. }
  133. }
  134. protected virtual string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
  135. {
  136. Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
  137. var pageSize = 200;
  138. var pageIndex = 1;
  139. var totalRecord = groupList.Count;
  140. var pageCount = (totalRecord + pageSize - 1) / pageSize;
  141. var batchUpdateSql = new StringBuilder();
  142. while (pageCount >= pageIndex)
  143. {
  144. var updateTable = new StringBuilder();
  145. var setValues = string.Join(",", groupList.First().Where(it => !it.IsPrimarykey && (!it.IsIdentity || (IsOffIdentity && it.IsIdentity))).Select(it =>
  146. {
  147. if (SetValues.IsValuable())
  148. {
  149. var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
  150. if (setValue != null && setValue.Any())
  151. {
  152. return setValue.First().Value;
  153. }
  154. }
  155. var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
  156. return result;
  157. }));
  158. batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
  159. var i = 0;
  160. foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
  161. {
  162. var isFirst = i == 0;
  163. if (!isFirst)
  164. {
  165. updateTable.Append(SqlTemplateBatchUnion);
  166. }
  167. updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), Builder.GetTranslationColumnName(it.DbColumnName)))));
  168. ++i;
  169. }
  170. pageIndex++;
  171. updateTable.Append("\r\n");
  172. string whereString = null;
  173. if (this.WhereValues.HasValue())
  174. {
  175. foreach (var item in WhereValues)
  176. {
  177. var isFirst = whereString == null;
  178. whereString += (isFirst ? null : " AND ");
  179. whereString += item;
  180. }
  181. }
  182. else if (PrimaryKeys.HasValue())
  183. {
  184. foreach (var item in PrimaryKeys)
  185. {
  186. var isFirst = whereString == null;
  187. whereString += (isFirst ? null : " AND ");
  188. whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
  189. }
  190. }
  191. batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString);
  192. }
  193. return batchUpdateSql.ToString();
  194. }
  195. protected virtual string ToSingleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
  196. {
  197. var columnsString = string.Join(",", groupList.First().Where(it => !it.IsPrimarykey && (!it.IsIdentity || (IsOffIdentity && it.IsIdentity))).Select(it =>
  198. {
  199. if (SetValues.IsValuable())
  200. {
  201. var setValue = SetValues.Where(sv => !it.IsPrimarykey && (!it.IsIdentity || (IsOffIdentity && it.IsIdentity))).Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName) || sv.Key == Builder.GetTranslationColumnName(it.PropertyName));
  202. if (setValue != null && setValue.Any())
  203. {
  204. return setValue.First().Value;
  205. }
  206. }
  207. var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + this.Context.Ado.SqlParameterKeyWord + it.DbColumnName;
  208. return result;
  209. }));
  210. string whereString = null;
  211. if (this.WhereValues.HasValue())
  212. {
  213. foreach (var item in WhereValues)
  214. {
  215. var isFirst = whereString == null;
  216. whereString += (isFirst ? " WHERE " : " AND ");
  217. whereString += item;
  218. }
  219. }
  220. else if (PrimaryKeys.HasValue())
  221. {
  222. foreach (var item in PrimaryKeys)
  223. {
  224. var isFirst = whereString == null;
  225. whereString += (isFirst ? " WHERE " : " AND ");
  226. whereString += Builder.GetTranslationColumnName(item) + "=" + this.Context.Ado.SqlParameterKeyWord + item;
  227. }
  228. }
  229. return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
  230. }
  231. public virtual object FormatValue(object value)
  232. {
  233. if (value == null)
  234. {
  235. return "NULL";
  236. }
  237. else
  238. {
  239. var type = value.GetType();
  240. if (type == UtilConstants.DateType)
  241. {
  242. var date = value.ObjToDate();
  243. if (date < Convert.ToDateTime("1900-1-1"))
  244. {
  245. date = Convert.ToDateTime("1900-1-1");
  246. }
  247. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  248. }
  249. else if (type == UtilConstants.ByteArrayType)
  250. {
  251. var bytesString = "0x" + BitConverter.ToString((byte[])value);
  252. return bytesString;
  253. }
  254. else if (type.IsEnum())
  255. {
  256. return Convert.ToInt64(value);
  257. }
  258. else if (type == UtilConstants.BoolType)
  259. {
  260. return value.ObjToBool() ? "1" : "0";
  261. }
  262. else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
  263. {
  264. return "N'" + value.ToString().ToSqlFilter() + "'";
  265. }
  266. else
  267. {
  268. return "N'" + value.ToString() + "'";
  269. }
  270. }
  271. }
  272. }
  273. }