- 完善 [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

@ -557,13 +557,19 @@ namespace FreeSql.Internal.CommonProvider
{
if (colidx > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
var val = col.GetMapValue(_source.First());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
if (col.Attribute.CanUpdate && string.IsNullOrEmpty(col.DbUpdateValue) == false)
sb.Append(col.DbUpdateValue);
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
var val = col.GetMapValue(_source.First());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
}
++colidx;
}
@ -589,32 +595,36 @@ namespace FreeSql.Internal.CommonProvider
if (colidx > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
var nulls = 0;
var cwsb = new StringBuilder().Append(cw);
foreach (var d in _source)
{
cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetMapValue(d);
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
{
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
if (val == null || val == DBNull.Value) nulls++;
}
cwsb.Append(" END");
if (nulls == _source.Count) sb.Append("NULL");
if (col.Attribute.CanUpdate && string.IsNullOrEmpty(col.DbUpdateValue) == false)
sb.Append(col.DbUpdateValue);
else
{
ToSqlCaseWhenEnd(cwsb, col);
sb.Append(cwsb.ToString());
var nulls = 0;
var cwsb = new StringBuilder().Append(cw);
foreach (var d in _source)
{
cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetMapValue(d);
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
{
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
if (val == null || val == DBNull.Value) nulls++;
}
cwsb.Append(" END");
if (nulls == _source.Count) sb.Append("NULL");
else
{
ToSqlCaseWhenEnd(cwsb, col);
sb.Append(cwsb.ToString());
}
cwsb.Clear();
}
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)
{