- 增加 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

@ -5236,7 +5236,7 @@
</member>
<member name="M:FreeSql.CoreStrings.Properties_AsRowLock_Must_Numeric_Byte(System.Object)">
<summary>
属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[],并且不可为 Nullable
属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[] 或者 string,并且不可为 Nullable
</summary>
</member>
<member name="P:FreeSql.CoreStrings.Properties_Cannot_Null">

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);

View File

@ -827,7 +827,7 @@ namespace FreeSql
policyName, UnavailableExceptionMessage);
/// <summary>
/// 属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[],并且不可为 Nullable
/// 属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[] 或者 string,并且不可为 Nullable
/// </summary>
public static string Properties_AsRowLock_Must_Numeric_Byte(object trytbVersionColumnCsName)
=> string.Format(

View File

@ -433,7 +433,7 @@
<value>FreeSql: The {policyName} status is unavailable and cannot be used until the background checker is restored. {UnavailableExceptionMessage}</value>
</data>
<data name="Properties_AsRowLock_Must_Numeric_Byte" xml:space="preserve">
<value>FreeSql: The property {trytbVersionColumnCsName} is labeled as a row lock (optimistic lock) (IsVersion), but it must be a numeric type or byte[], and it cannot be Nullable</value>
<value>FreeSql: The property {trytbVersionColumnCsName} is labeled as a row lock (optimistic lock) (IsVersion), but it must be a numeric type or byte[] or string, and it cannot be Nullable</value>
</data>
<data name="Properties_Cannot_Null" xml:space="preserve">
<value>FreeSql: Properrties parameter cannot be empty</value>

View File

@ -433,7 +433,7 @@
<value>【{policyName}】状态不可用,等待后台检查程序恢复方可使用。{UnavailableExceptionMessage}</value>
</data>
<data name="Properties_AsRowLock_Must_Numeric_Byte" xml:space="preserve">
<value>属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[],并且不可为 Nullable</value>
<value>属性{trytbVersionColumnCsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型 或者 byte[] 或者 string,并且不可为 Nullable</value>
</data>
<data name="Properties_Cannot_Null" xml:space="preserve">
<value>properties 参数不能为空</value>