- 增加 IInsertOrUpdate.UpdateSet 指定更新;

This commit is contained in:
2881099 2023-05-30 18:21:57 +08:00
parent a76f7907f9
commit 36c8a45e47
27 changed files with 369 additions and 85 deletions

View File

@ -566,14 +566,14 @@ namespace base_entity
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=2;AllowLoadLocalInfile=true") .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=2;AllowLoadLocalInfile=true")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") .UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2") //.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2")
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower) .UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2") //.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper) //.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
//.UseConnectionString(FreeSql.DataType.Dameng, "server=127.0.0.1;port=5236;user id=2user;password=123456789;database=2user;poolsize=5;min pool size=1") //.UseConnectionString(FreeSql.DataType.Dameng, "server=127.0.0.1;port=5236;user id=2user;password=123456789;database=2user;poolsize=5;min pool size=1")
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper) //.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
@ -601,6 +601,12 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
var iouSetSql01 = fsql.InsertOrUpdate<User1>()
.SetSource(Enumerable.Range(0, 5).Select(a => new User1 { Id = Guid.NewGuid(), Nickname = $"nickname{a}", Username = $"username{a}", Description = $"desc{a}" }).ToArray())
.UpdateSet((a, b) => a.Sort == b.Sort + 10)
.UpdateSet((a, b) => a.Nickname == "xxx")
.ToSql();
var sqlastable1 = fsql.Select<CurrentDetail>(101).AsTable((t, o) => "current_detail_230501").ToSql(); var sqlastable1 = fsql.Select<CurrentDetail>(101).AsTable((t, o) => "current_detail_230501").ToSql();
var sqlastable2 = fsql.Update<CurrentDetail>(101).AsTable("current_detail_230501").Set(t => t.StatuId, 1).ToSql(); var sqlastable2 = fsql.Update<CurrentDetail>(101).AsTable("current_detail_230501").Set(t => t.StatuId, 1).ToSql();
var sqlastable3 = fsql.Delete<CurrentDetail>(101).AsTable("current_detail_230501").ToSql(); var sqlastable3 = fsql.Delete<CurrentDetail>(101).AsTable("current_detail_230501").ToSql();

View File

@ -800,14 +800,5 @@
<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>

View File

