- 增加 IsVersion string 字符串乐观锁;#1178

This commit is contained in:
2881099
2022-07-04 17:15:30 +08:00
parent a219b39e5f
commit 84cfa65281
11 changed files with 169 additions and 12 deletions

View File

@ -188,10 +188,22 @@ namespace FreeSql.Internal.CommonProvider
col.SetValue(data, val = FreeUtil.NewMongodbId());
}
}
if (col.Attribute.IsVersion)
{
if (col.Attribute.MapType == typeof(byte[]))
{
if (val == null || (val is byte[] bytes && bytes.Length == 0))
col.SetValue(data, val = Utils.GuidToBytes(Guid.NewGuid()));
}
else if (col.Attribute.MapType == typeof(string))
{
var verval = col.GetDbValue(data) as string;
if (string.IsNullOrWhiteSpace(verval))
col.SetValue(data, val = Guid.NewGuid().ToString());
}
}
if (val == null && col.Attribute.MapType == typeof(string) && col.Attribute.IsNullable == false)
col.SetValue(data, val = "");
if (col.Attribute.MapType == typeof(byte[]) && (val == null || (val is byte[] bytes && bytes.Length == 0)) && col.Attribute.IsVersion)
col.SetValue(data, val = Utils.GuidToBytes(Guid.NewGuid()));
}
}

View File

@ -37,7 +37,7 @@ namespace FreeSql.Internal.CommonProvider
public DbConnection _connection;
public int _commandTimeout = 0;
public Action<StringBuilder> _interceptSql;
public byte[] _updateVersionValue;
public object _updateVersionValue;
}
public abstract partial class UpdateProvider<T1> : UpdateProvider, IUpdate<T1>
@ -139,7 +139,9 @@ namespace FreeSql.Internal.CommonProvider
throw new DbUpdateVersionException(CoreStrings.DbUpdateVersionException_RowLevelOptimisticLock(_source.Count, affrows), _table, sql, dbParms, affrows, _source.Select(a => (object)a));
foreach (var d in _source)
{
if (_versionColumn.Attribute.MapType == typeof(byte[]))
if (_versionColumn.Attribute.MapType == typeof(byte[]))
_orm.SetEntityValueWithPropertyName(_table.Type, d, _versionColumn.CsName, _updateVersionValue);
else if (_versionColumn.Attribute.MapType == typeof(string))
_orm.SetEntityValueWithPropertyName(_table.Type, d, _versionColumn.CsName, _updateVersionValue);
else
_orm.SetEntityIncrByWithPropertyName(_table.Type, d, _versionColumn.CsName, 1);
@ -1005,6 +1007,11 @@ namespace FreeSql.Internal.CommonProvider
_updateVersionValue = Utils.GuidToBytes(Guid.NewGuid());
sb.Append(", ").Append(vcname).Append(" = ").Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "uv", _versionColumn, _versionColumn.Attribute.MapType, _updateVersionValue));
}
else if (_versionColumn.Attribute.MapType == typeof(string))
{
_updateVersionValue = Guid.NewGuid().ToString();
sb.Append(", ").Append(vcname).Append(" = ").Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "uv", _versionColumn, _versionColumn.Attribute.MapType, _updateVersionValue));
}
else
sb.Append(", ").Append(vcname).Append(" = ").Append(_commonUtils.IsNull(vcname, 0)).Append(" + 1");
}

View File

@ -326,6 +326,7 @@ namespace FreeSql.Internal
break;
}
}
if (colattr.MapType == typeof(string) && colattr.IsVersion == true) colattr.StringLength = 40;
if (colattr.MapType == typeof(byte[]) && colattr.IsVersion == true) colattr.StringLength = 16;
if (colattr.MapType == typeof(byte[]) && colattr.StringLength != 0)
{
@ -395,7 +396,8 @@ namespace FreeSql.Internal
trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault();
if (trytb.VersionColumn != null)
{
if (trytb.VersionColumn.Attribute.MapType.IsNullableType() || trytb.VersionColumn.Attribute.MapType.IsNumberType() == false && trytb.VersionColumn.Attribute.MapType != typeof(byte[]))
if (trytb.VersionColumn.Attribute.MapType.IsNullableType() ||
trytb.VersionColumn.Attribute.MapType.IsNumberType() == false && !new[] { typeof(byte[]), typeof(string) }.Contains(trytb.VersionColumn.Attribute.MapType))
throw new Exception(CoreStrings.Properties_AsRowLock_Must_Numeric_Byte(trytb.VersionColumn.CsName));
}
tbattr?.ParseAsTable(trytb);