mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
- 增加 IFreeSql.InsertOrUpdate 方法 #316
This commit is contained in:
@ -0,0 +1,323 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.Dameng
|
||||
{
|
||||
public class DamengInsertOrUpdateTest
|
||||
{
|
||||
IFreeSql fsql => g.dameng;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnlyPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 2 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual
|
||||
UNION ALL
|
||||
SELECT 2 FROM dual
|
||||
UNION ALL
|
||||
SELECT 3 FROM dual
|
||||
UNION ALL
|
||||
SELECT 4 FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual
|
||||
UNION ALL
|
||||
SELECT 2 FROM dual
|
||||
UNION ALL
|
||||
SELECT 3 FROM dual
|
||||
UNION ALL
|
||||
SELECT 4 FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
}
|
||||
class tbiou01
|
||||
{
|
||||
public int id { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '011' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 2 as ID, '02' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04' FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '001' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '002' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '003' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '004' FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '01' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "02", name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '02' as ID2, '011' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 2 as ID1, '02' as ID2, '02' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '01' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', '02' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', '03' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', '04' FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '001' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', '002' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', '003' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', '004' FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou03>().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());
|
||||
}
|
||||
class tbiou03
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public string id2 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
|
||||
{
|
||||
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '011' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 2 as ID, '02' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', 0, systimestamp FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '001' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '002', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '003', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '004', 0, systimestamp FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou04
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
[Column(IsVersion = true)]
|
||||
public int version { get; set; }
|
||||
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,167 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.MySql
|
||||
{
|
||||
public class MySqlInsertOrUpdateTest
|
||||
{
|
||||
|
||||
IFreeSql fsql => g.mysql;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '01')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '011')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(2, '02')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"INSERT INTO `tbiou02`(`id`, `name`) VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou03`(`id1`, `id2`, `name`) VALUES(1, '01', '01')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou03`(`id1`, `id2`, `name`) VALUES(1, '01', '011')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou03`(`id1`, `id2`, `name`) VALUES(2, '02', '02')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"INSERT INTO `tbiou03`(`id1`, `id2`, `name`) VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"INSERT INTO `tbiou03`(`id1`, `id2`, `name`) VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou03>().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());
|
||||
}
|
||||
class tbiou03
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public string id2 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
|
||||
{
|
||||
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou04`(`id`, `name`, `version`, `CreateTime`) VALUES(1, '01', 0, now(3))
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`),
|
||||
`version` = `version` + 1", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou04`(`id`, `name`, `version`, `CreateTime`) VALUES(1, '011', 0, now(3))
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`),
|
||||
`version` = `version` + 1", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou04`(`id`, `name`, `version`, `CreateTime`) VALUES(2, '02', 0, now(3))
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`),
|
||||
`version` = `version` + 1", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"INSERT INTO `tbiou04`(`id`, `name`, `version`, `CreateTime`) VALUES(1, '01', 0, now(3)), (2, '02', 0, now(3)), (3, '03', 0, now(3)), (4, '04', 0, now(3))
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`),
|
||||
`version` = `version` + 1", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"INSERT INTO `tbiou04`(`id`, `name`, `version`, `CreateTime`) VALUES(1, '001', 0, now(3)), (2, '002', 0, now(3)), (3, '003', 0, now(3)), (4, '004', 0, now(3))
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`),
|
||||
`version` = `version` + 1", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou04
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
[Column(IsVersion = true)]
|
||||
public int version { get; set; }
|
||||
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,195 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Odbc.MySql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.MySql
|
||||
{
|
||||
public class OnDuplicateKeyUpdateTest
|
||||
{
|
||||
class TestOnDuplicateKeyUpdateInfo
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime time { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecuteAffrows()
|
||||
{
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 100, 101, 102 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(100, 'title-100', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = VALUES(`time`)", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 101, title = "title-101", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 102, title = "title-102", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(100, 'title-100', '2000-01-01 00:00:00.000'), (101, 'title-101', '2000-01-01 00:00:00.000'), (102, 'title-102', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = VALUES(`time`)", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IgnoreColumns()
|
||||
{
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = '2000-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
|
||||
}).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = CASE `id`
|
||||
WHEN 200 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 201 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 202 THEN '2000-01-01 00:00:00.000' END", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
|
||||
odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity()).IgnoreColumns(a => a.title);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2000-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
|
||||
}).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity()).IgnoreColumns(a => a.title);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = CASE `id`
|
||||
WHEN 200 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 201 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 202 THEN '2000-01-01 00:00:00.000' END", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateColumns()
|
||||
{
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = '2000-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
|
||||
}).InsertColumns(a => a.title).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`title` = VALUES(`title`),
|
||||
`time` = CASE `id`
|
||||
WHEN 300 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 301 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 302 THEN '2000-01-01 00:00:00.000' END", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
|
||||
odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().InsertIdentity()).UpdateColumns(a => a.time);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2000-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
|
||||
}).InsertColumns(a => a.title).NoneParameter().InsertIdentity()).UpdateColumns(a => a.time);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = CASE `id`
|
||||
WHEN 300 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 301 THEN '2000-01-01 00:00:00.000'
|
||||
WHEN 302 THEN '2000-01-01 00:00:00.000' END", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Set()
|
||||
{
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity()).Set(a => a.time, DateTime.Parse("2020-1-1"));
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity()).Set(a => a.time, DateTime.Parse("2020-1-1"));
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000'), (401, 'title-401', '2000-01-01 00:00:00.000'), (402, 'title-402', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000'", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
var dt2020 = DateTime.Parse("2020-1-1");
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
|
||||
odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity()).Set(a => a.time == dt2020);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity()).Set(a => a.time == dt2020);
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000'), (401, 'title-401', '2000-01-01 00:00:00.000'), (402, 'title-402', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000'", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
|
||||
odku1 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity()).Set(a => new { time = dt2020, title = a.title + "123" });
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000', `title` = concat(`title`, '123')", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcMySqlOnDuplicateKeyUpdate<TestOnDuplicateKeyUpdateInfo>(g.mysql.Insert(new[] {
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnDuplicateKeyUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity()).Set(a => new { time = dt2020, title = a.title + "123" });
|
||||
Assert.Equal(@"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(400, 'title-400', '2000-01-01 00:00:00.000'), (401, 'title-401', '2000-01-01 00:00:00.000'), (402, 'title-402', '2000-01-01 00:00:00.000')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`time` = '2020-01-01 00:00:00.000', `title` = concat(`title`, '123')", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,323 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.Oracle
|
||||
{
|
||||
public class OracleInsertOrUpdateTest
|
||||
{
|
||||
IFreeSql fsql => g.oracle;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnlyPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 2 as ID FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual
|
||||
UNION ALL
|
||||
SELECT 2 FROM dual
|
||||
UNION ALL
|
||||
SELECT 3 FROM dual
|
||||
UNION ALL
|
||||
SELECT 4 FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU01"" t1
|
||||
USING (SELECT 1 as ID FROM dual
|
||||
UNION ALL
|
||||
SELECT 2 FROM dual
|
||||
UNION ALL
|
||||
SELECT 3 FROM dual
|
||||
UNION ALL
|
||||
SELECT 4 FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"")
|
||||
values (t2.ID)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
}
|
||||
class tbiou01
|
||||
{
|
||||
public int id { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '011' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 2 as ID, '02' as NAME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04' FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO ""TBIOU02"" t1
|
||||
USING (SELECT 1 as ID, '001' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '002' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '003' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '004' FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"")
|
||||
values (t2.ID, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '01' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "02", name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '02' as ID2, '011' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 2 as ID1, '02' as ID2, '02' as NAME FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '01' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', '02' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', '03' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', '04' FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO ""TBIOU03"" t1
|
||||
USING (SELECT 1 as ID1, '01' as ID2, '001' as NAME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', '002' FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', '003' FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', '004' FROM dual ) t2 ON (t1.""ID1"" = t2.ID1 AND t1.""ID2"" = t2.ID2)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID1"", ""ID2"", ""NAME"")
|
||||
values (t2.ID1, t2.ID2, t2.NAME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou03>().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());
|
||||
}
|
||||
class tbiou03
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public string id2 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
|
||||
{
|
||||
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '011' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 2 as ID, '02' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '01' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '02', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '03', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '04', 0, systimestamp FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO ""TBIOU04"" t1
|
||||
USING (SELECT 1 as ID, '001' as NAME, 0 as VERSION, systimestamp as CREATETIME FROM dual
|
||||
UNION ALL
|
||||
SELECT 2, '002', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 3, '003', 0, systimestamp FROM dual
|
||||
UNION ALL
|
||||
SELECT 4, '004', 0, systimestamp FROM dual ) t2 ON (t1.""ID"" = t2.ID)
|
||||
WHEN MATCHED THEN
|
||||
update set ""NAME"" = t2.NAME, ""VERSION"" = t1.""VERSION"" + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert (""ID"", ""NAME"", ""VERSION"", ""CREATETIME"")
|
||||
values (t2.ID, t2.NAME, t2.VERSION, t2.CREATETIME)", sql);
|
||||
iou.ExecuteAffrows();
|
||||
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou04
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
[Column(IsVersion = true)]
|
||||
public int version { get; set; }
|
||||
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Odbc.PostgreSQL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.PostgreSQL
|
||||
{
|
||||
public class OnConflictDoUpdateTest
|
||||
{
|
||||
class TestOnConflictDoUpdateInfo
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime? time { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecuteAffrows()
|
||||
{
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 100, 101, 102 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = EXCLUDED.""time""", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 101, title = "title-101", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 102, title = "title-102", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000'), (101, 'title-101', '2000-01-01 00:00:00.000000'), (102, 'title-102', '2000-01-01 00:00:00.000000')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = EXCLUDED.""time""", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IgnoreColumns()
|
||||
{
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = '2000-01-01 00:00:00.000000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
|
||||
}).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = CASE EXCLUDED.""id""
|
||||
WHEN 200 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 201 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 202 THEN '2000-01-01 00:00:00.000000' END::timestamp", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
|
||||
odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity()).IgnoreColumns(a => a.title);
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = '2000-01-01 00:00:00.000000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
|
||||
}).IgnoreColumns(a => a.time).NoneParameter().InsertIdentity()).IgnoreColumns(a => a.title);
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = CASE EXCLUDED.""id""
|
||||
WHEN 200 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 201 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 202 THEN '2000-01-01 00:00:00.000000' END::timestamp", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateColumns()
|
||||
{
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = '2000-01-01 00:00:00.000000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
|
||||
}).InsertColumns(a => a.title).NoneParameter().InsertIdentity());
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""title"" = EXCLUDED.""title"",
|
||||
""time"" = CASE EXCLUDED.""id""
|
||||
WHEN 300 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 301 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 302 THEN '2000-01-01 00:00:00.000000' END::timestamp", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
|
||||
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
|
||||
odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().InsertIdentity()).UpdateColumns(a => a.time);
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = '2000-01-01 00:00:00.000000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
|
||||
}).InsertColumns(a => a.title).NoneParameter().InsertIdentity()).UpdateColumns(a => a.time);
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = CASE EXCLUDED.""id""
|
||||
WHEN 300 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 301 THEN '2000-01-01 00:00:00.000000'
|
||||
WHEN 302 THEN '2000-01-01 00:00:00.000000' END::timestamp", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Set()
|
||||
{
|
||||
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
|
||||
var odku1 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().InsertIdentity()).Set(a => a.time, DateTime.Parse("2020-1-1"));
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = '2020-01-01 00:00:00.000000'", odku1.ToSql());
|
||||
Assert.Equal(1, odku1.ExecuteAffrows());
|
||||
|
||||
var odku2 = new OdbcPostgreSQLOnConflictDoUpdate<TestOnConflictDoUpdateInfo>(g.pgsql.Insert(new[] {
|
||||
new TestOnConflictDoUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
|
||||
new TestOnConflictDoUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
|
||||
}).NoneParameter().InsertIdentity()).Set(a => a.time, DateTime.Parse("2020-1-1"));
|
||||
Assert.Equal(@"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""time"" = '2020-01-01 00:00:00.000000'", odku2.ToSql());
|
||||
odku2.ExecuteAffrows();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.PostgreSQL
|
||||
{
|
||||
public class PostgreSQLInsertOrUpdateTest
|
||||
{
|
||||
IFreeSql fsql => g.pgsql;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnlyPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou01""(""id"") VALUES(1)
|
||||
ON CONFLICT(""id"") DO NOTHING", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou01""(""id"") VALUES(1)
|
||||
ON CONFLICT(""id"") DO NOTHING", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou01""(""id"") VALUES(2)
|
||||
ON CONFLICT(""id"") DO NOTHING", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)
|
||||
ON CONFLICT(""id"") DO NOTHING", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou01""(""id"") VALUES(1), (2), (3), (4)
|
||||
ON CONFLICT(""id"") DO NOTHING", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
}
|
||||
class tbiou01
|
||||
{
|
||||
public int id { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou02""(""id"", ""name"") VALUES(1, '011')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou02""(""id"", ""name"") VALUES(2, '02')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"INSERT INTO ""tbiou02""(""id"", ""name"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"INSERT INTO ""tbiou02""(""id"", ""name"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01')
|
||||
ON CONFLICT(""id1"", ""id2"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "02", name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '02', '011')
|
||||
ON CONFLICT(""id1"", ""id2"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(2, '02', '02')
|
||||
ON CONFLICT(""id1"", ""id2"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"INSERT INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')
|
||||
ON CONFLICT(""id1"", ""id2"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"INSERT INTO ""tbiou03""(""id1"", ""id2"", ""name"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')
|
||||
ON CONFLICT(""id1"", ""id2"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name""", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou03>().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());
|
||||
}
|
||||
class tbiou03
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public string id2 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
|
||||
{
|
||||
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou04""(""id"", ""name"", ""version"", ""createtime"") VALUES(1, '01', 0, current_timestamp)
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name"",
|
||||
""version"" = ""tbiou04"".""version"" + 1", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou04""(""id"", ""name"", ""version"", ""createtime"") VALUES(1, '011', 0, current_timestamp)
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name"",
|
||||
""version"" = ""tbiou04"".""version"" + 1", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO ""tbiou04""(""id"", ""name"", ""version"", ""createtime"") VALUES(2, '02', 0, current_timestamp)
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name"",
|
||||
""version"" = ""tbiou04"".""version"" + 1", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"INSERT INTO ""tbiou04""(""id"", ""name"", ""version"", ""createtime"") VALUES(1, '01', 0, current_timestamp), (2, '02', 0, current_timestamp), (3, '03', 0, current_timestamp), (4, '04', 0, current_timestamp)
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name"",
|
||||
""version"" = ""tbiou04"".""version"" + 1", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"INSERT INTO ""tbiou04""(""id"", ""name"", ""version"", ""createtime"") VALUES(1, '001', 0, current_timestamp), (2, '002', 0, current_timestamp), (3, '003', 0, current_timestamp), (4, '004', 0, current_timestamp)
|
||||
ON CONFLICT(""id"") DO UPDATE SET
|
||||
""name"" = EXCLUDED.""name"",
|
||||
""version"" = ""tbiou04"".""version"" + 1", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou04
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
[Column(IsVersion = true)]
|
||||
public int version { get; set; }
|
||||
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,323 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Odbc.SqlServer
|
||||
{
|
||||
public class SqlServerInsertOrUpdateTest
|
||||
{
|
||||
IFreeSql fsql => g.sqlserver;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnlyPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou01] t1
|
||||
USING (SELECT 1 as id ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id])
|
||||
values (t2.id);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou01] t1
|
||||
USING (SELECT 1 as id ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id])
|
||||
values (t2.id);", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou01] t1
|
||||
USING (SELECT 2 as id ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id])
|
||||
values (t2.id);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou01] t1
|
||||
USING (SELECT 1 as id
|
||||
UNION ALL
|
||||
SELECT 2
|
||||
UNION ALL
|
||||
SELECT 3
|
||||
UNION ALL
|
||||
SELECT 4 ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id])
|
||||
values (t2.id);", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou01] t1
|
||||
USING (SELECT 1 as id
|
||||
UNION ALL
|
||||
SELECT 2
|
||||
UNION ALL
|
||||
SELECT 3
|
||||
UNION ALL
|
||||
SELECT 4 ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id])
|
||||
values (t2.id);", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
}
|
||||
class tbiou01
|
||||
{
|
||||
public int id { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou02] t1
|
||||
USING (SELECT 1 as id, N'01' as name ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name])
|
||||
values (t2.id, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou02] t1
|
||||
USING (SELECT 1 as id, N'011' as name ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name])
|
||||
values (t2.id, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou02] t1
|
||||
USING (SELECT 2 as id, N'02' as name ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name])
|
||||
values (t2.id, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO [tbiou02] t1
|
||||
USING (SELECT 1 as id, N'01' as name
|
||||
UNION ALL
|
||||
SELECT 2, N'02'
|
||||
UNION ALL
|
||||
SELECT 3, N'03'
|
||||
UNION ALL
|
||||
SELECT 4, N'04' ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name])
|
||||
values (t2.id, t2.name);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou02>().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(@"MERGE INTO [tbiou02] t1
|
||||
USING (SELECT 1 as id, N'001' as name
|
||||
UNION ALL
|
||||
SELECT 2, N'002'
|
||||
UNION ALL
|
||||
SELECT 3, N'003'
|
||||
UNION ALL
|
||||
SELECT 4, N'004' ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name])
|
||||
values (t2.id, t2.name);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
||||
USING (SELECT 1 as id1, N'01' as id2, N'01' as name ) t2 ON (t1.[id1] = t2.id1 AND t1.[id2] = t2.id2)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id1], [id2], [name])
|
||||
values (t2.id1, t2.id2, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "02", name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
||||
USING (SELECT 1 as id1, N'02' as id2, N'011' as name ) t2 ON (t1.[id1] = t2.id1 AND t1.[id2] = t2.id2)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id1], [id2], [name])
|
||||
values (t2.id1, t2.id2, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
||||
USING (SELECT 2 as id1, N'02' as id2, N'02' as name ) t2 ON (t1.[id1] = t2.id1 AND t1.[id2] = t2.id2)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id1], [id2], [name])
|
||||
values (t2.id1, t2.id2, t2.name);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO [tbiou03] t1
|
||||
USING (SELECT 1 as id1, N'01' as id2, N'01' as name
|
||||
UNION ALL
|
||||
SELECT 2, N'02', N'02'
|
||||
UNION ALL
|
||||
SELECT 3, N'03', N'03'
|
||||
UNION ALL
|
||||
SELECT 4, N'04', N'04' ) t2 ON (t1.[id1] = t2.id1 AND t1.[id2] = t2.id2)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id1], [id2], [name])
|
||||
values (t2.id1, t2.id2, t2.name);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou03>().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(@"MERGE INTO [tbiou03] t1
|
||||
USING (SELECT 1 as id1, N'01' as id2, N'001' as name
|
||||
UNION ALL
|
||||
SELECT 2, N'02', N'002'
|
||||
UNION ALL
|
||||
SELECT 3, N'03', N'003'
|
||||
UNION ALL
|
||||
SELECT 4, N'04', N'004' ) t2 ON (t1.[id1] = t2.id1 AND t1.[id2] = t2.id2)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id1], [id2], [name])
|
||||
values (t2.id1, t2.id2, t2.name);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou03>().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());
|
||||
}
|
||||
class tbiou03
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public string id2 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
|
||||
{
|
||||
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou04] t1
|
||||
USING (SELECT 1 as id, N'01' as name, 0 as version, getdate() as CreateTime ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name, [version] = t1.[version] + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name], [version], [CreateTime])
|
||||
values (t2.id, t2.name, t2.version, t2.CreateTime);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou04] t1
|
||||
USING (SELECT 1 as id, N'011' as name, 0 as version, getdate() as CreateTime ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name, [version] = t1.[version] + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name], [version], [CreateTime])
|
||||
values (t2.id, t2.name, t2.version, t2.CreateTime);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"MERGE INTO [tbiou04] t1
|
||||
USING (SELECT 2 as id, N'02' as name, 0 as version, getdate() as CreateTime ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name, [version] = t1.[version] + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name], [version], [CreateTime])
|
||||
values (t2.id, t2.name, t2.version, t2.CreateTime);", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO [tbiou04] t1
|
||||
USING (SELECT 1 as id, N'01' as name, 0 as version, getdate() as CreateTime
|
||||
UNION ALL
|
||||
SELECT 2, N'02', 0, getdate()
|
||||
UNION ALL
|
||||
SELECT 3, N'03', 0, getdate()
|
||||
UNION ALL
|
||||
SELECT 4, N'04', 0, getdate() ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name, [version] = t1.[version] + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name], [version], [CreateTime])
|
||||
values (t2.id, t2.name, t2.version, t2.CreateTime);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou04>().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(@"MERGE INTO [tbiou04] t1
|
||||
USING (SELECT 1 as id, N'001' as name, 0 as version, getdate() as CreateTime
|
||||
UNION ALL
|
||||
SELECT 2, N'002', 0, getdate()
|
||||
UNION ALL
|
||||
SELECT 3, N'003', 0, getdate()
|
||||
UNION ALL
|
||||
SELECT 4, N'004', 0, getdate() ) t2 ON (t1.[id] = t2.id)
|
||||
WHEN MATCHED THEN
|
||||
update set [name] = t2.name, [version] = t1.[version] + 1
|
||||
WHEN NOT MATCHED THEN
|
||||
insert ([id], [name], [version], [CreateTime])
|
||||
values (t2.id, t2.name, t2.version, t2.CreateTime);", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
}
|
||||
class tbiou04
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
[Column(IsVersion = true)]
|
||||
public int version { get; set; }
|
||||
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user