mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IUpdate.WhereCaseSource 方法,实现批量修改时的条件判断;
- 增加 FreeSql.DbContext 行级锁;
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.3.26</Version>
|
||||
<Version>0.3.27</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||
|
@ -101,6 +101,13 @@ namespace FreeSql {
|
||||
/// <param name="notExists">不存在</param>
|
||||
/// <returns></returns>
|
||||
IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class;
|
||||
/// <summary>
|
||||
/// 用于批量修改时,生成 where dbName = case when id = 1 then v1 end 的条件
|
||||
/// </summary>
|
||||
/// <param name="CsName">属性名</param>
|
||||
/// <param name="thenValue"></param>
|
||||
/// <returns></returns>
|
||||
IUpdate<T1> WhereCaseSource(string CsName, Func<string, string> thenValue);
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
|
||||
|
@ -20,6 +20,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
protected Func<string, string> _tableRule;
|
||||
protected StringBuilder _where = new StringBuilder();
|
||||
protected StringBuilder _set = new StringBuilder();
|
||||
protected StringBuilder _setIncr = new StringBuilder();
|
||||
protected List<DbParameter> _params = new List<DbParameter>();
|
||||
protected List<DbParameter> _paramsSource = new List<DbParameter>();
|
||||
protected bool _noneParameter;
|
||||
@ -40,6 +41,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_ignore.Clear();
|
||||
_where.Clear();
|
||||
_set.Clear();
|
||||
_setIncr.Clear();
|
||||
_params.Clear();
|
||||
_paramsSource.Clear();
|
||||
}
|
||||
@ -117,7 +119,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
expt = expt.Replace(replname, _commonUtils.IsNull(replname, _commonUtils.FormatSql("{0}", replval)));
|
||||
}
|
||||
}
|
||||
_set.Append(", ").Append(_commonUtils.QuoteSqlName(cols.First().Column.Attribute.Name)).Append(" = ").Append(expt);
|
||||
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(cols.First().Column.Attribute.Name)).Append(" = ").Append(expt);
|
||||
return this;
|
||||
}
|
||||
public IUpdate<T1> SetRaw(string sql, object parms = null) {
|
||||
@ -142,6 +144,45 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
public IUpdate<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items));
|
||||
public IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
|
||||
|
||||
public IUpdate<T1> WhereCaseSource(string CsName, Func<string, string> thenValue) {
|
||||
if (_source.Any() == false) return this;
|
||||
if (_table.ColumnsByCs.ContainsKey(CsName) == false) throw new Exception($"找不到 {CsName} 对应的列");
|
||||
if (thenValue == null) throw new ArgumentNullException("thenValue 参数不可为 null");
|
||||
|
||||
if (_source.Count == 0) return this;
|
||||
if (_source.Count == 1) {
|
||||
|
||||
var col = _table.ColumnsByCs[CsName];
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
var value = _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(_source.First()) : DBNull.Value;
|
||||
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.CsType, value)));
|
||||
|
||||
return this.Where(sb.ToString());
|
||||
|
||||
} else {
|
||||
var caseWhen = new StringBuilder();
|
||||
caseWhen.Append("CASE ");
|
||||
ToSqlCase(caseWhen, _table.Primarys);
|
||||
var cw = caseWhen.ToString();
|
||||
|
||||
var col = _table.ColumnsByCs[CsName];
|
||||
var sb = new StringBuilder();
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(cw);
|
||||
foreach (var d in _source) {
|
||||
sb.Append(" \r\nWHEN ");
|
||||
ToSqlWhen(sb, _table.Primarys, d);
|
||||
sb.Append(" THEN ");
|
||||
var value = _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value;
|
||||
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.CsType, value)));
|
||||
}
|
||||
sb.Append(" END");
|
||||
|
||||
return this.Where(sb.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys);
|
||||
protected abstract void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d);
|
||||
|
||||
@ -227,6 +268,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
} else
|
||||
return null;
|
||||
|
||||
sb.Append(_setIncr.ToString());
|
||||
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5));
|
||||
return sb.ToString();
|
||||
}
|
||||
|
Reference in New Issue
Block a user