Skip to content

Commit ede318c

Browse files
committed
- 修复 WithMemory 首行为 NULL 值的情况;#2047
1 parent ec748c4 commit ede318c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

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

FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,32 @@ public void WriteSourceSelectUnionAll(List<T1> source, StringBuilder sb, List<Db
295295
else
296296
{
297297
object val = col.GetDbValue(d);
298-
sb.Append(_commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val)));
298+
var valsql = _commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val));
299+
if (didx == 0 && valsql == "NULL")
300+
{
301+
var dbtype = _orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
302+
if (!string.IsNullOrWhiteSpace(dbtype))
303+
{
304+
switch (_orm.Ado.DataType)
305+
{
306+
case DataType.MsAccess:
307+
case DataType.Odbc:
308+
case DataType.Custom:
309+
break; // MsAccess 不支持 cast(null as xxx),直接用 NULL
310+
case DataType.PostgreSQL:
311+
case DataType.OdbcPostgreSQL:
312+
case DataType.CustomPostgreSQL:
313+
case DataType.KingbaseES:
314+
case DataType.ShenTong:
315+
valsql = $"NULL::{_orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype}";
316+
break;
317+
default:
318+
valsql = $"cast(NULL as {_orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype})";
319+
break;
320+
}
321+
}
322+
}
323+
sb.Append(valsql);
299324
}
300325
if (didx == 0) sb.Append(" as ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
301326
++colidx2;

0 commit comments

Comments
 (0)