diff --git a/XCode/DataAccessLayer/Common/DbSession.cs b/XCode/DataAccessLayer/Common/DbSession.cs index 7a6837df1..7aadd0ae3 100644 --- a/XCode/DataAccessLayer/Common/DbSession.cs +++ b/XCode/DataAccessLayer/Common/DbSession.cs @@ -763,8 +763,8 @@ public virtual Task ExecuteAsync(DbCommand cmd, Boolean query, Func批量插入或更新 /// 数据表 /// 要插入的字段,默认所有字段 - /// 主键已存在时,要更新的字段 - /// 主键已存在时,要累加更新的字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 /// 实体列表 /// public virtual Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) => throw new NotSupportedException(); @@ -835,6 +835,12 @@ protected virtual void BuildBatchValues(StringBuilder sb, DbBase db, String acti sb.Length--; } + /// 生成重复键更新语句 + /// SQL拼接器 + /// 数据库 + /// 所有字段 + /// 需要更新的字段。属性名,不是字段名 + /// 需要累加的字段。属性名,不是字段名 protected virtual void BuildDuplicateKey(StringBuilder sb, DbBase db, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns) { // 重复键执行update @@ -1024,7 +1030,7 @@ public void WriteSQL(String sql) sb.AppendFormat("{0}={1}", ps[i].ParameterName, sv); } sb.Append(']'); - sql = sb.Put(true); + sql = sb.Return(true); } // 截断超长字符串 diff --git a/XCode/DataAccessLayer/Common/IDbSession.cs b/XCode/DataAccessLayer/Common/IDbSession.cs index 7a5f136b5..d531df8bf 100644 --- a/XCode/DataAccessLayer/Common/IDbSession.cs +++ b/XCode/DataAccessLayer/Common/IDbSession.cs @@ -168,8 +168,8 @@ public interface IDbSession : IDisposable2 /// 批量更新 /// 数据表 /// 要更新的字段,默认所有字段 - /// 要更新的字段,默认脏数据 - /// 要累加更新的字段,默认累加 + /// 要更新的字段,默认脏数据。属性名,不是字段名 + /// 要累加更新的字段,默认累加。属性名,不是字段名 /// 实体列表 /// Int32 Update(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list); @@ -177,8 +177,8 @@ public interface IDbSession : IDisposable2 /// 批量插入或更新 /// 数据表 /// 要插入的字段,默认所有字段 - /// 主键已存在时,要更新的字段 - /// 主键已存在时,要累加更新的字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 /// 实体列表 /// Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list); diff --git a/XCode/DataAccessLayer/Database/DB2.cs b/XCode/DataAccessLayer/Database/DB2.cs index 4d59c2dfa..6378779f0 100644 --- a/XCode/DataAccessLayer/Database/DB2.cs +++ b/XCode/DataAccessLayer/Database/DB2.cs @@ -449,7 +449,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection sb.Length--; sb.Append(')'); - return sb.Put(true); + return sb.Return(true); } private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection ps, IEnumerable list) @@ -478,6 +478,13 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var ps = new HashSet(); @@ -502,7 +509,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio } sb.AppendLine("END;"); - var sql = sb.Put(true); + var sql = sb.Return(true); DefaultSpan.Current?.AppendTag(sql); var dps = GetParameters(columns, ps, list); @@ -552,7 +559,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio } sb.Length -= " And ".Length; - return sb.Put(true); + return sb.Return(true); } public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) diff --git a/XCode/DataAccessLayer/Database/DaMeng.cs b/XCode/DataAccessLayer/Database/DaMeng.cs index b2cc932c1..41e9d59c2 100644 --- a/XCode/DataAccessLayer/Database/DaMeng.cs +++ b/XCode/DataAccessLayer/Database/DaMeng.cs @@ -334,7 +334,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection sb.Length--; sb.Append(')'); - return sb.Put(true); + return sb.Return(true); } private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection ps, IEnumerable list) @@ -363,6 +363,13 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var ps = new HashSet(); @@ -387,7 +394,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio } sb.AppendLine("END;"); - var sql = sb.Put(true); + var sql = sb.Return(true); DefaultSpan.Current?.AppendTag(sql); var dps = GetParameters(columns, ps, list); @@ -437,7 +444,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio } sb.Length -= " And ".Length; - return sb.Put(true); + return sb.Return(true); } public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) diff --git a/XCode/DataAccessLayer/Database/Hana.cs b/XCode/DataAccessLayer/Database/Hana.cs index 3a845a2f5..03d3c6a38 100644 --- a/XCode/DataAccessLayer/Database/Hana.cs +++ b/XCode/DataAccessLayer/Database/Hana.cs @@ -267,7 +267,7 @@ on duplicate key update updatetime=values(updatetime); */ - private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -284,7 +284,7 @@ private String GetBatchSql(String action, IDataTable table, IDataColumn[] column // 重复键执行update BuildDuplicateKey(sb, db, columns, updateColumns, addColumns); - return sb.Put(true); + return sb.Return(true); } public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable list) @@ -305,7 +305,14 @@ public override Int32 Replace(IDataTable table, IDataColumn[] columns, IEnumerab return Execute(sql); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); return Execute(sql); @@ -586,7 +593,7 @@ public override String CreateTableSQL(IDataTable table) sb.Append(" DEFAULT CHARSET=utf8mb4"); sb.Append(';'); - return sb.Put(true); + return sb.Return(true); } public override String? AddTableDescriptionSQL(IDataTable table) diff --git a/XCode/DataAccessLayer/Database/HighGo.cs b/XCode/DataAccessLayer/Database/HighGo.cs index ff9839f17..b76649f59 100644 --- a/XCode/DataAccessLayer/Database/HighGo.cs +++ b/XCode/DataAccessLayer/Database/HighGo.cs @@ -96,7 +96,7 @@ public override Task QueryCountFastAsync(String tableName) #endregion #region 批量操作 - string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -154,14 +154,22 @@ string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, IColl sb.Length--; } } - return sb.Put(true); + return sb.Return(true); } public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, null, null, list); return Execute(sql); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); return Execute(sql); @@ -430,7 +438,7 @@ public override String CreateIndexSQL(IDataIndex index) var dcs = index.Table.GetColumns(index.Columns); sb.AppendFormat(" On {0} USING btree ({1})", FormatName(index.Table), dcs.Join(",", FormatName)); - return sb.Put(true); + return sb.Return(true); } #endregion } \ No newline at end of file diff --git a/XCode/DataAccessLayer/Database/KingBase.cs b/XCode/DataAccessLayer/Database/KingBase.cs index a10ed5c92..c9d74c716 100644 --- a/XCode/DataAccessLayer/Database/KingBase.cs +++ b/XCode/DataAccessLayer/Database/KingBase.cs @@ -116,7 +116,7 @@ public override Task InsertAndGetIdentityAsync(String sql, CommandType ty } #region 批量操作 - string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -133,14 +133,23 @@ string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, IColl // 重复键执行update BuildDuplicateKey(sb, db, columns, updateColumns, addColumns); - return sb.Put(true); + return sb.Return(true); } + public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, null, null, list); return Execute(sql); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); return Execute(sql); @@ -488,7 +497,7 @@ public override String CreateTableSQL(IDataTable table) sb.AppendLine(); sb.Append(");"); //if (!string.IsNullOrWhiteSpace(table.Description)) { sb.Append($" COMMENT '{table.Description}'"); } 非Mysql兼容模式下,不支持,所以直接注释掉 - return sb.Put(true); + return sb.Return(true); } public override String DropTableSQL(IDataTable table) => $"Drop Table If Exists {FormatName(table)}"; public override String? AddTableDescriptionSQL(IDataTable table) @@ -516,7 +525,7 @@ public override String CreateIndexSQL(IDataIndex index) var dcs = index.Table.GetColumns(index.Columns); sb.AppendFormat(" On {0} USING btree ({1})", FormatName(index.Table), dcs.Join(",", FormatName)); - return sb.Put(true); + return sb.Return(true); } public override String DropIndexSQL(IDataIndex index) => $"DROP INDEX IF EXISTS \"{index.Name}\""; //public virtual String AlertSequenceSQL(IDataTable table) diff --git a/XCode/DataAccessLayer/Database/MySql.cs b/XCode/DataAccessLayer/Database/MySql.cs index 26c74c712..3294b9691 100755 --- a/XCode/DataAccessLayer/Database/MySql.cs +++ b/XCode/DataAccessLayer/Database/MySql.cs @@ -348,6 +348,13 @@ public override Int32 Replace(IDataTable table, IDataColumn[] columns, IEnumerab return Execute(sql); } + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); @@ -723,7 +730,7 @@ public override String CreateTableSQL(IDataTable table) sb.Append(" DEFAULT CHARSET=utf8mb4"); sb.Append(';'); - return sb.Put(true); + return sb.Return(true); } public override String AddTableDescriptionSQL(IDataTable table) diff --git a/XCode/DataAccessLayer/Database/Oracle.cs b/XCode/DataAccessLayer/Database/Oracle.cs index 2580a8d18..bc55eab82 100644 --- a/XCode/DataAccessLayer/Database/Oracle.cs +++ b/XCode/DataAccessLayer/Database/Oracle.cs @@ -586,7 +586,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection sb.Length--; sb.Append(')'); - return sb.Put(true); + return sb.Return(true); } private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection ps, IEnumerable list) @@ -615,7 +615,14 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var ps = new HashSet(); var insert = GetInsertSql(table, columns, ps); @@ -639,7 +646,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio } sb.AppendLine("END;"); - var sql = sb.Put(true); + var sql = sb.Return(true); DefaultSpan.Current?.AppendTag(sql); var dps = GetParameters(columns, ps, list); @@ -647,7 +654,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio return Execute(sql, CommandType.Text, dps); } - private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, ICollection ps) + private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, ICollection ps) { if ((updateColumns == null || updateColumns.Count == 0) && (addColumns == null || addColumns.Count == 0)) return null; @@ -689,10 +696,10 @@ private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection } sb.Length -= " And ".Length; - return sb.Put(true); + return sb.Return(true); } - public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var ps = new HashSet(); var sql = GetUpdateSql(table, columns, updateColumns, addColumns, ps); diff --git a/XCode/DataAccessLayer/Database/PostgreSQL.cs b/XCode/DataAccessLayer/Database/PostgreSQL.cs index e73d7e39c..5eed3dc2f 100644 --- a/XCode/DataAccessLayer/Database/PostgreSQL.cs +++ b/XCode/DataAccessLayer/Database/PostgreSQL.cs @@ -362,10 +362,17 @@ public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerabl sb.Append(" Values"); BuildBatchValues(sb, db, action, table, columns, list); - var sql = sb.Put(true); + var sql = sb.Return(true); return Execute(sql); } + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { /* diff --git a/XCode/DataAccessLayer/Database/SQLite.cs b/XCode/DataAccessLayer/Database/SQLite.cs index a82ebbc99..ac6e37701 100755 --- a/XCode/DataAccessLayer/Database/SQLite.cs +++ b/XCode/DataAccessLayer/Database/SQLite.cs @@ -341,7 +341,7 @@ on duplicate key update updatetime=values(updatetime); */ - private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -400,7 +400,7 @@ private String GetBatchSql(String action, IDataTable table, IDataColumn[] column sb.Length--; } } - return sb.Put(true); + return sb.Return(true); } public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable list) @@ -421,7 +421,14 @@ public override Int32 Replace(IDataTable table, IDataColumn[] columns, IEnumerab return Execute(sql); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); return Execute(sql); diff --git a/XCode/DataAccessLayer/Database/SqlServer.cs b/XCode/DataAccessLayer/Database/SqlServer.cs index 45ed69882..afa5d18ce 100644 --- a/XCode/DataAccessLayer/Database/SqlServer.cs +++ b/XCode/DataAccessLayer/Database/SqlServer.cs @@ -185,7 +185,7 @@ private String FormatSqlserver2012SQL(String sql) if (!String.IsNullOrEmpty(builder.Having)) sb.Append(" Having " + builder.Having); if (!String.IsNullOrEmpty(builder.OrderBy)) sb.Append(" Order By " + builder.OrderBy); - return sb.Put(true); + return sb.Return(true); } public override SelectBuilder PageSplit(SelectBuilder builder, Int64 startRowIndex, Int64 maximumRows) @@ -644,10 +644,17 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection sb.Length--; sb.Append(')'); - return sb.Put(true); + return sb.Return(true); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var ps = new HashSet(); var insert = GetInsertSql(table, columns, ps); @@ -662,14 +669,14 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio sb.Append(insert); sb.AppendLine(";"); sb.AppendLine("END;"); - var sql = sb.Put(true); + var sql = sb.Return(true); DefaultSpan.Current?.AppendTag(sql); var dpsList = GetParametersList(columns, ps, list, true); return BatchExecute(sql, dpsList); } - private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, ICollection ps) + private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, ICollection ps) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -714,7 +721,7 @@ private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection } sb.Length -= " And ".Length; - return sb.Put(true); + return sb.Return(true); } #endregion diff --git a/XCode/DataAccessLayer/Database/TDengine.cs b/XCode/DataAccessLayer/Database/TDengine.cs index 771c20eb3..dabcc4c55 100644 --- a/XCode/DataAccessLayer/Database/TDengine.cs +++ b/XCode/DataAccessLayer/Database/TDengine.cs @@ -188,7 +188,7 @@ on duplicate key update updatetime=values(updatetime); */ - private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; @@ -205,7 +205,7 @@ private String GetBatchSql(String action, IDataTable table, IDataColumn[] column // 重复键执行update BuildDuplicateKey(sb, db, columns, updateColumns, addColumns); - return sb.Put(true); + return sb.Return(true); } public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable list) @@ -214,7 +214,14 @@ public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerabl return Execute(sql); } - public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection updateColumns, ICollection addColumns, IEnumerable list) + /// 批量插入或更新 + /// 数据表 + /// 要插入的字段,默认所有字段 + /// 主键已存在时,要更新的字段。属性名,不是字段名 + /// 主键已存在时,要累加更新的字段。属性名,不是字段名 + /// 实体列表 + /// + public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection? updateColumns, ICollection? addColumns, IEnumerable list) { var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list); return Execute(sql); @@ -397,7 +404,7 @@ public override String CreateTableSQL(IDataTable table) } sb.Append(';'); - return sb.Put(true); + return sb.Return(true); } public override String AddTableDescriptionSQL(IDataTable table) => diff --git a/XCode/Entity/EntityExtension.cs b/XCode/Entity/EntityExtension.cs index 076c4d0fd..995ce8d67 100644 --- a/XCode/Entity/EntityExtension.cs +++ b/XCode/Entity/EntityExtension.cs @@ -443,7 +443,7 @@ public static IList Valid(this IEnumerable list, Boolean isNew) where T /// public static Int32 BatchInsert(this IEnumerable list, IDataColumn[] columns, IEntitySession? session = null) where T : IEntity { - var option = new BatchOption { Columns = columns }; + var option = new BatchOption(columns, null, null); return BatchInsert(list, option, session); } @@ -537,7 +537,7 @@ public static Int32 BatchInsert(this IEnumerable list, BatchOption? option /// public static Int32 BatchInsertIgnore(this IEnumerable list, IDataColumn[] columns, IEntitySession? session = null) where T : IEntity { - var option = new BatchOption { Columns = columns }; + var option = new BatchOption(columns, null, null); return BatchInsertIgnore(list, option, session); } @@ -626,7 +626,7 @@ public static Int32 BatchInsertIgnore(this IEnumerable list, BatchOption? /// public static Int32 BatchReplace(this IEnumerable list, IDataColumn[] columns, IEntitySession? session = null) where T : IEntity { - var option = new BatchOption { Columns = columns }; + var option = new BatchOption(columns, null, null); return BatchReplace(list, option, session); } @@ -719,7 +719,7 @@ public static Int32 BatchReplace(this IEnumerable list, BatchOption? optio /// public static Int32 BatchUpdate(this IEnumerable list, IDataColumn[] columns, ICollection? updateColumns = null, ICollection? addColumns = null, IEntitySession? session = null) where T : IEntity { - var option = new BatchOption { Columns = columns, UpdateColumns = updateColumns, AddColumns = addColumns }; + var option = new BatchOption(columns, updateColumns, addColumns); return BatchUpdate(list, option, session); } @@ -757,7 +757,8 @@ public static Int32 BatchUpdate(this IEnumerable list, BatchOption? option // 创建时间等字段不参与Update dirtys = dirtys.Where(e => !e.Name.StartsWithIgnoreCase("Create")).ToArray(); - if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => dal.Db.FormatName(e)).ToArray(); + // 统一约定,updateColumns 外部传入Name,内部再根据columns转为专用字段名 + if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => e.Name).ToArray(); } var updateColumns = option.UpdateColumns; var addColumns = option.AddColumns ??= fact.AdditionalFields; @@ -811,7 +812,7 @@ public static Int32 BatchUpdate(this IEnumerable list, BatchOption? option /// public static Int32 Upsert(this IEnumerable list, IDataColumn[]? columns, ICollection? updateColumns = null, ICollection? addColumns = null, IEntitySession? session = null) where T : IEntity { - var option = new BatchOption { Columns = columns, UpdateColumns = updateColumns, AddColumns = addColumns }; + var option = new BatchOption(columns, updateColumns, addColumns); return BatchUpsert(list, option, session); } @@ -901,7 +902,8 @@ public static Int32 BatchUpsert(this IEnumerable list, BatchOption? option // 创建时间等字段不参与Update dirtys = dirtys.Where(e => !e.Name.StartsWithIgnoreCase("Create")).ToArray(); - if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => dal.Db.FormatName(e)).ToArray(); + // 统一约定,updateColumns 外部传入Name,内部再根据columns转为专用字段名 + if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => e.Name).ToArray(); } var updateColumns = option.UpdateColumns; var addColumns = option.AddColumns ??= fact.AdditionalFields; @@ -951,7 +953,7 @@ public static Int32 BatchUpsert(this IEnumerable list, BatchOption? option /// public static Int32 Upsert(this IEntity entity, IDataColumn[]? columns, ICollection? updateColumns = null, ICollection? addColumns = null, IEntitySession? session = null) { - var option = new BatchOption { Columns = columns, UpdateColumns = updateColumns, AddColumns = addColumns }; + var option = new BatchOption(columns, updateColumns, addColumns); return Upsert(entity, option, session); } diff --git a/XCode/Model/BatchOption.cs b/XCode/Model/BatchOption.cs index c7f9ac68b..ccf675efb 100644 --- a/XCode/Model/BatchOption.cs +++ b/XCode/Model/BatchOption.cs @@ -11,10 +11,10 @@ public class BatchOption /// 字段集合。为空时表示使用所有字段 public IDataColumn[]? Columns { get; set; } - /// 要更新的字段。用于Update/Upsert,默认脏数据 + /// 要更新的字段。用于Update/Upsert,默认脏数据。使用属性名而非字段名,各数据库生成SQL时从columns取字段名 public ICollection? UpdateColumns { get; set; } - /// 要累加更新的字段。用于Update/Upsert,默认累加 + /// 要累加更新的字段。用于Update/Upsert,默认累加。使用属性名而非字段名,各数据库生成SQL时从columns取字段名 public ICollection? AddColumns { get; set; } /// 是否完全插入所有字段。用于Insert/Upsert,默认false表示不插入没有脏数据的字段 @@ -22,4 +22,15 @@ public class BatchOption /// 批操作分批大小。默认0,使用数据链接设置或全局设置 public Int32 BatchSize { get; set; } + + /// 实例化批操作选项 + public BatchOption() { } + + /// 实例化批操作选项 + public BatchOption(IDataColumn[]? columns, ICollection? updateColumns, ICollection? addColumns) + { + Columns = columns; + UpdateColumns = updateColumns; + AddColumns = addColumns; + } }