mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
## v0.3.27
- 增加 行级锁功能,适用修改实体; - 增加 FreeSql.Repository 默认依赖注入的方式,同时保留原有 Autofac; - 优化 FreeSql.Repository Insert 逻辑,参考了 FreeSql.DbContext; - 优化 FreeSql.IUpdate 参照 IInsert 对大批量更新,拆分执行; - 修复 FreeSql.IInsert ClearData 重复利用的 bug(使用 IgnoreColumns 进行大批量插入时会发生);
This commit is contained in:
@ -16,7 +16,7 @@ namespace FreeSql.DataAnnotations {
|
||||
/// </summary>
|
||||
public string DbType { get; set; }
|
||||
|
||||
internal bool? _IsPrimary, _IsIdentity, _IsNullable, _IsIgnore;
|
||||
internal bool? _IsPrimary, _IsIdentity, _IsNullable, _IsIgnore, _IsVersion;
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
@ -33,6 +33,10 @@ namespace FreeSql.DataAnnotations {
|
||||
/// 忽略此列,不迁移、不插入
|
||||
/// </summary>
|
||||
public bool IsIgnore { get => _IsIgnore ?? false; set => _IsIgnore = value; }
|
||||
/// <summary>
|
||||
/// 设置行锁(乐观锁)版本号,每次更新累加版本号,若更新整个实体时会附带当前的版本号判断(修改失败时抛出异常)
|
||||
/// </summary>
|
||||
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据库默认值
|
||||
|
401
FreeSql/Extensions/EntityUtilExtensions.cs
Normal file
401
FreeSql/Extensions/EntityUtilExtensions.cs
Normal file
@ -0,0 +1,401 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Extensions.EntityUtil {
|
||||
public static class EntityUtilExtensions {
|
||||
|
||||
static MethodInfo MethodStringBuilderAppend = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) });
|
||||
static MethodInfo MethodStringBuilderToString = typeof(StringBuilder).GetMethod("ToString", new Type[0]);
|
||||
static PropertyInfo MethodStringBuilderLength = typeof(StringBuilder).GetProperty("Length");
|
||||
static MethodInfo MethodStringConcat = typeof(string).GetMethod("Concat", new Type[] { typeof(object) });
|
||||
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>> _dicGetEntityKeyString = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>>();
|
||||
/// <summary>
|
||||
/// 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值,返回 null
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="_table"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetEntityKeyString<TEntity>(this IFreeSql orm, TEntity item, string splitString = "*|_,[,_|*") {
|
||||
var func = _dicGetEntityKeyString.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, string>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var pks = _table.Primarys;
|
||||
var returnTarget = Expression.Label(typeof(string));
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var var2Sb = Expression.Variable(typeof(StringBuilder));
|
||||
var var3IsNull = Expression.Variable(typeof(bool));
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
|
||||
Expression.Assign(var2Sb, Expression.New(typeof(StringBuilder))),
|
||||
Expression.Assign(var3IsNull, Expression.Constant(false))
|
||||
});
|
||||
for (var a = 0; a < pks.Length; a++) {
|
||||
exps.Add(
|
||||
Expression.IfThen(
|
||||
Expression.IsFalse(var3IsNull),
|
||||
Expression.IfThenElse(
|
||||
Expression.Equal(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), Expression.Default(pks[a].CsType)),
|
||||
Expression.Assign(var3IsNull, Expression.Constant(true)),
|
||||
Expression.Block(
|
||||
new Expression[]{
|
||||
a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null,
|
||||
Expression.Call(var2Sb, MethodStringBuilderAppend,
|
||||
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object))
|
||||
)
|
||||
}.Where(c => c != null).ToArray()
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
exps.Add(
|
||||
Expression.IfThen(
|
||||
Expression.IsFalse(var3IsNull),
|
||||
Expression.Return(returnTarget, Expression.Call(var2Sb, MethodStringBuilderToString))
|
||||
)
|
||||
);
|
||||
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
|
||||
return Expression.Lambda<Func<object, string>>(Expression.Block(new[] { var1Parm, var2Sb, var3IsNull }, exps), new[] { parm1 }).Compile();
|
||||
});
|
||||
return func(item);
|
||||
}
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object[]>>> _dicGetEntityKeyValues = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object[]>>>();
|
||||
/// <summary>
|
||||
/// 获取实体的主键值,多个主键返回数组
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="_table"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static object[] GetEntityKeyValues<TEntity>(this IFreeSql orm, TEntity item) {
|
||||
var func = _dicGetEntityKeyValues.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, object[]>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var pks = _table.Primarys;
|
||||
var returnTarget = Expression.Label(typeof(object[]));
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var var2Ret = Expression.Variable(typeof(object[]));
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
|
||||
Expression.Assign(var2Ret, Expression.NewArrayBounds(typeof(object), Expression.Constant(pks.Length))),
|
||||
});
|
||||
for (var a = 0; a < pks.Length; a++) {
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.ArrayAccess(var2Ret, Expression.Constant(a)),
|
||||
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object))
|
||||
)
|
||||
);
|
||||
}
|
||||
exps.AddRange(new Expression[] {
|
||||
Expression.Return(returnTarget, var2Ret),
|
||||
Expression.Label(returnTarget, Expression.Default(typeof(object[])))
|
||||
});
|
||||
return Expression.Lambda<Func<object, object[]>>(Expression.Block(new[] { var1Parm, var2Ret }, exps), new[] { parm1 }).Compile();
|
||||
});
|
||||
return func(item);
|
||||
}
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>> _dicGetEntityString = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>>();
|
||||
/// <summary>
|
||||
/// 获取实体的所有数据,以 (1, 2, xxx) 的形式
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="_table"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetEntityString<TEntity>(this IFreeSql orm, TEntity item) {
|
||||
var func = _dicGetEntityString.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, string>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var cols = _table.Columns;
|
||||
var returnTarget = Expression.Label(typeof(string));
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var var2Sb = Expression.Variable(typeof(StringBuilder));
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
|
||||
Expression.Assign(var2Sb, Expression.New(typeof(StringBuilder))),
|
||||
Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant("(" ))
|
||||
});
|
||||
var a = 0;
|
||||
foreach (var col in cols.Values) {
|
||||
exps.Add(
|
||||
Expression.Block(
|
||||
new Expression[]{
|
||||
a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(", " )) : null,
|
||||
Expression.Call(var2Sb, MethodStringBuilderAppend,
|
||||
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[col.CsName]), typeof(object))
|
||||
)
|
||||
}.Where(c => c != null).ToArray()
|
||||
)
|
||||
);
|
||||
a++;
|
||||
}
|
||||
exps.AddRange(new Expression[] {
|
||||
Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(")" )),
|
||||
Expression.Return(returnTarget, Expression.Call(var2Sb, MethodStringBuilderToString)),
|
||||
Expression.Label(returnTarget, Expression.Default(typeof(string)))
|
||||
});
|
||||
return Expression.Lambda<Func<object, string>>(Expression.Block(new[] { var1Parm, var2Sb }, exps), new[] { parm1 }).Compile();
|
||||
});
|
||||
return func(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用新实体的值,复盖旧实体的值
|
||||
/// </summary>
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicCopyNewValueToEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>();
|
||||
public static void MapEntityValue<TEntity>(this IFreeSql orm, TEntity from, TEntity to) {
|
||||
var func = _dicCopyNewValueToEntity.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var parm2 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var var2Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
|
||||
Expression.Assign(var2Parm, Expression.TypeAs(parm2, t))
|
||||
});
|
||||
foreach (var prop in _table.Properties.Values) {
|
||||
if (_table.ColumnsByCs.ContainsKey(prop.Name)) {
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.MakeMemberAccess(var2Parm, prop),
|
||||
Expression.MakeMemberAccess(var1Parm, prop)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.MakeMemberAccess(var2Parm, prop),
|
||||
Expression.Default(prop.PropertyType)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return Expression.Lambda<Action<object, object>>(Expression.Block(new[] { var1Parm, var2Parm }, exps), new[] { parm1, parm2 }).Compile();
|
||||
});
|
||||
func(from, to);
|
||||
}
|
||||
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, long>>> _dicSetEntityIdentityValueWithPrimary = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, long>>>();
|
||||
/// <summary>
|
||||
/// 设置实体中主键内的自增字段值(若存在)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="orm"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="idtval"></param>
|
||||
public static void SetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity item, long idtval) {
|
||||
var func = _dicSetEntityIdentityValueWithPrimary.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, long>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var parm2 = Expression.Parameter(typeof(long));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
|
||||
});
|
||||
if (identitys.Any()) {
|
||||
var idts0 = identitys.First();
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.MakeMemberAccess(var1Parm, _table.Properties[idts0.CsName]),
|
||||
Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(idts0.CsType, Expression.Convert(parm2, typeof(object))), idts0.CsType)
|
||||
)
|
||||
);
|
||||
}
|
||||
return Expression.Lambda<Action<object, long>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2 }).Compile();
|
||||
});
|
||||
func(item, idtval);
|
||||
}
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, long>>> _dicGetEntityIdentityValueWithPrimary = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, long>>>();
|
||||
/// <summary>
|
||||
/// 获取实体中主键内的自增字段值(若存在)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="orm"></param>
|
||||
/// <param name="item"></param>
|
||||
public static long GetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity item) {
|
||||
var func = _dicGetEntityIdentityValueWithPrimary.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, long>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
|
||||
var returnTarget = Expression.Label(typeof(long));
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
|
||||
});
|
||||
if (identitys.Any()) {
|
||||
var idts0 = identitys.First();
|
||||
exps.Add(
|
||||
Expression.IfThen(
|
||||
Expression.NotEqual(
|
||||
Expression.MakeMemberAccess(var1Parm, _table.Properties[idts0.CsName]),
|
||||
Expression.Default(idts0.CsType)
|
||||
),
|
||||
Expression.Return(
|
||||
returnTarget,
|
||||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(
|
||||
typeof(long),
|
||||
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[idts0.CsName]), typeof(object))
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(long))));
|
||||
return Expression.Lambda<Func<object, long>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1 }).Compile();
|
||||
});
|
||||
return func(item);
|
||||
}
|
||||
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>> _dicClearEntityPrimaryValueWithIdentityAndGuid = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>>();
|
||||
/// <summary>
|
||||
/// 清除实体的主键值,将自增、Guid类型的主键值清除
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="orm"></param>
|
||||
/// <param name="item"></param>
|
||||
public static void ClearEntityPrimaryValueWithIdentityAndGuid<TEntity>(this IFreeSql orm, TEntity item) {
|
||||
var func = _dicClearEntityPrimaryValueWithIdentityAndGuid.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
|
||||
});
|
||||
foreach (var pk in _table.Primarys) {
|
||||
if (pk.CsType == typeof(Guid) || pk.CsType == typeof(Guid?) ||
|
||||
pk.Attribute.IsIdentity) {
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.MakeMemberAccess(var1Parm, _table.Properties[pk.CsName]),
|
||||
Expression.Default(pk.CsType)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return Expression.Lambda<Action<object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1 }).Compile();
|
||||
});
|
||||
func(item);
|
||||
}
|
||||
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>> _dicCompareEntityValueReturnColumns = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>>();
|
||||
/// <summary>
|
||||
/// 对比两个实体值,返回相同/或不相同的列名
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="orm"></param>
|
||||
/// <param name="up"></param>
|
||||
/// <param name="oldval"></param>
|
||||
/// <returns></returns>
|
||||
public static string[] CompareEntityValueReturnColumns<TEntity>(this IFreeSql orm, TEntity up, TEntity oldval, bool isEqual) {
|
||||
var func = _dicCompareEntityValueReturnColumns.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, object, bool, string[]>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var returnTarget = Expression.Label(typeof(string[]));
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var parm2 = Expression.Parameter(typeof(object));
|
||||
var parm3 = Expression.Parameter(typeof(bool));
|
||||
var var1Ret = Expression.Variable(typeof(List<string>));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var var2Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
|
||||
Expression.Assign(var2Parm, Expression.TypeAs(parm2, t)),
|
||||
Expression.Assign(var1Ret, Expression.New(typeof(List<string>)))
|
||||
});
|
||||
var a = 0;
|
||||
foreach (var prop in _table.Properties.Values) {
|
||||
if (_table.ColumnsByCs.TryGetValue(prop.Name, out var trycol) == false) continue;
|
||||
exps.Add(
|
||||
Expression.IfThenElse(
|
||||
Expression.Equal(
|
||||
Expression.MakeMemberAccess(var1Parm, prop),
|
||||
Expression.MakeMemberAccess(var2Parm, prop)
|
||||
),
|
||||
Expression.IfThen(
|
||||
Expression.IsTrue(parm3),
|
||||
Expression.Call(var1Ret, typeof(List<string>).GetMethod("Add", new Type[] { typeof(string) }), Expression.Constant(trycol.Attribute.Name))
|
||||
),
|
||||
Expression.IfThen(
|
||||
Expression.IsFalse(parm3),
|
||||
Expression.Call(var1Ret, typeof(List<string>).GetMethod("Add", new Type[] { typeof(string) }), Expression.Constant(trycol.Attribute.Name))
|
||||
)
|
||||
)
|
||||
);
|
||||
a++;
|
||||
}
|
||||
exps.Add(Expression.Return(returnTarget, Expression.Call(var1Ret, typeof(List<string>).GetMethod("ToArray", new Type[0]))));
|
||||
exps.Add(Expression.Label(returnTarget, Expression.Constant(new string[0])));
|
||||
return Expression.Lambda<Func<object, object, bool, string[]>>(Expression.Block(new[] { var1Ret, var1Parm, var2Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
|
||||
});
|
||||
return func(up, oldval, isEqual);
|
||||
}
|
||||
|
||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>> _dicSetEntityIncrByWithPropertyName = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>>();
|
||||
/// <summary>
|
||||
/// 设置实体中某属性的数值增加指定的值
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="orm"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="idtval"></param>
|
||||
public static void SetEntityIncrByWithPropertyName<TEntity>(this IFreeSql orm, TEntity item, string propertyName, int incrBy) {
|
||||
var func = _dicSetEntityIncrByWithPropertyName.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, string, int>>()).GetOrAdd(typeof(TEntity), t => {
|
||||
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||
var parm1 = Expression.Parameter(typeof(object));
|
||||
var parm2 = Expression.Parameter(typeof(string));
|
||||
var parm3 = Expression.Parameter(typeof(int));
|
||||
var var1Parm = Expression.Variable(t);
|
||||
var exps = new List<Expression>(new Expression[] {
|
||||
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
|
||||
});
|
||||
if (_table.Properties.ContainsKey(propertyName)) {
|
||||
var prop = _table.Properties[propertyName];
|
||||
exps.Add(
|
||||
Expression.Assign(
|
||||
Expression.MakeMemberAccess(var1Parm, prop),
|
||||
Expression.Add(
|
||||
Expression.MakeMemberAccess(var1Parm, prop),
|
||||
Expression.Convert(
|
||||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Convert(parm3, typeof(object))),
|
||||
prop.PropertyType
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
return Expression.Lambda<Action<object, string, int>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
|
||||
});
|
||||
func(item, propertyName, incrBy);
|
||||
}
|
||||
|
||||
static ConcurrentDictionary<Type, MethodInfo[]> _dicAppendEntityUpdateSetWithColumnMethods = new ConcurrentDictionary<Type, MethodInfo[]>();
|
||||
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, MethodInfo>> _dicAppendEntityUpdateSetWithColumnMethod = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, MethodInfo>>();
|
||||
/// <summary>
|
||||
/// 缓存执行 IUpdate.Set
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="columnType"></param>
|
||||
/// <param name="setExp"></param>
|
||||
public static void AppendEntityUpdateSetWithColumn<TEntity>(this IUpdate<TEntity> update, Type columnType, LambdaExpression setExp) where TEntity : class {
|
||||
|
||||
var setMethod = _dicAppendEntityUpdateSetWithColumnMethod.GetOrAdd(typeof(IUpdate<TEntity>), uptp => new ConcurrentDictionary<Type, MethodInfo>()).GetOrAdd(columnType, coltp => {
|
||||
var allMethods = _dicAppendEntityUpdateSetWithColumnMethods.GetOrAdd(typeof(IUpdate<TEntity>), uptp => uptp.GetMethods());
|
||||
return allMethods.Where(a => a.Name == "Set" && a.IsGenericMethod && a.GetParameters().Length == 1 && a.GetGenericArguments().First().Name == "TMember").FirstOrDefault()
|
||||
.MakeGenericMethod(columnType);
|
||||
});
|
||||
|
||||
setMethod.Invoke(update, new object[] { setExp });
|
||||
}
|
||||
}
|
||||
}
|
@ -101,13 +101,6 @@ 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:默认表名;返回值:新表名;
|
||||
|
@ -95,10 +95,16 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
}
|
||||
internal int SplitExecuteAffrows(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return 0;
|
||||
if (ss.Length == 1) return this.RawExecuteAffrows();
|
||||
|
||||
var ret = 0;
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = this.RawExecuteAffrows();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -120,14 +126,21 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
async internal Task<int> SplitExecuteAffrowsAsync(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return 0;
|
||||
if (ss.Length == 1) return await this.RawExecuteAffrowsAsync();
|
||||
|
||||
var ret = 0;
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = await this.RawExecuteAffrowsAsync();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -149,14 +162,21 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
internal long SplitExecuteIdentity(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return 0;
|
||||
if (ss.Length == 1) return this.RawExecuteIdentity();
|
||||
|
||||
long ret = 0;
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = this.RawExecuteIdentity();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -180,14 +200,21 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
async internal Task<long> SplitExecuteIdentityAsync(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return 0;
|
||||
if (ss.Length == 1) return await this.RawExecuteIdentityAsync();
|
||||
|
||||
long ret = 0;
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = await this.RawExecuteIdentityAsync();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -211,14 +238,21 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
internal List<T1> SplitExecuteInserted(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return new List<T1>();
|
||||
if (ss.Length == 1) return this.RawExecuteInserted();
|
||||
|
||||
var ret = new List<T1>();
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = this.RawExecuteInserted();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -240,14 +274,21 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
async internal Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
if (ss.Any() == false) return new List<T1>();
|
||||
if (ss.Length == 1) return await this.RawExecuteInsertedAsync();
|
||||
|
||||
var ret = new List<T1>();
|
||||
if (ss.Any() == false) {
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (ss.Length == 1) {
|
||||
ret = await this.RawExecuteInsertedAsync();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
@ -269,27 +310,20 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
#endregion
|
||||
|
||||
internal int RawExecuteAffrows() {
|
||||
var affrows = _orm.Ado.ExecuteNonQuery(_transaction, CommandType.Text, ToSql(), _params);
|
||||
this.ClearData();
|
||||
return affrows;
|
||||
}
|
||||
async internal Task<int> RawExecuteAffrowsAsync() {
|
||||
var affrows = await _orm.Ado.ExecuteNonQueryAsync(_transaction, CommandType.Text, ToSql(), _params);
|
||||
this.ClearData();
|
||||
return affrows;
|
||||
}
|
||||
internal int RawExecuteAffrows() => _orm.Ado.ExecuteNonQuery(_transaction, CommandType.Text, ToSql(), _params);
|
||||
internal Task<int> RawExecuteAffrowsAsync() => _orm.Ado.ExecuteNonQueryAsync(_transaction, CommandType.Text, ToSql(), _params);
|
||||
internal abstract long RawExecuteIdentity();
|
||||
internal abstract Task<long> RawExecuteIdentityAsync();
|
||||
internal abstract List<T1> RawExecuteInserted();
|
||||
internal abstract Task<List<T1>> RawExecuteInsertedAsync();
|
||||
|
||||
public virtual int ExecuteAffrows() => RawExecuteAffrows();
|
||||
public virtual Task<int> ExecuteAffrowsAsync() => RawExecuteAffrowsAsync();
|
||||
public abstract int ExecuteAffrows();
|
||||
public abstract Task<int> ExecuteAffrowsAsync();
|
||||
public abstract long ExecuteIdentity();
|
||||
public abstract Task<long> ExecuteIdentityAsync();
|
||||
public abstract List<T1> ExecuteInserted();
|
||||
|
@ -1,4 +1,5 @@
|
||||
using FreeSql.Internal.Model;
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -55,20 +56,197 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
return this;
|
||||
}
|
||||
|
||||
public int ExecuteAffrows() {
|
||||
protected void ValidateVersionAndThrow(int affrows) {
|
||||
if (_table.VersionColumn != null && _source.Count > 0) {
|
||||
if (affrows != _source.Count)
|
||||
throw new Exception($"记录可能不存在,或者【行级乐观锁】版本过旧,更新数量{_source.Count},影响的行数{affrows}。");
|
||||
foreach (var d in _source)
|
||||
_orm.SetEntityIncrByWithPropertyName(d, _table.VersionColumn.CsName, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#region 参数化数据限制,或values数量限制
|
||||
internal List<T1>[] SplitSource(int valuesLimit, int parameterLimit) {
|
||||
valuesLimit = valuesLimit - 1;
|
||||
parameterLimit = parameterLimit - 1;
|
||||
if (_source == null || _source.Any() == false) return new List<T1>[0];
|
||||
if (_source.Count == 1) return new[] { _source };
|
||||
if (_noneParameter) {
|
||||
if (_source.Count < valuesLimit) return new[] { _source };
|
||||
|
||||
var execCount = (int)Math.Ceiling(1.0 * _source.Count / valuesLimit);
|
||||
var ret = new List<T1>[execCount];
|
||||
for (var a = 0; a < execCount; a++) {
|
||||
var subSource = new List<T1>();
|
||||
subSource = _source.GetRange(a * valuesLimit, Math.Min(valuesLimit, _source.Count - a * valuesLimit));
|
||||
ret[a] = subSource;
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
var colSum = _table.Columns.Count - _ignore.Count;
|
||||
var takeMax = parameterLimit / colSum;
|
||||
var pamTotal = colSum * _source.Count;
|
||||
if (pamTotal < parameterLimit) return new[] { _source };
|
||||
|
||||
var execCount = (int)Math.Ceiling(1.0 * pamTotal / parameterLimit);
|
||||
var ret = new List<T1>[execCount];
|
||||
for (var a = 0; a < execCount; a++) {
|
||||
var subSource = new List<T1>();
|
||||
subSource = _source.GetRange(a * takeMax, Math.Min(takeMax, _source.Count - a * takeMax));
|
||||
ret[a] = subSource;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
internal int SplitExecuteAffrows(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
var ret = 0;
|
||||
if (ss.Length <= 1) {
|
||||
ret = this.RawExecuteAffrows();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret += this.RawExecuteAffrows();
|
||||
}
|
||||
} else {
|
||||
using (var conn = _orm.Ado.MasterPool.Get()) {
|
||||
_transaction = conn.Value.BeginTransaction();
|
||||
try {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret += this.RawExecuteAffrows();
|
||||
}
|
||||
_transaction.Commit();
|
||||
} catch {
|
||||
_transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
async internal Task<int> SplitExecuteAffrowsAsync(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
var ret = 0;
|
||||
if (ss.Length <= 1) {
|
||||
ret = await this.RawExecuteAffrowsAsync();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret += await this.RawExecuteAffrowsAsync();
|
||||
}
|
||||
} else {
|
||||
using (var conn = await _orm.Ado.MasterPool.GetAsync()) {
|
||||
_transaction = conn.Value.BeginTransaction();
|
||||
try {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret += await this.RawExecuteAffrowsAsync();
|
||||
}
|
||||
_transaction.Commit();
|
||||
} catch {
|
||||
_transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
internal List<T1> SplitExecuteUpdated(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
var ret = new List<T1>();
|
||||
if (ss.Length <= 1) {
|
||||
ret = this.RawExecuteUpdated();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret.AddRange(this.RawExecuteUpdated());
|
||||
}
|
||||
} else {
|
||||
using (var conn = _orm.Ado.MasterPool.Get()) {
|
||||
_transaction = conn.Value.BeginTransaction();
|
||||
try {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret.AddRange(this.RawExecuteUpdated());
|
||||
}
|
||||
_transaction.Commit();
|
||||
} catch {
|
||||
_transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
async internal Task<List<T1>> SplitExecuteUpdatedAsync(int valuesLimit, int parameterLimit) {
|
||||
var ss = SplitSource(valuesLimit, parameterLimit);
|
||||
var ret = new List<T1>();
|
||||
if (ss.Length <= 1) {
|
||||
ret = await this.RawExecuteUpdatedAsync();
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
if (_transaction != null) {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret.AddRange(await this.RawExecuteUpdatedAsync());
|
||||
}
|
||||
} else {
|
||||
using (var conn = await _orm.Ado.MasterPool.GetAsync()) {
|
||||
_transaction = conn.Value.BeginTransaction();
|
||||
try {
|
||||
for (var a = 0; a < ss.Length; a++) {
|
||||
_source = ss[a];
|
||||
ret.AddRange(await this.RawExecuteUpdatedAsync());
|
||||
}
|
||||
_transaction.Commit();
|
||||
} catch {
|
||||
_transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
ClearData();
|
||||
return ret;
|
||||
}
|
||||
#endregion
|
||||
|
||||
internal int RawExecuteAffrows() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
var affrows = _orm.Ado.ExecuteNonQuery(_transaction, CommandType.Text, sql, _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(affrows);
|
||||
return affrows;
|
||||
}
|
||||
async public Task<int> ExecuteAffrowsAsync() {
|
||||
async internal Task<int> RawExecuteAffrowsAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
var affrows = await _orm.Ado.ExecuteNonQueryAsync(_transaction, CommandType.Text, sql, _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(affrows);
|
||||
return affrows;
|
||||
}
|
||||
internal abstract List<T1> RawExecuteUpdated();
|
||||
internal abstract Task<List<T1>> RawExecuteUpdatedAsync();
|
||||
|
||||
public abstract int ExecuteAffrows();
|
||||
public abstract Task<int> ExecuteAffrowsAsync();
|
||||
public abstract List<T1> ExecuteUpdated();
|
||||
public abstract Task<List<T1>> ExecuteUpdatedAsync();
|
||||
|
||||
@ -88,7 +266,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
public IUpdate<T1> SetSource(IEnumerable<T1> source) {
|
||||
if (source == null || source.Any() == false) return this;
|
||||
_source.AddRange(source.Where(a => a != null));
|
||||
return this.Where(_source);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value) {
|
||||
@ -144,12 +322,12 @@ 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;
|
||||
protected string WhereCaseSource(string CsName, Func<string, string> thenValue) {
|
||||
if (_source.Any() == false) return null;
|
||||
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 == 0) return null;
|
||||
if (_source.Count == 1) {
|
||||
|
||||
var col = _table.ColumnsByCs[CsName];
|
||||
@ -159,7 +337,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
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());
|
||||
return sb.ToString();
|
||||
|
||||
} else {
|
||||
var caseWhen = new StringBuilder();
|
||||
@ -169,17 +347,24 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
|
||||
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");
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
|
||||
return this.Where(sb.ToString());
|
||||
var isnull = false;
|
||||
var cwsb = new StringBuilder().Append(cw);
|
||||
foreach (var d in _source) {
|
||||
cwsb.Append(" \r\nWHEN ");
|
||||
ToSqlWhen(cwsb, _table.Primarys, d);
|
||||
cwsb.Append(" THEN ");
|
||||
var value = _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value;
|
||||
cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.CsType, value)));
|
||||
if (isnull == false) isnull = value == null || value == DBNull.Value;
|
||||
}
|
||||
cwsb.Append(" END");
|
||||
if (isnull == false) sb.Append(cwsb.ToString());
|
||||
else sb.Append("NULL");
|
||||
cwsb.Clear();
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,7 +376,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
return this;
|
||||
}
|
||||
public string ToSql() {
|
||||
if (_where.Length == 0) return null;
|
||||
if (_where.Length == 0 && _source.Any() == false) return null;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("UPDATE ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append(" SET ");
|
||||
@ -224,14 +409,6 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
var caseWhen = new StringBuilder();
|
||||
caseWhen.Append("CASE ");
|
||||
ToSqlCase(caseWhen, _table.Primarys);
|
||||
//if (_table.Primarys.Length > 1) caseWhen.Append("CONCAT(");
|
||||
//var pkidx = 0;
|
||||
//foreach (var pk in _table.Primarys) {
|
||||
// if (pkidx > 0) caseWhen.Append(", ");
|
||||
// caseWhen.Append(_commonUtils.QuoteSqlName(pk.Attribute.Name));
|
||||
// ++pkidx;
|
||||
//}
|
||||
//if (_table.Primarys.Length > 1) caseWhen.Append(")");
|
||||
var cw = caseWhen.ToString();
|
||||
|
||||
_paramsSource.Clear();
|
||||
@ -239,37 +416,56 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
foreach (var col in _table.Columns.Values) {
|
||||
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.CsName) == false) {
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(cw);
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
|
||||
var isnull = false;
|
||||
var cwsb = new StringBuilder().Append(cw);
|
||||
foreach (var d in _source) {
|
||||
sb.Append(" \r\nWHEN ");
|
||||
ToSqlWhen(sb, _table.Primarys, d);
|
||||
//if (_table.Primarys.Length > 1) sb.Append("CONCAT(");
|
||||
//pkidx = 0;
|
||||
//foreach (var pk in _table.Primarys) {
|
||||
// if (pkidx > 0) sb.Append(", ");
|
||||
// sb.Append(_commonUtils.FormatSql("{0}", _table.Properties.TryGetValue(pk.CsName, out var tryp2) ? tryp2.GetValue(d) : null));
|
||||
// ++pkidx;
|
||||
//}
|
||||
//if (_table.Primarys.Length > 1) sb.Append(")");
|
||||
sb.Append(" THEN ");
|
||||
cwsb.Append(" \r\nWHEN ");
|
||||
ToSqlWhen(cwsb, _table.Primarys, d);
|
||||
cwsb.Append(" THEN ");
|
||||
var value = _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value;
|
||||
if (_noneParameter) {
|
||||
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.CsType, value));
|
||||
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.CsType, value));
|
||||
} else {
|
||||
sb.Append(_commonUtils.QuoteWriteParamter(col.CsType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
|
||||
cwsb.Append(_commonUtils.QuoteWriteParamter(col.CsType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
|
||||
_commonUtils.AppendParamter(_paramsSource, null, col.CsType, value);
|
||||
}
|
||||
if (isnull == false) isnull = value == null || value == DBNull.Value;
|
||||
}
|
||||
sb.Append(" END");
|
||||
cwsb.Append(" END");
|
||||
if (isnull == false) sb.Append(cwsb.ToString());
|
||||
else sb.Append("NULL");
|
||||
cwsb.Clear();
|
||||
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
if (colidx == 0) return null;
|
||||
} else
|
||||
} else if (_setIncr.Length == 0)
|
||||
return null;
|
||||
|
||||
sb.Append(_setIncr.ToString());
|
||||
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5));
|
||||
if (_setIncr.Length > 0)
|
||||
sb.Append(_set.Length > 0 ? _setIncr.ToString() : _setIncr.ToString().Substring(2));
|
||||
|
||||
if (_table.VersionColumn != null) {
|
||||
var vcname = _commonUtils.QuoteSqlName(_table.VersionColumn.Attribute.Name);
|
||||
sb.Append(", ").Append(vcname).Append(" = ").Append(vcname).Append(" + 1");
|
||||
}
|
||||
|
||||
sb.Append(" \r\nWHERE ");
|
||||
if (_source.Any())
|
||||
sb.Append("(").Append(_commonUtils.WhereItems(_table, "", _source)).Append(")");
|
||||
|
||||
if (_where.Length > 0)
|
||||
sb.Append(_source.Any() ? _where.ToString() : _where.ToString().Substring(5));
|
||||
|
||||
if (_table.VersionColumn != null) {
|
||||
var versionCondi = WhereCaseSource(_table.VersionColumn.CsName, sqlval => sqlval);
|
||||
if (string.IsNullOrEmpty(versionCondi) == false)
|
||||
sb.Append(" AND ").Append(versionCondi);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.DatabaseModel;
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections;
|
||||
@ -79,6 +80,7 @@ namespace FreeSql.Internal {
|
||||
if (attr._IsIdentity == null) attr._IsIdentity = trycol.IsIdentity;
|
||||
if (attr._IsNullable == null) attr._IsNullable = trycol.IsNullable;
|
||||
if (attr._IsIgnore == null) attr._IsIgnore = trycol.IsIgnore;
|
||||
if (attr._IsVersion == null) attr._IsVersion = trycol.IsVersion;
|
||||
if (attr.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue;
|
||||
return attr;
|
||||
}
|
||||
@ -136,7 +138,8 @@ namespace FreeSql.Internal {
|
||||
if (table.Primarys.Length == 1) {
|
||||
var sbin = new StringBuilder();
|
||||
sbin.Append(aliasAndDot).Append(this.QuoteSqlName(table.Primarys.First().Attribute.Name));
|
||||
var indt = its.Select(a => table.Properties.TryGetValue(table.Primarys.First().CsName, out var trycol) ? this.FormatSql("{0}", trycol.GetValue(a)) : null).Where(a => a != null).ToArray();
|
||||
var indt = its.Select(a => /*this.FormatSql("{0}", _orm.GetEntityKeyValues(a))*/
|
||||
table.Properties.TryGetValue(table.Primarys.First().CsName, out var trycol) ? this.FormatSql("{0}", trycol.GetValue(a)) : null).Where(a => a != null).ToArray();
|
||||
if (indt.Any() == false) return null;
|
||||
if (indt.Length == 1) sbin.Append(" = ").Append(indt.First());
|
||||
else sbin.Append(" IN (").Append(string.Join(",", indt)).Append(")");
|
||||
|
@ -17,6 +17,7 @@ namespace FreeSql.Internal.Model {
|
||||
public string DbOldName { get; set; }
|
||||
public string SelectFilter { get; set; }
|
||||
|
||||
public ColumnInfo VersionColumn { get; set; }
|
||||
|
||||
ConcurrentDictionary<string, TableRef> _refs { get; } = new ConcurrentDictionary<string, TableRef>(StringComparer.CurrentCultureIgnoreCase);
|
||||
|
||||
|
@ -107,6 +107,11 @@ namespace FreeSql.Internal {
|
||||
trytb.Columns.Add(colattr.Name, col);
|
||||
trytb.ColumnsByCs.Add(p.Name, col);
|
||||
}
|
||||
trytb.VersionColumn = trytb.Columns.Values.Where(a => a.Attribute.IsVersion == true).LastOrDefault();
|
||||
if (trytb.VersionColumn != null) {
|
||||
if (trytb.VersionColumn.CsType.IsNullableType() || trytb.VersionColumn.CsType.IsNumberType() == false)
|
||||
throw new Exception($"属性{trytb.VersionColumn.CsName} 被标注为行锁(乐观锁)(IsVersion),但其必须为数字类型,并且不可为 Nullable");
|
||||
}
|
||||
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
|
||||
if (trytb.Primarys.Any() == false) {
|
||||
var identcol = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).FirstOrDefault();
|
||||
|
@ -23,17 +23,13 @@ namespace FreeSql.MySql.Curd {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT LAST_INSERT_ID();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT LAST_INSERT_ID();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
async internal override Task<long> RawExecuteIdentityAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT LAST_INSERT_ID();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT LAST_INSERT_ID();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
internal override List<T1> RawExecuteInserted() {
|
||||
var sql = this.ToSql();
|
||||
@ -48,9 +44,7 @@ namespace FreeSql.MySql.Curd {
|
||||
sb.Append(_commonUtils.QuoteReadColumn(col.CsType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
async internal override Task<List<T1>> RawExecuteInsertedAsync() {
|
||||
var sql = this.ToSql();
|
||||
@ -65,9 +59,7 @@ namespace FreeSql.MySql.Curd {
|
||||
sb.Append(_commonUtils.QuoteReadColumn(col.CsType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,13 @@ namespace FreeSql.MySql.Curd {
|
||||
: base(orm, commonUtils, commonExpression, dywhere) {
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteUpdated() {
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(500, 3000);
|
||||
public override Task<int> ExecuteAffrowsAsync() => base.SplitExecuteAffrowsAsync(500, 3000);
|
||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(500, 3000);
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() => base.SplitExecuteUpdatedAsync(500, 3000);
|
||||
|
||||
|
||||
internal override List<T1> RawExecuteUpdated() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -28,10 +34,10 @@ namespace FreeSql.MySql.Curd {
|
||||
++colidx;
|
||||
}
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
async public override Task<List<T1>> ExecuteUpdatedAsync() {
|
||||
async internal override Task<List<T1>> RawExecuteUpdatedAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -45,7 +51,7 @@ namespace FreeSql.MySql.Curd {
|
||||
++colidx;
|
||||
}
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -95,9 +95,7 @@ namespace FreeSql.Oracle.Curd {
|
||||
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.CsType, 0) as OracleParameter;
|
||||
identParam.Direction = ParameterDirection.Output;
|
||||
_orm.Ado.ExecuteNonQuery(_transaction, CommandType.Text, $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}", _params.Concat(new[] { identParam }).ToArray());
|
||||
var id = long.TryParse(string.Concat(identParam.Value), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(identParam.Value), out var trylng) ? trylng : 0;
|
||||
}
|
||||
async internal override Task<long> RawExecuteIdentityAsync() {
|
||||
var sql = this.ToSql();
|
||||
@ -111,25 +109,21 @@ namespace FreeSql.Oracle.Curd {
|
||||
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.CsType, 0) as OracleParameter;
|
||||
identParam.Direction = ParameterDirection.Output;
|
||||
await _orm.Ado.ExecuteNonQueryAsync(_transaction, CommandType.Text, $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}", _params.Concat(new[] { identParam }).ToArray());
|
||||
var id = long.TryParse(string.Concat(identParam.Value), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(identParam.Value), out var trylng) ? trylng : 0;
|
||||
}
|
||||
|
||||
internal override List<T1> RawExecuteInserted() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
this.ExecuteAffrows();
|
||||
this.ClearData();
|
||||
this.RawExecuteAffrows();
|
||||
return _source;
|
||||
}
|
||||
async internal override Task<List<T1>> RawExecuteInsertedAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
await this.ExecuteAffrowsAsync();
|
||||
this.ClearData();
|
||||
await this.RawExecuteAffrowsAsync();
|
||||
return _source;
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,16 @@ namespace FreeSql.Oracle.Curd {
|
||||
: base(orm, commonUtils, commonExpression, dywhere) {
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteUpdated() {
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(200, 999);
|
||||
public override Task<int> ExecuteAffrowsAsync() => base.SplitExecuteAffrowsAsync(200, 999);
|
||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(200, 999);
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() => base.SplitExecuteUpdatedAsync(200, 999);
|
||||
|
||||
|
||||
internal override List<T1> RawExecuteUpdated() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() {
|
||||
internal override Task<List<T1>> RawExecuteUpdatedAsync() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
@ -29,9 +29,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
_orm.Ado.ExecuteNonQuery(_transaction, CommandType.Text, sql, _params);
|
||||
return 0;
|
||||
}
|
||||
var id = long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, " RETURNING ", _commonUtils.QuoteSqlName(identCols.First().Value.Attribute.Name)), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, " RETURNING ", _commonUtils.QuoteSqlName(identCols.First().Value.Attribute.Name)), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
async internal override Task<long> RawExecuteIdentityAsync() {
|
||||
var sql = this.ToSql();
|
||||
@ -42,9 +40,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
await _orm.Ado.ExecuteNonQueryAsync(_transaction, CommandType.Text, sql, _params);
|
||||
return 0;
|
||||
}
|
||||
var id = long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, " RETURNING ", _commonUtils.QuoteSqlName(identCols.First().Value.Attribute.Name)), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, " RETURNING ", _commonUtils.QuoteSqlName(identCols.First().Value.Attribute.Name)), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
|
||||
internal override List<T1> RawExecuteInserted() {
|
||||
@ -60,9 +56,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
sb.Append(_commonUtils.QuoteReadColumn(col.CsType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
async internal override Task<List<T1>> RawExecuteInsertedAsync() {
|
||||
var sql = this.ToSql();
|
||||
@ -77,9 +71,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
sb.Append(_commonUtils.QuoteReadColumn(col.CsType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,13 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
: base(orm, commonUtils, commonExpression, dywhere) {
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteUpdated() {
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(500, 3000);
|
||||
public override Task<int> ExecuteAffrowsAsync() => base.SplitExecuteAffrowsAsync(500, 3000);
|
||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(500, 3000);
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() => base.SplitExecuteUpdatedAsync(500, 3000);
|
||||
|
||||
|
||||
internal override List<T1> RawExecuteUpdated() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -28,10 +34,10 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
++colidx;
|
||||
}
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
async public override Task<List<T1>> ExecuteUpdatedAsync() {
|
||||
async internal override Task<List<T1>> RawExecuteUpdatedAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -45,7 +51,7 @@ namespace FreeSql.PostgreSQL.Curd {
|
||||
++colidx;
|
||||
}
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -26,17 +26,13 @@ namespace FreeSql.SqlServer.Curd {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT SCOPE_IDENTITY();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT SCOPE_IDENTITY();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
async internal override Task<long> RawExecuteIdentityAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT SCOPE_IDENTITY();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT SCOPE_IDENTITY();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
|
||||
internal override List<T1> RawExecuteInserted() {
|
||||
@ -57,9 +53,7 @@ namespace FreeSql.SqlServer.Curd {
|
||||
sb.Insert(0, sql.Substring(0, validx + 1));
|
||||
sb.Append(sql.Substring(validx + 1));
|
||||
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
async internal override Task<List<T1>> RawExecuteInsertedAsync() {
|
||||
var sql = this.ToSql();
|
||||
@ -79,9 +73,7 @@ namespace FreeSql.SqlServer.Curd {
|
||||
sb.Insert(0, sql.Substring(0, validx + 1));
|
||||
sb.Append(sql.Substring(validx + 1));
|
||||
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
this.ClearData();
|
||||
return ret;
|
||||
return await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params);
|
||||
}
|
||||
}
|
||||
}
|
@ -15,7 +15,13 @@ namespace FreeSql.SqlServer.Curd {
|
||||
: base(orm, commonUtils, commonExpression, dywhere) {
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteUpdated() {
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(500, 2100);
|
||||
public override Task<int> ExecuteAffrowsAsync() => base.SplitExecuteAffrowsAsync(500, 2100);
|
||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(500, 2100);
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() => base.SplitExecuteUpdatedAsync(500, 2100);
|
||||
|
||||
|
||||
internal override List<T1> RawExecuteUpdated() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -34,10 +40,10 @@ namespace FreeSql.SqlServer.Curd {
|
||||
sb.Append(sql.Substring(validx));
|
||||
|
||||
var ret = _orm.Ado.Query<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
async public override Task<List<T1>> ExecuteUpdatedAsync() {
|
||||
async internal override Task<List<T1>> RawExecuteUpdatedAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
@ -56,7 +62,7 @@ namespace FreeSql.SqlServer.Curd {
|
||||
sb.Append(sql.Substring(validx));
|
||||
|
||||
var ret = await _orm.Ado.QueryAsync<T1>(_transaction, CommandType.Text, sb.ToString(), _params.Concat(_paramsSource).ToArray());
|
||||
this.ClearData();
|
||||
ValidateVersionAndThrow(ret.Count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -25,32 +25,26 @@ namespace FreeSql.Sqlite.Curd {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT last_insert_rowid();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_transaction, CommandType.Text, string.Concat(sql, "; SELECT last_insert_rowid();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
async internal override Task<long> RawExecuteIdentityAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
|
||||
var id = long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT last_insert_rowid();"), _params)), out var trylng) ? trylng : 0;
|
||||
this.ClearData();
|
||||
return id;
|
||||
return long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_transaction, CommandType.Text, string.Concat(sql, "; SELECT last_insert_rowid();"), _params)), out var trylng) ? trylng : 0;
|
||||
}
|
||||
internal override List<T1> RawExecuteInserted() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
this.ExecuteAffrows();
|
||||
this.ClearData();
|
||||
this.RawExecuteAffrows();
|
||||
return _source;
|
||||
}
|
||||
async internal override Task<List<T1>> RawExecuteInsertedAsync() {
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
await this.ExecuteAffrowsAsync();
|
||||
this.ClearData();
|
||||
await this.RawExecuteAffrowsAsync();
|
||||
return _source;
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,16 @@ namespace FreeSql.Sqlite.Curd {
|
||||
: base(orm, commonUtils, commonExpression, dywhere) {
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteUpdated() {
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(200, 999);
|
||||
public override Task<int> ExecuteAffrowsAsync() => base.SplitExecuteAffrowsAsync(200, 999);
|
||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(200, 999);
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() => base.SplitExecuteUpdatedAsync(200, 999);
|
||||
|
||||
|
||||
internal override List<T1> RawExecuteUpdated() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public override Task<List<T1>> ExecuteUpdatedAsync() {
|
||||
internal override Task<List<T1>> RawExecuteUpdatedAsync() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user