mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
- 增加 IInsertOrUpdate.SetSource(items, tempPrimarys) 指定临时主键参数;#1160
This commit is contained in:
parent
5fec1254cf
commit
1155ffc781
@ -795,5 +795,14 @@
|
|||||||
<param name="that"></param>
|
<param name="that"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
|
||||||
|
<summary>
|
||||||
|
批量注入 Repository,可以参考代码自行调整
|
||||||
|
</summary>
|
||||||
|
<param name="services"></param>
|
||||||
|
<param name="globalDataFilter"></param>
|
||||||
|
<param name="assemblies"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
</members>
|
</members>
|
||||||
</doc>
|
</doc>
|
||||||
|
@ -373,11 +373,10 @@ WHEN NOT MATCHED THEN
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void InsertOrUpdate_OnColumns()
|
public void InsertOrUpdate_TempPrimarys()
|
||||||
{
|
{
|
||||||
var iou = fsql.InsertOrUpdate<tbiou03>()
|
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" } })
|
.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" } }, a => a.name);
|
||||||
.OnColumns("name");
|
|
||||||
var sql = iou.ToSql();
|
var sql = iou.ToSql();
|
||||||
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
Assert.Equal(@"MERGE INTO [tbiou03] t1
|
||||||
USING (SELECT 1 as [id1], N'01' as [id2], N'001' as [name]
|
USING (SELECT 1 as [id1], N'01' as [id2], N'001' as [name]
|
||||||
@ -388,7 +387,7 @@ UNION ALL
|
|||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT 4, N'04', N'004' ) t2 ON (t1.[name] = t2.[name])
|
SELECT 4, N'04', N'004' ) t2 ON (t1.[name] = t2.[name])
|
||||||
WHEN MATCHED THEN
|
WHEN MATCHED THEN
|
||||||
update set [name] = t2.[name]
|
update set [id1] = t2.[id1], [id2] = t2.[id2]
|
||||||
WHEN NOT MATCHED THEN
|
WHEN NOT MATCHED THEN
|
||||||
insert ([id1], [id2], [name])
|
insert ([id1], [id2], [name])
|
||||||
values (t2.[id1], t2.[id2], t2.[name]);", sql);
|
values (t2.[id1], t2.[id2], t2.[name]);", sql);
|
||||||
|
@ -1112,6 +1112,7 @@ SELECT ");
|
|||||||
public InsertOrUpdateDictImpl WherePrimary(params string[] primarys)
|
public InsertOrUpdateDictImpl WherePrimary(params string[] primarys)
|
||||||
{
|
{
|
||||||
UpdateDictImpl.SetTablePrimary(_insertOrUpdateProvider._table, primarys);
|
UpdateDictImpl.SetTablePrimary(_insertOrUpdateProvider._table, primarys);
|
||||||
|
_insertOrUpdateProvider._tempPrimarys = _insertOrUpdateProvider._table.Primarys;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1778,11 +1778,15 @@
|
|||||||
<param name="source">实体</param>
|
<param name="source">实体</param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.IInsertOrUpdate`1.SetSource(System.Collections.Generic.IEnumerable{`0})">
|
<member name="M:FreeSql.IInsertOrUpdate`1.SetSource(System.Collections.Generic.IEnumerable{`0},System.Linq.Expressions.Expression{System.Func{`0,System.Object}})">
|
||||||
<summary>
|
<summary>
|
||||||
添加或更新,设置实体集合
|
添加或更新,设置实体集合
|
||||||
</summary>
|
</summary>
|
||||||
<param name="source">实体集合</param>
|
<param name="source">实体集合</param>
|
||||||
|
<param name="tempPrimarys">
|
||||||
|
根据临时主键插入或更新,a => a.Name | a => new{a.Name,a.Time} | a => new[]{"name","time"}<para></para>
|
||||||
|
注意:不处理自增,因某些数据库依赖主键或唯一键,所以指定临时主键仅对 SqlServer/PostgreSQL/Firebird/达梦/南大通用/金仓/神通 有效
|
||||||
|
</param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.IInsertOrUpdate`1.IfExistsDoNothing">
|
<member name="M:FreeSql.IInsertOrUpdate`1.IfExistsDoNothing">
|
||||||
|
@ -39,8 +39,12 @@ namespace FreeSql
|
|||||||
/// 添加或更新,设置实体集合
|
/// 添加或更新,设置实体集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source">实体集合</param>
|
/// <param name="source">实体集合</param>
|
||||||
|
/// <param name="tempPrimarys">
|
||||||
|
/// 根据临时主键插入或更新,a => a.Name | a => new{a.Name,a.Time} | a => new[]{"name","time"}<para></para>
|
||||||
|
/// 注意:不处理自增,因某些数据库依赖主键或唯一键,所以指定临时主键仅对 SqlServer/PostgreSQL/Firebird/达梦/南大通用/金仓/神通 有效
|
||||||
|
/// </param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source);
|
IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source, Expression<Func<T1, object>> tempPrimarys = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当记录存在时,什么都不做<para></para>
|
/// 当记录存在时,什么都不做<para></para>
|
||||||
|
@ -14,22 +14,27 @@ using System.Threading.Tasks;
|
|||||||
namespace FreeSql.Internal.CommonProvider
|
namespace FreeSql.Internal.CommonProvider
|
||||||
{
|
{
|
||||||
|
|
||||||
public abstract partial class InsertOrUpdateProvider<T1> : IInsertOrUpdate<T1> where T1 : class
|
public abstract partial class InsertOrUpdateProvider
|
||||||
{
|
{
|
||||||
public IFreeSql _orm;
|
public IFreeSql _orm;
|
||||||
public CommonUtils _commonUtils;
|
public CommonUtils _commonUtils;
|
||||||
public CommonExpression _commonExpression;
|
public CommonExpression _commonExpression;
|
||||||
public List<T1> _source = new List<T1>();
|
|
||||||
public bool _doNothing = false;
|
public bool _doNothing = false;
|
||||||
public Dictionary<string, bool> _updateIgnore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
public Dictionary<string, bool> _updateIgnore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
public Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
public Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||||
public TableInfo _table;
|
public TableInfo _table;
|
||||||
|
public ColumnInfo[] _tempPrimarys;
|
||||||
public Func<string, string> _tableRule;
|
public Func<string, string> _tableRule;
|
||||||
public DbParameter[] _params;
|
public DbParameter[] _params;
|
||||||
public DbTransaction _transaction;
|
public DbTransaction _transaction;
|
||||||
public DbConnection _connection;
|
public DbConnection _connection;
|
||||||
public int _commandTimeout = 0;
|
public int _commandTimeout = 0;
|
||||||
public ColumnInfo IdentityColumn { get; private set; }
|
public ColumnInfo IdentityColumn { get; protected set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract partial class InsertOrUpdateProvider<T1> : InsertOrUpdateProvider, IInsertOrUpdate<T1> where T1 : class
|
||||||
|
{
|
||||||
|
public List<T1> _source = new List<T1>();
|
||||||
|
|
||||||
public InsertOrUpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
public InsertOrUpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
||||||
{
|
{
|
||||||
@ -37,6 +42,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
_commonUtils = commonUtils;
|
_commonUtils = commonUtils;
|
||||||
_commonExpression = commonExpression;
|
_commonExpression = commonExpression;
|
||||||
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
||||||
|
_tempPrimarys = _table?.Primarys ?? new ColumnInfo[0];
|
||||||
if (_table == null && typeof(T1) != typeof(Dictionary<string, object>))
|
if (_table == null && typeof(T1) != typeof(Dictionary<string, object>))
|
||||||
throw new Exception(CoreStrings.InsertOrUpdate_NotSuport_Generic_UseEntity(typeof(T1)));
|
throw new Exception(CoreStrings.InsertOrUpdate_NotSuport_Generic_UseEntity(typeof(T1)));
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
||||||
@ -107,12 +113,18 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IInsertOrUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
|
public IInsertOrUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
|
||||||
public IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source)
|
public IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source, Expression<Func<T1, object>> tempPrimarys = null)
|
||||||
{
|
{
|
||||||
if (source == null || source.Any() == false) return this;
|
if (source == null || source.Any() == false) return this;
|
||||||
UpdateProvider<T1>.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
|
UpdateProvider<T1>.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
|
||||||
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
||||||
_source.AddRange(source.Where(a => a != null));
|
_source.AddRange(source.Where(a => a != null));
|
||||||
|
|
||||||
|
if (tempPrimarys != null)
|
||||||
|
{
|
||||||
|
var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, tempPrimarys?.Body, false, null).Distinct().ToDictionary(a => a);
|
||||||
|
_tempPrimarys = cols.Keys.Select(a => _table.Columns.TryGetValue(a, out var col) ? col : null).ToArray().Where(a => a != null).ToArray();
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +173,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
if (entityType == _table.Type) return this;
|
if (entityType == _table.Type) return this;
|
||||||
var newtb = _commonUtils.GetTableByEntity(entityType);
|
var newtb = _commonUtils.GetTableByEntity(entityType);
|
||||||
_table = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("IInsertOrUpdate"));
|
_table = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("IInsertOrUpdate"));
|
||||||
|
_tempPrimarys = _table.Primarys;
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
||||||
IdentityColumn = _table.Primarys.Where(a => a.Attribute.IsIdentity).FirstOrDefault();
|
IdentityColumn = _table.Primarys.Where(a => a.Attribute.IsIdentity).FirstOrDefault();
|
||||||
return this;
|
return this;
|
||||||
@ -221,7 +234,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
if (source.Any() == false) return NativeTuple.Create(new List<T1>[0], new List<T1>[0]);
|
if (source.Any() == false) return NativeTuple.Create(new List<T1>[0], new List<T1>[0]);
|
||||||
if (_SplitSourceByIdentityValueIsNullFlag == 1) return NativeTuple.Create(new[] { source }, new List<T1>[0]);
|
if (_SplitSourceByIdentityValueIsNullFlag == 1) return NativeTuple.Create(new[] { source }, new List<T1>[0]);
|
||||||
if (_SplitSourceByIdentityValueIsNullFlag == 2) return NativeTuple.Create(new List<T1>[0], new[] { source });
|
if (_SplitSourceByIdentityValueIsNullFlag == 2) return NativeTuple.Create(new List<T1>[0], new[] { source });
|
||||||
if (IdentityColumn == null) return NativeTuple.Create(LocalSplitSourceByAsTable(source), new List<T1>[0]);
|
if (IdentityColumn == null || _tempPrimarys != _table.Primarys) return NativeTuple.Create(LocalSplitSourceByAsTable(source), new List<T1>[0]);
|
||||||
var item1 = new List<T1>();
|
var item1 = new List<T1>();
|
||||||
var item2 = new List<T1>();
|
var item2 = new List<T1>();
|
||||||
foreach (var item in source)
|
foreach (var item in source)
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.Dameng.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.Firebird.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.GBase.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -43,7 +43,8 @@ namespace FreeSql.KingbaseES
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var ocdu = new KingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
var ocdu = new KingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
||||||
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
ocdu._tempPrimarys = _tempPrimarys;
|
||||||
|
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
||||||
if (_doNothing == true || cols.Any() == false)
|
if (_doNothing == true || cols.Any() == false)
|
||||||
ocdu.DoNothing();
|
ocdu.DoNothing();
|
||||||
|
@ -18,7 +18,7 @@ namespace FreeSql.KingbaseES
|
|||||||
internal KingbaseESUpdate<T1> _update => _updatePriv ??
|
internal KingbaseESUpdate<T1> _update => _updatePriv ??
|
||||||
(_updatePriv = new KingbaseESUpdate<T1>(_insert.InternalOrm, _insert.InternalCommonUtils, _insert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
(_updatePriv = new KingbaseESUpdate<T1>(_insert.InternalOrm, _insert.InternalCommonUtils, _insert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
||||||
.NoneParameter().SetSource(_insert.InternalSource) as KingbaseESUpdate<T1>);
|
.NoneParameter().SetSource(_insert.InternalSource) as KingbaseESUpdate<T1>);
|
||||||
ColumnInfo[] _columns;
|
internal ColumnInfo[] _tempPrimarys;
|
||||||
bool _doNothing;
|
bool _doNothing;
|
||||||
|
|
||||||
public KingbaseESOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
public KingbaseESOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
||||||
@ -34,11 +34,11 @@ namespace FreeSql.KingbaseES
|
|||||||
foreach (var col in _insert.InternalTable.Columns.Values)
|
foreach (var col in _insert.InternalTable.Columns.Values)
|
||||||
if (cols.ContainsKey(col.Attribute.Name))
|
if (cols.ContainsKey(col.Attribute.Name))
|
||||||
colsList.Add(col);
|
colsList.Add(col);
|
||||||
_columns = colsList.ToArray();
|
_tempPrimarys = colsList.ToArray();
|
||||||
}
|
}
|
||||||
if (_columns == null || _columns.Any() == false)
|
if (_tempPrimarys == null || _tempPrimarys.Any() == false)
|
||||||
_columns = _insert.InternalTable.Primarys;
|
_tempPrimarys = _insert.InternalTable.Primarys;
|
||||||
if (_columns.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
@ -96,10 +96,10 @@ namespace FreeSql.KingbaseES
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_insert.ToSql()).Append("\r\nON CONFLICT(");
|
sb.Append(_insert.ToSql()).Append("\r\nON CONFLICT(");
|
||||||
for (var a = 0; a < _columns.Length; a++)
|
for (var a = 0; a < _tempPrimarys.Length; a++)
|
||||||
{
|
{
|
||||||
if (a > 0) sb.Append(", ");
|
if (a > 0) sb.Append(", ");
|
||||||
sb.Append(_insert.InternalCommonUtils.QuoteSqlName(_columns[a].Attribute.Name));
|
sb.Append(_insert.InternalCommonUtils.QuoteSqlName(_tempPrimarys[a].Attribute.Name));
|
||||||
}
|
}
|
||||||
if (_doNothing)
|
if (_doNothing)
|
||||||
{
|
{
|
||||||
|
@ -47,14 +47,14 @@ namespace FreeSql.MySql.Curd
|
|||||||
insert.InsertIdentity();
|
insert.InsertIdentity();
|
||||||
if (_doNothing == false)
|
if (_doNothing == false)
|
||||||
{
|
{
|
||||||
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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
sql = new OnDuplicateKeyUpdate<T1>(insert)
|
sql = new OnDuplicateKeyUpdate<T1>(insert)
|
||||||
.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray())
|
.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray())
|
||||||
.ToSql();
|
.ToSql();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
||||||
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(").Append(
|
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(").Append(
|
||||||
_orm.Select<T1>()
|
_orm.Select<T1>()
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.Odbc.Dameng
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -45,7 +45,8 @@ namespace FreeSql.Odbc.KingbaseES
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var ocdu = new OdbcKingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
var ocdu = new OdbcKingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
||||||
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
ocdu._tempPrimarys = _tempPrimarys;
|
||||||
|
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
||||||
if (_doNothing == true || cols.Any() == false)
|
if (_doNothing == true || cols.Any() == false)
|
||||||
ocdu.DoNothing();
|
ocdu.DoNothing();
|
||||||
|
@ -18,7 +18,7 @@ namespace FreeSql.Odbc.KingbaseES
|
|||||||
internal OdbcKingbaseESUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
internal OdbcKingbaseESUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
||||||
(_pgsqlUpdatePriv = new OdbcKingbaseESUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
(_pgsqlUpdatePriv = new OdbcKingbaseESUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
||||||
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as OdbcKingbaseESUpdate<T1>);
|
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as OdbcKingbaseESUpdate<T1>);
|
||||||
ColumnInfo[] _columns;
|
internal ColumnInfo[] _tempPrimarys;
|
||||||
bool _doNothing;
|
bool _doNothing;
|
||||||
|
|
||||||
public OdbcKingbaseESOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
public OdbcKingbaseESOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
||||||
@ -34,11 +34,11 @@ namespace FreeSql.Odbc.KingbaseES
|
|||||||
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
||||||
if (cols.ContainsKey(col.Attribute.Name))
|
if (cols.ContainsKey(col.Attribute.Name))
|
||||||
colsList.Add(col);
|
colsList.Add(col);
|
||||||
_columns = colsList.ToArray();
|
_tempPrimarys = colsList.ToArray();
|
||||||
}
|
}
|
||||||
if (_columns == null || _columns.Any() == false)
|
if (_tempPrimarys == null || _tempPrimarys.Any() == false)
|
||||||
_columns = _pgsqlInsert.InternalTable.Primarys;
|
_tempPrimarys = _pgsqlInsert.InternalTable.Primarys;
|
||||||
if (_columns.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
@ -96,10 +96,10 @@ namespace FreeSql.Odbc.KingbaseES
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
||||||
for (var a = 0; a < _columns.Length; a++)
|
for (var a = 0; a < _tempPrimarys.Length; a++)
|
||||||
{
|
{
|
||||||
if (a > 0) sb.Append(", ");
|
if (a > 0) sb.Append(", ");
|
||||||
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_columns[a].Attribute.Name));
|
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_tempPrimarys[a].Attribute.Name));
|
||||||
}
|
}
|
||||||
if (_doNothing)
|
if (_doNothing)
|
||||||
{
|
{
|
||||||
|
@ -46,14 +46,14 @@ namespace FreeSql.Odbc.MySql
|
|||||||
insert.InsertIdentity();
|
insert.InsertIdentity();
|
||||||
if (_doNothing == false)
|
if (_doNothing == false)
|
||||||
{
|
{
|
||||||
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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
sql = new OdbcMySqlOnDuplicateKeyUpdate<T1>(insert)
|
sql = new OdbcMySqlOnDuplicateKeyUpdate<T1>(insert)
|
||||||
.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray())
|
.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray())
|
||||||
.ToSql();
|
.ToSql();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
||||||
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(").Append(
|
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(").Append(
|
||||||
_orm.Select<T1>()
|
_orm.Select<T1>()
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.Odbc.Oracle
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -43,7 +43,8 @@ namespace FreeSql.Odbc.PostgreSQL
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var ocdu = new OdbcPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
var ocdu = new OdbcPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity());
|
||||||
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
ocdu._tempPrimarys = _tempPrimarys;
|
||||||
|
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
||||||
if (_doNothing == true || cols.Any() == false)
|
if (_doNothing == true || cols.Any() == false)
|
||||||
ocdu.DoNothing();
|
ocdu.DoNothing();
|
||||||
|
@ -18,7 +18,7 @@ namespace FreeSql.Odbc.PostgreSQL
|
|||||||
internal OdbcPostgreSQLUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
internal OdbcPostgreSQLUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
||||||
(_pgsqlUpdatePriv = new OdbcPostgreSQLUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
(_pgsqlUpdatePriv = new OdbcPostgreSQLUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
||||||
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as OdbcPostgreSQLUpdate<T1>);
|
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as OdbcPostgreSQLUpdate<T1>);
|
||||||
ColumnInfo[] _columns;
|
internal ColumnInfo[] _tempPrimarys;
|
||||||
bool _doNothing;
|
bool _doNothing;
|
||||||
|
|
||||||
public OdbcPostgreSQLOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
public OdbcPostgreSQLOnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
||||||
@ -34,11 +34,11 @@ namespace FreeSql.Odbc.PostgreSQL
|
|||||||
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
||||||
if (cols.ContainsKey(col.Attribute.Name))
|
if (cols.ContainsKey(col.Attribute.Name))
|
||||||
colsList.Add(col);
|
colsList.Add(col);
|
||||||
_columns = colsList.ToArray();
|
_tempPrimarys = colsList.ToArray();
|
||||||
}
|
}
|
||||||
if (_columns == null || _columns.Any() == false)
|
if (_tempPrimarys == null || _tempPrimarys.Any() == false)
|
||||||
_columns = _pgsqlInsert.InternalTable.Primarys;
|
_tempPrimarys = _pgsqlInsert.InternalTable.Primarys;
|
||||||
if (_columns.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
@ -96,10 +96,10 @@ namespace FreeSql.Odbc.PostgreSQL
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
||||||
for (var a = 0; a < _columns.Length; a++)
|
for (var a = 0; a < _tempPrimarys.Length; a++)
|
||||||
{
|
{
|
||||||
if (a > 0) sb.Append(", ");
|
if (a > 0) sb.Append(", ");
|
||||||
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_columns[a].Attribute.Name));
|
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_tempPrimarys[a].Attribute.Name));
|
||||||
}
|
}
|
||||||
if (_doNothing)
|
if (_doNothing)
|
||||||
{
|
{
|
||||||
|
@ -31,15 +31,15 @@ namespace FreeSql.Odbc.SqlServer
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.Oracle.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -18,7 +18,7 @@ namespace FreeSql.PostgreSQL.Curd
|
|||||||
internal PostgreSQLUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
internal PostgreSQLUpdate<T1> _pgsqlUpdate => _pgsqlUpdatePriv ??
|
||||||
(_pgsqlUpdatePriv = new PostgreSQLUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
(_pgsqlUpdatePriv = new PostgreSQLUpdate<T1>(_pgsqlInsert.InternalOrm, _pgsqlInsert.InternalCommonUtils, _pgsqlInsert.InternalCommonExpression, null) { InternalTableAlias = "EXCLUDED" }
|
||||||
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as PostgreSQLUpdate<T1>);
|
.NoneParameter().SetSource(_pgsqlInsert.InternalSource) as PostgreSQLUpdate<T1>);
|
||||||
ColumnInfo[] _columns;
|
internal ColumnInfo[] _tempPrimarys;
|
||||||
bool _doNothing;
|
bool _doNothing;
|
||||||
|
|
||||||
public OnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
public OnConflictDoUpdate(IInsert<T1> insert, Expression<Func<T1, object>> columns = null)
|
||||||
@ -34,11 +34,11 @@ namespace FreeSql.PostgreSQL.Curd
|
|||||||
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
|
||||||
if (cols.ContainsKey(col.Attribute.Name))
|
if (cols.ContainsKey(col.Attribute.Name))
|
||||||
colsList.Add(col);
|
colsList.Add(col);
|
||||||
_columns = colsList.ToArray();
|
_tempPrimarys = colsList.ToArray();
|
||||||
}
|
}
|
||||||
if (_columns == null || _columns.Any() == false)
|
if (_tempPrimarys == null || _tempPrimarys.Any() == false)
|
||||||
_columns = _pgsqlInsert.InternalTable.Primarys;
|
_tempPrimarys = _pgsqlInsert.InternalTable.Primarys;
|
||||||
if (_columns.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
@ -96,10 +96,10 @@ namespace FreeSql.PostgreSQL.Curd
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
|
||||||
for (var a = 0; a < _columns.Length; a++)
|
for (var a = 0; a < _tempPrimarys.Length; a++)
|
||||||
{
|
{
|
||||||
if (a > 0) sb.Append(", ");
|
if (a > 0) sb.Append(", ");
|
||||||
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_columns[a].Attribute.Name));
|
sb.Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_tempPrimarys[a].Attribute.Name));
|
||||||
}
|
}
|
||||||
if (_doNothing)
|
if (_doNothing)
|
||||||
{
|
{
|
||||||
|
@ -43,7 +43,8 @@ namespace FreeSql.PostgreSQL.Curd
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
|
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
|
||||||
var cols = _table.Columns.Values.Where(a => a.Attribute.IsPrimary == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
ocdu._tempPrimarys = _tempPrimarys;
|
||||||
|
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
ocdu.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray());
|
||||||
if (_doNothing == true || cols.Any() == false)
|
if (_doNothing == true || cols.Any() == false)
|
||||||
ocdu.DoNothing();
|
ocdu.DoNothing();
|
||||||
|
@ -31,13 +31,13 @@ namespace FreeSql.ShenTong.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder().Append("MERGE INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(" t1 \r\nUSING (");
|
var sb = new StringBuilder().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");
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -10,8 +10,6 @@ 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)
|
||||||
{
|
{
|
||||||
@ -33,20 +31,15 @@ namespace FreeSql.SqlServer.Curd
|
|||||||
|
|
||||||
string getMergeSql(List<T1> data)
|
string getMergeSql(List<T1> data)
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.InsertOrUpdate_Must_Primary_Key(_table.CsName));
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
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);
|
||||||
IEnumerable<string> onColumns;
|
sb.Append(" ) t2 ON (").Append(string.Join(" AND ", _tempPrimarys.Select(a => $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}"))).Append(") \r\n");
|
||||||
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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
|
||||||
if (_doNothing == false && cols.Any())
|
if (_doNothing == false && cols.Any())
|
||||||
sb.Append("WHEN MATCHED THEN \r\n")
|
sb.Append("WHEN MATCHED THEN \r\n")
|
||||||
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
.Append(" update set ").Append(string.Join(", ", cols.Select(a =>
|
||||||
|
@ -49,25 +49,6 @@ 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>
|
||||||
|
@ -53,7 +53,7 @@ namespace FreeSql.Sqlite.Curd
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_table.Primarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + Sqlite ", _table.CsName));
|
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + Sqlite ", _table.CsName));
|
||||||
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append(" \r\n WHERE NOT EXISTS(").Append(
|
sb.Append(" \r\n WHERE NOT EXISTS(").Append(
|
||||||
_orm.Select<T1>()
|
_orm.Select<T1>()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user