- 增加 MySql 特有功能 On Duplicate Key Update 功能;

This commit is contained in:
28810 2019-11-11 22:08:21 +08:00
parent f2cb3bd5fe
commit be77060ea8
11 changed files with 619 additions and 7 deletions

View File

@ -99,6 +99,13 @@
清空状态数据 清空状态数据
</summary> </summary>
</member> </member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)"> <member name="M:FreeSql.DbSet`1.Add(`0)">
<summary> <summary>
添加 添加

View File

@ -0,0 +1,201 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySqlConnector
{
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 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"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`)");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void IgnoreColumns()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
ON DUPLICATE KEY UPDATE
`title` = VALUES(`title`),
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnDuplicateKeyUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
ON DUPLICATE KEY UPDATE
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void UpdateColumns()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
ON DUPLICATE KEY UPDATE
`title` = VALUES(`title`),
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnDuplicateKeyUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
ON DUPLICATE KEY UPDATE
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void Set()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku1.ToSql(), @"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'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
var dt2020 = DateTime.Parse("2020-1-1");
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => a.time == dt2020);
Assert.Equal(odku1.ToSql(), @"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'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().Set(a => a.time == dt2020);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
Assert.Equal(odku1.ToSql(), @"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')");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void SetRaw()
{
}
}
}

View File

@ -0,0 +1,4 @@
[*.cs]
# xUnit2000: Constants and literals should be the expected argument
dotnet_diagnostic.xUnit2000.severity = none

View File

@ -0,0 +1,201 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.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 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"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`)");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void IgnoreColumns()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
ON DUPLICATE KEY UPDATE
`title` = VALUES(`title`),
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnDuplicateKeyUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
ON DUPLICATE KEY UPDATE
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void UpdateColumns()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnDuplicateKeyUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
ON DUPLICATE KEY UPDATE
`title` = VALUES(`title`),
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate();
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnDuplicateKeyUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku1.ToSql(), @"INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(300, 'title-300')
ON DUPLICATE KEY UPDATE
`time` = '2000-01-01 00:00:00.000'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void Set()
{
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
var odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku1.ToSql(), @"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'");
Assert.Equal(1, odku1.ExecuteAffrows());
var odku2 = 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().OnDuplicateKeyUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
var dt2020 = DateTime.Parse("2020-1-1");
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => a.time == dt2020);
Assert.Equal(odku1.ToSql(), @"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'");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().Set(a => a.time == dt2020);
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
g.mysql.Delete<TestOnDuplicateKeyUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
odku1 = g.mysql.Insert(new TestOnDuplicateKeyUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnDuplicateKeyUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
Assert.Equal(odku1.ToSql(), @"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')");
Assert.Equal(1, odku1.ExecuteAffrows());
odku2 = 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().OnDuplicateKeyUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
Assert.Equal(odku2.ToSql(), @"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.ExecuteAffrows();
}
[Fact]
public void SetRaw()
{
}
}
}

View File

@ -419,8 +419,8 @@ namespace FreeSql.Internal.CommonProvider
var colidx = 0; var colidx = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx > 0) sb.Append(", "); if (colidx > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)); sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
@ -437,8 +437,8 @@ namespace FreeSql.Internal.CommonProvider
var colidx2 = 0; var colidx2 = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx2 > 0) sb.Append(", "); if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d); object val = col.GetMapValue(d);

View File

@ -1,7 +1,9 @@
using FreeSql.Internal; using FreeSql.Internal;
using FreeSql.Internal.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -15,6 +17,17 @@ namespace FreeSql.MySql.Curd
{ {
} }
internal IFreeSql InternalOrm => _orm;
internal TableInfo InternalTable => _table;
internal DbParameter[] InternalParams => _params;
internal DbConnection InternalConnection => _connection;
internal DbTransaction InternalTransaction => _transaction;
internal CommonUtils InternalCommonUtils => _commonUtils;
internal CommonExpression InternalCommonExpression => _commonExpression;
internal List<T1> InternalSource => _source;
internal Dictionary<string, bool> InternalIgnore => _ignore;
internal void InternalClearData() => ClearData();
public override int ExecuteAffrows() => base.SplitExecuteAffrows(5000, 3000); public override int ExecuteAffrows() => base.SplitExecuteAffrows(5000, 3000);
public override long ExecuteIdentity() => base.SplitExecuteIdentity(5000, 3000); public override long ExecuteIdentity() => base.SplitExecuteIdentity(5000, 3000);
public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(5000, 3000); public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(5000, 3000);

View File

