- 完善 [Column(ServerTime = Utc)] 特性,对 Update 时也能生效;

This commit is contained in:
28810 2020-01-06 19:36:30 +08:00
parent bbba06a343
commit fbbd74f54c
11 changed files with 82 additions and 37 deletions

View File

@ -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]

View File

@ -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]

View File

@ -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 };

View File

@ -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]

View File

@ -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]

View File

@ -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; }
}

View File

@ -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]

View File

@ -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; }

View File

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

View File

@ -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; }

View File

@ -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)
{