mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
-
This commit is contained in:
parent
849a707ad4
commit
22fa8e501e
@ -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<Student>().Select(it => it.Id).First(),
|
||||
CreateTime = DateTime.Now,
|
||||
Name = ""
|
||||
};
|
||||
db.Updateable(data).AS("student").ExecuteCommand();
|
||||
Id = db.Queryable<Student>().Select(it => it.Id).First(),
|
||||
CreateTime = DateTime.Now,
|
||||
Name = "",
|
||||
};
|
||||
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();
|
||||
data.CreateTime = time.Value;
|
||||
db.Updateable(data).AS("student").ExecuteCommand();
|
||||
var time = db.Queryable<StudentVersion>().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
|
||||
|
||||
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)
|
||||
{
|
||||
@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace SqlSugar
|
||||
private List<string> IgnoreColumnNameList { get; set; }
|
||||
private List<string> 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<T> CloseVersionValidation()
|
||||
public IUpdateable<T> 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));
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ namespace SqlSugar
|
||||
IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false);
|
||||
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
||||
IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod);
|
||||
IUpdateable<T> CloseVersionValidation();
|
||||
IUpdateable<T> IsEnableUpdateVersionValidation();
|
||||
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
||||
IUpdateable<T> RemoveDataCache();
|
||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||
|
@ -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'));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user