- 优化 lambda 使用 a == null ? 1 : 0 支持类似这样直接判断实体的情况;

This commit is contained in:
28810 2020-08-07 01:36:11 +08:00
parent 7efe02f69c
commit 25e73117b1
11 changed files with 94 additions and 51 deletions

View File

@ -41,10 +41,10 @@ namespace FreeSql.Tests.Odbc.KingbaseES
items[0].Clicks = null; items[0].Clicks = null;
sql = update.SetSource(items).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = CASE \"ID\" WHEN 1 THEN NULL WHEN 2 THEN 100 WHEN 3 THEN 200 WHEN 4 THEN 300 WHEN 5 THEN 400 WHEN 6 THEN 500 WHEN 7 THEN 600 WHEN 8 THEN 700 WHEN 9 THEN 800 WHEN 10 THEN 900 END::int4, \"TITLE\" = CASE \"ID\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::varchar, \"CREATETIME\" = CASE \"ID\" WHEN 1 THEN '0001-01-01 00:00:00.000000' WHEN 2 THEN '0001-01-01 00:00:00.000000' WHEN 3 THEN '0001-01-01 00:00:00.000000' WHEN 4 THEN '0001-01-01 00:00:00.000000' WHEN 5 THEN '0001-01-01 00:00:00.000000' WHEN 6 THEN '0001-01-01 00:00:00.000000' WHEN 7 THEN '0001-01-01 00:00:00.000000' WHEN 8 THEN '0001-01-01 00:00:00.000000' WHEN 9 THEN '0001-01-01 00:00:00.000000' WHEN 10 THEN '0001-01-01 00:00:00.000000' END::timestamp WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = CASE \"ID\" WHEN 1 THEN NULL WHEN 2 THEN 100 WHEN 3 THEN 200 WHEN 4 THEN 300 WHEN 5 THEN 400 WHEN 6 THEN 500 WHEN 7 THEN 600 WHEN 8 THEN 700 WHEN 9 THEN 800 WHEN 10 THEN 900 END::int4, \"TITLE\" = CASE \"ID\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::text, \"CREATETIME\" = CASE \"ID\" WHEN 1 THEN '0001-01-01 00:00:00.000000' WHEN 2 THEN '0001-01-01 00:00:00.000000' WHEN 3 THEN '0001-01-01 00:00:00.000000' WHEN 4 THEN '0001-01-01 00:00:00.000000' WHEN 5 THEN '0001-01-01 00:00:00.000000' WHEN 6 THEN '0001-01-01 00:00:00.000000' WHEN 7 THEN '0001-01-01 00:00:00.000000' WHEN 8 THEN '0001-01-01 00:00:00.000000' WHEN 9 THEN '0001-01-01 00:00:00.000000' WHEN 10 THEN '0001-01-01 00:00:00.000000' END::timestamp WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = CASE \"ID\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::varchar WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = CASE \"ID\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::text WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CREATETIME\" = '2020-01-01 00:00:00.000000' WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CREATETIME\" = '2020-01-01 00:00:00.000000' WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);

View File

