mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
Merge pull request #1160 from ly303550688/oncolumns
增加 sqlserver InsertOrUpdate 指定On字段
This commit is contained in:
commit
5fec1254cf
@ -1,4 +1,4 @@
|
|||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using FreeSql.Tests.DataContext.SqlServer;
|
using FreeSql.Tests.DataContext.SqlServer;
|
||||||
using SaleIDO.Entity.Storeage;
|
using SaleIDO.Entity.Storeage;
|
||||||
using System;
|
using System;
|
||||||
@ -371,6 +371,31 @@ WHEN NOT MATCHED THEN
|
|||||||
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();
|
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());
|
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id1).Count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void InsertOrUpdate_OnColumns()
|
||||||
|
{
|
||||||
|
var 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" } })
|
||||||
|
.OnColumns("name");
|
||||||
|
var sql = iou.ToSql();
|
||||||
|
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
||||||
|
USING (SELECT 1 as [id1], N'01' as [id2], N'001' as [name]
|
||||||
|
UNION ALL
|
||||||
|
SELECT 2, N'02', N'002'
|
||||||
|
UNION ALL
|
||||||
|
SELECT 3, N'03', N'003'
|
||||||
|
UNION ALL
|
||||||
|
SELECT 4, N'04', N'004' ) t2 ON (t1.[name] = t2.[name])
|
||||||
|
WHEN MATCHED THEN
|
||||||
|
update set [name] = t2.[name]
|
||||||
|
WHEN NOT MATCHED THEN
|
||||||
|
insert ([id1], [id2], [name])
|
||||||
|
values (t2.[id1], t2.[id2], t2.[name]);", sql);
|
||||||
|
Assert.Equal(4, iou.ExecuteAffrows());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class tbiou03
|
class tbiou03
|
||||||
{
|
{
|
||||||
[Column(IsPrimary = true)]
|
[Column(IsPrimary = true)]
|
||||||
|
@ -10,6 +10,8 @@ namespace FreeSql.SqlServer.Curd
|
|||||||
|
|
||||||
class SqlServerInsertOrUpdate<T1> : Internal.CommonProvider.InsertOrUpdateProvider<T1> where T1 : class
|
class SqlServerInsertOrUpdate<T1> : Internal.CommonProvider.InsertOrUpdateProvider<T1> where T1 : class
|
||||||
{
|
{
|
||||||
|
internal string[] _columns;
|
||||||
|
|
||||||
public SqlServerInsertOrUpdate(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
public SqlServerInsertOrUpdate(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
||||||
: base(orm, commonUtils, commonExpression)
|
: base(orm, commonUtils, commonExpression)
|
||||||
{
|
{
|
||||||
@ -37,7 +39,12 @@ namespace FreeSql.SqlServer.Curd
|
|||||||
if (IdentityColumn != null) sb.Append("SET IDENTITY_INSERT ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" ON;\r\n");
|
if (IdentityColumn != null) sb.Append("SET IDENTITY_INSERT ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" ON;\r\n");
|
||||||
sb.Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
sb.Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
||||||
WriteSourceSelectUnionAll(data, sb, dbParams);
|
WriteSourceSelectUnionAll(data, sb, dbParams);
|
||||||
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _table.Primarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).Append(") \r\n");
|
IEnumerable<string> onColumns;
|
||||||
|
if (_columns?.Length > 0)
|
||||||
|
onColumns = _columns.Select(a => $"t1.{_commonUtils.QuoteSqlName(a)} = t2.{_commonUtils.QuoteSqlName(a)}");
|
||||||
|
else
|
||||||
|
onColumns = _table.Primarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}");
|
||||||
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", onColumns)).Append(") \r\n");
|
||||||
|
|
||||||
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
|
@ -49,6 +49,25 @@ public static partial class FreeSqlSqlServerGlobalExtensions
|
|||||||
}
|
}
|
||||||
internal static ConcurrentDictionary<Guid, NativeTuple<SqlServerLock, Dictionary<Type, bool>>> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<Guid, NativeTuple<SqlServerLock, Dictionary<Type, bool>>>();
|
internal static ConcurrentDictionary<Guid, NativeTuple<SqlServerLock, Dictionary<Type, bool>>> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<Guid, NativeTuple<SqlServerLock, Dictionary<Type, bool>>>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用merge on条件替换默认主键条件
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <param name="columns"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public static IInsertOrUpdate<T> OnColumns<T>(this IInsertOrUpdate<T> that, params string[] columns) where T : class
|
||||||
|
{
|
||||||
|
var insertOrUpdate = that as FreeSql.SqlServer.Curd.SqlServerInsertOrUpdate<T>;
|
||||||
|
if (insertOrUpdate == null) throw new Exception(CoreStrings.S_Features_Unique("OnColumns", "SqlServer"));
|
||||||
|
if (columns.Length > 0)
|
||||||
|
{
|
||||||
|
insertOrUpdate._columns = columns;
|
||||||
|
}
|
||||||
|
return that;
|
||||||
|
}
|
||||||
|
|
||||||
#region ExecuteSqlBulkCopy
|
#region ExecuteSqlBulkCopy
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user