From 22fa8e501e23a9f6ccce0ead972de5eab7b60471 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 13 Oct 2018 05:44:02 +0800 Subject: [PATCH] - --- .../Demos/F_VersionValidation.cs | 46 +++++++++++-------- Src/Asp.Net/SqlServerTest/Program.cs | 35 +------------- .../UpdateProvider/UpdateableProvider.cs | 14 +++--- Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs | 2 +- Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 21 ++------- 5 files changed, 37 insertions(+), 81 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs b/Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs index ea46120f9..5600faa3f 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs @@ -14,24 +14,28 @@ namespace OrmTest.Demo var db = GetInstance(); try { - for (int i = 0; i < 10; i++) + + var data = new StudentVersion() { - var data = new StudentVersion() - { - Id = db.Queryable().Select(it => it.Id).First(), - CreateTime = DateTime.Now, - Name = "" - }; - db.Updateable(data).AS("student").ExecuteCommand(); - - var time = db.Queryable().Where(it=>it.Id==data.Id).Select(it => it.CreateTime).Single(); - data.CreateTime = time.Value; - db.Updateable(data).AS("student").ExecuteCommand(); - - data.CreateTime = time.Value.AddMilliseconds(-1); - db.Updateable(data).AS("student").CloseVersionValidation().ExecuteCommand();//Close Version Validation - db.Updateable(data).AS("student").ExecuteCommand(); - } + Id = db.Queryable().Select(it => it.Id).First(), + CreateTime = DateTime.Now, + Name = "", + }; + db.Updateable(data).IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand(); + + var time = db.Queryable().Where(it => it.Id == data.Id).Select(it => it.Timestamp).Single(); + + data.Timestamp = time; + + //is ok + db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand(); + //updated Timestamp change + + //is error + db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand(); + + //IsEnableUpdateVersionValidation Types of support int or long or byte[](Timestamp) or Datetime + } catch (Exception ex) { @@ -39,18 +43,20 @@ namespace OrmTest.Demo { Console.Write(ex.Message); } - else { + else + { } } } - + [SqlSugar.SugarTable("Student")] public class StudentVersion { public int Id { get; set; } public string Name { get; set; } - [SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true)] public DateTime CreateTime { get; set; } + [SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true,IsOnlyIgnoreInsert=true)] + public byte[] Timestamp { get; set; } } } } diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index e4c2cad69..aa5509e0f 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -17,40 +17,7 @@ namespace OrmTest { static void Main(string[] args) { - // /***Unit Test***/ - new Select(1).Init(); - new Field(1).Init(); - new Where(1).Init(); - new Method(1).Init(); - new JoinQuery(1).Init(); - new SingleQuery(1).Init(); - new SelectQuery(1).Init(); - new AutoClose(1).Init(); - new Insert(1).Init(); - new Delete(1).Init(); - new Update(1).Init(); - new Mapping(1).Init(); - new DataTest(1).Init(); - new EnumTest(1).Init(); - /***Performance Test***/ - new SqlSugarPerformance(100).Select(); - - /***Demo***/ - Demo.Query.Init(); - Demo.Insert.Init(); - Demo.Delete.Init(); - Demo.Update.Init(); - Demo.DbFirst.Init(); - Demo.JoinSql.Init(); - Demo.Filter.Init(); - Demo.ComplexModel.Init(); - Demo.CodeFirst.Init(); - Demo.Aop.Init(); - Demo.MasterSlave.Init(); - Demo.SharedConnection.Init(); - Demo.ExtSqlFun.Init(); - Demo.QueryableView.Init(); - Demo.AttributeDemo.Init(); + Demo.VersionValidation.Init(); } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 5f3238b7c..abe59ec07 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -23,7 +23,7 @@ namespace SqlSugar private List IgnoreColumnNameList { get; set; } private List WhereColumnList { get; set; } private bool IsOffIdentity { get; set; } - private bool IsVersionValidation = true; + private bool IsVersionValidation { get; set; } public MappingTableList OldMappingTableList { get; set; } public bool IsAs { get; set; } public virtual int ExecuteCommand() @@ -80,9 +80,9 @@ namespace SqlSugar return this; } - public IUpdateable CloseVersionValidation() + public IUpdateable IsEnableUpdateVersionValidation() { - this.IsVersionValidation = false; + this.IsVersionValidation = true; return this; } @@ -480,10 +480,8 @@ namespace SqlSugar { var currentVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(UpdateObjs.Last(), null); var dbVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(dbInfo, null); - if (currentVersion == null) - currentVersion = UtilMethods.DefaultForType(versionColumn.PropertyInfo.PropertyType); - if (dbVersion == null) - dbVersion = UtilMethods.DefaultForType(versionColumn.PropertyInfo.PropertyType); + Check.Exception(currentVersion == null, "ValidateVersion entity property {0} is not null", versionColumn.PropertyName); + Check.Exception(dbVersion == null, "ValidateVersion database column {0} is not null", versionColumn.DbColumnName); if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.LongType)) { if (Convert.ToInt64(dbVersion) > Convert.ToInt64(currentVersion)) @@ -500,7 +498,7 @@ namespace SqlSugar } else if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.ByteArrayType)) { - if (UtilMethods.IsBigOne((byte[])dbVersion, (byte[])currentVersion)) + if (UtilMethods.GetLong((byte[])dbVersion)>UtilMethods.GetLong((byte[])currentVersion)) { throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", versionColumn.PropertyName)); } diff --git a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs index 4aa61c1fa..21dee2c27 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs @@ -41,7 +41,7 @@ namespace SqlSugar IUpdateable IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false); IUpdateable IgnoreColumns(Expression> columns); IUpdateable IgnoreColumns(Func ignoreColumMethod); - IUpdateable CloseVersionValidation(); + IUpdateable IsEnableUpdateVersionValidation(); IUpdateable ReSetValue(Expression> setValueExpression); IUpdateable RemoveDataCache(); KeyValuePair> ToSql(); diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index de19333c6..821b2b45d 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -109,25 +109,10 @@ namespace SqlSugar return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; } - internal static bool IsBigOne(byte[] bytearray1, byte[] bytearray2) + internal static Int64 GetLong(byte[] bytes) { - int result = 0; - if (bytearray1.Length != bytearray2.Length) - { - result = bytearray1.Length - bytearray2.Length; - } - else - { - for (int i = 0; i < bytearray1.Length; i++) - { - if (bytearray1[i] != bytearray2[i]) - { - result = (int)(bytearray1[i] - bytearray2[i]); - break; - } - } - } - return result<0; + return Convert.ToInt64(string.Join("", bytes).PadRight(20, '0')); } + } }