@ -1921,6 +1921,16 @@
<param name="columns">属性名,或者字段名</param> <param name="columns">属性名,或者字段名</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.IInsertOrUpdate`1.UpdateSet``1(System.Linq.Expressions.Expression{System.Func{`0,`0,``0}})">
<summary>
设置列的联表值,格式:<para></para>
UpdateSet((a, b) => a.Clicks == b.xxx)<para></para>
UpdateSet((a, b) => a.Clicks == a.Clicks + 1)
</summary>
<typeparam name="TMember"></typeparam>
<param name="exp"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IInsertOrUpdate`1.AsTable(System.Func{System.String,System.String})"> <member name="M:FreeSql.IInsertOrUpdate`1.AsTable(System.Func{System.String,System.String})">
<summary> <summary>
设置表名规则,可用于分库/分表参数1默认表名返回值新表名 设置表名规则,可用于分库/分表参数1默认表名返回值新表名

View File

@ -86,6 +86,16 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
IInsertOrUpdate<T1> UpdateColumns(string[] columns); IInsertOrUpdate<T1> UpdateColumns(string[] columns);
/// <summary>
/// 设置列的联表值,格式:<para></para>
/// UpdateSet((a, b) => a.Clicks == b.xxx)<para></para>
/// UpdateSet((a, b) => a.Clicks == a.Clicks + 1)
/// </summary>
/// <typeparam name="TMember"></typeparam>
/// <param name="exp"></param>
/// <returns></returns>
IInsertOrUpdate<T1> UpdateSet<TMember>(Expression<Func<T1, T1, TMember>> exp);
/// <summary> /// <summary>
/// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名 /// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名
/// </summary> /// </summary>

View File

@ -23,6 +23,7 @@ namespace FreeSql.Internal.CommonProvider
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 Dictionary<string, string> _updateSetDict = new Dictionary<string, string>();
public TableInfo _table; public TableInfo _table;
public ColumnInfo[] _tempPrimarys; public ColumnInfo[] _tempPrimarys;
public Func<string, string> _tableRule; public Func<string, string> _tableRule;
@ -87,6 +88,55 @@ namespace FreeSql.Internal.CommonProvider
return this; return this;
} }
public IInsertOrUpdate<T1> UpdateSet<TMember>(Expression<Func<T1, T1, TMember>> exp)
{
var body = exp?.Body;
var nodeType = body?.NodeType;
if (nodeType == ExpressionType.Convert)
{
body = (body as UnaryExpression)?.Operand;
nodeType = body?.NodeType;
}
switch (nodeType)
{
case ExpressionType.Equal:
break;
default:
throw new Exception("格式错了,请使用 .Set((a,b) => a.name == b.xname)");
}
var equalBinaryExp = body as BinaryExpression;
var cols = new List<SelectColumnInfo>();
_commonExpression.ExpressionSelectColumn_MemberAccess(null, null, cols, SelectTableInfoType.From, equalBinaryExp.Left, true, null);
if (cols.Count != 1) return this;
var col = cols[0].Column;
var valueSql = "";
if (equalBinaryExp.Right.IsParameter())
{
var tmpQuery = _orm.Select<T1, T1>();
var tmpQueryProvider = tmpQuery as Select0Provider;
tmpQueryProvider._tables[0].Alias = "t1";
tmpQueryProvider._tables[0].Parameter = exp.Parameters[0];
tmpQueryProvider._tables[1].Alias = "t2";
tmpQueryProvider._tables[1].Parameter = exp.Parameters[1];
var valueExp = Expression.Lambda<Func<T1, T1, object>>(Expression.Convert(equalBinaryExp.Right, typeof(object)), exp.Parameters);
tmpQuery.GroupBy(valueExp);
valueSql = tmpQueryProvider._groupby?.Remove(0, " \r\nGROUP BY ".Length);
}
else
{
valueSql = _commonExpression.ExpressionLambdaToSql(equalBinaryExp.Right, new CommonExpression.ExpTSC
{
isQuoteName = true,
mapType = equalBinaryExp.Right is BinaryExpression ? null : col.Attribute.MapType
});
}
if (string.IsNullOrEmpty(valueSql)) return this;
_updateSetDict[col.Attribute.Name] = valueSql;
return this;
}
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict) public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data?.Any() != true) return; if (data?.Any() != true) return;

View File

@ -894,7 +894,9 @@ namespace FreeSql.Internal.CommonProvider
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = "); sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum; var valsameIf = col.Attribute.MapType.IsNumberType() ||
new[] { typeof(string), typeof(DateTime), typeof(DateTime?) }.Contains(col.Attribute.MapType) ||
col.Attribute.MapType.NullableTypeOrThis().IsEnum;
var ds = _source.Select(a => col.GetDbValue(a)).ToArray(); var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
if (valsameIf && ds.All(a => object.Equals(a, ds[0]))) if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
{ {
@ -1149,7 +1151,9 @@ namespace FreeSql.Internal.CommonProvider
sb.Append(col.DbUpdateValue); sb.Append(col.DbUpdateValue);
else else
{ {
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum; var valsameIf = col.Attribute.MapType.IsNumberType() ||
new[] { typeof(string), typeof(DateTime), typeof(DateTime?) }.Contains(col.Attribute.MapType) ||
col.Attribute.MapType.NullableTypeOrThis().IsEnum;
var ds = _source.Select(a => col.GetDbValue(a)).ToArray(); var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
if (valsameIf && ds.All(a => object.Equals(a, ds[0]))) if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
{ {

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Custom.MySql namespace FreeSql.Custom.MySql
{ {
@ -62,10 +63,26 @@ namespace FreeSql.Custom.MySql
insert.InsertIdentity(); insert.InsertIdentity();
if (_doNothing == false) if (_doNothing == false)
{ {
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false);
sql = new CustomMySqlOnDuplicateKeyUpdate<T1>(insert) sql = new CustomMySqlOnDuplicateKeyUpdate<T1>(insert)
.UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray()) .UpdateColumns(cols.Select(a => a.Attribute.Name).ToArray())
.ToSql(); .ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = VALUES({field})";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return _commonUtils.QuoteSqlName(m.Groups[2].Value);
return $"VALUES({_commonUtils.QuoteSqlName(m.Groups[2].Value)})";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
else else
{ {

View File

@ -39,14 +39,18 @@ namespace FreeSql.Custom.Oracle
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Custom.PostgreSQL namespace FreeSql.Custom.PostgreSQL
{ {
@ -57,11 +58,29 @@ namespace FreeSql.Custom.PostgreSQL
{ {
var ocdu = new CustomPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new CustomPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -41,14 +41,18 @@ namespace FreeSql.Custom.SqlServer
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -39,14 +39,18 @@ namespace FreeSql.Dameng.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -39,14 +39,18 @@ namespace FreeSql.Firebird.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -39,14 +39,18 @@ namespace FreeSql.GBase.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.KingbaseES namespace FreeSql.KingbaseES
{ {
@ -57,11 +58,29 @@ namespace FreeSql.KingbaseES
{ {
var ocdu = new KingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new KingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.MySql.Curd namespace FreeSql.MySql.Curd
{ {
@ -62,10 +63,26 @@ namespace FreeSql.MySql.Curd
insert.InsertIdentity(); insert.InsertIdentity();
if (_doNothing == false) if (_doNothing == false)
{ {
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = VALUES({field})";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return _commonUtils.QuoteSqlName(m.Groups[2].Value);
return $"VALUES({_commonUtils.QuoteSqlName(m.Groups[2].Value)})";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
else else
{ {

View File

@ -39,14 +39,18 @@ namespace FreeSql.Odbc.Dameng
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.KingbaseES namespace FreeSql.Odbc.KingbaseES
{ {
@ -58,11 +59,29 @@ namespace FreeSql.Odbc.KingbaseES
{ {
var ocdu = new OdbcKingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new OdbcKingbaseESOnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.MySql namespace FreeSql.Odbc.MySql
{ {
@ -62,10 +63,26 @@ namespace FreeSql.Odbc.MySql
insert.InsertIdentity(); insert.InsertIdentity();
if (_doNothing == false) if (_doNothing == false)
{ {
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = VALUES({field})";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return _commonUtils.QuoteSqlName(m.Groups[2].Value);
return $"VALUES({_commonUtils.QuoteSqlName(m.Groups[2].Value)})";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
else else
{ {

View File

@ -39,14 +39,18 @@ namespace FreeSql.Odbc.Oracle
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Odbc.PostgreSQL namespace FreeSql.Odbc.PostgreSQL
{ {
@ -57,11 +58,29 @@ namespace FreeSql.Odbc.PostgreSQL
{ {
var ocdu = new OdbcPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new OdbcPostgreSQLOnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -41,14 +41,18 @@ namespace FreeSql.Odbc.SqlServer
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -39,14 +39,18 @@ namespace FreeSql.Oracle.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.PostgreSQL.Curd namespace FreeSql.PostgreSQL.Curd
{ {
@ -57,11 +58,29 @@ namespace FreeSql.PostgreSQL.Curd
{ {
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.QuestDb.Curd namespace FreeSql.QuestDb.Curd
{ {
@ -57,11 +58,29 @@ namespace FreeSql.QuestDb.Curd
{ {
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity()); var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys; ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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();
sql = ocdu.ToSql(); sql = ocdu.ToSql();
if (_updateSetDict.Any())
{
var findregex = new Regex("(t1|t2)." + _commonUtils.QuoteSqlName("test").Replace("test", "(\\w+)"));
var tableName = _commonUtils.QuoteSqlName(TableRuleInvoke());
foreach (var usd in _updateSetDict)
{
var field = _commonUtils.QuoteSqlName(usd.Key);
var findsql = $"{field} = EXCLUDED.{field}";
var usdval = findregex.Replace(usd.Value, m =>
{
if (m.Groups[1].Value == "t1") return $"{tableName}.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
return $"EXCLUDED.{_commonUtils.QuoteSqlName(m.Groups[2].Value)}";
});
sql = sql.Replace(findsql, $"{field} = {usdval}");
}
}
} }
if (string.IsNullOrEmpty(sql)) return null; if (string.IsNullOrEmpty(sql)) return null;
if (insert._params?.Any() == true) dbParams.AddRange(insert._params); if (insert._params?.Any() == true) dbParams.AddRange(insert._params);

View File

@ -39,14 +39,18 @@ namespace FreeSql.ShenTong.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -41,14 +41,18 @@ namespace FreeSql.SqlServer.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);

View File

@ -45,14 +45,18 @@ namespace FreeSql.Xugu.Curd
WriteSourceSelectUnionAll(data, sb, dbParams); WriteSourceSelectUnionAll(data, sb, dbParams);
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"); 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 => _tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false); var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _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 =>
a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ? {
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" : if (_updateSetDict.TryGetValue(a.Attribute.Name, out var valsql))
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}" return $"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = {valsql}";
))).Append(" \r\n"); return a.Attribute.IsVersion && a.Attribute.MapType != typeof(byte[]) ?
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t1.{_commonUtils.QuoteSqlName(a.Attribute.Name)} + 1" :
$"{_commonUtils.QuoteSqlName(a.Attribute.Name)} = t2.{_commonUtils.QuoteSqlName(a.Attribute.Name)}";
}))).Append(" \r\n");
cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true); cols = _table.Columns.Values.Where(a => a.Attribute.CanInsert == true);
if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false); if (tempPrimaryIsIdentity == false) cols = cols.Where(a => a.Attribute.IsIdentity == false || string.IsNullOrEmpty(a.DbInsertValue) == false);