MySqlBlukCopy.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using MySqlConnector;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace SqlSugar
  11. {
  12. public class MySqlBlukCopy<T>
  13. {
  14. internal SqlSugarProvider Context { get; set; }
  15. internal ISqlBuilder Builder { get; set; }
  16. internal T[] Entitys { get; set; }
  17. internal string Chara { get; set; }
  18. private MySqlBlukCopy()
  19. {
  20. }
  21. public MySqlBlukCopy(SqlSugarProvider context, ISqlBuilder builder, T []entitys)
  22. {
  23. this.Context = context;
  24. this.Builder = builder;
  25. this.Entitys = entitys;
  26. }
  27. public bool ExecuteBulkCopy(string characterSet)
  28. {
  29. this.Chara = characterSet;
  30. return ExecuteBulkCopy();
  31. }
  32. public bool ExecuteBulkCopy()
  33. {
  34. var IsBulkLoad = false;
  35. if (Entitys == null || Entitys.Length <= 0)
  36. return IsBulkLoad;
  37. if (Entitys.First() == null && Entitys.Length ==1)
  38. return IsBulkLoad;
  39. DataTable dt = new DataTable();
  40. Type type = typeof(T);
  41. var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
  42. dt.TableName = this.Builder.GetTranslationColumnName(entity.DbTableName);
  43. //if (this.Context.MappingTables != null && this.Context.MappingTables.Any(it => it.EntityName == it.EntityName))
  44. //{
  45. // dt.TableName = this.Builder.GetTranslationColumnName(this.Context.MappingTables.First(it => it.EntityName == it.EntityName).DbTableName);
  46. //}
  47. //创建属性的集合
  48. List<PropertyInfo> pList = new List<PropertyInfo>();
  49. //把所有的public属性加入到集合 并添加DataTable的列
  50. Array.ForEach(entity.Columns.ToArray(), p => {
  51. if (!p.IsIgnore&& !p.IsOnlyIgnoreInsert)
  52. {
  53. pList.Add(p.PropertyInfo); dt.Columns.Add(p.DbColumnName);
  54. }
  55. });
  56. DataRow row = null;
  57. foreach (T item in Entitys)
  58. {
  59. row = dt.NewRow();
  60. pList.ForEach(p =>
  61. {
  62. var name = p.Name;
  63. if (entity.Columns.Any(it => it.PropertyName == name))
  64. {
  65. name = entity.Columns.First(it => it.PropertyName == name).DbColumnName;
  66. }
  67. row[name] = GetValue(p, item);
  68. });
  69. dt.Rows.Add(row);
  70. }
  71. var dllPath =Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "failFiles");
  72. DirectoryInfo dir = new DirectoryInfo(dllPath);
  73. if (!dir.Exists)
  74. {
  75. dir.Create();
  76. }
  77. var fileName =Path.Combine( dllPath , Guid.NewGuid().ToString() + ".csv");
  78. var dataTableToCsv = DataTableToCsvString(dt);
  79. File.WriteAllText(fileName, dataTableToCsv, new UTF8Encoding(false));
  80. MySqlConnection conn = this.Context.Ado.Connection as MySqlConnection;
  81. try
  82. {
  83. this.Context.Ado.Open();
  84. // IsolationLevel.Parse
  85. MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
  86. {
  87. CharacterSet = GetChara(),
  88. FieldTerminator = ",",
  89. FieldQuotationCharacter = '"',
  90. EscapeCharacter = '"',
  91. LineTerminator = Environment.NewLine,
  92. FileName = fileName,
  93. NumberOfLinesToSkip = 0,
  94. TableName = dt.TableName,
  95. Local = true,
  96. };
  97. bulk.Columns.AddRange(dt.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).Distinct().ToArray());
  98. IsBulkLoad = bulk.Load() > 0;
  99. //执行成功才删除文件
  100. if (IsBulkLoad && File.Exists(fileName))
  101. {
  102. File.Delete(fileName);
  103. }
  104. }
  105. catch (MySqlException ex)
  106. {
  107. throw ex;
  108. }
  109. finally
  110. {
  111. CloseDb();
  112. }
  113. return IsBulkLoad; ;
  114. }
  115. public Task<bool> ExecuteBulkCopyAsync()
  116. {
  117. return Task.FromResult(ExecuteBulkCopy());
  118. }
  119. public Task<bool> ExecuteBulkCopyAsync(string characterSet)
  120. {
  121. this.Chara = characterSet;
  122. return Task.FromResult(ExecuteBulkCopy());
  123. }
  124. #region Helper
  125. private string GetChara()
  126. {
  127. if (this.Chara == null)
  128. {
  129. return "utf8mb4";
  130. }
  131. else
  132. {
  133. return this.Chara;
  134. }
  135. }
  136. private void CloseDb()
  137. {
  138. if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
  139. {
  140. this.Context.Ado.Connection.Close();
  141. }
  142. }
  143. /// <summary>
  144. ///DataTable to CSV
  145. /// </summary>
  146. /// <param name="table">datatable</param>
  147. /// <returns>CSV</returns>
  148. public string DataTableToCsvString(DataTable table)
  149. {
  150. if (table.Rows.Count == 0)
  151. return "";
  152. StringBuilder sb = new StringBuilder();
  153. DataColumn colum;
  154. foreach (DataRow row in table.Rows)
  155. {
  156. for (int i = 0; i < table.Columns.Count; i++)
  157. {
  158. colum = table.Columns[i];
  159. if (i != 0) sb.Append(",");
  160. if (colum.DataType == typeof(string) && (row[colum].ToString().Contains(",") || row[colum].ToString().Contains("\r") || row[colum].ToString().Contains("\"") || row[colum].ToString().Contains("\n")))
  161. {
  162. sb.Append("\"" + row[colum].ToString().Replace("\"", "\"\"") + "\"");
  163. }
  164. else if (colum.DataType == typeof(bool))
  165. {
  166. if (row[colum] == DBNull.Value)
  167. {
  168. sb.Append("NULL");
  169. }
  170. else
  171. {
  172. sb.Append(row[colum].ObjToBool() ? 1 : 0);
  173. }
  174. }
  175. else if (colum.DataType == UtilConstants.DateType&& row[colum] != null && row[colum] != DBNull.Value)
  176. {
  177. sb.Append(row[colum].ObjToDate().ToString("yyyy-MM-dd HH:mm:ss.fff"));
  178. }
  179. else if (row[colum] == null || row[colum] == DBNull.Value)
  180. {
  181. sb.Append("NULL");
  182. }
  183. else sb.Append(row[colum].ToString());
  184. }
  185. sb.AppendLine();
  186. }
  187. return sb.ToString();
  188. }
  189. private static object GetValue(PropertyInfo p, T item)
  190. {
  191. var result= p.GetValue(item, null);
  192. if (result != null && UtilMethods.GetUnderType(p.PropertyType) == UtilConstants.BoolType)
  193. {
  194. if (result.ObjToBool() == false)
  195. {
  196. result = null;
  197. }
  198. }
  199. return result;
  200. }
  201. #endregion
  202. }
  203. }