Browse Source

-

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
22fa8e501e
  1. 34
      Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs
  2. 33
      Src/Asp.Net/SqlServerTest/Program.cs
  3. 14
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  4. 2
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs
  5. 21
      Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

34
Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs

@ -14,24 +14,28 @@ namespace OrmTest.Demo
var db = GetInstance(); var db = GetInstance();
try try
{ {
for (int i = 0; i < 10; i++)
{
var data = new StudentVersion() var data = new StudentVersion()
{ {
Id = db.Queryable<Student>().Select(it => it.Id).First(), Id = db.Queryable<Student>().Select(it => it.Id).First(),
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
Name = "" Name = "",
}; };
db.Updateable(data).AS("student").ExecuteCommand(); db.Updateable(data).IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
var time = db.Queryable<Student>().Where(it=>it.Id==data.Id).Select(it => it.CreateTime).Single(); var time = db.Queryable<StudentVersion>().Where(it => it.Id == data.Id).Select(it => it.Timestamp).Single();
data.CreateTime = time.Value;
db.Updateable(data).AS("student").ExecuteCommand(); 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
data.CreateTime = time.Value.AddMilliseconds(-1);
db.Updateable(data).AS("student").CloseVersionValidation().ExecuteCommand();//Close Version Validation
db.Updateable(data).AS("student").ExecuteCommand();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -39,18 +43,20 @@ namespace OrmTest.Demo
{ {
Console.Write(ex.Message); Console.Write(ex.Message);
} }
else { else
{
} }
} }
} }
[SqlSugar.SugarTable("Student")]
public class StudentVersion public class StudentVersion
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true)]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true,IsOnlyIgnoreInsert=true)]
public byte[] Timestamp { get; set; }
} }
} }
} }

33
Src/Asp.Net/SqlServerTest/Program.cs

@ -17,40 +17,7 @@ namespace OrmTest
{ {
static void Main(string[] args) 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(); Demo.VersionValidation.Init();
} }
} }

14
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

@ -23,7 +23,7 @@ namespace SqlSugar
private List<string> IgnoreColumnNameList { get; set; } private List<string> IgnoreColumnNameList { get; set; }
private List<string> WhereColumnList { get; set; } private List<string> WhereColumnList { get; set; }
private bool IsOffIdentity { get; set; } private bool IsOffIdentity { get; set; }
private bool IsVersionValidation = true; private bool IsVersionValidation { get; set; }
public MappingTableList OldMappingTableList { get; set; } public MappingTableList OldMappingTableList { get; set; }
public bool IsAs { get; set; } public bool IsAs { get; set; }
public virtual int ExecuteCommand() public virtual int ExecuteCommand()
@ -80,9 +80,9 @@ namespace SqlSugar
return this; return this;
} }
public IUpdateable<T> CloseVersionValidation() public IUpdateable<T> IsEnableUpdateVersionValidation()
{ {
this.IsVersionValidation = false; this.IsVersionValidation = true;
return this; return this;
} }
@ -480,10 +480,8 @@ namespace SqlSugar
{ {
var currentVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(UpdateObjs.Last(), null); var currentVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(UpdateObjs.Last(), null);
var dbVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(dbInfo, null); var dbVersion = this.EntityInfo.Type.GetProperty(versionColumn.PropertyName).GetValue(dbInfo, null);
if (currentVersion == null) Check.Exception(currentVersion == null, "ValidateVersion entity property {0} is not null", versionColumn.PropertyName);
currentVersion = UtilMethods.DefaultForType(versionColumn.PropertyInfo.PropertyType); Check.Exception(dbVersion == null, "ValidateVersion database column {0} is not null", versionColumn.DbColumnName);
if (dbVersion == null)
dbVersion = UtilMethods.DefaultForType(versionColumn.PropertyInfo.PropertyType);
if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.LongType)) if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.LongType))
{ {
if (Convert.ToInt64(dbVersion) > Convert.ToInt64(currentVersion)) if (Convert.ToInt64(dbVersion) > Convert.ToInt64(currentVersion))
@ -500,7 +498,7 @@ namespace SqlSugar
} }
else if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.ByteArrayType)) 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)); throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", versionColumn.PropertyName));
} }

2
Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

@ -41,7 +41,7 @@ namespace SqlSugar
IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false); IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false);
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns); IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod); IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod);
IUpdateable<T> CloseVersionValidation(); IUpdateable<T> IsEnableUpdateVersionValidation();
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression); IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
IUpdateable<T> RemoveDataCache(); IUpdateable<T> RemoveDataCache();
KeyValuePair<string,List<SugarParameter>> ToSql(); KeyValuePair<string,List<SugarParameter>> ToSql();

21
Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

@ -109,25 +109,10 @@ namespace SqlSugar
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
} }
internal static bool IsBigOne(byte[] bytearray1, byte[] bytearray2) internal static Int64 GetLong(byte[] bytes)
{ {
int result = 0; return Convert.ToInt64(string.Join("", bytes).PadRight(20, '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;
} }
} }
} }

Loading…
Cancel
Save