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.

77 lines
2.8 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SqlSugar
  6. {
  7. public class SqliteUpdateBuilder : UpdateBuilder
  8. {
  9. protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
  10. {
  11. var sb = new StringBuilder();
  12. sb.AppendLine(string.Join("\r\n", groupList.Select(t =>
  13. {
  14. var updateTable = string.Format("UPDATE {0} SET", base.GetTableNameStringNoWith);
  15. var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(m)).ToArray());
  16. var pkList = t.Where(s => s.IsPrimarykey).ToList();
  17. var whereList = new List<string>();
  18. foreach (var item in pkList)
  19. {
  20. var isFirst = pkList.First() == item;
  21. var whereString = isFirst ? " " : " AND ";
  22. whereString += GetOracleUpdateColums(item);
  23. whereList.Add(whereString);
  24. }
  25. return string.Format("{0} {1} WHERE {2};", updateTable, setValues, string.Join("AND", whereList));
  26. }).ToArray()));
  27. return sb.ToString();
  28. }
  29. private string GetOracleUpdateColums(DbColumnInfo m)
  30. {
  31. return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(), FormatValue(m.Value));
  32. }
  33. public override object FormatValue(object value)
  34. {
  35. if (value == null)
  36. {
  37. return "NULL";
  38. }
  39. else
  40. {
  41. var type = value.GetType();
  42. if (type == UtilConstants.DateType)
  43. {
  44. var date = value.ObjToDate();
  45. if (date < Convert.ToDateTime("1900-1-1"))
  46. {
  47. date = Convert.ToDateTime("1900-1-1");
  48. }
  49. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  50. }
  51. else if (type.IsEnum())
  52. {
  53. return Convert.ToInt64(value);
  54. }
  55. else if (type == UtilConstants.ByteArrayType)
  56. {
  57. var bytesString = "0x" + BitConverter.ToString((byte[])value);
  58. return bytesString;
  59. }
  60. else if (type == UtilConstants.BoolType)
  61. {
  62. return value.ObjToBool() ? "1" : "0";
  63. }
  64. else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
  65. {
  66. return "'" + value.ToString().ToSqlFilter() + "'";
  67. }
  68. else
  69. {
  70. return "'" + value.ToString() + "'";
  71. }
  72. }
  73. }
  74. }
  75. }