@ -18,6 +18,12 @@ namespace FreeSql.MySql.Curd
{ {
} }
internal StringBuilder InternalSbSet => _set;
internal StringBuilder InternalSbSetIncr => _setIncr;
internal Dictionary<string, bool> InternalIgnore => _ignore;
internal void InternalResetSource(List<T1> source) => _source = source;
internal string InternalWhereCaseSource(string CsName, Func<string, string> thenValue) => WhereCaseSource(CsName, thenValue);
public override int ExecuteAffrows() => base.SplitExecuteAffrows(500, 3000); public override int ExecuteAffrows() => base.SplitExecuteAffrows(500, 3000);
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(500, 3000); public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(500, 3000);

View File

@ -0,0 +1,167 @@
using FreeSql.Aop;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace FreeSql.MySql.Curd
{
public class OnDuplicateKeyUpdate<T1> where T1 : class
{
internal MySqlInsert<T1> _mysqlInsert;
internal MySqlUpdate<T1> _mysqlUpdatePriv;
internal MySqlUpdate<T1> _mysqlUpdate => _mysqlUpdatePriv ?? (_mysqlUpdatePriv = new MySqlUpdate<T1>(_mysqlInsert.InternalOrm, _mysqlInsert.InternalCommonUtils, _mysqlInsert.InternalCommonExpression, null).NoneParameter().SetSource(_mysqlInsert.InternalSource) as MySqlUpdate<T1>);
public OnDuplicateKeyUpdate(IInsert<T1> insert)
{
_mysqlInsert = insert as MySqlInsert<T1>;
if (_mysqlInsert == null) throw new Exception("OnDuplicateKeyUpdate 是 FreeSql.Provider.MySql/FreeSql.Provider.MySqlConnector 特有的功能");
}
protected void ClearData()
{
_mysqlInsert.InternalClearData();
_mysqlUpdatePriv = null;
}
public OnDuplicateKeyUpdate<T1> IgnoreColumns(Expression<Func<T1, object>> columns)
{
_mysqlUpdate.IgnoreColumns(columns);
return this;
}
public OnDuplicateKeyUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns)
{
_mysqlUpdate.UpdateColumns(columns);
return this;
}
public OnDuplicateKeyUpdate<T1> IgnoreColumns(string[] columns)
{
_mysqlUpdate.IgnoreColumns(columns);
return this;
}
public OnDuplicateKeyUpdate<T1> UpdateColumns(string[] columns)
{
_mysqlUpdate.UpdateColumns(columns);
return this;
}
public OnDuplicateKeyUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value)
{
_mysqlUpdate.Set(column, value);
return this;
}
public OnDuplicateKeyUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp)
{
_mysqlUpdate.Set(exp);
return this;
}
public OnDuplicateKeyUpdate<T1> SetRaw(string sql)
{
_mysqlUpdate.SetRaw(sql);
return this;
}
public string ToSql()
{
var sb = new StringBuilder();
sb.Append(_mysqlInsert.ToSql()).Append("\r\nON DUPLICATE KEY UPDATE\r\n");
var sbSetEmpty = _mysqlUpdate.InternalSbSet.Length == 0;
var sbSetIncrEmpty = _mysqlUpdate.InternalSbSetIncr.Length == 0;
if (sbSetEmpty == false || sbSetIncrEmpty == false)
{
if (sbSetEmpty == false) sb.Append(_mysqlUpdate.InternalSbSet.ToString().Substring(2));
if (sbSetIncrEmpty == false) sb.Append(sbSetEmpty ? _mysqlUpdate.InternalSbSetIncr.ToString().Substring(2) : _mysqlUpdate.InternalSbSetIncr.ToString());
}
else
{
var colidx = 0;
foreach (var col in _mysqlInsert.InternalTable.Columns.Values)
{
if (col.Attribute.IsPrimary || _mysqlUpdate.InternalIgnore.ContainsKey(col.Attribute.Name)) continue;
if (colidx > 0) sb.Append(", \r\n");
if (col.Attribute.IsVersion == true)
{
var field = _mysqlInsert.InternalCommonUtils.QuoteSqlName(col.Attribute.Name);
sb.Append(field).Append(" = ").Append(field).Append(" + 1");
}
else if (_mysqlInsert.InternalIgnore.ContainsKey(col.Attribute.Name))
sb.Append(_mysqlUpdate.InternalWhereCaseSource(col.CsName, sqlval => sqlval).Trim());
else
{
var field = _mysqlInsert.InternalCommonUtils.QuoteSqlName(col.Attribute.Name);
sb.Append(field).Append(" = VALUES(").Append(field).Append(")");
}
++colidx;
}
}
return sb.ToString();
}
public long ExecuteAffrows()
{
var sql = this.ToSql();
if (string.IsNullOrEmpty(sql)) return 0;
var before = new CurdBeforeEventArgs(_mysqlInsert.InternalTable.Type, _mysqlInsert.InternalTable, CurdType.Insert, sql, _mysqlInsert.InternalParams);
_mysqlInsert.InternalOrm.Aop.CurdBefore?.Invoke(_mysqlInsert, before);
long ret = 0;
Exception exception = null;
try
{
ret = _mysqlInsert.InternalOrm.Ado.ExecuteNonQuery(_mysqlInsert.InternalConnection, _mysqlInsert.InternalTransaction, CommandType.Text, sql, _mysqlInsert.InternalParams);
}
catch (Exception ex)
{
exception = ex;
throw ex;
}
finally
{
var after = new CurdAfterEventArgs(before, exception, ret);
_mysqlInsert.InternalOrm.Aop.CurdAfter?.Invoke(_mysqlInsert, after);
ClearData();
}
return ret;
}
#if net40
#else
async public Task<long> ExecuteAffrowsAsync()
{
var sql = this.ToSql();
if (string.IsNullOrEmpty(sql)) return 0;
var before = new CurdBeforeEventArgs(_mysqlInsert.InternalTable.Type, _mysqlInsert.InternalTable, CurdType.Insert, sql, _mysqlInsert.InternalParams);
_mysqlInsert.InternalOrm.Aop.CurdBefore?.Invoke(_mysqlInsert, before);
long ret = 0;
Exception exception = null;
try
{
ret = await _mysqlInsert.InternalOrm.Ado.ExecuteNonQueryAsync(_mysqlInsert.InternalConnection, _mysqlInsert.InternalTransaction, CommandType.Text, sql, _mysqlInsert.InternalParams);
}
catch (Exception ex)
{
exception = ex;
throw ex;
}
finally
{
var after = new CurdAfterEventArgs(before, exception, ret);
_mysqlInsert.InternalOrm.Aop.CurdAfter?.Invoke(_mysqlInsert, after);
ClearData();
}
return ret;
}
#endif
}
}

