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.

130 lines
5.4 KiB

2 years ago
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. namespace SqlSugar
  5. {
  6. public class OracleInsertBuilder : InsertBuilder
  7. {
  8. public override string SqlTemplate
  9. {
  10. get
  11. {
  12. return @"INSERT INTO {0}
  13. ({1})
  14. VALUES
  15. ({2}) ";
  16. }
  17. }
  18. public override string SqlTemplateBatch
  19. {
  20. get
  21. {
  22. return "INSERT INTO {0} ({1})";
  23. }
  24. }
  25. public override string ToSqlString()
  26. {
  27. var identities = this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).ToList();
  28. if (IsNoInsertNull)
  29. {
  30. DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
  31. }
  32. var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
  33. var isSingle = groupList.Count() == 1;
  34. var columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
  35. if (isSingle)
  36. {
  37. var columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.DbColumnName));
  38. if (identities.HasValue())
  39. {
  40. columnsString = columnsString.TrimEnd(',') + "," + string.Join(",", identities.Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
  41. columnParametersString = columnParametersString.TrimEnd(',') + "," + string.Join(",", identities.Select(it => it.OracleSequenceName + ".nextval"));
  42. }
  43. return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
  44. }
  45. else
  46. {
  47. var batchInsetrSql = new StringBuilder();
  48. var pageSize = 200;
  49. var pageIndex = 1;
  50. var totalRecord = groupList.Count;
  51. var pageCount = (totalRecord + pageSize - 1) / pageSize;
  52. if (identities.HasValue())
  53. {
  54. columnsString = columnsString.TrimEnd(',') + "," + string.Join(",", identities.Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
  55. }
  56. while (pageCount >= pageIndex)
  57. {
  58. batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
  59. var i = 0;
  60. foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
  61. {
  62. var isFirst = i == 0;
  63. if (!isFirst)
  64. {
  65. batchInsetrSql.Append(SqlTemplateBatchUnion);
  66. }
  67. var insertColumns = string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), Builder.GetTranslationColumnName(it.DbColumnName))));
  68. if (identities.HasValue())
  69. {
  70. insertColumns = insertColumns.TrimEnd(',') + "," + string.Join(",", identities.Select(it =>
  71. {
  72. var seqValue = this.OracleSeqInfoList[it.OracleSequenceName];
  73. this.OracleSeqInfoList[it.OracleSequenceName] = this.OracleSeqInfoList[it.OracleSequenceName] + 1;
  74. return seqValue + 1+" AS "+it.DbColumnName;
  75. }));
  76. }
  77. batchInsetrSql.Append("\r\n SELECT " + insertColumns + " FROM DUAL ");
  78. ++i;
  79. }
  80. pageIndex++;
  81. batchInsetrSql.Append("\r\n;\r\n");
  82. }
  83. return "BEGIN\r\n"+ batchInsetrSql.ToString()+"\r\nEND;";
  84. }
  85. }
  86. public override object FormatValue(object value)
  87. {
  88. if (value == null)
  89. {
  90. return "NULL";
  91. }
  92. else
  93. {
  94. var type = value.GetType();
  95. if (type == UtilConstants.DateType)
  96. {
  97. var date = value.ObjToDate();
  98. if (date < Convert.ToDateTime("1900-1-1"))
  99. {
  100. date = Convert.ToDateTime("1900-1-1");
  101. }
  102. return "to_date('"+ date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') ";
  103. }
  104. else if (type.IsEnum())
  105. {
  106. return Convert.ToInt64(value);
  107. }
  108. else if (type == UtilConstants.ByteArrayType)
  109. {
  110. var bytesString = "0x" + BitConverter.ToString((byte[])value);
  111. return bytesString;
  112. }
  113. else if (type == UtilConstants.BoolType)
  114. {
  115. return value.ObjToBool() ? "1" : "0";
  116. }
  117. else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
  118. {
  119. return "N'" + value.ToString().ToSqlFilter() + "'";
  120. }
  121. else
  122. {
  123. return "N'" + value.ToString() + "'";
  124. }
  125. }
  126. }
  127. }
  128. }