From 9748e1e67d1242c99ada2687320a331357bf886e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 6 Oct 2022 13:49:05 +0800 Subject: [PATCH] Synchronization code --- .../Oracle/SqlBuilder/OracleInsertBuilder.cs | 122 +++++++++++------- 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs index 15e742a42..89e1e48be 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs @@ -49,57 +49,89 @@ namespace SqlSugar } else { - StringBuilder batchInsetrSql = new StringBuilder(); - batchInsetrSql.AppendLine("INSERT ALL"); - foreach (var item in groupList) + var bigSize = 500; + if (groupList.Count < bigSize) { - batchInsetrSql.Append("INTO " + GetTableNameString + " "); - string insertColumns = ""; - - batchInsetrSql.Append("("); - batchInsetrSql.Append(columnsString); - if (identities.HasValue()) - { - batchInsetrSql.Append("," + string.Join(",", identities.Select(it => Builder.GetTranslationColumnName(it.DbColumnName)))); - } - batchInsetrSql.Append(") VALUES"); - - - batchInsetrSql.Append("("); - insertColumns = string.Join(",", item.Select(it =>FormatValue(it.Value,it.PropertyName))); - batchInsetrSql.Append(insertColumns); - if (identities.HasValue()) - { - batchInsetrSql.Append(","); - foreach (var idn in identities) - { - var seqvalue = this.OracleSeqInfoList[idn.OracleSequenceName]; - this.OracleSeqInfoList[idn.OracleSequenceName] = this.OracleSeqInfoList[idn.OracleSequenceName] + 1; - if (identities.Last() == idn) - { - batchInsetrSql.Append(seqvalue ); - } - else - { - batchInsetrSql.Append(seqvalue + ","); - } - } - } - batchInsetrSql.AppendLine(") "); - + string result = Small(identities, groupList, columnsString); + return result; } - if (identities.HasValue()) + else { - batchInsetrSql.AppendLine("SELECT "+ (this.OracleSeqInfoList.First().Value-1) + " FROM DUAL"); + string result = Big(identities, groupList, columnsString); + return result; } - else - { - batchInsetrSql.AppendLine("SELECT 1 FROM DUAL"); - } - var result= batchInsetrSql.ToString(); - return result; } } + private string Big(List identities, List> groupList, string columnsString) + { + this.Context.Utilities.PageEach(groupList, 100, groupListPasge => + { + var sql = Small(identities, groupListPasge, columnsString); + this.Context.Ado.ExecuteCommand(sql); + }); + if (identities!=null&identities.Count > 0&& this.OracleSeqInfoList!=null&& this.OracleSeqInfoList.Any()) + { + return $"SELECT {this.OracleSeqInfoList.First().Value-1} FROM DUAL"; + } + else + { + return $"SELECT {groupList.Count} FROM DUAL"; + } + } + + private string Small(List identities, List> groupList, string columnsString) + { + StringBuilder batchInsetrSql = new StringBuilder(); + batchInsetrSql.AppendLine("INSERT ALL"); + foreach (var item in groupList) + { + batchInsetrSql.Append("INTO " + GetTableNameString + " "); + string insertColumns = ""; + + batchInsetrSql.Append("("); + batchInsetrSql.Append(columnsString); + if (identities.HasValue()) + { + batchInsetrSql.Append("," + string.Join(",", identities.Select(it => Builder.GetTranslationColumnName(it.DbColumnName)))); + } + batchInsetrSql.Append(") VALUES"); + + + batchInsetrSql.Append("("); + insertColumns = string.Join(",", item.Select(it => FormatValue(it.Value, it.PropertyName))); + batchInsetrSql.Append(insertColumns); + if (identities.HasValue()) + { + batchInsetrSql.Append(","); + foreach (var idn in identities) + { + var seqvalue = this.OracleSeqInfoList[idn.OracleSequenceName]; + this.OracleSeqInfoList[idn.OracleSequenceName] = this.OracleSeqInfoList[idn.OracleSequenceName] + 1; + if (identities.Last() == idn) + { + batchInsetrSql.Append(seqvalue); + } + else + { + batchInsetrSql.Append(seqvalue + ","); + } + } + } + batchInsetrSql.AppendLine(") "); + + } + if (identities.HasValue()) + { + batchInsetrSql.AppendLine("SELECT " + (this.OracleSeqInfoList.First().Value - 1) + " FROM DUAL"); + } + else + { + batchInsetrSql.AppendLine("SELECT 1 FROM DUAL"); + } + var result = batchInsetrSql.ToString(); + return result; + } + int i = 0; public object FormatValue(object value,string name) {