@ -42,10 +42,10 @@ namespace FreeSql.Tests.Odbc.PostgreSQL
items[0].Clicks = null; items[0].Clicks = null;
sql = update.SetSource(items).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"tb_topic\" SET \"clicks\" = CASE \"id\" WHEN 1 THEN NULL WHEN 2 THEN 100 WHEN 3 THEN 200 WHEN 4 THEN 300 WHEN 5 THEN 400 WHEN 6 THEN 500 WHEN 7 THEN 600 WHEN 8 THEN 700 WHEN 9 THEN 800 WHEN 10 THEN 900 END::int4, \"title\" = CASE \"id\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::varchar, \"createtime\" = CASE \"id\" WHEN 1 THEN '0001-01-01 00:00:00.000000' WHEN 2 THEN '0001-01-01 00:00:00.000000' WHEN 3 THEN '0001-01-01 00:00:00.000000' WHEN 4 THEN '0001-01-01 00:00:00.000000' WHEN 5 THEN '0001-01-01 00:00:00.000000' WHEN 6 THEN '0001-01-01 00:00:00.000000' WHEN 7 THEN '0001-01-01 00:00:00.000000' WHEN 8 THEN '0001-01-01 00:00:00.000000' WHEN 9 THEN '0001-01-01 00:00:00.000000' WHEN 10 THEN '0001-01-01 00:00:00.000000' END::timestamp WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"tb_topic\" SET \"clicks\" = CASE \"id\" WHEN 1 THEN NULL WHEN 2 THEN 100 WHEN 3 THEN 200 WHEN 4 THEN 300 WHEN 5 THEN 400 WHEN 6 THEN 500 WHEN 7 THEN 600 WHEN 8 THEN 700 WHEN 9 THEN 800 WHEN 10 THEN 900 END::int4, \"title\" = CASE \"id\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::text, \"createtime\" = CASE \"id\" WHEN 1 THEN '0001-01-01 00:00:00.000000' WHEN 2 THEN '0001-01-01 00:00:00.000000' WHEN 3 THEN '0001-01-01 00:00:00.000000' WHEN 4 THEN '0001-01-01 00:00:00.000000' WHEN 5 THEN '0001-01-01 00:00:00.000000' WHEN 6 THEN '0001-01-01 00:00:00.000000' WHEN 7 THEN '0001-01-01 00:00:00.000000' WHEN 8 THEN '0001-01-01 00:00:00.000000' WHEN 9 THEN '0001-01-01 00:00:00.000000' WHEN 10 THEN '0001-01-01 00:00:00.000000' END::timestamp WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = CASE \"id\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::varchar WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = CASE \"id\" WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END::text WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"tb_topic\" SET \"createtime\" = '2020-01-01 00:00:00.000000' WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"tb_topic\" SET \"createtime\" = '2020-01-01 00:00:00.000000' WHERE (\"id\" IN (1,2,3,4,5,6,7,8,9,10))", sql);

View File

