Skip to content

Commit da15b4f

Browse files
author
yangl
committed
PG时间类型也需要转换,否则插入或更新会报错
1 parent 2588ccb commit da15b4f

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Topic
2121
public TestTypeInfo Type { get; set; }
2222
public string Title { get; set; }
2323
public DateTime CreateTime { get; set; }
24+
public DateTime? UpdateTime { get; set; }
2425
}
2526
class TestTypeInfo
2627
{
@@ -2054,6 +2055,16 @@ public void ToTreeList()
20542055
.AsTreeCte().Any());
20552056
}
20562057

2058+
[Fact]
2059+
public void WithMemoryTest()
2060+
{
2061+
var fsql = g.pgsql;
2062+
var topics = new List<Topic>() { new Topic { Id = 1, CreateTime = new DateTime(1900, 1, 1), UpdateTime = new DateTime(1900, 1, 2) } };
2063+
var sql = fsql.Select<Topic>().WithMemory(topics).ToSql();
2064+
Assert.Equal(@"SELECT a.""id"", a.""clicks"", a.""typeguid"", a.""title"", a.""createtime"", a.""updatetime""
2065+
FROM ( SELECT 1 as ""id"", 0 as ""clicks"", 0 as ""typeguid"", NULL::varchar as ""title"", '1900-01-01 00:00:00.000000'::timestamp as ""createtime"", '1900-01-02 00:00:00.000000'::timestamp as ""updatetime"" ) a", sql);
2066+
}
2067+
20572068
[Table(Name = "D_District")]
20582069
public class BaseDistrict
20592070
{

FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -296,29 +296,32 @@ public void WriteSourceSelectUnionAll(List<T1> source, StringBuilder sb, List<Db
296296
{
297297
object val = col.GetDbValue(d);
298298
var valsql = _commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val));
299-
if (didx == 0 && valsql == "NULL")
299+
if (didx == 0)//首行需要类型
300300
{
301-
var dbtype = _orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
302-
if (!string.IsNullOrWhiteSpace(dbtype))
301+
if (valsql == "NULL" || col.DbTypeText == "TIMESTAMP")//时间类型转换
303302
{
304-
switch (_orm.Ado.DataType)
303+
var dbtype = _orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
304+
if (!string.IsNullOrWhiteSpace(dbtype))
305305
{
306-
case DataType.Oracle:
307-
case DataType.OdbcOracle:
308-
case DataType.CustomOracle:
309-
case DataType.Dameng:
310-
break; // Oracle 不支持 cast(null as xxx),直接用 NULL
311-
case DataType.MsAccess:
312-
case DataType.Odbc:
313-
case DataType.Custom:
314-
break; // MsAccess 不支持 cast(null as xxx),直接用 NULL
315-
case DataType.PostgreSQL:
316-
case DataType.OdbcPostgreSQL:
317-
case DataType.CustomPostgreSQL:
318-
case DataType.KingbaseES:
319-
case DataType.ShenTong:
320-
valsql = $"NULL::{_orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype}";
321-
break; // #2047
306+
switch (_orm.Ado.DataType)
307+
{
308+
case DataType.Oracle:
309+
case DataType.OdbcOracle:
310+
case DataType.CustomOracle:
311+
case DataType.Dameng:
312+
break; // Oracle 不支持 cast(null as xxx),直接用 NULL
313+
case DataType.MsAccess:
314+
case DataType.Odbc:
315+
case DataType.Custom:
316+
break; // MsAccess 不支持 cast(null as xxx),直接用 NULL
317+
case DataType.PostgreSQL:
318+
case DataType.OdbcPostgreSQL:
319+
case DataType.CustomPostgreSQL:
320+
case DataType.KingbaseES:
321+
case DataType.ShenTong:
322+
valsql = $"{valsql}::{dbtype}";
323+
break; // #2047
324+
}
322325
}
323326
}
324327
}
@@ -344,7 +347,7 @@ public void WriteSourceSelectUnionAll(List<T1> source, StringBuilder sb, List<Db
344347
}
345348
}
346349

347-
byte _SplitSourceByIdentityValueIsNullFlag = 0 ;//防止重复计算 SplitSource
350+
byte _SplitSourceByIdentityValueIsNullFlag = 0;//防止重复计算 SplitSource
348351
/// <summary>
349352
/// 如果实体类有自增属性,分成两个 List,有值的Item1 merge,无值的Item2 insert
350353
/// </summary>

0 commit comments

Comments
 (0)