mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 所有国产数据库支持 CustomMySql、CustomPostgreSQL、CustomOracle、CustomSqlServer 自定义适配;
This commit is contained in:
@ -0,0 +1,104 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.MySql
|
||||
{
|
||||
public class MySqlDeleteTest
|
||||
{
|
||||
|
||||
IDelete<Topic> delete => g.mysql.Delete<Topic>(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[Table(Name = "tb_topic_delete")]
|
||||
class Topic
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dywhere()
|
||||
{
|
||||
Assert.Null(g.mysql.Delete<Topic>().ToSql());
|
||||
var sql = g.mysql.Delete<Topic>(new[] { 1, 2 }).ToSql();
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` IN (1,2))", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ToSql();
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` IN (1,2))", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new { id = 1 }).ToSql();
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = g.mysql.Delete<MultiPkTopic>(new[] { new { Id1 = 1, Id2 = 10 }, new { Id1 = 2, Id2 = 20 } }).ToSql();
|
||||
Assert.Equal("DELETE FROM `MultiPkTopic` WHERE (`Id1` = 1 AND `Id2` = 10 OR `Id1` = 2 AND `Id2` = 20)", sql);
|
||||
}
|
||||
class MultiPkTopic
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int Id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public int Id2 { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Where()
|
||||
{
|
||||
var sql = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = delete.Where("id = @id", new { id = 1 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (id = @id)", sql);
|
||||
|
||||
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||
sql = delete.Where(item).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` = 1)", sql);
|
||||
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
sql = delete.Where(items).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("DELETE FROM `tb_topic_delete` WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteAffrows()
|
||||
{
|
||||
|
||||
var id = g.mysql.Insert<Topic>(new Topic { Title = "xxxx" }).ExecuteIdentity();
|
||||
Assert.Equal(1, delete.Where(a => a.Id == id).ExecuteAffrows());
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteDeleted()
|
||||
{
|
||||
|
||||
//delete.Where(a => a.Id > 0).ExecuteDeleted();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
Assert.Null(g.mysql.Delete<Topic>().ToSql());
|
||||
var sql = g.mysql.Delete<Topic>(new[] { 1, 2 }).AsTable(a => "TopicAsTable").ToSql();
|
||||
Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` IN (1,2))", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).AsTable(a => "TopicAsTable").ToSql();
|
||||
Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).AsTable(a => "TopicAsTable").ToSql();
|
||||
Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` IN (1,2))", sql);
|
||||
|
||||
sql = g.mysql.Delete<Topic>(new { id = 1 }).AsTable(a => "TopicAsTable").ToSql();
|
||||
Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1)", sql);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,453 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.MySql
|
||||
{
|
||||
public class MySqlInsertOrUpdateIfExistsDoNothingTest
|
||||
{
|
||||
|
||||
IFreeSql fsql => g.mysql;
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimary()
|
||||
{
|
||||
fsql.Delete<tbioudb02>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbioudb02>().IfExistsDoNothing().SetSource(new tbioudb02 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb02`(`id`, `name`) SELECT 1, '01'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb02>().IfExistsDoNothing().SetSource(new tbioudb02 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb02`(`id`, `name`) SELECT 1, '011'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb02>().IfExistsDoNothing().SetSource(new tbioudb02 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb02`(`id`, `name`) SELECT 2, '02'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb02>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '02'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '03'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '04'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb02>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '002'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '003'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '004'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb02` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbioudb02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count());
|
||||
}
|
||||
class tbioudb02
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
[Fact]
|
||||
public void InsertOrUpdate_OnePrimaryAndIdentity()
|
||||
{
|
||||
fsql.Delete<tbioudb022>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`id`, `name`) SELECT 1, '01'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`id`, `name`) SELECT 1, '011'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`id`, `name`) SELECT 2, '02'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '02'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '03'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '04'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '002'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '003'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '004'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbioudb022>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count());
|
||||
|
||||
//--no primary
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { name = "01" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`name`) VALUES('01')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`name`) VALUES('011')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new tbioudb022 { name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`name`) VALUES('02')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new[] { new tbioudb022 { name = "01" }, new tbioudb022 { name = "02" }, new tbioudb022 { name = "03" }, new tbioudb022 { name = "04" } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`name`) VALUES('01'), ('02'), ('03'), ('04')", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().IfExistsDoNothing().SetSource(new[] { new tbioudb022 { name = "001" }, new tbioudb022 { name = "002" }, new tbioudb022 { name = "003" }, new tbioudb022 { name = "004" } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb022`(`name`) VALUES('001'), ('002'), ('003'), ('004')", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
//--no primary and yes
|
||||
iou = fsql.InsertOrUpdate<tbioudb022>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '100002'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '100003'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '100004'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb022` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)
|
||||
|
||||
;
|
||||
|
||||
INSERT INTO `tbioudb022`(`name`) VALUES('00001'), ('00002'), ('00003'), ('00004')", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
lst = fsql.Select<tbioudb022>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count());
|
||||
}
|
||||
class tbioudb022
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertOrUpdate_TwoPrimary()
|
||||
{
|
||||
fsql.Delete<tbioudb03>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbioudb03>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb03` a
|
||||
WHERE (a.`id1` = 1 AND a.`id2` = '01')
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb03>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb03` a
|
||||
WHERE (a.`id1` = 1 AND a.`id2` = '01')
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb03>().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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb03` a
|
||||
WHERE (a.`id1` = 2 AND a.`id2` = '02')
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb03>().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'
|
||||
FROM dual 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'
|
||||
FROM dual 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'
|
||||
FROM dual 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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb03` a
|
||||
WHERE (a.`id1` = 4 AND a.`id2` = '04')
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb03>().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'
|
||||
FROM dual 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'
|
||||
FROM dual 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'
|
||||
FROM dual 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'
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb03` a
|
||||
WHERE (a.`id1` = 4 AND a.`id2` = '04')
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbioudb03>().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());
|
||||
}
|
||||
class tbioudb03
|
||||
{
|
||||
[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<tbioudb04>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbioudb04>().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, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb04>().IfExistsDoNothing().SetSource(new tbioudb04 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb04`(`id`, `name`, `version`, `CreateTime`) SELECT 1, '011', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb04>().IfExistsDoNothing().SetSource(new tbioudb04 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbioudb04`(`id`, `name`, `version`, `CreateTime`) SELECT 2, '02', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb04>().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, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '02', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '03', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '04', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbioudb04>().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, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 1)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 2, '002', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 2)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 3, '003', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 3)
|
||||
limit 0,1)
|
||||
UNION ALL
|
||||
SELECT 4, '004', 0, now(3)
|
||||
FROM dual WHERE NOT EXISTS(SELECT 1
|
||||
FROM `tbioudb04` a
|
||||
WHERE (a.`id` = 4)
|
||||
limit 0,1)", sql);
|
||||
Assert.Equal(0, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbioudb04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "0" + a.id).Count());
|
||||
}
|
||||
class tbioudb04
|
||||
{
|
||||
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,254 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.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_OnePrimaryAndIdentity()
|
||||
{
|
||||
fsql.Delete<tbiou022>().Where("1=1").ExecuteAffrows();
|
||||
var iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 1, name = "01" });
|
||||
var sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(1, '01')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 1, name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(1, '011')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
Assert.Equal(2, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { id = 2, name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(2, '02')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().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(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().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(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)", sql);
|
||||
Assert.Equal(8, iou.ExecuteAffrows());
|
||||
var lst = fsql.Select<tbiou022>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
|
||||
|
||||
//--no primary
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { name = "01" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`name`) VALUES('01')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { name = "011" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`name`) VALUES('011')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new tbiou022 { name = "02" });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`name`) VALUES('02')", sql);
|
||||
Assert.Equal(1, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new[] { new tbiou022 { name = "01" }, new tbiou022 { name = "02" }, new tbiou022 { name = "03" }, new tbiou022 { name = "04" } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`name`) VALUES('01'), ('02'), ('03'), ('04')", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().SetSource(new[] { new tbiou022 { name = "001" }, new tbiou022 { name = "002" }, new tbiou022 { name = "003" }, new tbiou022 { name = "004" } });
|
||||
sql = iou.ToSql();
|
||||
Assert.Equal(@"INSERT INTO `tbiou022`(`name`) VALUES('001'), ('002'), ('003'), ('004')", sql);
|
||||
Assert.Equal(4, iou.ExecuteAffrows());
|
||||
|
||||
//--no primary and yes
|
||||
iou = fsql.InsertOrUpdate<tbiou022>().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(@"INSERT INTO `tbiou022`(`id`, `name`) VALUES(1, '100001'), (2, '100002'), (3, '100003'), (4, '100004')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`name` = VALUES(`name`)
|
||||
|
||||
;
|
||||
|
||||
INSERT INTO `tbiou022`(`name`) VALUES('00001'), ('00002'), ('00003'), ('00004')", sql);
|
||||
Assert.Equal(12, iou.ExecuteAffrows());
|
||||
lst = fsql.Select<tbiou022>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
|
||||
Assert.Equal(4, lst.Where(a => a.name == "10000" + a.id).Count());
|
||||
}
|
||||
class tbiou022
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
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,162 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.MySql
|
||||
{
|
||||
public class MySqlInsertTest
|
||||
{
|
||||
|
||||
IInsert<Topic> insert => g.mysql.Insert<Topic>(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[Table(Name = "tb_topic_insert")]
|
||||
class Topic
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
class TestEnumInsertTb
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int id { get; set; }
|
||||
public TestEnumInserTbType type { get; set; }
|
||||
public DateTime time { get; set; } = new DateTime();
|
||||
}
|
||||
enum TestEnumInserTbType { str1, biggit, sum211 }
|
||||
|
||||
[Fact]
|
||||
public void AppendData()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items.First()).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`, `Title`, `CreateTime`) VALUES(0, 'newtitle0', '0001-01-01 00:00:00.000')", sql);
|
||||
|
||||
sql = insert.AppendData(items).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`, `Title`, `CreateTime`) VALUES(0, 'newtitle0', '0001-01-01 00:00:00.000'), (100, 'newtitle1', '0001-01-01 00:00:00.000'), (200, 'newtitle2', '0001-01-01 00:00:00.000'), (300, 'newtitle3', '0001-01-01 00:00:00.000'), (400, 'newtitle4', '0001-01-01 00:00:00.000'), (500, 'newtitle5', '0001-01-01 00:00:00.000'), (600, 'newtitle6', '0001-01-01 00:00:00.000'), (700, 'newtitle7', '0001-01-01 00:00:00.000'), (800, 'newtitle8', '0001-01-01 00:00:00.000'), (900, 'newtitle9', '0001-01-01 00:00:00.000')", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Title`) VALUES('newtitle0'), ('newtitle1'), ('newtitle2'), ('newtitle3'), ('newtitle4'), ('newtitle5'), ('newtitle6'), ('newtitle7'), ('newtitle8'), ('newtitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`, `Title`) VALUES(0, 'newtitle0'), (100, 'newtitle1'), (200, 'newtitle2'), (300, 'newtitle3'), (400, 'newtitle4'), (500, 'newtitle5'), (600, 'newtitle6'), (700, 'newtitle7'), (800, 'newtitle8'), (900, 'newtitle9')", sql);
|
||||
|
||||
sql = g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211, time = DateTime.Parse("2019-09-19 21:26:51.030") }).ToSql();
|
||||
Assert.Equal("INSERT INTO `TestEnumInsertTb`(`type`, `time`) VALUES('sum211', '2019-09-19 21:26:51.030')", sql);
|
||||
|
||||
sql = g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ToSql();
|
||||
Assert.Equal("INSERT INTO `TestEnumInsertTb`(`type`, `time`) VALUES('sum211', '0001-01-01 00:00:00.000')", sql);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertColumns()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Title`) VALUES('newtitle0'), ('newtitle1'), ('newtitle2'), ('newtitle3'), ('newtitle4'), ('newtitle5'), ('newtitle6'), ('newtitle7'), ('newtitle8'), ('newtitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => new { a.Title, a.Clicks }).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`, `Title`) VALUES(0, 'newtitle0'), (100, 'newtitle1'), (200, 'newtitle2'), (300, 'newtitle3'), (400, 'newtitle4'), (500, 'newtitle5'), (600, 'newtitle6'), (700, 'newtitle7'), (800, 'newtitle8'), (900, 'newtitle9')", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void IgnoreColumns()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`, `Title`) VALUES(0, 'newtitle0'), (100, 'newtitle1'), (200, 'newtitle2'), (300, 'newtitle3'), (400, 'newtitle4'), (500, 'newtitle5'), (600, 'newtitle6'), (700, 'newtitle7'), (800, 'newtitle8'), (900, 'newtitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
|
||||
Assert.Equal("INSERT INTO `tb_topic_insert`(`Clicks`) VALUES(0), (100), (200), (300), (400), (500), (600), (700), (800), (900)", sql);
|
||||
|
||||
g.mysql.Delete<TopicIgnore>().Where("1=1").ExecuteAffrows();
|
||||
var itemsIgnore = new List<TopicIgnore>();
|
||||
for (var a = 0; a < 2072; a++) itemsIgnore.Add(new TopicIgnore { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||
g.mysql.Insert<TopicIgnore>().AppendData(itemsIgnore).IgnoreColumns(a => new { a.Title }).ExecuteAffrows();
|
||||
Assert.Equal(2072, itemsIgnore.Count);
|
||||
Assert.Equal(2072, g.mysql.Select<TopicIgnore>().Where(a => a.Title == null).Count());
|
||||
}
|
||||
[Table(Name = "tb_topic_insertIgnoreColumns")]
|
||||
class TopicIgnore
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteAffrows()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||||
|
||||
Assert.Equal(1, insert.AppendData(items.First()).ExecuteAffrows());
|
||||
Assert.Equal(10, insert.AppendData(items).ExecuteAffrows());
|
||||
|
||||
Assert.Equal(1, g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteAffrows());
|
||||
Assert.Equal(1, g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteAffrows());
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteIdentity()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
Assert.NotEqual(0, insert.AppendData(items.First()).ExecuteIdentity());
|
||||
|
||||
var id = g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteIdentity();
|
||||
Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select<TestEnumInsertTb>().Where(a => a.id == id).First()?.type);
|
||||
id = g.mysql.Insert<TestEnumInsertTb>().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteIdentity();
|
||||
Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select<TestEnumInsertTb>().Where(a => a.id == id).First()?.type);
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteInserted()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
//insert.AppendData(items.First()).ExecuteInserted();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newTitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items.First()).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`, `CreateTime`) VALUES(0, 'newTitle0', '0001-01-01 00:00:00.000')", sql);
|
||||
|
||||
sql = insert.AppendData(items).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`, `CreateTime`) VALUES(0, 'newTitle0', '0001-01-01 00:00:00.000'), (100, 'newTitle1', '0001-01-01 00:00:00.000'), (200, 'newTitle2', '0001-01-01 00:00:00.000'), (300, 'newTitle3', '0001-01-01 00:00:00.000'), (400, 'newTitle4', '0001-01-01 00:00:00.000'), (500, 'newTitle5', '0001-01-01 00:00:00.000'), (600, 'newTitle6', '0001-01-01 00:00:00.000'), (700, 'newTitle7', '0001-01-01 00:00:00.000'), (800, 'newTitle8', '0001-01-01 00:00:00.000'), (900, 'newTitle9', '0001-01-01 00:00:00.000')", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => a.Title).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Title`) VALUES('newTitle0'), ('newTitle1'), ('newTitle2'), ('newTitle3'), ('newTitle4'), ('newTitle5'), ('newTitle6'), ('newTitle7'), ('newTitle8'), ('newTitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(0, 'newTitle0'), (100, 'newTitle1'), (200, 'newTitle2'), (300, 'newTitle3'), (400, 'newTitle4'), (500, 'newTitle5'), (600, 'newTitle6'), (700, 'newTitle7'), (800, 'newTitle8'), (900, 'newTitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => a.Title).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Title`) VALUES('newTitle0'), ('newTitle1'), ('newTitle2'), ('newTitle3'), ('newTitle4'), ('newTitle5'), ('newTitle6'), ('newTitle7'), ('newTitle8'), ('newTitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => new { a.Title, a.Clicks }).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(0, 'newTitle0'), (100, 'newTitle1'), (200, 'newTitle2'), (300, 'newTitle3'), (400, 'newTitle4'), (500, 'newTitle5'), (600, 'newTitle6'), (700, 'newTitle7'), (800, 'newTitle8'), (900, 'newTitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(0, 'newTitle0'), (100, 'newTitle1'), (200, 'newTitle2'), (300, 'newTitle3'), (400, 'newTitle4'), (500, 'newTitle5'), (600, 'newTitle6'), (700, 'newTitle7'), (800, 'newTitle8'), (900, 'newTitle9')", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).AsTable(a => "Topic_InsertAsTable").ToSql();
|
||||
Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`) VALUES(0), (100), (200), (300), (400), (500), (600), (700), (800), (900)", sql);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,317 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.MySql
|
||||
{
|
||||
public class MySqlUpdateTest
|
||||
{
|
||||
IUpdate<Topic> update => g.mysql.Update<Topic>();
|
||||
|
||||
[Table(Name = "tb_topic")]
|
||||
class Topic
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public int Id { get; set; }
|
||||
public int? Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
class TestEnumUpdateTb
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int id { get; set; }
|
||||
public TestEnumUpdateTbType type { get; set; }
|
||||
public DateTime time { get; set; } = new DateTime();
|
||||
}
|
||||
enum TestEnumUpdateTbType { str1, biggit, sum211 }
|
||||
|
||||
[Fact]
|
||||
public void Dywhere()
|
||||
{
|
||||
Assert.Null(g.mysql.Update<Topic>().ToSql());
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='test' \r\nWHERE (`Id` IN (1,2))", g.mysql.Update<Topic>(new[] { 1, 2 }).SetRaw("title='test'").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update<Topic>(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` IN (1,2))", g.mysql.Update<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update<Topic>(new { id = 1 }).SetRaw("title='test1'").ToSql());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetSource()
|
||||
{
|
||||
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = NULL, `Title` = 'newtitle', `CreateTime` = '0001-01-01 00:00:00.000' WHERE (`Id` = 1)", sql);
|
||||
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
items[0].Clicks = null;
|
||||
|
||||
sql = update.SetSource(items).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN NULL WHEN 2 THEN 100 WHEN 3 THEN 200 WHEN 4 THEN 300 WHEN 5 THEN 400 WHEN 6 THEN 500 WHEN 7 THEN 600 WHEN 8 THEN 700 WHEN 9 THEN 800 WHEN 10 THEN 900 END, `Title` = CASE `Id` WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END, `CreateTime` = CASE `Id` WHEN 1 THEN '0001-01-01 00:00:00.000' WHEN 2 THEN '0001-01-01 00:00:00.000' WHEN 3 THEN '0001-01-01 00:00:00.000' WHEN 4 THEN '0001-01-01 00:00:00.000' WHEN 5 THEN '0001-01-01 00:00:00.000' WHEN 6 THEN '0001-01-01 00:00:00.000' WHEN 7 THEN '0001-01-01 00:00:00.000' WHEN 8 THEN '0001-01-01 00:00:00.000' WHEN 9 THEN '0001-01-01 00:00:00.000' WHEN 10 THEN '0001-01-01 00:00:00.000' END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||
|
||||
sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN 'newtitle0' WHEN 2 THEN 'newtitle1' WHEN 3 THEN 'newtitle2' WHEN 4 THEN 'newtitle3' WHEN 5 THEN 'newtitle4' WHEN 6 THEN 'newtitle5' WHEN 7 THEN 'newtitle6' WHEN 8 THEN 'newtitle7' WHEN 9 THEN 'newtitle8' WHEN 10 THEN 'newtitle9' END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||
|
||||
sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `CreateTime` = '2020-01-01 00:00:00.000' WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||
|
||||
sql = g.mysql.Insert<TestEnumUpdateTb>().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("INSERT INTO `TestEnumUpdateTb`(`type`, `time`) VALUES('sum211', '0001-01-01 00:00:00.000')", sql);
|
||||
var id = g.mysql.Insert<TestEnumUpdateTb>().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
|
||||
Assert.True(id > 0);
|
||||
Assert.Equal(TestEnumUpdateTbType.sum211, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211', `time` = '0001-01-01 00:00:00.000' WHERE (`id` = 0)", sql);
|
||||
g.mysql.Update<TestEnumUpdateTb>().SetSource(new TestEnumUpdateTb { id = (int)id, type = TestEnumUpdateTbType.biggit }).ExecuteAffrows();
|
||||
Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
|
||||
sql = g.mysql.Insert<TestEnumUpdateTb>().NoneParameter().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("INSERT INTO `TestEnumUpdateTb`(`type`, `time`) VALUES('sum211', '0001-01-01 00:00:00.000')", sql);
|
||||
id = g.mysql.Insert<TestEnumUpdateTb>().NoneParameter().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
|
||||
Assert.True(id > 0);
|
||||
Assert.Equal(TestEnumUpdateTbType.sum211, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211', `time` = '0001-01-01 00:00:00.000' WHERE (`id` = 0)", sql);
|
||||
g.mysql.Update<TestEnumUpdateTb>().NoneParameter().SetSource(new TestEnumUpdateTb { id = (int)id, type = TestEnumUpdateTbType.biggit }).ExecuteAffrows();
|
||||
Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
|
||||
sql = g.mysql.Update<ts_source_mpk>().SetSource(new[] {
|
||||
new ts_source_mpk { id1 = 1, id2 = 7, xx = "a1" },
|
||||
new ts_source_mpk { id1 = 1, id2 = 8, xx = "b122" }
|
||||
}).NoneParameter().ToSql().Replace("\r\n", "");
|
||||
}
|
||||
public class ts_source_mpk
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public int id1 { get; set; }
|
||||
[Column(IsPrimary = true)]
|
||||
public int id2 { get; set; }
|
||||
public string xx { get; set; }
|
||||
}
|
||||
[Fact]
|
||||
public void SetSourceIgnore()
|
||||
{
|
||||
Assert.Equal("UPDATE `tssi01` SET `tint` = 10 WHERE (`id` = '00000000-0000-0000-0000-000000000000')",
|
||||
g.mysql.Update<tssi01>().NoneParameter()
|
||||
.SetSourceIgnore(new tssi01 { id = Guid.Empty, tint = 10 }, col => col == null).ToSql().Replace("\r\n", ""));
|
||||
}
|
||||
public class tssi01
|
||||
{
|
||||
[Column(CanUpdate = false)]
|
||||
public Guid id { get; set; }
|
||||
public int tint { get; set; }
|
||||
public string title { get; set; }
|
||||
}
|
||||
[Fact]
|
||||
public void IgnoreColumns()
|
||||
{
|
||||
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Title` = 'newtitle' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).IgnoreColumns(a => a.time).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).IgnoreColumns(a => a.time).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void UpdateColumns()
|
||||
{
|
||||
var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Title` = 'newtitle' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void Set()
|
||||
{
|
||||
var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Title` = 'newtitle' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Title` = 'newtitle', `CreateTime` = '2020-01-01 00:00:00.000' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`, 0) * 10 div 1 WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Id - 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Id` = (`Id` - 10) WHERE (`Id` = 1)", sql);
|
||||
|
||||
int incrv = 10;
|
||||
sql = update.Set(a => a.Clicks * incrv / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`, 0) * 10 div 1 WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Id - incrv).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Id` = (`Id` - 10) WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Clicks == a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = `Clicks` * 10 div 1 WHERE (`Id` = 1)", sql);
|
||||
|
||||
var dt2000 = DateTime.Parse("2000-01-01");
|
||||
sql = update.Set(a => a.Clicks == (a.CreateTime > dt2000 ? 1 : 2)).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = case when `CreateTime` > '2000-01-01 00:00:00.000' then 1 else 2 end WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Id == 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Id` = 10 WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Set(a => a.Clicks == null).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = NULL WHERE (`Id` = 1)", sql);
|
||||
|
||||
var id = g.mysql.Insert<TestEnumUpdateTb>().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
|
||||
Assert.True(id > 0);
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.biggit).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal($"UPDATE `TestEnumUpdateTb` SET `type` = 'biggit' WHERE (`id` = {id})", sql);
|
||||
g.mysql.Update<TestEnumUpdateTb>().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.biggit).ExecuteAffrows();
|
||||
Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.str1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal($"UPDATE `TestEnumUpdateTb` SET `type` = 'str1' WHERE (`id` = {id})", sql);
|
||||
g.mysql.Update<TestEnumUpdateTb>().NoneParameter().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.str1).ExecuteAffrows();
|
||||
Assert.Equal(TestEnumUpdateTbType.str1, g.mysql.Select<TestEnumUpdateTb>().Where(a => a.id == id).First()?.type);
|
||||
}
|
||||
public class tenumcls
|
||||
{
|
||||
public Guid id { get; set; }
|
||||
public tenum status { get; set; }
|
||||
}
|
||||
public enum tenum
|
||||
{
|
||||
WaitPay = 1,
|
||||
Pay = 3,
|
||||
Finsh = 8,
|
||||
Cacel = 16,
|
||||
Refunding = 32
|
||||
}
|
||||
[Fact]
|
||||
public void SetEnum()
|
||||
{
|
||||
var fsql = g.mysql;
|
||||
//#184
|
||||
fsql.Delete<tenumcls>(Guid.Parse("5e83a910-672f-847c-00c1-316b71d153fb")).ExecuteAffrows();
|
||||
var item = new tenumcls { id = Guid.Parse("5e83a910-672f-847c-00c1-316b71d153fb"), status = tenum.Finsh };
|
||||
Assert.Equal("INSERT INTO `tenumcls`(`id`, `status`) VALUES('5e83a910-672f-847c-00c1-316b71d153fb', 'Finsh')",
|
||||
fsql.Insert<tenumcls>().NoneParameter().AppendData(item).ToSql());
|
||||
Assert.Equal(1, fsql.Insert<tenumcls>().NoneParameter().AppendData(item).ExecuteAffrows());
|
||||
var item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Finsh, item2.status);
|
||||
|
||||
Assert.Equal(@"UPDATE `tenumcls` SET `status` = case when `id` = '5e83a910-672f-847c-00c1-316b71d153fb' then 'Pay' else 'Refunding' end
|
||||
WHERE (`id` = '5e83a910-672f-847c-00c1-316b71d153fb')",
|
||||
fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == (a.id == item.id ? tenum.Pay : tenum.Refunding)).ToSql());
|
||||
Assert.Equal(1, fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == (a.id == item.id ? tenum.Pay : tenum.Refunding)).ExecuteAffrows());
|
||||
item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Pay, item2.status);
|
||||
|
||||
Assert.Equal(@"UPDATE `tenumcls` SET `status` = 'Finsh'
|
||||
WHERE (`id` = '5e83a910-672f-847c-00c1-316b71d153fb')",
|
||||
fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == tenum.Finsh).ToSql());
|
||||
Assert.Equal(1, fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == tenum.Finsh).ExecuteAffrows());
|
||||
item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Finsh, item2.status);
|
||||
|
||||
Assert.Equal(@"UPDATE `tenumcls` SET `status` = 'Pay'
|
||||
WHERE (`id` = '5e83a910-672f-847c-00c1-316b71d153fb')",
|
||||
fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == tenum.Pay).ToSql());
|
||||
Assert.Equal(1, fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status == tenum.Pay).ExecuteAffrows());
|
||||
item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Pay, item2.status);
|
||||
|
||||
Assert.Equal(@"UPDATE `tenumcls` SET `status` = 'Finsh'
|
||||
WHERE (`id` = '5e83a910-672f-847c-00c1-316b71d153fb')",
|
||||
fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status, tenum.Finsh).ToSql());
|
||||
Assert.Equal(1, fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status, tenum.Finsh).ExecuteAffrows());
|
||||
item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Finsh, item2.status);
|
||||
|
||||
Assert.Equal(@"UPDATE `tenumcls` SET `status` = 'Pay'
|
||||
WHERE (`id` = '5e83a910-672f-847c-00c1-316b71d153fb')",
|
||||
fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status, tenum.Pay).ToSql());
|
||||
Assert.Equal(1, fsql.Update<tenumcls>(item).NoneParameter().Set(a => a.status, tenum.Pay).ExecuteAffrows());
|
||||
|
||||
Assert.Equal(@"SELECT a.`id`, a.`status`
|
||||
FROM `tenumcls` a
|
||||
WHERE (a.`id` = '5e83a910-672f-847c-00c1-316b71d153fb' AND a.`status` = case when a.`id` = '5e83a910-672f-847c-00c1-316b71d153fb' then 'Pay' else 'Refunding' end)
|
||||
limit 0,1", fsql.Select<tenumcls>().Where(a => a.id == item.id && a.status == (a.id == item.id ? tenum.Pay : tenum.Refunding)).Limit(1).ToSql());
|
||||
item2 = fsql.Select<tenumcls>().Where(a => a.id == item.id && a.status == (a.id == item.id ? tenum.Pay : tenum.Refunding)).First();
|
||||
Assert.Equal(item.id, item2.id);
|
||||
Assert.Equal(tenum.Pay, item2.status);
|
||||
}
|
||||
[Fact]
|
||||
public void SetRaw()
|
||||
{
|
||||
var sql = update.Where(a => a.Id == 1).SetRaw("clicks = clicks + ?", new { incrClick = 1 }).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET clicks = clicks + ? WHERE (`Id` = 1)", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void SetDto()
|
||||
{
|
||||
var sql = update.SetDto(new { clicks = 1, title = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = 1, `Title` = 'xxx' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.SetDto(new Dictionary<string, object> { ["clicks"] = 1, ["title"] = "xxx" }).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET `Clicks` = 1, `Title` = 'xxx' WHERE (`Id` = 1)", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void Where()
|
||||
{
|
||||
var sql = update.Where(a => a.Id == 1).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` = 1)", sql);
|
||||
|
||||
sql = update.Where("id = ?", new { id = 1 }).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (id = ?)", sql);
|
||||
|
||||
var item = new Topic { Id = 1, Title = "newtitle" };
|
||||
sql = update.Where(item).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` = 1)", sql);
|
||||
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
sql = update.Where(items).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
|
||||
|
||||
sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().Where(a => a.id == 0 && a.type == TestEnumUpdateTbType.str1)
|
||||
.Set(a => a.type, TestEnumUpdateTbType.sum211).ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0 AND `type` = 'str1')", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteAffrows()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var time = DateTime.Now;
|
||||
var items222 = g.mysql.Select<Topic>().Where(a => a.CreateTime > time).Limit(10).ToList();
|
||||
|
||||
update.SetSource(items.First()).NoneParameter().ExecuteAffrows();
|
||||
update.SetSource(items).NoneParameter().ExecuteAffrows();
|
||||
}
|
||||
[Fact]
|
||||
public void ExecuteUpdated()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
Assert.Null(g.mysql.Update<Topic>().ToSql());
|
||||
Assert.Equal("UPDATE `tb_topicAsTable` SET title='test' \r\nWHERE (`Id` IN (1,2))", g.mysql.Update<Topic>(new[] { 1, 2 }).SetRaw("title='test'").AsTable(a => "tb_topicAsTable").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update<Topic>(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` IN (1,2))", g.mysql.Update<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
|
||||
Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update<Topic>(new { id = 1 }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,195 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Custom.MySql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.Custom.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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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 CustomMySqlOnDuplicateKeyUpdate<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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user