@ -10,7 +10,7 @@ namespace FreeSql.Tests.ShenTong
{ {
IUpdate<Topic> update => g.shentong.Update<Topic>(); IUpdate<Topic> update => g.shentong.Update<Topic>();
[Table(Name = "tb_topic")] [Table(Name = "tb_topic_insert")]
class Topic class Topic
{ {
[Column(IsIdentity = true, IsPrimary = true)] [Column(IsIdentity = true, IsPrimary = true)]
@ -25,30 +25,30 @@ namespace FreeSql.Tests.ShenTong
public void Dywhere() public void Dywhere()
{ {
Assert.Null(g.shentong.Update<Topic>().ToSql()); Assert.Null(g.shentong.Update<Topic>().ToSql());
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='test' \r\nWHERE (\"ID\" = 1 OR \"ID\" = 2)", g.shentong.Update<Topic>(new[] { 1, 2 }).SetRaw("title='test'").ToSql()); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='test' \r\nWHERE (\"ID\" = 1 OR \"ID\" = 2)", g.shentong.Update<Topic>(new[] { 1, 2 }).SetRaw("title='test'").ToSql());
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='test1' \r\nWHERE (\"ID\" = 1)", g.shentong.Update<Topic>(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").ToSql()); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='test1' \r\nWHERE (\"ID\" = 1)", g.shentong.Update<Topic>(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").ToSql());
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='test1' \r\nWHERE (\"ID\" = 1 OR \"ID\" = 2)", g.shentong.Update<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").ToSql()); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='test1' \r\nWHERE (\"ID\" = 1 OR \"ID\" = 2)", g.shentong.Update<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").ToSql());
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='test1' \r\nWHERE (\"ID\" = 1)", g.shentong.Update<Topic>(new { id = 1 }).SetRaw("title='test1'").ToSql()); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='test1' \r\nWHERE (\"ID\" = 1)", g.shentong.Update<Topic>(new { id = 1 }).SetRaw("title='test1'").ToSql());
} }
[Fact] [Fact]
public void SetSource() public void SetSource()
{ {
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).ToSql().Replace("\r\n", ""); var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1, \"CREATETIME\" = @p_2 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1, \"CREATETIME\" = @p_2 WHERE (\"ID\" = 1)", sql);
var items = new List<Topic>(); var items = new List<Topic>();
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 }); for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
items[0].Clicks = null; items[0].Clicks = null;
sql = update.SetSource(items).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = CASE \"ID\" WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END, \"TITLE\" = CASE \"ID\" WHEN 1 THEN @p_10 WHEN 2 THEN @p_11 WHEN 3 THEN @p_12 WHEN 4 THEN @p_13 WHEN 5 THEN @p_14 WHEN 6 THEN @p_15 WHEN 7 THEN @p_16 WHEN 8 THEN @p_17 WHEN 9 THEN @p_18 WHEN 10 THEN @p_19 END, \"CREATETIME\" = CASE \"ID\" WHEN 1 THEN @p_20 WHEN 2 THEN @p_21 WHEN 3 THEN @p_22 WHEN 4 THEN @p_23 WHEN 5 THEN @p_24 WHEN 6 THEN @p_25 WHEN 7 THEN @p_26 WHEN 8 THEN @p_27 WHEN 9 THEN @p_28 WHEN 10 THEN @p_29 END WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = CASE \"ID\" WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END, \"TITLE\" = CASE \"ID\" WHEN 1 THEN @p_10 WHEN 2 THEN @p_11 WHEN 3 THEN @p_12 WHEN 4 THEN @p_13 WHEN 5 THEN @p_14 WHEN 6 THEN @p_15 WHEN 7 THEN @p_16 WHEN 8 THEN @p_17 WHEN 9 THEN @p_18 WHEN 10 THEN @p_19 END, \"CREATETIME\" = CASE \"ID\" WHEN 1 THEN @p_20 WHEN 2 THEN @p_21 WHEN 3 THEN @p_22 WHEN 4 THEN @p_23 WHEN 5 THEN @p_24 WHEN 6 THEN @p_25 WHEN 7 THEN @p_26 WHEN 8 THEN @p_27 WHEN 9 THEN @p_28 WHEN 10 THEN @p_29 END WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = CASE \"ID\" WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"TITLE\" = CASE \"ID\" WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", ""); sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CREATETIME\" = @p_0 WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CREATETIME\" = @p_0 WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
sql = g.shentong.Update<ts_source_mpk>().SetSource(new[] { sql = g.shentong.Update<ts_source_mpk>().SetSource(new[] {
new ts_source_mpk { id1 = 1, id2 = 7, xx = "a1" }, new ts_source_mpk { id1 = 1, id2 = 7, xx = "a1" },
@ -81,85 +81,85 @@ namespace FreeSql.Tests.ShenTong
public void IgnoreColumns() public void IgnoreColumns()
{ {
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", ""); var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql);
} }
[Fact] [Fact]
public void UpdateColumns() public void UpdateColumns()
{ {
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", ""); var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql);
} }
[Fact] [Fact]
public void Set() public void Set()
{ {
var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", ""); var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"TITLE\" = @p_0 WHERE (\"ID\" = 1)", sql);
sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", ""); sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = @p_0, \"CREATETIME\" = @p_1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"TITLE\" = @p_0, \"CREATETIME\" = @p_1 WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = coalesce(\"CLICKS\", 0) * 10 / 1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = coalesce(\"CLICKS\", 0) * 10 / 1 WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Id - 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Id - 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"ID\" = (\"ID\" - 10) WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"ID\" = (\"ID\" - 10) WHERE (\"ID\" = 1)", sql);
int incrv = 10; int incrv = 10;
sql = update.Set(a => a.Clicks * incrv / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Clicks * incrv / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = coalesce(\"CLICKS\", 0) * 10 / 1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = coalesce(\"CLICKS\", 0) * 10 / 1 WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Id - incrv).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Id - incrv).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"ID\" = (\"ID\" - 10) WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"ID\" = (\"ID\" - 10) WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Clicks == a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Clicks == a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = \"CLICKS\" * 10 / 1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = \"CLICKS\" * 10 / 1 WHERE (\"ID\" = 1)", sql);
var dt2000 = DateTime.Parse("2000-01-01"); var dt2000 = DateTime.Parse("2000-01-01");
sql = update.Set(a => a.Clicks == (a.CreateTime > dt2000 ? 1 : 2)).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Clicks == (a.CreateTime > dt2000 ? 1 : 2)).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = case when \"CREATETIME\" > '2000-01-01 00:00:00.000000' then 1 else 2 end WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = case when \"CREATETIME\" > '2000-01-01 00:00:00.000000' then 1 else 2 end WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Id == 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Id == 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"ID\" = 10 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"ID\" = 10 WHERE (\"ID\" = 1)", sql);
sql = update.Set(a => a.Clicks == null).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.Set(a => a.Clicks == null).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = NULL WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = NULL WHERE (\"ID\" = 1)", sql);
} }
[Fact] [Fact]
public void SetRaw() public void SetRaw()
{ {
var sql = update.Where(a => a.Id == 1).SetRaw("clicks = clicks + @incrClick", new { incrClick = 1 }).ToSql().Replace("\r\n", ""); var sql = update.Where(a => a.Id == 1).SetRaw("clicks = clicks + @incrClick", new { incrClick = 1 }).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET clicks = clicks + @incrClick WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET clicks = clicks + @incrClick WHERE (\"ID\" = 1)", sql);
} }
[Fact] [Fact]
public void SetDto() public void SetDto()
{ {
var sql = update.SetDto(new { clicks = 1, title = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); var sql = update.SetDto(new { clicks = 1, title = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1 WHERE (\"ID\" = 1)", sql);
sql = update.NoneParameter().SetDto(new { clicks = 1, title = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.NoneParameter().SetDto(new { clicks = 1, title = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = 1, \"TITLE\" = 'xxx' WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = 1, \"TITLE\" = 'xxx' WHERE (\"ID\" = 1)", sql);
sql = update.SetDto(new Dictionary<string, object> { ["clicks"] = 1, ["title"] = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.SetDto(new Dictionary<string, object> { ["clicks"] = 1, ["title"] = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1 WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = @p_0, \"TITLE\" = @p_1 WHERE (\"ID\" = 1)", sql);
sql = update.NoneParameter().SetDto(new Dictionary<string, object> { ["clicks"] = 1, ["title"] = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", ""); sql = update.NoneParameter().SetDto(new Dictionary<string, object> { ["clicks"] = 1, ["title"] = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET \"CLICKS\" = 1, \"TITLE\" = 'xxx' WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET \"CLICKS\" = 1, \"TITLE\" = 'xxx' WHERE (\"ID\" = 1)", sql);
} }
[Fact] [Fact]
public void Where() public void Where()
{ {
var sql = update.Where(a => a.Id == 1).SetRaw("title='newtitle'").ToSql().Replace("\r\n", ""); var sql = update.Where(a => a.Id == 1).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='newtitle' WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='newtitle' WHERE (\"ID\" = 1)", sql);
sql = update.Where("id = @id", new { id = 1 }).SetRaw("title='newtitle'").ToSql().Replace("\r\n", ""); sql = update.Where("id = @id", new { id = 1 }).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='newtitle' WHERE (id = @id)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='newtitle' WHERE (id = @id)", sql);
var item = new Topic { Id = 1, Title = "newtitle" }; var item = new Topic { Id = 1, Title = "newtitle" };
sql = update.Where(item).SetRaw("title='newtitle'").ToSql().Replace("\r\n", ""); sql = update.Where(item).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='newtitle' WHERE (\"ID\" = 1)", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='newtitle' WHERE (\"ID\" = 1)", sql);
var items = new List<Topic>(); var items = new List<Topic>();
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 }); for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
sql = update.Where(items).SetRaw("title='newtitle'").ToSql().Replace("\r\n", ""); sql = update.Where(items).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
Assert.Equal("UPDATE \"TB_TOPIC\" SET title='newtitle' WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql); Assert.Equal("UPDATE \"TB_TOPIC_INSERT\" SET title='newtitle' WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
} }
[Fact] [Fact]
public void ExecuteAffrows() public void ExecuteAffrows()

View File

@ -147,6 +147,8 @@ namespace FreeSql.Tests
public class LinUser public class LinUser
{ {
public long id { get; set; } public long id { get; set; }
public string name { get; set; }
public string nick { get; set; }
} }
public class Comment public class Comment
@ -532,7 +534,12 @@ namespace FreeSql.Tests
var comments2 = g.mysql.Select<Comment>() var comments2 = g.mysql.Select<Comment>()
.Include(r => r.UserInfo) .Include(r => r.UserInfo)
.From<UserLike>((z, b) => z.LeftJoin(u => u.Id == b.SubjectId)) .From<UserLike>((z, b) => z.LeftJoin(u => u.Id == b.SubjectId))
.ToList((a, b) => new { comment = a, b.SubjectId, user = a.UserInfo }); .ToList((a, b) => new { comment = a, b.SubjectId, user = a.UserInfo,
testb1 = a.UserInfo == null ? 1 : 0,
testb2 = a.UserInfo != null ? 2 : 0,
testb4 = b == null ? 3 : 0,
testb5 = b != null ? 4 : 0,
});
g.sqlite.Delete<SysModulePermission>().Where("1=1").ExecuteAffrows(); g.sqlite.Delete<SysModulePermission>().Where("1=1").ExecuteAffrows();
g.sqlite.Delete<SysModuleButton>().Where("1=1").ExecuteAffrows(); g.sqlite.Delete<SysModuleButton>().Where("1=1").ExecuteAffrows();

View File

@ -227,7 +227,9 @@ namespace FreeSql.Tests
.When(a.Id == 4, 13) .When(a.Id == 4, 13)
.When(a.Id == 5, SqlExt.Case().When(b.Id == 1, 10000).Else(999).End()) .When(a.Id == 5, SqlExt.Case().When(b.Id == 1, 10000).Else(999).End())
.End(), .End(),
groupct1 = SqlExt.GroupConcat(a.Id).Distinct().OrderBy(b.EdiId).Separator("_").ToValue() groupct1 = SqlExt.GroupConcat(a.Id).Distinct().OrderBy(b.EdiId).Separator("_").ToValue(),
testb1 = b == null ? 1 : 0,
testb2 = b != null ? 1 : 0,
}); });
var sqlextGroupConcatToList = g.mysql.Select<Edi, EdiItem>() var sqlextGroupConcatToList = g.mysql.Select<Edi, EdiItem>()
.InnerJoin((a, b) => b.Id == a.Id) .InnerJoin((a, b) => b.Id == a.Id)
@ -242,7 +244,9 @@ namespace FreeSql.Tests
.When(a.Id == 4, 13) .When(a.Id == 4, 13)
.When(a.Id == 5, SqlExt.Case().When(b.Id == 1, 10000).Else(999).End()) .When(a.Id == 5, SqlExt.Case().When(b.Id == 1, 10000).Else(999).End())
.End(), .End(),
groupct1 = SqlExt.GroupConcat(a.Id).Distinct().OrderBy(b.EdiId).Separator("_").ToValue() groupct1 = SqlExt.GroupConcat(a.Id).Distinct().OrderBy(b.EdiId).Separator("_").ToValue(),
testb1 = b == null ? 1 : 0,
testb2 = b != null ? 1 : 0,
}); });
var sqlextCase = g.sqlserver.Select<Edi, EdiItem>() var sqlextCase = g.sqlserver.Select<Edi, EdiItem>()
@ -281,7 +285,9 @@ namespace FreeSql.Tests
.GroupBy((a, b) => new { aid = a.Id, bid = b.Id }) .GroupBy((a, b) => new { aid = a.Id, bid = b.Id })
.ToDictionary(a => new .ToDictionary(a => new
{ {
sum = a.Sum(a.Value.Item2.EdiId) sum = a.Sum(a.Value.Item2.EdiId),
testb1 = a.Value.Item2 == null ? 1 : 0,
testb2 = a.Value.Item2 != null ? 1 : 0,
}); });
var sqlextCaseGroupBy2 = g.sqlserver.Select<Edi, EdiItem>() var sqlextCaseGroupBy2 = g.sqlserver.Select<Edi, EdiItem>()
@ -289,7 +295,9 @@ namespace FreeSql.Tests
.GroupBy((a, b) => new { aid = a.Id, bid = b.Id }) .GroupBy((a, b) => new { aid = a.Id, bid = b.Id })
.ToList(a => new .ToList(a => new
{ {
a.Key, sum = a.Sum(a.Value.Item2.EdiId) a.Key, sum = a.Sum(a.Value.Item2.EdiId),
testb1 = a.Value.Item2 == null ? 1 : 0,
testb2 = a.Value.Item2 != null ? 1 : 0,
}); });

View File

@ -496,6 +496,13 @@ namespace FreeSql.Internal
return ExpressionLambdaToSql(Expression.Call(leftExp, MethodDateTimeSubtractTimeSpan, rightExp), tsc); return ExpressionLambdaToSql(Expression.Call(leftExp, MethodDateTimeSubtractTimeSpan, rightExp), tsc);
} }
return $"({ExpressionLambdaToSql(leftExp, tsc)} {oper} {ExpressionLambdaToSql(rightExp, tsc)})"; return $"({ExpressionLambdaToSql(leftExp, tsc)} {oper} {ExpressionLambdaToSql(rightExp, tsc)})";
case "=":
case "<>":
var exptb = _common.GetTableByEntity(leftExp.Type);
if (exptb != null) leftExp = Expression.MakeMemberAccess(leftExp, exptb.Properties[(exptb.Primarys.FirstOrDefault() ?? exptb.Columns.FirstOrDefault().Value).CsName]);
exptb = _common.GetTableByEntity(leftExp.Type);
if (exptb?.Primarys.Any() == true) rightExp = Expression.MakeMemberAccess(rightExp, exptb.Properties[(exptb.Primarys.FirstOrDefault() ?? exptb.Columns.FirstOrDefault().Value).CsName]);
break;
} }
Type oldMapType = null; Type oldMapType = null;

View File

@ -83,7 +83,7 @@ namespace FreeSql.Odbc.KingbaseES
{ {
if (pkidx > 0) caseWhen.Append(" || '+' || "); if (pkidx > 0) caseWhen.Append(" || '+' || ");
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append("."); if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::varchar"); caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::text");
++pkidx; ++pkidx;
} }
caseWhen.Append(")"); caseWhen.Append(")");
@ -101,7 +101,7 @@ namespace FreeSql.Odbc.KingbaseES
foreach (var pk in _table.Primarys) foreach (var pk in _table.Primarys)
{ {
if (pkidx > 0) sb.Append(" || '+' || "); if (pkidx > 0) sb.Append(" || '+' || ");
sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::varchar"); sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::text");
++pkidx; ++pkidx;
} }
sb.Append(")"); sb.Append(")");
@ -110,6 +110,11 @@ namespace FreeSql.Odbc.KingbaseES
protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col)
{ {
if (_noneParameter == false) return; if (_noneParameter == false) return;
if (col.Attribute.MapType == typeof(string))
{
sb.Append("::text");
return;
}
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype; var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
if (dbtype == null) return; if (dbtype == null) return;

View File

@ -114,7 +114,7 @@ namespace FreeSql.Odbc.KingbaseES
public override string[] SplitTableName(string name) => GetSplitTableNames(name, '"', '"', 2); public override string[] SplitTableName(string name) => GetSplitTableNames(name, '"', '"', 2);
public override string QuoteParamterName(string name) => $"@{name.ToUpper()}"; public override string QuoteParamterName(string name) => $"@{name.ToUpper()}";
public override string IsNull(string sql, object value) => $"coalesce({sql}, {value})"; public override string IsNull(string sql, object value) => $"coalesce({sql}, {value})";
public override string StringConcat(string[] objs, Type[] types) => $"{string.Join(" || ", objs.Select((a, b) => b == 0 ? $"{a}::varchar" : a))}"; //First ::varchar public override string StringConcat(string[] objs, Type[] types) => $"{string.Join(" || ", objs.Select((a, b) => b == 0 ? $"{a}::text" : a))}"; //First ::text
public override string Mod(string left, string right, Type leftType, Type rightType) => $"{left} % {right}"; public override string Mod(string left, string right, Type leftType, Type rightType) => $"{left} % {right}";
public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} / {right}"; public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} / {right}";
public override string Now => "current_timestamp"; public override string Now => "current_timestamp";

View File

@ -83,7 +83,7 @@ namespace FreeSql.Odbc.PostgreSQL
{ {
if (pkidx > 0) caseWhen.Append(" || '+' || "); if (pkidx > 0) caseWhen.Append(" || '+' || ");
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append("."); if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::varchar"); caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::text");
++pkidx; ++pkidx;
} }
caseWhen.Append(")"); caseWhen.Append(")");
@ -101,7 +101,7 @@ namespace FreeSql.Odbc.PostgreSQL
foreach (var pk in _table.Primarys) foreach (var pk in _table.Primarys)
{ {
if (pkidx > 0) sb.Append(" || '+' || "); if (pkidx > 0) sb.Append(" || '+' || ");
sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::varchar"); sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::text");
++pkidx; ++pkidx;
} }
sb.Append(")"); sb.Append(")");
@ -110,6 +110,11 @@ namespace FreeSql.Odbc.PostgreSQL
protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col)
{ {
if (_noneParameter == false) return; if (_noneParameter == false) return;
if (col.Attribute.MapType == typeof(string))
{
sb.Append("::text");
return;
}
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype; var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
if (dbtype == null) return; if (dbtype == null) return;

View File

@ -83,7 +83,7 @@ namespace FreeSql.PostgreSQL.Curd
{ {
if (pkidx > 0) caseWhen.Append(" || '+' || "); if (pkidx > 0) caseWhen.Append(" || '+' || ");
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append("."); if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::varchar"); caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::text");
++pkidx; ++pkidx;
} }
caseWhen.Append(")"); caseWhen.Append(")");
@ -101,7 +101,7 @@ namespace FreeSql.PostgreSQL.Curd
foreach (var pk in _table.Primarys) foreach (var pk in _table.Primarys)
{ {
if (pkidx > 0) sb.Append(" || '+' || "); if (pkidx > 0) sb.Append(" || '+' || ");
sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::varchar"); sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::text");
++pkidx; ++pkidx;
} }
sb.Append(")"); sb.Append(")");
@ -110,6 +110,11 @@ namespace FreeSql.PostgreSQL.Curd
protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col)
{ {
if (_noneParameter == false) return; if (_noneParameter == false) return;
if (col.Attribute.MapType == typeof(string))
{
sb.Append("::text");
return;
}
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype; var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
if (dbtype == null) return; if (dbtype == null) return;

View File

@ -1,4 +1,5 @@
using FreeSql.Internal; using FreeSql.DataAnnotations;
using FreeSql.Internal;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -83,7 +84,7 @@ namespace FreeSql.ShenTong.Curd
{ {
if (pkidx > 0) caseWhen.Append(" || '+' || "); if (pkidx > 0) caseWhen.Append(" || '+' || ");
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append("."); if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::varchar"); caseWhen.Append(_commonUtils.QuoteReadColumn(pk.CsType, pk.Attribute.MapType, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::text");
++pkidx; ++pkidx;
} }
caseWhen.Append(")"); caseWhen.Append(")");
@ -101,7 +102,7 @@ namespace FreeSql.ShenTong.Curd
foreach (var pk in _table.Primarys) foreach (var pk in _table.Primarys)
{ {
if (pkidx > 0) sb.Append(" || '+' || "); if (pkidx > 0) sb.Append(" || '+' || ");
sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::varchar"); sb.Append(_commonUtils.FormatSql("{0}", pk.GetMapValue(d))).Append("::text");
++pkidx; ++pkidx;
} }
sb.Append(")"); sb.Append(")");
@ -110,6 +111,11 @@ namespace FreeSql.ShenTong.Curd
protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) protected override void ToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col)
{ {
if (_noneParameter == false) return; if (_noneParameter == false) return;
if (col.Attribute.MapType == typeof(string))
{
sb.Append("::text");
return;
}
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype; var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
if (dbtype == null) return; if (dbtype == null) return;