mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 完善 [Column(ServerTime = Utc)] 特性,对 Update 时也能生效;
This commit is contained in:
parent
bbba06a343
commit
fbbd74f54c
@ -28,6 +28,8 @@ namespace FreeSql.Tests.MySqlConnector
|
||||
Assert.NotNull(item2);
|
||||
Assert.Equal(item.编号, item2.编号);
|
||||
Assert.Equal(item.标题, item2.标题);
|
||||
|
||||
g.mysql.Update<测试中文表2>().SetSource(item2).ExecuteAffrows();
|
||||
}
|
||||
class 测试中文表2
|
||||
{
|
||||
@ -36,8 +38,11 @@ namespace FreeSql.Tests.MySqlConnector
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -36,8 +36,11 @@ namespace FreeSql.Tests.MsAccess
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -32,9 +32,12 @@ namespace FreeSql.Tests.MySql
|
||||
|
||||
public string 标题 { get; protected set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; protected set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
|
||||
public static 测试中文表2 Create(string title, DateTime ctm)
|
||||
{
|
||||
return new 测试中文表2 { 标题 = title, 创建时间 = ctm };
|
||||
|
@ -36,8 +36,11 @@ namespace FreeSql.Tests.Oracle
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -43,8 +43,11 @@ namespace FreeSql.Tests.PostgreSQL
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -37,8 +37,11 @@ namespace FreeSql.Tests.SqlServer
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
@ -36,8 +36,11 @@ namespace FreeSql.Tests.Sqlite
|
||||
|
||||
public string 标题 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -121,8 +121,11 @@ namespace FreeSql.Tests
|
||||
|
||||
public byte[] Binary { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Utc, CanUpdate = false)]
|
||||
public DateTime 创建时间 { get; set; }
|
||||
|
||||
[Column(ServerTime = DateTimeKind.Utc)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
public DateTime 更新时间 { get; set; }
|
||||
|
||||
[Column(InsertValueSql = "'123'")]
|
||||
public string InsertValue2 { get; set; }
|
||||
|
@ -557,6 +557,11 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
|
||||
if (col.Attribute.CanUpdate && string.IsNullOrEmpty(col.DbUpdateValue) == false)
|
||||
sb.Append(col.DbUpdateValue);
|
||||
else
|
||||
{
|
||||
var val = col.GetMapValue(_source.First());
|
||||
if (_noneParameter)
|
||||
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
|
||||
@ -565,6 +570,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
|
||||
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
|
||||
}
|
||||
}
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
@ -589,6 +595,10 @@ namespace FreeSql.Internal.CommonProvider
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
|
||||
if (col.Attribute.CanUpdate && string.IsNullOrEmpty(col.DbUpdateValue) == false)
|
||||
sb.Append(col.DbUpdateValue);
|
||||
else
|
||||
{
|
||||
var nulls = 0;
|
||||
var cwsb = new StringBuilder().Append(cw);
|
||||
foreach (var d in _source)
|
||||
@ -614,7 +624,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
sb.Append(cwsb.ToString());
|
||||
}
|
||||
cwsb.Clear();
|
||||
|
||||
}
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
@ -626,6 +636,13 @@ namespace FreeSql.Internal.CommonProvider
|
||||
if (_setIncr.Length > 0)
|
||||
sb.Append(_set.Length > 0 ? _setIncr.ToString() : _setIncr.ToString().Substring(2));
|
||||
|
||||
if (_source.Any() == false)
|
||||
{
|
||||
foreach (var col in _table.Columns.Values)
|
||||
if (col.Attribute.CanUpdate && string.IsNullOrEmpty(col.DbUpdateValue) == false)
|
||||
sb.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(col.DbUpdateValue);
|
||||
}
|
||||
|
||||
if (_table.VersionColumn != null)
|
||||
{
|
||||
var vcname = _commonUtils.QuoteSqlName(_table.VersionColumn.Attribute.Name);
|
||||
|
@ -16,6 +16,7 @@ namespace FreeSql.Internal.Model
|
||||
public string DbTypeText { get; internal set; }
|
||||
public string DbDefaultValue { get; internal set; }
|
||||
public string DbInsertValue { get; internal set; }
|
||||
public string DbUpdateValue { get; internal set; }
|
||||
public int DbSize { get; internal set; }
|
||||
public byte DbPrecision { get; internal set; }
|
||||
public byte DbScale { get; internal set; }
|
||||
|
@ -188,8 +188,9 @@ namespace FreeSql.Internal
|
||||
//}
|
||||
if (colattr.ServerTime != DateTimeKind.Unspecified && new[] { typeof(DateTime), typeof(DateTimeOffset) }.Contains(colattr.MapType.NullableTypeOrThis()))
|
||||
{
|
||||
col.DbDefaultValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc;
|
||||
col.DbDefaultValue = "'1970-1-1'";
|
||||
col.DbInsertValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc;
|
||||
col.DbUpdateValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc;
|
||||
}
|
||||
if (string.IsNullOrEmpty(colattr.InsertValueSql) == false)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user