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.

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