Skip to content

Commit

Permalink
Merge pull request #1762 from Naughtyhusky/master
Browse files Browse the repository at this point in the history
修复ExecuteMySqlBulkCopyAsync方法会出现空引用的异常
  • Loading branch information
luoyunchong authored Mar 27, 2024
2 parents 7314fa1 + f287bbe commit 1e089af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
9 changes: 9 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace FreeSql.Tests.MySqlConnector
Expand All @@ -11,6 +12,31 @@ public class MySqlInsertOrUpdateTest

IFreeSql fsql => g.mysql;

[Fact]
public void InsertOrUpdate_ExecuteMySqlBulkCopy_Test()
{
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
var 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" } });
var 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);
Assert.Equal(4, iou.ExecuteMySqlBulkCopy());
}

[Fact]
public async Task InsertOrUpdate_ExecuteMySqlBulkCopyAsync_Test()
{
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
var 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" } });
var 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);
Assert.Equal(4, await iou.ExecuteMySqlBulkCopyAsync());
}


[Fact]
public void InsertOrUpdate_OnePrimary()
{
Expand Down
1 change: 1 addition & 0 deletions FreeSql/Internal/CommonProvider/UpdateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static int ExecuteBulkCommand<T1>(List<T1> _source, ColumnInfo[] _tempPri
#else
public static Task<int> ExecuteBulkUpdateAsync<T1>(UpdateProvider<T1> update, NativeTuple<string, string, string, string, string[]> state, Func<IInsert<T1>, Task> funcBulkCopy) where T1 : class =>
ExecuteBulkCommandAsync(update._source, update._tempPrimarys, update._orm, update._connection, update._transaction, update._table, state, funcBulkCopy);

public static Task<int> ExecuteBulkUpsertAsync<T1>(InsertOrUpdateProvider<T1> upsert, NativeTuple<string, string, string, string, string[]> state, Func<IInsert<T1>, Task> funcBulkCopy) where T1 : class =>
ExecuteBulkCommandAsync(upsert._source, upsert._tempPrimarys, upsert._orm, upsert._connection, upsert._transaction, upsert._table, state, funcBulkCopy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ public static void ExecuteMySqlBulkCopy<T>(this IInsert<T> that, int? bulkCopyTi
#else
public static Task<int> ExecuteMySqlBulkCopyAsync<T>(this IInsertOrUpdate<T> that, int? bulkCopyTimeout = null, CancellationToken cancellationToken = default) where T : class
{
var upsert = that as UpdateProvider<T>;
var upsert = that as InsertOrUpdateProvider<T>;
if (upsert._source.Any() != true || upsert._tempPrimarys.Any() == false) return Task.FromResult(0);
var state = ExecuteMySqlBulkCopyState(upsert);
return UpdateProvider.ExecuteBulkUpdateAsync(upsert, state, insert => insert.ExecuteMySqlBulkCopyAsync(bulkCopyTimeout, cancellationToken));
return UpdateProvider.ExecuteBulkUpsertAsync(upsert, state, insert => insert.ExecuteMySqlBulkCopyAsync(bulkCopyTimeout, cancellationToken));
}
public static Task<int> ExecuteMySqlBulkCopyAsync<T>(this IUpdate<T> that, int? bulkCopyTimeout = null, CancellationToken cancellationToken = default) where T : class
{
Expand Down

0 comments on commit 1e089af

Please sign in to comment.