Skip to content

Commit 1250f4f

Browse files
committed
- 优化 IUpdate 批量 case when 同值的 SQL 生成;#1393
1 parent 496a969 commit 1250f4f

File tree

2 files changed

+142
-33
lines changed

2 files changed

+142
-33
lines changed

FreeSql/FreeSql.xml

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -894,28 +894,30 @@ protected string WhereCaseSource(string CsName, Func<string, string> thenValue)
894894
var sb = new StringBuilder();
895895
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
896896

897-
string valsqlOld = null;
898-
var valsqlOldStats = 1; //start 1
899-
var nullStats = 0;
900-
var cwsb = new StringBuilder().Append(cw);
901-
foreach (var d in _source)
897+
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum;
898+
var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
899+
if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
902900
{
903-
cwsb.Append(" \r\nWHEN ");
904-
ToSqlWhen(cwsb, _tempPrimarys, d);
905-
cwsb.Append(" THEN ");
906-
var val = col.GetDbValue(d);
907-
var valsql = thenValue(_commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val)));
908-
cwsb.Append(valsql);
909-
if (valsqlOld == null) valsqlOld = valsql;
910-
else if (valsqlOld == valsql) valsqlOldStats++;
911-
if (val == null || val == DBNull.Value) nullStats++;
901+
var val = ds.First();
902+
var colsql = thenValue(_commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val)));
903+
sb.Append(colsql);
904+
}
905+
else
906+
{
907+
var cwsb = new StringBuilder().Append(cw);
908+
foreach (var d in _source)
909+
{
910+
cwsb.Append(" \r\nWHEN ");
911+
ToSqlWhen(cwsb, _tempPrimarys, d);
912+
cwsb.Append(" THEN ");
913+
var val = col.GetDbValue(d);
914+
var colsql = thenValue(_commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val)));
915+
cwsb.Append(colsql);
916+
}
917+
cwsb.Append(" END");
918+
sb.Append(cwsb);
919+
cwsb.Clear();
912920
}
913-
cwsb.Append(" END");
914-
if (nullStats == _source.Count) sb.Append("NULL");
915-
else if (valsqlOldStats == _source.Count) sb.Append(valsqlOld);
916-
else sb.Append(cwsb);
917-
cwsb.Clear();
918-
919921
return sb.ToString();
920922
}
921923
}
@@ -1147,30 +1149,39 @@ public virtual void ToSqlExtension110(StringBuilder sb, bool isAsTableSplited)
11471149
sb.Append(col.DbUpdateValue);
11481150
else
11491151
{
1150-
var nulls = 0;
1151-
var cwsb = new StringBuilder().Append(cw);
1152-
foreach (var d in _source)
1152+
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum;
1153+
var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
1154+
if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
11531155
{
1154-
cwsb.Append(" \r\nWHEN ");
1155-
ToSqlWhen(cwsb, _tempPrimarys, d);
1156-
cwsb.Append(" THEN ");
1157-
var val = col.GetDbValue(d);
1158-
1156+
var val = ds.First();
11591157
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
11601158
_commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
1161-
cwsb.Append(_commonUtils.RewriteColumn(col, colsql));
1159+
sb.Append(_commonUtils.RewriteColumn(col, colsql));
11621160
if (_noneParameter == false)
11631161
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
1164-
if (val == null || val == DBNull.Value) nulls++;
11651162
}
1166-
cwsb.Append(" END");
1167-
if (nulls == _source.Count) sb.Append("NULL");
11681163
else
11691164
{
1165+
var cwsb = new StringBuilder().Append(cw);
1166+
foreach (var d in _source)
1167+
{
1168+
cwsb.Append(" \r\nWHEN ");
1169+
ToSqlWhen(cwsb, _tempPrimarys, d);
1170+
cwsb.Append(" THEN ");
1171+
var val = col.GetDbValue(d);
1172+
1173+
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
1174+
_commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
1175+
colsql = _commonUtils.RewriteColumn(col, colsql);
1176+
cwsb.Append(colsql);
1177+
if (_noneParameter == false)
1178+
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
1179+
}
1180+
cwsb.Append(" END");
11701181
ToSqlCaseWhenEnd(cwsb, col);
11711182
sb.Append(cwsb);
1183+
cwsb.Clear();
11721184
}
1173-
cwsb.Clear();
11741185
}
11751186
++colidx;
11761187
}

0 commit comments

Comments
 (0)