From 94fa653ec95043ba98fb350ddfe8be8d7ea9ab06 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 10 Oct 2023 03:38:05 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=81=A2=E5=A4=8D=20=E4=BB=A3=E7=A0=81=20p?= =?UTF-8?q?review=20=E6=94=B9=E5=8A=A8=EF=BC=8C=E4=BC=98=E5=8C=96=E8=A1=A5?= =?UTF-8?q?=E5=85=A8=20CanUpdate=20false=EF=BC=9B#1630?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/DbSet/DbSetAsync.cs | 2 +- FreeSql.DbContext/DbSet/DbSetSync.cs | 2 +- ...liteInsertOrUpdateIfExistsDoNothingTest.cs | 290 ++---------------- .../Sqlite/Curd/SqliteInsertOrUpdateTest.cs | 54 ++-- FreeSql/Internal/UtilsExpressionTree.cs | 9 +- 5 files changed, 56 insertions(+), 301 deletions(-) diff --git a/FreeSql.DbContext/DbSet/DbSetAsync.cs b/FreeSql.DbContext/DbSet/DbSetAsync.cs index a8967488..18763e4e 100644 --- a/FreeSql.DbContext/DbSet/DbSetAsync.cs +++ b/FreeSql.DbContext/DbSet/DbSetAsync.cs @@ -404,7 +404,7 @@ namespace FreeSql if (data?.Count > 0) { - if (cuig.Length >= _table.Columns.Count - _table.Primarys.Length) + if (cuig.Length == _table.Columns.Count) return ups.Length == data.Count ? -998 : -997; var update = this.OrmUpdate(data.Select(a => a.Value)).IgnoreColumns(cuig); diff --git a/FreeSql.DbContext/DbSet/DbSetSync.cs b/FreeSql.DbContext/DbSet/DbSetSync.cs index 04016961..19bcc519 100644 --- a/FreeSql.DbContext/DbSet/DbSetSync.cs +++ b/FreeSql.DbContext/DbSet/DbSetSync.cs @@ -438,7 +438,7 @@ namespace FreeSql if (data?.Count > 0) { - if (cuig.Length >= _table.Columns.Count - _table.Primarys.Length) + if (cuig.Length == _table.Columns.Count) return ups.Length == data.Count ? -998 : -997; var update = this.OrmUpdate(data.Select(a => a.Value)).IgnoreColumns(cuig); diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateIfExistsDoNothingTest.cs b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateIfExistsDoNothingTest.cs index 46e0685d..1879055a 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateIfExistsDoNothingTest.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateIfExistsDoNothingTest.cs @@ -1,4 +1,4 @@ -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; using System; using System.Collections.Generic; using System.Linq; @@ -17,83 +17,27 @@ namespace FreeSql.Tests.Sqlite fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb02 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb02""(""id"", ""name"") SELECT 1, '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb02""(""id"", ""name"") VALUES(1, '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb02 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb02""(""id"", ""name"") SELECT 1, '011' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb02""(""id"", ""name"") VALUES(1, '011')", sql); Assert.Equal(0, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb02 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb02""(""id"", ""name"") SELECT 2, '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 2) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb02""(""id"", ""name"") VALUES(2, '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb02 { id = 1, name = "01" }, new tbioudb02 { id = 2, name = "02" }, new tbioudb02 { id = 3, name = "03" }, new tbioudb02 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb02""(""id"", ""name"") SELECT 1, '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '03' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '04' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); Assert.Equal(2, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb02 { id = 1, name = "001" }, new tbioudb02 { id = 2, name = "002" }, new tbioudb02 { id = 3, name = "003" }, new tbioudb02 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb02""(""id"", ""name"") SELECT 1, '001' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '002' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '003' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '004' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb02"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); Assert.Equal(0, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count()); @@ -109,83 +53,27 @@ UNION ALL fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb022 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 1, '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(1, '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb022 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 1, '011' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(1, '011')", sql); Assert.Equal(0, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb022 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 2, '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 2) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(2, '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb022 { id = 1, name = "01" }, new tbioudb022 { id = 2, name = "02" }, new tbioudb022 { id = 3, name = "03" }, new tbioudb022 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 1, '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '03' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '04' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); Assert.Equal(2, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb022 { id = 1, name = "001" }, new tbioudb022 { id = 2, name = "002" }, new tbioudb022 { id = 3, name = "003" }, new tbioudb022 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 1, '001' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '002' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '003' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '004' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); Assert.Equal(0, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count()); @@ -219,29 +107,7 @@ UNION ALL //--no primary and yes iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb022 { id = 1, name = "100001" }, new tbioudb022 { name = "00001" }, new tbioudb022 { id = 2, name = "100002" }, new tbioudb022 { name = "00002" }, new tbioudb022 { id = 3, name = "100003" }, new tbioudb022 { name = "00003" }, new tbioudb022 { id = 4, name = "100004" }, new tbioudb022 { name = "00004" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb022""(""id"", ""name"") SELECT 1, '100001' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '100002' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '100003' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '100004' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb022"" a - WHERE (a.""id"" = 4) - limit 0,1) + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb022""(""id"", ""name"") VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004') ; @@ -263,83 +129,27 @@ INSERT INTO ""tbioudb022""(""name"") VALUES('00001'), ('00002'), ('00003'), ('00 fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb03 { id1 = 1, id2 = "01", name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb03""(""id1"", ""id2"", ""name"") SELECT 1, '01', '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 1 AND a.""id2"" = '01') - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb03 { id1 = 1, id2 = "01", name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb03""(""id1"", ""id2"", ""name"") SELECT 1, '01', '011' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 1 AND a.""id2"" = '01') - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '011')", sql); Assert.Equal(0, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb03 { id1 = 2, id2 = "02", name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb03""(""id1"", ""id2"", ""name"") SELECT 2, '02', '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 2 AND a.""id2"" = '02') - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb03 { id1 = 1, id2 = "01", name = "01" }, new tbioudb03 { id1 = 2, id2 = "02", name = "02" }, new tbioudb03 { id1 = 3, id2 = "03", name = "03" }, new tbioudb03 { id1 = 4, id2 = "04", name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb03""(""id1"", ""id2"", ""name"") SELECT 1, '01', '01' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 1 AND a.""id2"" = '01') - limit 0,1) -UNION ALL - SELECT 2, '02', '02' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 2 AND a.""id2"" = '02') - limit 0,1) -UNION ALL - SELECT 3, '03', '03' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 3 AND a.""id2"" = '03') - limit 0,1) -UNION ALL - SELECT 4, '04', '04' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 4 AND a.""id2"" = '04') - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')", sql); Assert.Equal(2, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb03 { id1 = 1, id2 = "01", name = "001" }, new tbioudb03 { id1 = 2, id2 = "02", name = "002" }, new tbioudb03 { id1 = 3, id2 = "03", name = "003" }, new tbioudb03 { id1 = 4, id2 = "04", name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb03""(""id1"", ""id2"", ""name"") SELECT 1, '01', '001' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 1 AND a.""id2"" = '01') - limit 0,1) -UNION ALL - SELECT 2, '02', '002' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 2 AND a.""id2"" = '02') - limit 0,1) -UNION ALL - SELECT 3, '03', '003' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 3 AND a.""id2"" = '03') - limit 0,1) -UNION ALL - SELECT 4, '04', '004' - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb03"" a - WHERE (a.""id1"" = 4 AND a.""id2"" = '04') - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')", sql); Assert.Equal(0, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => a.id1 == 1 && a.id2 == "01" || a.id1 == 2 && a.id2 == "02" || a.id1 == 3 && a.id2 == "03" || a.id1 == 4 && a.id2 == "04").ToList(); Assert.Equal(4, lst.Where(a => a.name == "0" + a.id1).Count()); @@ -359,83 +169,27 @@ UNION ALL fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb04 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") SELECT 1, '01', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb04 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") SELECT 1, '011', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 1) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '011', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(0, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new tbioudb04 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") SELECT 2, '02', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 2) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(2, '02', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb04 { id = 1, name = "01" }, new tbioudb04 { id = 2, name = "02" }, new tbioudb04 { id = 3, name = "03" }, new tbioudb04 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") SELECT 1, '01', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '02', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '03', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '04', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime')), (2, '02', 0, datetime(current_timestamp,'localtime')), (3, '03', 0, datetime(current_timestamp,'localtime')), (4, '04', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(2, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().IfExistsDoNothing().SetSource(new[] { new tbioudb04 { id = 1, name = "001" }, new tbioudb04 { id = 2, name = "002" }, new tbioudb04 { id = 3, name = "003" }, new tbioudb04 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"INSERT INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") SELECT 1, '001', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 1) - limit 0,1) -UNION ALL - SELECT 2, '002', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 2) - limit 0,1) -UNION ALL - SELECT 3, '003', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 3) - limit 0,1) -UNION ALL - SELECT 4, '004', 0, datetime(current_timestamp,'localtime') - WHERE NOT EXISTS(SELECT 1 - FROM ""tbioudb04"" a - WHERE (a.""id"" = 4) - limit 0,1)", sql); + Assert.Equal(@"INSERT OR IGNORE INTO ""tbioudb04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '001', 0, datetime(current_timestamp,'localtime')), (2, '002', 0, datetime(current_timestamp,'localtime')), (3, '003', 0, datetime(current_timestamp,'localtime')), (4, '004', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(0, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count()); diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateTest.cs b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateTest.cs index d0e650ad..200aee51 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateTest.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteInsertOrUpdateTest.cs @@ -1,4 +1,4 @@ -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; using System; using System.Collections.Generic; using System.Linq; @@ -16,27 +16,27 @@ namespace FreeSql.Tests.Sqlite fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().SetSource(new tbiou01 { id = 1 }); var sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou01 { id = 1 }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1)", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou01 { id = 2 }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(2)", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(2)", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql); Assert.Equal(4, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)", sql); Assert.Equal(4, iou.ExecuteAffrows()); } class tbiou01 @@ -50,27 +50,27 @@ namespace FreeSql.Tests.Sqlite fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().SetSource(new tbiou02 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou02 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '011')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '011')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou02 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(2, '02')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(2, '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou02 { id = 1, name = "01" }, new tbiou02 { id = 2, name = "02" }, new tbiou02 { id = 3, name = "03" }, new tbiou02 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); Assert.Equal(4, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou02 { id = 1, name = "001" }, new tbiou02 { id = 2, name = "002" }, new tbiou02 { id = 3, name = "003" }, new tbiou02 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); Assert.Equal(4, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count()); @@ -87,27 +87,27 @@ namespace FreeSql.Tests.Sqlite fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().SetSource(new tbiou022 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou022 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '011')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '011')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou022 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(2, '02')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(2, '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou022 { id = 1, name = "01" }, new tbiou022 { id = 2, name = "02" }, new tbiou022 { id = 3, name = "03" }, new tbiou022 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')", sql); Assert.Equal(4, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou022 { id = 1, name = "001" }, new tbiou022 { id = 2, name = "002" }, new tbiou022 { id = 3, name = "003" }, new tbiou022 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')", sql); Assert.Equal(4, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count()); @@ -141,7 +141,7 @@ namespace FreeSql.Tests.Sqlite //--no primary and yes iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou022 { id = 1, name = "100001" }, new tbiou022 { name = "00001" }, new tbiou022 { id = 2, name = "100002" }, new tbiou022 { name = "00002" }, new tbiou022 { id = 3, name = "100003" }, new tbiou022 { name = "00003" }, new tbiou022 { id = 4, name = "100004" }, new tbiou022 { name = "00004" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004') + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou022""(""id"", ""name"") VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004') ; @@ -163,27 +163,27 @@ INSERT INTO ""tbiou022""(""name"") VALUES('00001'), ('00002'), ('00003'), ('0000 fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '011')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '011')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "01" }, new tbiou03 { id1 = 2, id2 = "02", name = "02" }, new tbiou03 { id1 = 3, id2 = "03", name = "03" }, new tbiou03 { id1 = 4, id2 = "04", name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')", sql); Assert.Equal(4, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "001" }, new tbiou03 { id1 = 2, id2 = "02", name = "002" }, new tbiou03 { id1 = 3, id2 = "03", name = "003" }, new tbiou03 { id1 = 4, id2 = "04", name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')", sql); Assert.Equal(4, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => a.id1 == 1 && a.id2 == "01" || a.id1 == 2 && a.id2 == "02" || a.id1 == 3 && a.id2 == "03" || a.id1 == 4 && a.id2 == "04").ToList(); Assert.Equal(4, lst.Where(a => a.name == "00" + a.id1).Count()); @@ -203,27 +203,27 @@ INSERT INTO ""tbiou022""(""name"") VALUES('00001'), ('00002'), ('00003'), ('0000 fsql.Delete().Where("1=1").ExecuteAffrows(); var iou = fsql.InsertOrUpdate().SetSource(new tbiou04 { id = 1, name = "01" }); var sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime'))", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou04 { id = 1, name = "011" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '011', 0, datetime(current_timestamp,'localtime'))", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '011', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new tbiou04 { id = 2, name = "02" }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(2, '02', 0, datetime(current_timestamp,'localtime'))", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(2, '02', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(1, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou04 { id = 1, name = "01" }, new tbiou04 { id = 2, name = "02" }, new tbiou04 { id = 3, name = "03" }, new tbiou04 { id = 4, name = "04" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime')), (2, '02', 0, datetime(current_timestamp,'localtime')), (3, '03', 0, datetime(current_timestamp,'localtime')), (4, '04', 0, datetime(current_timestamp,'localtime'))", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '01', 0, datetime(current_timestamp,'localtime')), (2, '02', 0, datetime(current_timestamp,'localtime')), (3, '03', 0, datetime(current_timestamp,'localtime')), (4, '04', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(4, iou.ExecuteAffrows()); iou = fsql.InsertOrUpdate().SetSource(new[] { new tbiou04 { id = 1, name = "001" }, new tbiou04 { id = 2, name = "002" }, new tbiou04 { id = 3, name = "003" }, new tbiou04 { id = 4, name = "004" } }); sql = iou.ToSql(); - Assert.Equal(@"REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '001', 0, datetime(current_timestamp,'localtime')), (2, '002', 0, datetime(current_timestamp,'localtime')), (3, '003', 0, datetime(current_timestamp,'localtime')), (4, '004', 0, datetime(current_timestamp,'localtime'))", sql); + Assert.Equal(@"INSERT OR REPLACE INTO ""tbiou04""(""id"", ""name"", ""version"", ""CreateTime"") VALUES(1, '001', 0, datetime(current_timestamp,'localtime')), (2, '002', 0, datetime(current_timestamp,'localtime')), (3, '003', 0, datetime(current_timestamp,'localtime')), (4, '004', 0, datetime(current_timestamp,'localtime'))", sql); Assert.Equal(4, iou.ExecuteAffrows()); var lst = fsql.Select().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList(); Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count()); diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index c64c9ffc..fc6b3a20 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -509,10 +509,6 @@ namespace FreeSql.Internal }); } trytb.Indexes = indexesDict.Values.ToArray(); - trytb.ColumnsByPosition = columnsList.Where(a => a.Attribute.Position > 0).OrderBy(a => a.Attribute.Position) - .Concat(columnsList.Where(a => a.Attribute.Position == 0)) - .Concat(columnsList.Where(a => a.Attribute.Position < 0).OrderBy(a => a.Attribute.Position)).ToArray(); - trytb.ColumnsByCanUpdateDbUpdateValue = columnsList.Where(a => a.Attribute.CanUpdate == true && string.IsNullOrEmpty(a.DbUpdateValue) == false).ToArray(); trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray(); if (trytb.Primarys.Any() == false) @@ -547,6 +543,7 @@ namespace FreeSql.Internal } foreach (var col in trytb.Columns.Values) { + if (col.Attribute.IsPrimary == false && col.Attribute.IsIdentity) col.Attribute.CanUpdate = false; var ltp = @"\(([^\)]+)\)"; col.DbTypeText = Regex.Replace(col.Attribute.DbType.Replace("NOT NULL", "").Replace(" NULL", "").Trim(), ltp, ""); var m = Regex.Match(col.Attribute.DbType, ltp); @@ -582,6 +579,10 @@ namespace FreeSql.Internal } } trytb.IsRereadSql = trytb.Columns.Where(a => string.IsNullOrWhiteSpace(a.Value.Attribute.RereadSql) == false).Any(); + trytb.ColumnsByPosition = columnsList.Where(a => a.Attribute.Position > 0).OrderBy(a => a.Attribute.Position) + .Concat(columnsList.Where(a => a.Attribute.Position == 0)) + .Concat(columnsList.Where(a => a.Attribute.Position < 0).OrderBy(a => a.Attribute.Position)).ToArray(); + trytb.ColumnsByCanUpdateDbUpdateValue = columnsList.Where(a => a.Attribute.CanUpdate == true && string.IsNullOrEmpty(a.DbUpdateValue) == false).ToArray(); tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb); #region 查找导航属性的关系、virtual 属性延时加载,动态产生新的重写类