View File

@ -1,4 +1,7 @@
public static partial class FreeSqlMySqlGlobalExtensions using FreeSql;
using FreeSql.MySql.Curd;
public static partial class FreeSqlMySqlGlobalExtensions
{ {
/// <summary> /// <summary>
@ -9,4 +12,14 @@
/// <returns></returns> /// <returns></returns>
public static string FormatMySql(this string that, params object[] args) => _mysqlAdo.Addslashes(that, args); public static string FormatMySql(this string that, params object[] args) => _mysqlAdo.Addslashes(that, args);
static FreeSql.MySql.MySqlAdo _mysqlAdo = new FreeSql.MySql.MySqlAdo(); static FreeSql.MySql.MySqlAdo _mysqlAdo = new FreeSql.MySql.MySqlAdo();
/// <summary>
/// MySql 特有的功能On Duplicate Key Update<para></para>
/// 注意:此功能会开启插入【自增列】
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static OnDuplicateKeyUpdate<T1> OnDuplicateKeyUpdate<T1>(this IInsert<T1> that) where T1 : class => new FreeSql.MySql.Curd.OnDuplicateKeyUpdate<T1>(that.InsertIdentity());
} }

View File

@ -37,8 +37,8 @@ namespace FreeSql.Odbc.Oracle
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (col.Attribute.IsIdentity) _identCol = col; if (col.Attribute.IsIdentity) _identCol = col;
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx > 0) sbtb.Append(", "); if (colidx > 0) sbtb.Append(", ");
sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)); sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
@ -58,8 +58,8 @@ namespace FreeSql.Odbc.Oracle
var colidx2 = 0; var colidx2 = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx2 > 0) sb.Append(", "); if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d); object val = col.GetMapValue(d);

View File

@ -39,8 +39,8 @@ namespace FreeSql.Oracle.Curd
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (col.Attribute.IsIdentity) _identCol = col; if (col.Attribute.IsIdentity) _identCol = col;
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx > 0) sbtb.Append(", "); if (colidx > 0) sbtb.Append(", ");
sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)); sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
@ -60,8 +60,8 @@ namespace FreeSql.Oracle.Curd
var colidx2 = 0; var colidx2 = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx2 > 0) sb.Append(", "); if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d); object val = col.GetMapValue(d);