From 3e30501e1d89dbdd8a69cc51a506551e7ff3bb62 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 25 Mar 2023 17:11:27 +0800 Subject: [PATCH] Synchronization code --- .../Abstract/SaveableProvider/Storageable.cs | 24 +++++++++++++++++++ .../UpdateProvider/UpdateableHelper.cs | 1 + .../SqlSugar/Entities/DefaultCustom.cs | 18 ++++++++++++++ .../SqlSugar/Interface/IStorageable.cs | 3 +++ .../PostgreSQL/PostgreSQLProvider.cs | 2 ++ 5 files changed, 48 insertions(+) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 57dfd155b..55437e97d 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -89,6 +89,20 @@ namespace SqlSugar return this; } + public IStorageable DefaultAddElseUpdate() + { + var column = this.Context.EntityMaintenance.GetEntityInfo().Columns.FirstOrDefault(it=>it.IsPrimarykey); + if (column == null) Check.ExceptionEasy("DefaultAddElseUpdate() need primary key", "DefaultAddElseUpdate()这个方法只能用于主键"); + return this.SplitUpdate(it => + { + var itemPkValue = column.PropertyInfo.GetValue(it.Item); + var defaultValue =UtilMethods.GetDefaultValue(column.PropertyInfo.PropertyType); + var result= itemPkValue != null && itemPkValue.ObjToString() != defaultValue.ObjToString(); + return result; + + }).SplitInsert(it => true); + } + public int ExecuteCommand() { var result = 0; @@ -107,6 +121,16 @@ namespace SqlSugar result +=await x.AsUpdateable.ExecuteCommandAsync(); return result; } + public int ExecuteSqlBulkCopy() + { + var storage = this.ToStorage(); + return storage.BulkCopy() + storage.BulkUpdate(); + } + public async Task ExecuteSqlBulkCopyAsync() + { + var storage =await this.ToStorageAsync(); + return await storage.BulkCopyAsync() + await storage.BulkUpdateAsync(); + } public StorageableResult ToStorage() { if (whereFuncs == null || whereFuncs.Count == 0) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs index 9abd8147f..f7ba54fa2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs @@ -139,6 +139,7 @@ namespace SqlSugar CheckWhere(); PreToSql(); AutoRemoveDataCache(); + Check.ExceptionEasy(this.UpdateParameterIsNull&&this.UpdateBuilder.DbColumnInfoList.Count() == this.UpdateObjs.Length && this.UpdateObjs.Length==this.UpdateBuilder.DbColumnInfoList.Count(it => it.IsPrimarykey), "The primary key cannot be updated", "主键不能更新,更新主键会对代码逻辑存在未知隐患,如果非要更新:建议你删除在插入或者新建一个没主键的类。"); Check.Exception(UpdateBuilder.WhereValues.IsNullOrEmpty() && GetPrimaryKeys().IsNullOrEmpty(), "You cannot have no primary key and no conditions"); string sql = UpdateBuilder.ToSqlString(); ValidateVersion(); diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/DefaultCustom.cs b/Src/Asp.NetCore2/SqlSugar/Entities/DefaultCustom.cs index f5c130371..8341d9490 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/DefaultCustom.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/DefaultCustom.cs @@ -32,4 +32,22 @@ namespace SqlSugar.DbConvert return (T)Enum.Parse(undertype, str); } } + + + public class CommonPropertyConvert : ISugarDataConverter + { + public SugarParameter ParameterConverter(object columnValue, int columnIndex) + { + var name = "@Common" + columnIndex; + Type undertype = SqlSugar.UtilMethods.GetUnderType(typeof(T));//获取没有nullable的枚举类型 + return new SugarParameter(name, columnValue, undertype); + } + + public T QueryConverter(IDataRecord dr, int i) + { + + var value = dr.GetValue(i); + return (T)UtilMethods.ChangeType2(value, typeof(T)); + } + } } diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs index 0897a7a5b..82793b3b2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs @@ -27,6 +27,9 @@ namespace SqlSugar IStorageable As(string tableName); int ExecuteCommand(); Task ExecuteCommandAsync(); + int ExecuteSqlBulkCopy(); + Task ExecuteSqlBulkCopyAsync(); + IStorageable DefaultAddElseUpdate(); } public class StorageableInfo where T : class, new() diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs index 0fb3f5311..f47a46eef 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs @@ -203,6 +203,7 @@ namespace SqlSugar { typeof(bool[]),NpgsqlDbType.Boolean}, {typeof(DateTime[]),NpgsqlDbType.Date}, {typeof(float[]),NpgsqlDbType.Real}, + {typeof(Guid[]),NpgsqlDbType.Uuid}, { typeof(int?[]),NpgsqlDbType.Integer}, @@ -213,6 +214,7 @@ namespace SqlSugar { typeof(byte?[]),NpgsqlDbType.Bytea}, { typeof(bool?[]),NpgsqlDbType.Boolean}, {typeof(DateTime?[]),NpgsqlDbType.Date}, + {typeof(Guid?[]),NpgsqlDbType.Uuid}, { typeof(string[]), NpgsqlDbType.Text},