mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-25 01:14:33 +08:00
Add Enable Update Version Validation
This commit is contained in:
parent
38b107895a
commit
3a6c54d9a6
@ -8,8 +8,8 @@ namespace OrmTest
|
|||||||
{
|
{
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest";
|
public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest";
|
||||||
public static string ConnectionString2 = "server=.;uid=sa;pwd=@jhl85661501;database=SQLSUGAR4XTEST";
|
public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST";
|
||||||
public static string ConnectionString3 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtest";
|
public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
55
Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs
Normal file
55
Src/Asp.Net/SqlServerTest/Demos/F_VersionValidation.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using OrmTest.Demo;
|
||||||
|
using OrmTest.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OrmTest.Demo
|
||||||
|
{
|
||||||
|
public class VersionValidation : DemoBase
|
||||||
|
{
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
var db = GetInstance();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
var data = new StudentVersion()
|
||||||
|
{
|
||||||
|
Id = db.Queryable<Student>().Select(it => it.Id).First(),
|
||||||
|
CreateTime = DateTime.Now,
|
||||||
|
Name = ""
|
||||||
|
};
|
||||||
|
db.Updateable(data).AS("student").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();
|
||||||
|
|
||||||
|
data.CreateTime = time.Value.AddMilliseconds(-1);
|
||||||
|
db.Updateable(data).AS("student").ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (ex is SqlSugar.VersionExceptions)
|
||||||
|
{
|
||||||
|
Console.Write(ex.Message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StudentVersion
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true)]
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -51,6 +51,7 @@ namespace OrmTest
|
|||||||
Demo.ExtSqlFun.Init();
|
Demo.ExtSqlFun.Init();
|
||||||
Demo.QueryableView.Init();
|
Demo.QueryableView.Init();
|
||||||
Demo.AttributeDemo.Init();
|
Demo.AttributeDemo.Init();
|
||||||
|
Demo.VersionValidation.Init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Demos\F_VersionValidation.cs" />
|
||||||
<Compile Include="Models\CapitalEntity.cs" />
|
<Compile Include="Models\CapitalEntity.cs" />
|
||||||
<Compile Include="Config.cs" />
|
<Compile Include="Config.cs" />
|
||||||
<Compile Include="Demos\5_CodeFirst.cs" />
|
<Compile Include="Demos\5_CodeFirst.cs" />
|
||||||
|
@ -158,6 +158,7 @@ namespace SqlSugar
|
|||||||
column.DecimalDigits = sugarColumn.DecimalDigits;
|
column.DecimalDigits = sugarColumn.DecimalDigits;
|
||||||
column.OracleSequenceName = sugarColumn.OracleSequenceName;
|
column.OracleSequenceName = sugarColumn.OracleSequenceName;
|
||||||
column.IsOnlyIgnoreInsert = sugarColumn.IsOnlyIgnoreInsert;
|
column.IsOnlyIgnoreInsert = sugarColumn.IsOnlyIgnoreInsert;
|
||||||
|
column.IsEnableUpdateVersionValidation = sugarColumn.IsEnableUpdateVersionValidation;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -31,9 +31,11 @@ namespace SqlSugar
|
|||||||
AutoRemoveDataCache();
|
AutoRemoveDataCache();
|
||||||
Check.Exception(UpdateBuilder.WhereValues.IsNullOrEmpty() && GetPrimaryKeys().IsNullOrEmpty(), "You cannot have no primary key and no conditions");
|
Check.Exception(UpdateBuilder.WhereValues.IsNullOrEmpty() && GetPrimaryKeys().IsNullOrEmpty(), "You cannot have no primary key and no conditions");
|
||||||
string sql = UpdateBuilder.ToSqlString();
|
string sql = UpdateBuilder.ToSqlString();
|
||||||
|
ValidateVersion();
|
||||||
RestoreMapping();
|
RestoreMapping();
|
||||||
return this.Ado.ExecuteCommand(sql, UpdateBuilder.Parameters == null ? null : UpdateBuilder.Parameters.ToArray());
|
return this.Ado.ExecuteCommand(sql, UpdateBuilder.Parameters == null ? null : UpdateBuilder.Parameters.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ExecuteCommandHasChange()
|
public bool ExecuteCommandHasChange()
|
||||||
{
|
{
|
||||||
return this.ExecuteCommand() > 0;
|
return this.ExecuteCommand() > 0;
|
||||||
@ -64,7 +66,8 @@ namespace SqlSugar
|
|||||||
IsAs = true;
|
IsAs = true;
|
||||||
OldMappingTableList = this.Context.MappingTables;
|
OldMappingTableList = this.Context.MappingTables;
|
||||||
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
|
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
|
||||||
if (this.Context.MappingTables.Any(it => it.EntityName == entityName)) {
|
if (this.Context.MappingTables.Any(it => it.EntityName == entityName))
|
||||||
|
{
|
||||||
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
|
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
|
||||||
}
|
}
|
||||||
this.Context.MappingTables.Add(entityName, tableName);
|
this.Context.MappingTables.Add(entityName, tableName);
|
||||||
@ -150,7 +153,8 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns) {
|
public IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns)
|
||||||
|
{
|
||||||
var binaryExp = columns.Body as BinaryExpression;
|
var binaryExp = columns.Body as BinaryExpression;
|
||||||
Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
|
Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
|
||||||
Check.Exception(!(binaryExp.Left is MemberExpression) && !(binaryExp.Left is UnaryExpression), "No support {0}", columns.ToString());
|
Check.Exception(!(binaryExp.Left is MemberExpression) && !(binaryExp.Left is UnaryExpression), "No support {0}", columns.ToString());
|
||||||
@ -211,10 +215,12 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public IUpdateable<T> Where(string whereSql, object parameters = null)
|
public IUpdateable<T> Where(string whereSql, object parameters = null)
|
||||||
{
|
{
|
||||||
if (whereSql.HasValue()) {
|
if (whereSql.HasValue())
|
||||||
|
{
|
||||||
UpdateBuilder.WhereValues.Add(whereSql);
|
UpdateBuilder.WhereValues.Add(whereSql);
|
||||||
}
|
}
|
||||||
if (parameters != null) {
|
if (parameters != null)
|
||||||
|
{
|
||||||
UpdateBuilder.Parameters.AddRange(Context.Ado.GetParameters(parameters));
|
UpdateBuilder.Parameters.AddRange(Context.Ado.GetParameters(parameters));
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -331,7 +337,8 @@ namespace SqlSugar
|
|||||||
foreach (var item in this.UpdateBuilder.DbColumnInfoList)
|
foreach (var item in this.UpdateBuilder.DbColumnInfoList)
|
||||||
{
|
{
|
||||||
if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List<SugarParameter>();
|
if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List<SugarParameter>();
|
||||||
if (this.UpdateBuilder.SetValues.Any(it =>this.SqlBuilder.GetNoTranslationColumnName(it.Key) == item.PropertyName)) {
|
if (this.UpdateBuilder.SetValues.Any(it => this.SqlBuilder.GetNoTranslationColumnName(it.Key) == item.PropertyName))
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
this.UpdateBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType));
|
this.UpdateBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType));
|
||||||
@ -449,5 +456,54 @@ namespace SqlSugar
|
|||||||
asyncUpdateableBuilder.SetValues = this.UpdateBuilder.SetValues;
|
asyncUpdateableBuilder.SetValues = this.UpdateBuilder.SetValues;
|
||||||
return asyncUpdateable;
|
return asyncUpdateable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateVersion()
|
||||||
|
{
|
||||||
|
var versionColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsEnableUpdateVersionValidation);
|
||||||
|
if (versionColumn != null)
|
||||||
|
{
|
||||||
|
var pks = this.UpdateBuilder.DbColumnInfoList.Where(it => it.IsPrimarykey).ToList();
|
||||||
|
List<IConditionalModel> conModels = new List<IConditionalModel>();
|
||||||
|
foreach (var item in pks)
|
||||||
|
{
|
||||||
|
conModels.Add(new ConditionalModel() { FieldName = item.DbColumnName, ConditionalType = ConditionalType.Equal, FieldValue = item.Value.ObjToString() });
|
||||||
|
}
|
||||||
|
var dbInfo = this.Context.Queryable<T>().Where(conModels).First();
|
||||||
|
if (dbInfo != null)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.LongType))
|
||||||
|
{
|
||||||
|
if (Convert.ToInt64(dbVersion) > Convert.ToInt64(currentVersion))
|
||||||
|
{
|
||||||
|
throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", versionColumn.PropertyName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.DateType))
|
||||||
|
{
|
||||||
|
if (dbVersion.ObjToDate() > currentVersion.ObjToDate())
|
||||||
|
{
|
||||||
|
throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", versionColumn.PropertyName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (versionColumn.PropertyInfo.PropertyType.IsIn(UtilConstants.ByteArrayType))
|
||||||
|
{
|
||||||
|
if (UtilMethods.IsBigOne((byte[])dbVersion, (byte[])currentVersion))
|
||||||
|
{
|
||||||
|
throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", versionColumn.PropertyName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Check.ThrowNotSupportedException(string.Format("UpdateVersionValidation Not Supported Type [ {0} ] , {1}", versionColumn.PropertyInfo.PropertyType, versionColumn.PropertyName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ namespace SqlSugar
|
|||||||
public bool IsNullable { get; set; }
|
public bool IsNullable { get; set; }
|
||||||
public bool IsIdentity { get; set; }
|
public bool IsIdentity { get; set; }
|
||||||
public bool IsPrimarykey { get; set; }
|
public bool IsPrimarykey { get; set; }
|
||||||
|
public bool IsEnableUpdateVersionValidation { get; set; }
|
||||||
public string EntityName { get; set; }
|
public string EntityName { get; set; }
|
||||||
public string DbTableName { get; set; }
|
public string DbTableName { get; set; }
|
||||||
public bool IsIgnore { get; set; }
|
public bool IsIgnore { get; set; }
|
||||||
|
@ -105,6 +105,15 @@ namespace SqlSugar
|
|||||||
get { return _IsOnlyIgnoreInsert; }
|
get { return _IsOnlyIgnoreInsert; }
|
||||||
set { _IsOnlyIgnoreInsert = value; }
|
set { _IsOnlyIgnoreInsert = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private bool _IsEnableUpdateVersionValidation;
|
||||||
|
public bool IsEnableUpdateVersionValidation {
|
||||||
|
get { return _IsEnableUpdateVersionValidation; }
|
||||||
|
set { _IsEnableUpdateVersionValidation = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -52,4 +52,9 @@ namespace SqlSugar
|
|||||||
return string.Format("{0} : '{1}' \r\n", key, value);
|
return string.Format("{0} : '{1}' \r\n", key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class VersionExceptions : UtilExceptions
|
||||||
|
{
|
||||||
|
public VersionExceptions(string message)
|
||||||
|
: base(message){ }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,5 +103,31 @@ namespace SqlSugar
|
|||||||
action();
|
action();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static object DefaultForType(Type targetType)
|
||||||
|
{
|
||||||
|
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool IsBigOne(byte[] bytearray1, byte[] bytearray2)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user