mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
增加 sqlserver InsertOrUpdate 指定On字段
This commit is contained in:
@ -10,6 +10,8 @@ namespace FreeSql.SqlServer.Curd
|
||||
|
||||
class SqlServerInsertOrUpdate<T1> : Internal.CommonProvider.InsertOrUpdateProvider<T1> where T1 : class
|
||||
{
|
||||
internal string[] _columns;
|
||||
|
||||
public SqlServerInsertOrUpdate(IFreeSql orm, CommonUtils commonUtils, CommonExpression 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");
|
||||
sb.Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
||||
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);
|
||||
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>>>();
|
||||
|
||||
/// <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
|
||||
/// <summary>
|
||||
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
||||
|
Reference in New Issue
Block a user