mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 03:53:21 +08:00
## v0.3.22
- 优化 导航属性 ManyToOne 名称查找规则; - 增加 IFreeSql.Aop 属性,未来所有拦截方法都在这里,第一期支持如下: * 监控 ToList 返回的的数据,用于拦截重新装饰; * 监视 Where,包括 select/update/delete,返回值 true 时可使上层不被执行; * 可自定义解析表达式; - 增加 ISelect.TractToList,用于单次跟踪或审核实体; - 优化 FreeSql.DbContext SaveChanges;
This commit is contained in:
parent
c20a0bbd54
commit
7f3aa84ffe
@ -28,6 +28,8 @@ namespace dbcontext_01.Controllers
|
|||||||
try {
|
try {
|
||||||
using (var ctx = new SongContext()) {
|
using (var ctx = new SongContext()) {
|
||||||
|
|
||||||
|
ctx.Songs.Select.Where(a => a.Id > 10).ToList();
|
||||||
|
|
||||||
var song = new Song { };
|
var song = new Song { };
|
||||||
ctx.Songs.Add(song);
|
ctx.Songs.Add(song);
|
||||||
id = song.Id;
|
id = song.Id;
|
||||||
@ -90,7 +92,7 @@ namespace dbcontext_01.Controllers
|
|||||||
var item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
var item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
||||||
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
|
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
|
||||||
|
|
||||||
return item22.Title;
|
return item22.Id.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET api/values/5
|
// GET api/values/5
|
||||||
|
@ -8,7 +8,7 @@ namespace dbcontext_01 {
|
|||||||
public class SongContext : DbContext {
|
public class SongContext : DbContext {
|
||||||
|
|
||||||
public DbSet<Song> Songs { get; set; }
|
public DbSet<Song> Songs { get; set; }
|
||||||
public DbSet<Song> Tags { get; set; }
|
public DbSet<Tag> Tags { get; set; }
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
builder.UseFreeSql(dbcontext_01.Startup.Fsql);
|
builder.UseFreeSql(dbcontext_01.Startup.Fsql);
|
||||||
|
@ -32,8 +32,10 @@ namespace FreeSql {
|
|||||||
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
|
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
|
||||||
|
|
||||||
prop.SetValue(this, set);
|
prop.SetValue(this, set);
|
||||||
AllSets.Add(prop, set);
|
AllSets.Add(prop.Name, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//_fsql.Aop.ToList += AopToList;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
@ -43,7 +45,7 @@ namespace FreeSql {
|
|||||||
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
||||||
public object Set(Type entityType) => Activator.CreateInstance(typeof(BaseDbSet<>).MakeGenericType(entityType), this);
|
public object Set(Type entityType) => Activator.CreateInstance(typeof(BaseDbSet<>).MakeGenericType(entityType), this);
|
||||||
|
|
||||||
protected Dictionary<PropertyInfo, object> AllSets => new Dictionary<PropertyInfo, object>();
|
protected Dictionary<string, object> AllSets { get; } = new Dictionary<string, object>();
|
||||||
|
|
||||||
public long SaveChanges() {
|
public long SaveChanges() {
|
||||||
ExecCommand();
|
ExecCommand();
|
||||||
@ -53,91 +55,61 @@ namespace FreeSql {
|
|||||||
|
|
||||||
internal class ExecCommandInfo {
|
internal class ExecCommandInfo {
|
||||||
public ExecCommandInfoType actionType { get; set; }
|
public ExecCommandInfoType actionType { get; set; }
|
||||||
public Type entityType { get; set; }
|
|
||||||
public object dbSet { get; set; }
|
public object dbSet { get; set; }
|
||||||
|
public Type stateType { get; set; }
|
||||||
public object state { get; set; }
|
public object state { get; set; }
|
||||||
}
|
}
|
||||||
internal enum ExecCommandInfoType { Insert, Update, Delete }
|
internal enum ExecCommandInfoType { Insert, Update, Delete }
|
||||||
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
|
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
|
||||||
internal long _affrows = 0;
|
internal long _affrows = 0;
|
||||||
|
|
||||||
internal void EnqueueAction(ExecCommandInfoType actionType, Type entityType, object dbSet, object state) {
|
internal void EnqueueAction(ExecCommandInfoType actionType, object dbSet, Type stateType, object state) {
|
||||||
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, entityType = entityType, dbSet = dbSet, state = state });
|
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], int>> _dicExecCommandInsert = new ConcurrentDictionary<Type, Func<object, object[], int>>();
|
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], int>> _dicExecCommandDelete = new ConcurrentDictionary<Type, Func<object, object[], int>>();
|
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], bool, int>> _dicExecCommandUpdate = new ConcurrentDictionary<Type, Func<object, object[], bool, int>>();
|
|
||||||
internal void ExecCommand() {
|
internal void ExecCommand() {
|
||||||
ExecCommandInfo oldinfo = null;
|
ExecCommandInfo oldinfo = null;
|
||||||
var states = new List<object>();
|
var states = new List<object>();
|
||||||
|
|
||||||
Action funcInsert = () => {
|
Func<string, int> dbContextBetch = methodName => {
|
||||||
var insertFunc = _dicExecCommandInsert.GetOrAdd(oldinfo.entityType, t => {
|
if (_dicExecCommandDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
|
||||||
var arrType = t.MakeArrayType();
|
trydic = new Dictionary<string, Func<object, object[], int>>();
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
|
||||||
var dbsetTypeInsert = dbsetType.GetMethod("OrmInsert", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
var arrType = oldinfo.stateType.MakeArrayType();
|
||||||
var insertBuilder = typeof(IInsert<>).MakeGenericType(t);
|
var dbsetType = oldinfo.dbSet.GetType().BaseType;
|
||||||
var insertExecuteAffrows = insertBuilder.GetMethod("ExecuteAffrows", new Type[0]);
|
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
||||||
|
|
||||||
var returnTarget = Expression.Label(typeof(int));
|
var returnTarget = Expression.Label(typeof(int));
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
var parm1DbSet = Expression.Parameter(typeof(object));
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
var parm2Vals = Expression.Parameter(typeof(object[]));
|
||||||
var var1Vals = Expression.Variable(arrType);
|
var var1Vals = Expression.Variable(arrType);
|
||||||
return Expression.Lambda<Func<object, object[], int>>(Expression.Block(
|
tryfunc = Expression.Lambda<Func<object, object[], int>>(Expression.Block(
|
||||||
new[] { var1Vals },
|
new[] { var1Vals },
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
||||||
Expression.Return(returnTarget,
|
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
|
||||||
Expression.Call(
|
|
||||||
Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
|
|
||||||
insertExecuteAffrows
|
|
||||||
)
|
|
||||||
),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
), new[] { parm1DbSet, parm2Vals }).Compile();
|
||||||
});
|
trydic.Add(methodName, tryfunc);
|
||||||
_affrows += insertFunc(oldinfo.dbSet, states.ToArray());
|
}
|
||||||
states.Clear();
|
return tryfunc(oldinfo.dbSet, states.ToArray());
|
||||||
};
|
};
|
||||||
Action funcDelete = () => {
|
Action funcDelete = () => {
|
||||||
var deleteFunc = _dicExecCommandDelete.GetOrAdd(oldinfo.entityType, t => {
|
_affrows += dbContextBetch("DbContextBetchRemove");
|
||||||
var arrType = t.MakeArrayType();
|
states.Clear();
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
};
|
||||||
var dbsetTypeDelete = dbsetType.GetMethod("DbContextBetchRemove", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
Action funcInsert = () => {
|
||||||
|
_affrows += dbContextBetch("DbContextBetchAdd");
|
||||||
var returnTarget = Expression.Label(typeof(int));
|
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
|
||||||
var var1Vals = Expression.Variable(arrType);
|
|
||||||
return Expression.Lambda<Func<object, object[], int>>(Expression.Block(
|
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeDelete, var1Vals)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
|
||||||
});
|
|
||||||
_affrows += deleteFunc(oldinfo.dbSet, states.ToArray());
|
|
||||||
states.Clear();
|
states.Clear();
|
||||||
};
|
};
|
||||||
Action<bool> funcUpdate = isLiveUpdate => {
|
Action<bool> funcUpdate = isLiveUpdate => {
|
||||||
var updateFunc = _dicExecCommandUpdate.GetOrAdd(oldinfo.entityType, t => {
|
var affrows = 0;
|
||||||
var arrType = t.MakeArrayType();
|
if (isLiveUpdate) affrows = dbContextBetch("DbContextBetchUpdateNow");
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
else affrows = dbContextBetch("DbContextBetchUpdate");
|
||||||
var dbsetTypeUpdate = dbsetType.GetMethod("DbContextBetchUpdate", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType, typeof(bool) }, null);
|
if (affrows == -999) { //最后一个元素已被删除
|
||||||
|
states.RemoveAt(states.Count - 1);
|
||||||
var returnTarget = Expression.Label(typeof(int));
|
return;
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
}
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
|
||||||
var parm3IsLiveUpdate = Expression.Parameter(typeof(bool));
|
|
||||||
var var1Vals = Expression.Variable(arrType);
|
|
||||||
return Expression.Lambda<Func<object, object[], bool, int>>(Expression.Block(
|
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeUpdate, var1Vals, parm3IsLiveUpdate)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals, parm3IsLiveUpdate }).Compile();
|
|
||||||
});
|
|
||||||
var affrows = updateFunc(oldinfo.dbSet, states.ToArray(), isLiveUpdate);
|
|
||||||
if (affrows > 0) {
|
if (affrows > 0) {
|
||||||
_affrows += affrows;
|
_affrows += affrows;
|
||||||
var islastNotUpdated = states.Count != affrows;
|
var islastNotUpdated = states.Count != affrows;
|
||||||
@ -146,16 +118,16 @@ namespace FreeSql {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
while(_actions.Any() || states.Any()) {
|
while (_actions.Any() || states.Any()) {
|
||||||
var info = _actions.Any() ? _actions.Dequeue() : null;
|
var info = _actions.Any() ? _actions.Dequeue() : null;
|
||||||
if (oldinfo == null) oldinfo = info;
|
if (oldinfo == null) oldinfo = info;
|
||||||
var isLiveUpdate = false;
|
var isLiveUpdate = false;
|
||||||
|
|
||||||
if (_actions.Any() == false && states.Any() ||
|
if (_actions.Any() == false && states.Any() ||
|
||||||
info != null && oldinfo.actionType != info.actionType ||
|
info != null && oldinfo.actionType != info.actionType ||
|
||||||
info != null && oldinfo.entityType != info.entityType) {
|
info != null && oldinfo.stateType != info.stateType) {
|
||||||
|
|
||||||
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
|
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
|
||||||
//最后一个,合起来发送
|
//最后一个,合起来发送
|
||||||
states.Add(info.state);
|
states.Add(info.state);
|
||||||
info = null;
|
info = null;
|
||||||
@ -224,6 +196,7 @@ namespace FreeSql {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
|
//_fsql.Aop.ToList -= AopToList;
|
||||||
this.Rollback();
|
this.Rollback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,79 +17,49 @@ namespace FreeSql {
|
|||||||
return _affrows;
|
return _affrows;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], Task<int>>> _dicExecCommandAsyncInsert = new ConcurrentDictionary<Type, Func<object, object[], Task<int>>>();
|
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandAsyncDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], Task<int>>> _dicExecCommandAsyncDelete = new ConcurrentDictionary<Type, Func<object, object[], Task<int>>>();
|
|
||||||
static ConcurrentDictionary<Type, Func<object, object[], bool, Task<int>>> _dicExecCommandAsyncUpdate = new ConcurrentDictionary<Type, Func<object, object[], bool, Task<int>>>();
|
|
||||||
async internal Task ExecCommandAsync() {
|
async internal Task ExecCommandAsync() {
|
||||||
ExecCommandInfo oldinfo = null;
|
ExecCommandInfo oldinfo = null;
|
||||||
var states = new List<object>();
|
var states = new List<object>();
|
||||||
|
|
||||||
Func<Task> funcInsert = async () => {
|
Func<string, Task<int>> dbContextBetch = methodName => {
|
||||||
var insertFunc = _dicExecCommandAsyncInsert.GetOrAdd(oldinfo.entityType, t => {
|
if (_dicExecCommandAsyncDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
|
||||||
var arrType = t.MakeArrayType();
|
trydic = new Dictionary<string, Func<object, object[], Task<int>>>();
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
|
||||||
var dbsetTypeInsert = dbsetType.GetMethod("OrmInsert", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
var arrType = oldinfo.stateType.MakeArrayType();
|
||||||
var insertBuilder = typeof(IInsert<>).MakeGenericType(t);
|
var dbsetType = oldinfo.dbSet.GetType().BaseType;
|
||||||
var insertExecuteAffrows = insertBuilder.GetMethod("ExecuteAffrowsAsync", new Type[0]);
|
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
||||||
|
|
||||||
var returnTarget = Expression.Label(typeof(Task<int>));
|
var returnTarget = Expression.Label(typeof(Task<int>));
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
var parm1DbSet = Expression.Parameter(typeof(object));
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
var parm2Vals = Expression.Parameter(typeof(object[]));
|
||||||
var var1Vals = Expression.Variable(arrType);
|
var var1Vals = Expression.Variable(arrType);
|
||||||
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
|
tryfunc = Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
|
||||||
new[] { var1Vals },
|
new[] { var1Vals },
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
||||||
Expression.Return(returnTarget,
|
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
|
||||||
Expression.Call(
|
|
||||||
Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
|
|
||||||
insertExecuteAffrows
|
|
||||||
)
|
|
||||||
),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
|
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
), new[] { parm1DbSet, parm2Vals }).Compile();
|
||||||
});
|
trydic.Add(methodName, tryfunc);
|
||||||
_affrows += await insertFunc(oldinfo.dbSet, states.ToArray());
|
}
|
||||||
states.Clear();
|
return tryfunc(oldinfo.dbSet, states.ToArray());
|
||||||
};
|
};
|
||||||
Func<Task> funcDelete = async () => {
|
Func<Task> funcDelete = async () => {
|
||||||
var deleteFunc = _dicExecCommandAsyncDelete.GetOrAdd(oldinfo.entityType, t => {
|
_affrows += await dbContextBetch("DbContextBetchRemoveAsync");
|
||||||
var arrType = t.MakeArrayType();
|
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
|
||||||
var dbsetTypeDelete = dbsetType.GetMethod("DbContextBetchRemoveAsync", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
|
||||||
|
|
||||||
var returnTarget = Expression.Label(typeof(Task<int>));
|
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
|
||||||
var var1Vals = Expression.Variable(arrType);
|
|
||||||
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
|
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeDelete, var1Vals)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
|
||||||
});
|
|
||||||
_affrows += await deleteFunc(oldinfo.dbSet, states.ToArray());
|
|
||||||
states.Clear();
|
states.Clear();
|
||||||
};
|
};
|
||||||
Func<bool, Task> funcUpdate = async (isLiveUpdate) => {
|
Func<Task> funcInsert = async () => {
|
||||||
var updateFunc = _dicExecCommandAsyncUpdate.GetOrAdd(oldinfo.entityType, t => {
|
_affrows += await dbContextBetch("DbContextBetchAddAsync");
|
||||||
var arrType = t.MakeArrayType();
|
states.Clear();
|
||||||
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
|
};
|
||||||
var dbsetTypeUpdate = dbsetType.GetMethod("DbContextBetchUpdateAsync", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType, typeof(bool) }, null);
|
Func<bool, Task> funcUpdate = async isLiveUpdate => {
|
||||||
|
var affrows = 0;
|
||||||
var returnTarget = Expression.Label(typeof(Task<int>));
|
if (isLiveUpdate) affrows = await dbContextBetch("DbContextBetchUpdateNowAsync");
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
else affrows = await dbContextBetch("DbContextBetchUpdateAsync");
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
if (affrows == -999) { //最后一个元素已被删除
|
||||||
var parm3IsLiveUpdate = Expression.Parameter(typeof(bool));
|
states.RemoveAt(states.Count - 1);
|
||||||
var var1Vals = Expression.Variable(arrType);
|
return;
|
||||||
return Expression.Lambda<Func<object, object[], bool, Task<int>>>(Expression.Block(
|
}
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeUpdate, var1Vals, parm3IsLiveUpdate)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals, parm3IsLiveUpdate }).Compile();
|
|
||||||
});
|
|
||||||
var affrows = await updateFunc(oldinfo.dbSet, states.ToArray(), isLiveUpdate);
|
|
||||||
if (affrows > 0) {
|
if (affrows > 0) {
|
||||||
_affrows += affrows;
|
_affrows += affrows;
|
||||||
var islastNotUpdated = states.Count != affrows;
|
var islastNotUpdated = states.Count != affrows;
|
||||||
@ -98,16 +68,16 @@ namespace FreeSql {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
while(_actions.Any() || states.Any()) {
|
while (_actions.Any() || states.Any()) {
|
||||||
var info = _actions.Any() ? _actions.Dequeue() : null;
|
var info = _actions.Any() ? _actions.Dequeue() : null;
|
||||||
if (oldinfo == null) oldinfo = info;
|
if (oldinfo == null) oldinfo = info;
|
||||||
var isLiveUpdate = false;
|
var isLiveUpdate = false;
|
||||||
|
|
||||||
if (_actions.Any() == false && states.Any() ||
|
if (_actions.Any() == false && states.Any() ||
|
||||||
info != null && oldinfo.actionType != info.actionType ||
|
info != null && oldinfo.actionType != info.actionType ||
|
||||||
info != null && oldinfo.entityType != info.entityType) {
|
info != null && oldinfo.stateType != info.stateType) {
|
||||||
|
|
||||||
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
|
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
|
||||||
//最后一个,合起来发送
|
//最后一个,合起来发送
|
||||||
states.Add(info.state);
|
states.Add(info.state);
|
||||||
info = null;
|
info = null;
|
||||||
|
@ -10,172 +10,92 @@ using System.Linq.Expressions;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using FreeSql.Extensions;
|
||||||
|
|
||||||
namespace FreeSql {
|
namespace FreeSql {
|
||||||
public abstract partial class DbSet<TEntity> where TEntity : class {
|
public abstract partial class DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
protected DbContext _ctx;
|
protected DbContext _ctx;
|
||||||
|
IFreeSql _fsql => _ctx._fsql;
|
||||||
|
|
||||||
protected ISelect<TEntity> OrmSelect(object dywhere) => _ctx._fsql.Select<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction(false));
|
protected ISelect<TEntity> OrmSelect(object dywhere) => _fsql.Select<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
||||||
|
|
||||||
protected IInsert<TEntity> OrmInsert() => _ctx._fsql.Insert<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
protected IInsert<TEntity> OrmInsert(TEntity source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IInsert<TEntity> OrmInsert(TEntity source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
protected IInsert<TEntity> OrmInsert(TEntity[] source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IInsert<TEntity> OrmInsert(TEntity[] source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
protected IInsert<TEntity> OrmInsert(IEnumerable<TEntity> source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IInsert<TEntity> OrmInsert(IEnumerable<TEntity> source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
|
||||||
protected IUpdate<TEntity> OrmUpdate(object dywhere) => _ctx._fsql.Update<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IUpdate<TEntity> OrmUpdate(object dywhere) => _fsql.Update<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
protected IDelete<TEntity> OrmDelete(object dywhere) => _ctx._fsql.Delete<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
|
protected IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
|
||||||
public ISelect<TEntity> Select => this.OrmSelect(null);
|
public ISelect<TEntity> Select => this.OrmSelect(null);
|
||||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
||||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
||||||
|
|
||||||
protected Dictionary<string, TEntity> _vals = new Dictionary<string, TEntity>();
|
protected Dictionary<string, EntityState> _vals = new Dictionary<string, EntityState>();
|
||||||
TableInfo _tablePriv;
|
TableInfo _tablePriv;
|
||||||
protected TableInfo _table => _tablePriv ?? (_tablePriv = _ctx._orm.CodeFirst.GetTableByEntity(_entityType));
|
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
|
||||||
|
ColumnInfo[] _tableIdentitysPriv;
|
||||||
|
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
|
||||||
protected Type _entityType = typeof(TEntity);
|
protected Type _entityType = typeof(TEntity);
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Func<TEntity, string>> _dicGetEntityKeyString = new ConcurrentDictionary<Type, Func<TEntity, string>>();
|
public class EntityState {
|
||||||
static MethodInfo MethodStringBuilderAppend = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) });
|
public TEntity Value { get; set; }
|
||||||
static MethodInfo MethodStringBuilderToString = typeof(StringBuilder).GetMethod("ToString", new Type[0]);
|
public string Key { get; set; }
|
||||||
static PropertyInfo MethodStringBuilderLength = typeof(StringBuilder).GetProperty("Length");
|
public DateTime Time { get; set; }
|
||||||
static MethodInfo MethodStringConcat = typeof(string).GetMethod("Concat", new Type[]{ typeof(object) });
|
|
||||||
string GetEntityKeyString(TEntity item) {
|
|
||||||
var func = _dicGetEntityKeyString.GetOrAdd(_entityType, t => {
|
|
||||||
var pks = _table.Primarys;
|
|
||||||
var returnTarget = Expression.Label(typeof(string));
|
|
||||||
var parm1 = Expression.Parameter(_entityType);
|
|
||||||
var var1Sb = Expression.Variable(typeof(StringBuilder));
|
|
||||||
var var3IsNull = Expression.Variable(typeof(bool));
|
|
||||||
var exps = new List<Expression>();
|
|
||||||
|
|
||||||
exps.AddRange(new Expression[] {
|
|
||||||
Expression.Assign(var1Sb, Expression.New(typeof(StringBuilder))),
|
|
||||||
Expression.Assign(var3IsNull, Expression.Constant(false))
|
|
||||||
});
|
|
||||||
for (var a = 0; a < pks.Length; a++) {
|
|
||||||
exps.Add(
|
|
||||||
Expression.IfThen(
|
|
||||||
Expression.Equal(var3IsNull, Expression.Constant(false)),
|
|
||||||
Expression.IfThenElse(
|
|
||||||
Expression.Equal(Expression.MakeMemberAccess(parm1, _table.Properties[pks[a].CsName]), Expression.Default(pks[a].CsType)),
|
|
||||||
Expression.Assign(var3IsNull, Expression.Constant(true)),
|
|
||||||
Expression.Block(
|
|
||||||
new Expression[]{
|
|
||||||
a > 0 ? Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant("*|_,,_|*" )) : null,
|
|
||||||
Expression.Call(var1Sb, MethodStringBuilderAppend,
|
|
||||||
Expression.Convert(Expression.MakeMemberAccess(parm1, _table.Properties[pks[a].CsName]), typeof(object))
|
|
||||||
)
|
|
||||||
}.Where(c => c != null).ToArray()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
exps.Add(
|
|
||||||
Expression.IfThen(
|
|
||||||
Expression.Equal(var3IsNull, Expression.Constant(false)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(var1Sb, MethodStringBuilderToString))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
|
|
||||||
return Expression.Lambda<Func<TEntity, string>>(Expression.Block(new[] { var1Sb, var3IsNull }, exps), new[] { parm1 }).Compile();
|
|
||||||
});
|
|
||||||
return func(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Action<TEntity, TEntity>> _dicCopyNewValueToEntity = new ConcurrentDictionary<Type, Action<TEntity, TEntity>>();
|
int DbContextBetcAdd(EntityState[] dels) {
|
||||||
void CopyNewValueToEntity(TEntity old, TEntity newvalue) {
|
if (dels.Any() == false) return 0;
|
||||||
var func = _dicCopyNewValueToEntity.GetOrAdd(_entityType, t => {
|
var affrows = this.OrmInsert(dels.Select(a => a.Value)).ExecuteAffrows();
|
||||||
var parm1 = Expression.Parameter(_entityType);
|
return affrows;
|
||||||
var parm2 = Expression.Parameter(_entityType);
|
|
||||||
var exps = new List<Expression>();
|
|
||||||
foreach (var prop in _table.Properties.Values) {
|
|
||||||
if (_table.ColumnsByCs.ContainsKey(prop.Name)) {
|
|
||||||
exps.Add(
|
|
||||||
Expression.Assign(
|
|
||||||
Expression.MakeMemberAccess(parm1, prop),
|
|
||||||
Expression.MakeMemberAccess(parm2, prop)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
exps.Add(
|
|
||||||
Expression.Assign(
|
|
||||||
Expression.MakeMemberAccess(parm1, prop),
|
|
||||||
Expression.Default(prop.PropertyType)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Expression.Lambda<Action<TEntity, TEntity>>(Expression.Block(exps), new[] { parm1, parm2 }).Compile();
|
|
||||||
});
|
|
||||||
func(old, newvalue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Action<TEntity, long>> _dicSetEntityIdentityValue = new ConcurrentDictionary<Type, Action<TEntity, long>>();
|
|
||||||
void SetEntityIdentityValue(TEntity old, long idtval) {
|
|
||||||
var func = _dicSetEntityIdentityValue.GetOrAdd(_entityType, t => {
|
|
||||||
var parm1 = Expression.Parameter(_entityType);
|
|
||||||
var parm2 = Expression.Parameter(typeof(long));
|
|
||||||
var exps = new List<Expression>();
|
|
||||||
exps.Add(
|
|
||||||
Expression.Assign(
|
|
||||||
Expression.MakeMemberAccess(parm1, _table.Properties[_table.Primarys[0].CsName]),
|
|
||||||
Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(_table.Primarys[0].CsType, Expression.Convert(parm2, typeof(object))), _table.Primarys[0].CsType)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return Expression.Lambda<Action<TEntity, long>>(Expression.Block(exps), new[] { parm1, parm2 }).Compile();
|
|
||||||
});
|
|
||||||
func(old, idtval);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Add(TEntity source) {
|
public void Add(TEntity source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
var key = GetEntityKeyString(source);
|
var key = _fsql.GetEntityKeyString(source);
|
||||||
TEntity newval = null;
|
EntityState state = new EntityState();
|
||||||
if (string.IsNullOrEmpty(key)) {
|
if (string.IsNullOrEmpty(key)) {
|
||||||
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
|
switch(_fsql.Ado.DataType) {
|
||||||
|
|
||||||
switch(_ctx._orm.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
if (ids.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
_ctx.ExecCommand();
|
_ctx.ExecCommand();
|
||||||
var idtval = this.OrmInsert(source).ExecuteIdentity();
|
var idtval = this.OrmInsert(source).ExecuteIdentity();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
SetEntityIdentityValue(source, idtval);
|
_fsql.SetEntityIdentityValue(source, idtval);
|
||||||
} else {
|
} else {
|
||||||
_ctx.ExecCommand();
|
_ctx.ExecCommand();
|
||||||
newval = this.OrmInsert(source).ExecuteInserted().First();
|
state.Value = this.OrmInsert(source).ExecuteInserted().First();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
CopyNewValueToEntity(source, newval);
|
_fsql.CopyEntityValue(source, state.Value);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
if (ids.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
_ctx.ExecCommand();
|
_ctx.ExecCommand();
|
||||||
var idtval = this.OrmInsert(source).ExecuteIdentity();
|
var idtval = this.OrmInsert(source).ExecuteIdentity();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
SetEntityIdentityValue(source, idtval);
|
_fsql.SetEntityIdentityValue(source, idtval);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("DbSet.Add 失败,由于实体没有主键值,或者没有配置自增,或者自增列数不为1。");
|
throw new Exception($"DbSet.Add 失败,未设置主键的值,或者没有配置自增,或者自增列数不为1:{_fsql.GetEntityString(source)}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = GetEntityKeyString(source);
|
state.Key = key = _fsql.GetEntityKeyString(source);
|
||||||
|
state.Time = DateTime.Now;
|
||||||
} else {
|
} else {
|
||||||
if (_vals.ContainsKey(key))
|
if (_vals.ContainsKey(key))
|
||||||
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加。");
|
throw new Exception($"DbSet.Add 失败,实体数据已存在,请勿重复添加:{_fsql.GetEntityString(source)}");
|
||||||
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
|
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), state);
|
||||||
}
|
}
|
||||||
if (newval == null) {
|
if (state.Value == null) {
|
||||||
newval = Activator.CreateInstance<TEntity>();
|
state.Value = Activator.CreateInstance<TEntity>();
|
||||||
CopyNewValueToEntity(newval, source);
|
_fsql.CopyEntityValue(state.Value, source); //copy, 记录旧值版本
|
||||||
}
|
}
|
||||||
_vals.Add(key, newval);
|
_vals.Add(key, state);
|
||||||
}
|
}
|
||||||
public void AddRange(TEntity[] source) {
|
public void AddRange(TEntity[] source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
@ -188,96 +108,52 @@ namespace FreeSql {
|
|||||||
Add(item);
|
Add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Func<TEntity, TEntity, string>> _dicCompareUpdateIngoreColumns = new ConcurrentDictionary<Type, Func<TEntity, TEntity, string>>();
|
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
||||||
string CompareUpdateIngoreColumns(TEntity up, TEntity oldval) {
|
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
||||||
var func = _dicCompareUpdateIngoreColumns.GetOrAdd(_entityType, t => {
|
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
||||||
var returnTarget = Expression.Label(typeof(string));
|
|
||||||
var parm1 = Expression.Parameter(_entityType);
|
|
||||||
var parm2 = Expression.Parameter(_entityType);
|
|
||||||
var var1Sb = Expression.Variable(typeof(StringBuilder));
|
|
||||||
var exps = new List<Expression>();
|
|
||||||
|
|
||||||
exps.AddRange(new Expression[] {
|
|
||||||
Expression.Assign(var1Sb, Expression.New(typeof(StringBuilder)))
|
|
||||||
});
|
|
||||||
var a = 0;
|
|
||||||
foreach (var prop in _table.Properties.Values) {
|
|
||||||
if (_table.ColumnsByCs.TryGetValue(prop.Name, out var trycol) == false) continue;
|
|
||||||
exps.Add(
|
|
||||||
Expression.IfThen(
|
|
||||||
Expression.Equal(
|
|
||||||
Expression.MakeMemberAccess(parm1, prop),
|
|
||||||
Expression.MakeMemberAccess(parm2, prop)
|
|
||||||
),
|
|
||||||
Expression.Block(
|
|
||||||
new Expression[]{
|
|
||||||
a > 0 ? Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant(", " )) : null,
|
|
||||||
Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant(trycol.Attribute.Name))
|
|
||||||
}.Where(c => c != null).ToArray()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
a++;
|
|
||||||
}
|
|
||||||
exps.Add(Expression.Return(returnTarget, Expression.Call(var1Sb, MethodStringBuilderToString)));
|
|
||||||
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
|
|
||||||
return Expression.Lambda<Func<TEntity, TEntity, string>>(Expression.Block(new[] { var1Sb }, exps), new[] { parm1, parm2 }).Compile();
|
|
||||||
});
|
|
||||||
return func(up, oldval);
|
|
||||||
}
|
|
||||||
int DbContextBetchUpdate(TEntity[] ups, bool isLiveUpdate) {
|
|
||||||
if (ups.Any() == false) return 0;
|
if (ups.Any() == false) return 0;
|
||||||
var uplst1 = ups[ups.Length - 1];
|
var uplst1 = ups[ups.Length - 1];
|
||||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
||||||
|
|
||||||
var lstkey1 = GetEntityKeyString(uplst1);
|
if (_vals.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||||
if (_vals.TryGetValue(lstkey1, out var lstval1) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
|
var lstval2 = default(EntityState);
|
||||||
TEntity lstval2 = default(TEntity);
|
if (uplst2 != null && _vals.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"DbSet.Update 失败,实体应该先查询再修改:{_fsql.GetEntityString(uplst2.Value)}");
|
||||||
if (uplst2 != null) {
|
|
||||||
var lstkey2 = GetEntityKeyString(uplst2);
|
|
||||||
if (_vals.TryGetValue(lstkey2, out lstval2) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
|
|
||||||
}
|
|
||||||
|
|
||||||
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
|
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
||||||
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
|
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
||||||
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 0) {
|
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
||||||
//最后一个不保存
|
//最后一个不保存
|
||||||
var ignores = cuig2.Split(new[] { ", " }, StringSplitOptions.None);
|
|
||||||
var source = ups.ToList();
|
var source = ups.ToList();
|
||||||
source.RemoveAt(ups.Length - 1);
|
source.RemoveAt(ups.Length - 1);
|
||||||
var affrows = this.OrmUpdate(null).SetSource(source).IgnoreColumns(ignores).ExecuteAffrows();
|
var affrows = this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig2).ExecuteAffrows();
|
||||||
foreach(var newval in source) {
|
foreach (var newval in source) {
|
||||||
var newkey = GetEntityKeyString(newval);
|
if (_vals.TryGetValue(newval.Key, out var tryold))
|
||||||
if (_vals.TryGetValue(newkey, out var tryold))
|
_fsql.CopyEntityValue(tryold.Value, newval.Value);
|
||||||
CopyNewValueToEntity(tryold, newval);
|
|
||||||
}
|
}
|
||||||
return affrows;
|
return affrows;
|
||||||
} else if (isLiveUpdate) {
|
} else if (isLiveUpdate) {
|
||||||
//立即保存
|
//立即保存
|
||||||
var ignores = cuig1.Split(new[] { ", " }, StringSplitOptions.None);
|
var source = ups;
|
||||||
var affrows = this.OrmUpdate(null).SetSource(ups).IgnoreColumns(ignores).ExecuteAffrows();
|
var affrows = this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig1).ExecuteAffrows();
|
||||||
foreach (var newval in ups) {
|
foreach (var newval in source) {
|
||||||
var newkey = GetEntityKeyString(newval);
|
if (_vals.TryGetValue(newval.Key, out var tryold))
|
||||||
if (_vals.TryGetValue(newkey, out var tryold))
|
_fsql.CopyEntityValue(tryold.Value, newval.Value);
|
||||||
CopyNewValueToEntity(tryold, newval);
|
|
||||||
}
|
}
|
||||||
return Math.Min(ups.Length, affrows);
|
return Math.Min(ups.Length, affrows);
|
||||||
}
|
}
|
||||||
//等待下次对比再保存
|
//等待下次对比再保存
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(TEntity source) {
|
public void Update(TEntity source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
if (_table.Primarys.Any() == false) throw new Exception("DbSet.Update 失败,实体没有主键。");
|
if (_table.Primarys.Any() == false) throw new Exception($"DbSet.Update 失败,实体没有主键:{_fsql.GetEntityString(source)}");
|
||||||
var key = GetEntityKeyString(source);
|
var key = _fsql.GetEntityKeyString(source);
|
||||||
if (string.IsNullOrEmpty(key)) throw new Exception("DbSet.Update 失败,实体没有设置主键值。");
|
if (string.IsNullOrEmpty(key)) throw new Exception($"DbSet.Update 失败,未设置主键的值:{_fsql.GetEntityString(source)}");
|
||||||
|
if (_vals.TryGetValue(key, out var tryval) == false) throw new Exception($"DbSet.Update 失败,实体未被跟踪,更新前应该先做查询:{_fsql.GetEntityString(source)}");
|
||||||
|
|
||||||
var snap = Activator.CreateInstance<TEntity>();
|
var snap = Activator.CreateInstance<TEntity>();
|
||||||
CopyNewValueToEntity(snap, source);
|
_fsql.CopyEntityValue(snap, source); //copy,避免SaveChanges前对象再次被修改
|
||||||
if (_vals.TryGetValue(key, out var val) == false) _vals.Add(key, snap);
|
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), new EntityState { Value = snap, Key = key, Time = DateTime.Now });
|
||||||
|
|
||||||
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Update, _entityType, this, snap);
|
|
||||||
}
|
}
|
||||||
public void UpdateRange(TEntity[] source) {
|
public void UpdateRange(TEntity[] source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
@ -290,28 +166,26 @@ namespace FreeSql {
|
|||||||
Update(item);
|
Update(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DbContextBetchRemove(TEntity[] dels) {
|
int DbContextBetchRemove(EntityState[] dels) {
|
||||||
if (dels.Any() == false) return 0;
|
if (dels.Any() == false) return 0;
|
||||||
|
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
||||||
var affrows = this.OrmDelete(dels).ExecuteAffrows();
|
//foreach (var del in dels)
|
||||||
foreach(var del in dels) {
|
// _vals.Remove(del.Key);
|
||||||
var key = GetEntityKeyString(del);
|
|
||||||
_vals.Remove(key);
|
|
||||||
}
|
|
||||||
return affrows;
|
return affrows;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(TEntity source) {
|
public void Remove(TEntity source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
if (_table.Primarys.Any() == false) throw new Exception("DbSet.Remove 失败,实体没有主键。");
|
if (_table.Primarys.Any() == false) throw new Exception($"DbSet.Remove 失败,实体没有主键:{_fsql.GetEntityString(source)}");
|
||||||
var key = GetEntityKeyString(source);
|
var key = _fsql.GetEntityKeyString(source);
|
||||||
if (string.IsNullOrEmpty(key)) throw new Exception("DbSet.Remove 失败,实体没有设置主键值。");
|
if (string.IsNullOrEmpty(key)) throw new Exception($"DbSet.Remove 失败,未设置主键的值:{_fsql.GetEntityString(source)}");
|
||||||
|
if (_vals.TryGetValue(key, out var tryval) == false) throw new Exception($"DbSet.Remove 失败,实体未被跟踪,删除前应该先做查询:{_fsql.GetEntityString(source)}");
|
||||||
|
|
||||||
var snap = Activator.CreateInstance<TEntity>();
|
var snap = Activator.CreateInstance<TEntity>();
|
||||||
CopyNewValueToEntity(snap, source);
|
_fsql.CopyEntityValue(snap, source); //copy,避免SaveChanges前对象再次被修改
|
||||||
if (_vals.TryGetValue(key, out var val) == false) _vals.Add(key, snap);
|
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Delete, this, typeof(EntityState), new EntityState { Value = snap, Key = key, Time = DateTime.Now });
|
||||||
|
|
||||||
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Delete, _entityType, this, snap);
|
_vals.Remove(key);
|
||||||
|
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(source);
|
||||||
}
|
}
|
||||||
public void RemoveRange(TEntity[] source) {
|
public void RemoveRange(TEntity[] source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
@ -323,6 +197,23 @@ namespace FreeSql {
|
|||||||
foreach (var item in source)
|
foreach (var item in source)
|
||||||
Remove(item);
|
Remove(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TrackToList(object list) {
|
||||||
|
if (list == null) return;
|
||||||
|
var ls = list as IList<TEntity>;
|
||||||
|
|
||||||
|
foreach (var item in ls) {
|
||||||
|
var key = _fsql.GetEntityKeyString(item);
|
||||||
|
if (_vals.ContainsKey(key)) {
|
||||||
|
_fsql.CopyEntityValue(_vals[key].Value, item);
|
||||||
|
_vals[key].Time = DateTime.Now;
|
||||||
|
} else {
|
||||||
|
var snap = Activator.CreateInstance<TEntity>();
|
||||||
|
_fsql.CopyEntityValue(snap, item);
|
||||||
|
_vals.Add(key, new EntityState { Value = snap, Key = key, Time = DateTime.Now });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||||
|
@ -10,57 +10,62 @@ using System.Linq.Expressions;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using FreeSql.Extensions;
|
||||||
|
|
||||||
namespace FreeSql {
|
namespace FreeSql {
|
||||||
partial class DbSet<TEntity> {
|
abstract partial class DbSet<TEntity> {
|
||||||
|
|
||||||
|
async Task<int> DbContextBetcAddAsync(EntityState[] dels) {
|
||||||
|
if (dels.Any() == false) return 0;
|
||||||
|
var affrows = await this.OrmInsert(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||||
|
return affrows;
|
||||||
|
}
|
||||||
async public Task AddAsync(TEntity source) {
|
async public Task AddAsync(TEntity source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
var key = GetEntityKeyString(source);
|
var key = _fsql.GetEntityKeyString(source);
|
||||||
TEntity newval = null;
|
EntityState state = new EntityState();
|
||||||
if (string.IsNullOrEmpty(key)) {
|
if (string.IsNullOrEmpty(key)) {
|
||||||
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
|
switch (_fsql.Ado.DataType) {
|
||||||
|
|
||||||
switch (_ctx._orm.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
if (ids.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
await _ctx.ExecCommandAsync();
|
_ctx.ExecCommand();
|
||||||
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
|
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
SetEntityIdentityValue(source, idtval);
|
_fsql.SetEntityIdentityValue(source, idtval);
|
||||||
} else {
|
} else {
|
||||||
await _ctx.ExecCommandAsync();
|
_ctx.ExecCommand();
|
||||||
newval = (await this.OrmInsert(source).ExecuteInsertedAsync()).First();
|
state.Value = (await this.OrmInsert(source).ExecuteInsertedAsync()).First();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
CopyNewValueToEntity(source, newval);
|
_fsql.CopyEntityValue(source, state.Value);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
if (ids.Length == 1 && _table.Primarys.Length == 1) {
|
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||||
await _ctx.ExecCommandAsync();
|
_ctx.ExecCommand();
|
||||||
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
|
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
|
||||||
_ctx._affrows++;
|
_ctx._affrows++;
|
||||||
SetEntityIdentityValue(source, idtval);
|
_fsql.SetEntityIdentityValue(source, idtval);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("DbSet.Add 失败,由于实体没有主键值,或者没有配置自增,或者自增列数不为1。");
|
throw new Exception($"DbSet.Add 失败,未设置主键的值,或者没有配置自增,或者自增列数不为1:{_fsql.GetEntityString(source)}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = GetEntityKeyString(source);
|
state.Key = key = _fsql.GetEntityKeyString(source);
|
||||||
|
state.Time = DateTime.Now;
|
||||||
} else {
|
} else {
|
||||||
if (_vals.ContainsKey(key))
|
if (_vals.ContainsKey(key))
|
||||||
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加。");
|
throw new Exception($"DbSet.Add 失败,实体数据已存在,请勿重复添加:{_fsql.GetEntityString(source)}");
|
||||||
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
|
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), state);
|
||||||
}
|
}
|
||||||
if (newval == null) {
|
if (state.Value == null) {
|
||||||
newval = Activator.CreateInstance<TEntity>();
|
state.Value = Activator.CreateInstance<TEntity>();
|
||||||
CopyNewValueToEntity(newval, source);
|
_fsql.CopyEntityValue(state.Value, source); //copy, 记录旧值版本
|
||||||
}
|
}
|
||||||
_vals.Add(key, newval);
|
_vals.Add(key, state);
|
||||||
}
|
}
|
||||||
async public Task AddRangeAsync(TEntity[] source) {
|
async public Task AddRangeAsync(TEntity[] source) {
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||||
@ -73,56 +78,47 @@ namespace FreeSql {
|
|||||||
await AddAsync(item);
|
await AddAsync(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task<int> DbContextBetchUpdateAsync(TEntity[] ups, bool isLiveUpdate) {
|
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
|
||||||
|
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
|
||||||
|
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
|
||||||
if (ups.Any() == false) return 0;
|
if (ups.Any() == false) return 0;
|
||||||
var uplst1 = ups[ups.Length - 1];
|
var uplst1 = ups[ups.Length - 1];
|
||||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
||||||
|
|
||||||
var lstkey1 = GetEntityKeyString(uplst1);
|
if (_vals.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||||
if (_vals.TryGetValue(lstkey1, out var lstval1) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
|
var lstval2 = default(EntityState);
|
||||||
TEntity lstval2 = default(TEntity);
|
if (uplst2 != null && _vals.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"DbSet.Update 失败,实体应该先查询再修改:{_fsql.GetEntityString(uplst2.Value)}");
|
||||||
if (uplst2 != null) {
|
|
||||||
var lstkey2 = GetEntityKeyString(uplst2);
|
|
||||||
if (_vals.TryGetValue(lstkey2, out lstval2) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
|
|
||||||
}
|
|
||||||
|
|
||||||
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
|
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
||||||
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
|
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
||||||
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 0) {
|
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
||||||
//最后一个不保存
|
//最后一个不保存
|
||||||
var ignores = cuig2.Split(new[] { ", " }, StringSplitOptions.None);
|
|
||||||
var source = ups.ToList();
|
var source = ups.ToList();
|
||||||
source.RemoveAt(ups.Length - 1);
|
source.RemoveAt(ups.Length - 1);
|
||||||
var affrows = await this.OrmUpdate(null).SetSource(source).IgnoreColumns(ignores).ExecuteAffrowsAsync();
|
var affrows = await this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig2).ExecuteAffrowsAsync();
|
||||||
foreach (var newval in source) {
|
foreach (var newval in source) {
|
||||||
var newkey = GetEntityKeyString(newval);
|
if (_vals.TryGetValue(newval.Key, out var tryold))
|
||||||
if (_vals.TryGetValue(newkey, out var tryold))
|
_fsql.CopyEntityValue(tryold.Value, newval.Value);
|
||||||
CopyNewValueToEntity(tryold, newval);
|
|
||||||
}
|
}
|
||||||
return affrows;
|
return affrows;
|
||||||
} else if (isLiveUpdate) {
|
} else if (isLiveUpdate) {
|
||||||
//立即保存
|
//立即保存
|
||||||
var ignores = cuig1.Split(new[] { ", " }, StringSplitOptions.None);
|
var source = ups;
|
||||||
var affrows = await this.OrmUpdate(null).SetSource(ups).IgnoreColumns(ignores).ExecuteAffrowsAsync();
|
var affrows = await this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig1).ExecuteAffrowsAsync();
|
||||||
foreach (var newval in ups) {
|
foreach (var newval in source) {
|
||||||
var newkey = GetEntityKeyString(newval);
|
if (_vals.TryGetValue(newval.Key, out var tryold))
|
||||||
if (_vals.TryGetValue(newkey, out var tryold))
|
_fsql.CopyEntityValue(tryold.Value, newval.Value);
|
||||||
CopyNewValueToEntity(tryold, newval);
|
|
||||||
}
|
}
|
||||||
return Math.Min(ups.Length, affrows);
|
return Math.Min(ups.Length, affrows);
|
||||||
}
|
}
|
||||||
//等待下次对比再保存
|
//等待下次对比再保存
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
|
||||||
async Task<int> DbContextBetchRemoveAsync(TEntity[] dels) {
|
|
||||||
if (dels.Any() == false) return 0;
|
if (dels.Any() == false) return 0;
|
||||||
|
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||||
var affrows = await this.OrmDelete(dels).ExecuteAffrowsAsync();
|
//foreach (var del in dels)
|
||||||
foreach (var del in dels) {
|
// _vals.Remove(del.Key);
|
||||||
var key = GetEntityKeyString(del);
|
|
||||||
_vals.Remove(key);
|
|
||||||
}
|
|
||||||
return affrows;
|
return affrows;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
278
FreeSql.DbContext/EntityUtil.cs
Normal file
278
FreeSql.DbContext/EntityUtil.cs
Normal file
@ -0,0 +1,278 @@
|
|||||||
|
using FreeSql.Internal.Model;
|
||||||
|
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 {
|
||||||
|
public static class EntityUtilFreeSqlExtensions {
|
||||||
|
|
||||||
|
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>
|
||||||
|
/// 获取实体的主键值,以 "*|_,[,_|*" 分割
|
||||||
|
/// </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.Equal(var3IsNull, Expression.Constant(false)),
|
||||||
|
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.Equal(var3IsNull, Expression.Constant(false)),
|
||||||
|
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, 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 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)),
|
||||||
|
Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant("(" ))
|
||||||
|
});
|
||||||
|
var a = 0;
|
||||||
|
foreach (var col in cols.Values) {
|
||||||
|
exps.Add(
|
||||||
|
Expression.IfThen(
|
||||||
|
Expression.Equal(var3IsNull, Expression.Constant(false)),
|
||||||
|
Expression.IfThenElse(
|
||||||
|
Expression.Equal(Expression.MakeMemberAccess(var1Parm, _table.Properties[col.CsName]), Expression.Default(col.CsType)),
|
||||||
|
Expression.Assign(var3IsNull, Expression.Constant(true)),
|
||||||
|
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.IfThen(
|
||||||
|
Expression.Equal(var3IsNull, Expression.Constant(false)),
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用新实体的值,复盖旧实体的值
|
||||||
|
/// </summary>
|
||||||
|
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicCopyNewValueToEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>();
|
||||||
|
public static void CopyEntityValue<TEntity>(this IFreeSql orm, TEntity oldValue, TEntity newValue) {
|
||||||
|
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(var1Parm, prop),
|
||||||
|
Expression.MakeMemberAccess(var2Parm, prop)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
exps.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.MakeMemberAccess(var1Parm, prop),
|
||||||
|
Expression.Default(prop.PropertyType)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Expression.Lambda<Action<object, object>>(Expression.Block(new[] { var1Parm, var2Parm }, exps), new[] { parm1, parm2 }).Compile();
|
||||||
|
});
|
||||||
|
func(oldValue, newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, long>>> _dicSetEntityIdentityValue = 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 SetEntityIdentityValue<TEntity>(this IFreeSql orm, TEntity item, long idtval) {
|
||||||
|
var func = _dicSetEntityIdentityValue.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))
|
||||||
|
});
|
||||||
|
foreach (var pk in identitys) {
|
||||||
|
exps.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.MakeMemberAccess(var1Parm, _table.Properties[pk.CsName]),
|
||||||
|
Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(pk.CsType, Expression.Convert(parm2, typeof(object))), pk.CsType)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Expression.Lambda<Action<object, long>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2 }).Compile();
|
||||||
|
});
|
||||||
|
func(item, idtval);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.21</Version>
|
<Version>0.3.22</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.21</Version>
|
<Version>0.3.22</Version>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
|
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
|
||||||
|
@ -33,11 +33,13 @@ namespace FreeSql.Tests.PostgreSQL {
|
|||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Query() {
|
public void Query() {
|
||||||
var t3 = g.pgsql.Ado.Query<xxx>("select * from song");
|
|
||||||
|
|
||||||
var t4 = g.pgsql.Ado.Query<(int, string, string)>("select * from song");
|
g.pgsql.CodeFirst.SyncStructure<xxx>();
|
||||||
|
var t3 = g.pgsql.Ado.Query<xxx>("select * from xxx");
|
||||||
|
|
||||||
var t5 = g.pgsql.Ado.Query<dynamic>("select * from song");
|
var t4 = g.pgsql.Ado.Query<(int, string, string)>("select * from xxx");
|
||||||
|
|
||||||
|
var t5 = g.pgsql.Ado.Query<dynamic>("select * from xxx");
|
||||||
}
|
}
|
||||||
|
|
||||||
class xxx {
|
class xxx {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.21</Version>
|
<Version>0.3.22</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||||
|
@ -16,6 +16,13 @@ namespace FreeSql {
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
TSelect WithTransaction(DbTransaction transaction);
|
TSelect WithTransaction(DbTransaction transaction);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 审核或跟踪 ToList 即将返回的数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="list"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
TSelect TrackToList(Action<object> action);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行SQL查询,返回 DataTable
|
/// 执行SQL查询,返回 DataTable
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1,20 +1,64 @@
|
|||||||
//using FreeSql.DatabaseModel;
|
using FreeSql.DatabaseModel;
|
||||||
//using System;
|
using System;
|
||||||
//using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
//namespace FreeSql {
|
namespace FreeSql {
|
||||||
// public interface IAop {
|
public interface IAop {
|
||||||
|
|
||||||
// ISelect<T1> SelectFitler<T1>(ISelect<T1> select) where T1 : class;
|
/// <summary>
|
||||||
// //ISelect<T1, T2, T3> SelectFitler<T1, T2, T3>(ISelect<T1, T2, T3> select) where T1 : class where T2 : class where T3 : class;
|
/// 监控 ToList 返回的的数据,用于拦截重新装饰
|
||||||
// //ISelect<T1, T2, T3, T4> SelectFitler<T1, T2, T3, T4>(ISelect<T1, T2, T3, T4> select) where T1 : class where T2 : class where T3 : class where T4 : class;
|
/// </summary>
|
||||||
// //ISelect<T1, T2, T3, T4, T5, T6> SelectFitler<T1, T2, T3, T4, T5, T6>(ISelect<T1, T2, T3, T4, T5, T6> select) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class;
|
EventHandler<AopToListEventArgs> ToList { get; set; }
|
||||||
// //ISelect<T1, T2, T3, T4, T5, T6, T7> SelectFitler<T1, T2, T3, T4, T5, T6, T7>(ISelect<T1, T2, T3, T4, T5, T6, T7> select) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class;
|
|
||||||
// //ISelect<T1, T2, T3, T4, T5, T6, T7, T8> SelectFitler<T1, T2, T3, T4, T5, T6, T7, T8>(ISelect<T1, T2, T3, T4, T5, T6, T7, T8> select) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class;
|
|
||||||
// //ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> SelectFitler<T1, T2, T3, T4, T5, T6, T7, T8, T9>(ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> select) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class;
|
|
||||||
// //ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> SelectFitler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> select) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class;
|
|
||||||
|
|
||||||
// IUpdate<T1> UpdateFitler<T1>(IUpdate<T1> update) where T1 : class;
|
/// <summary>
|
||||||
// IDelete<T1> DeleteFitler<T1>(IUpdate<T1> delete) where T1 : class;
|
/// 监视 Where,包括 select/update/delete,可控制使上层不被执行。
|
||||||
// }
|
/// </summary>
|
||||||
//}
|
EventHandler<AopWhereEventArgs> Where { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 可自定义解析表达式
|
||||||
|
/// </summary>
|
||||||
|
EventHandler<AopParseExpressionEventArgs> ParseExpression { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AopToListEventArgs : EventArgs {
|
||||||
|
public AopToListEventArgs(object list) {
|
||||||
|
this.List = list;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 可重新装饰的引用数据
|
||||||
|
/// </summary>
|
||||||
|
public object List { get; }
|
||||||
|
}
|
||||||
|
public class AopWhereEventArgs : EventArgs {
|
||||||
|
public AopWhereEventArgs(params object[] parameters) {
|
||||||
|
this.Parameters = parameters;
|
||||||
|
}
|
||||||
|
public object[] Parameters { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 可使上层不被执行这个条件
|
||||||
|
/// </summary>
|
||||||
|
public bool IsCancel { get; set; }
|
||||||
|
}
|
||||||
|
public class AopParseExpressionEventArgs : EventArgs {
|
||||||
|
public AopParseExpressionEventArgs(Expression expression, Func<Expression, string> freeParse) {
|
||||||
|
this.Expression = expression;
|
||||||
|
this.FreeParse = freeParse;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内置解析功能,可辅助您进行解析
|
||||||
|
/// </summary>
|
||||||
|
public Func<Expression, string> FreeParse { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 需要您解析的表达式
|
||||||
|
/// </summary>
|
||||||
|
public Expression Expression { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 解析后的内容
|
||||||
|
/// </summary>
|
||||||
|
public string Result { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -95,6 +95,10 @@ public interface IFreeSql {
|
|||||||
/// 数据库访问对象
|
/// 数据库访问对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IAdo Ado { get; }
|
IAdo Ado { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 所有拦截方法都在这里
|
||||||
|
/// </summary>
|
||||||
|
IAop Aop { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CodeFirst 模式开发相关方法
|
/// CodeFirst 模式开发相关方法
|
||||||
|
@ -227,8 +227,13 @@ namespace FreeSql.Internal {
|
|||||||
static ConcurrentDictionary<Type, MethodInfo> _dicExpressionLambdaToSqlAsSelectAnyMethodInfo = new ConcurrentDictionary<Type, MethodInfo>();
|
static ConcurrentDictionary<Type, MethodInfo> _dicExpressionLambdaToSqlAsSelectAnyMethodInfo = new ConcurrentDictionary<Type, MethodInfo>();
|
||||||
static ConcurrentDictionary<Type, PropertyInfo> _dicNullableValueProperty = new ConcurrentDictionary<Type, PropertyInfo>();
|
static ConcurrentDictionary<Type, PropertyInfo> _dicNullableValueProperty = new ConcurrentDictionary<Type, PropertyInfo>();
|
||||||
static ConcurrentDictionary<Type, Expression> _dicFreeSqlGlobalExtensionsAsSelectExpression = new ConcurrentDictionary<Type, Expression>();
|
static ConcurrentDictionary<Type, Expression> _dicFreeSqlGlobalExtensionsAsSelectExpression = new ConcurrentDictionary<Type, Expression>();
|
||||||
internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, Func<Expression[], string> getSelectGroupingMapString, SelectTableInfoType tbtype, bool isQuoteName) {
|
internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, Func<Expression[], string> getSelectGroupingMapString, SelectTableInfoType tbtype, bool isQuoteName, bool isDiyParse = true) {
|
||||||
if (exp == null) return "";
|
if (exp == null) return "";
|
||||||
|
if (isDiyParse) {
|
||||||
|
var args = new AopParseExpressionEventArgs(exp, ukexp => ExpressionLambdaToSql(exp, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, false));
|
||||||
|
_common._orm.Aop.ParseExpression?.Invoke(this, args);
|
||||||
|
if (string.IsNullOrEmpty(args.Result) == false) return args.Result;
|
||||||
|
}
|
||||||
switch (exp.NodeType) {
|
switch (exp.NodeType) {
|
||||||
case ExpressionType.Not: return $"not({ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName)})";
|
case ExpressionType.Not: return $"not({ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName)})";
|
||||||
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
|
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
|
||||||
|
13
FreeSql/Internal/CommonProvider/AopProvider.cs
Normal file
13
FreeSql/Internal/CommonProvider/AopProvider.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.Internal.CommonProvider {
|
||||||
|
class AopProvider : IAop {
|
||||||
|
public EventHandler<AopToListEventArgs> ToList { get; set; }
|
||||||
|
public EventHandler<AopWhereEventArgs> Where { get; set; }
|
||||||
|
public EventHandler<AopParseExpressionEventArgs> ParseExpression { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -51,6 +51,10 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
public IDelete<T1> Where(Expression<Func<T1, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, exp?.Body, null));
|
public IDelete<T1> Where(Expression<Func<T1, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, exp?.Body, null));
|
||||||
public IDelete<T1> Where(string sql, object parms = null) {
|
public IDelete<T1> Where(string sql, object parms = null) {
|
||||||
if (string.IsNullOrEmpty(sql)) return this;
|
if (string.IsNullOrEmpty(sql)) return this;
|
||||||
|
var args = new AopWhereEventArgs(sql, parms);
|
||||||
|
_orm.Aop.Where?.Invoke(this, new AopWhereEventArgs(sql, parms));
|
||||||
|
if (args.IsCancel == true) return this;
|
||||||
|
|
||||||
if (++_whereTimes > 1) _where.Append(" AND ");
|
if (++_whereTimes > 1) _where.Append(" AND ");
|
||||||
_where.Append("(").Append(sql).Append(")");
|
_where.Append("(").Append(sql).Append(")");
|
||||||
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
||||||
|
@ -28,6 +28,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
protected CommonUtils _commonUtils;
|
protected CommonUtils _commonUtils;
|
||||||
protected CommonExpression _commonExpression;
|
protected CommonExpression _commonExpression;
|
||||||
protected DbTransaction _transaction;
|
protected DbTransaction _transaction;
|
||||||
|
protected Action<object> _trackToList;
|
||||||
|
|
||||||
internal static void CopyData(Select0Provider<TSelect, T1> from, object to, ReadOnlyCollection<ParameterExpression> lambParms) {
|
internal static void CopyData(Select0Provider<TSelect, T1> from, object to, ReadOnlyCollection<ParameterExpression> lambParms) {
|
||||||
var toType = to?.GetType();
|
var toType = to?.GetType();
|
||||||
@ -60,6 +61,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
//toType.GetField("_commonUtils", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._commonUtils);
|
//toType.GetField("_commonUtils", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._commonUtils);
|
||||||
//toType.GetField("_commonExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._commonExpression);
|
//toType.GetField("_commonExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._commonExpression);
|
||||||
toType.GetField("_transaction", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._transaction);
|
toType.GetField("_transaction", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._transaction);
|
||||||
|
toType.GetField("_trackToList", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._trackToList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Select0Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) {
|
public Select0Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) {
|
||||||
@ -71,6 +73,11 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
|
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TSelect TrackToList(Action<object> track) {
|
||||||
|
_trackToList = track;
|
||||||
|
return this as TSelect;
|
||||||
|
}
|
||||||
|
|
||||||
public TSelect WithTransaction(DbTransaction transaction) {
|
public TSelect WithTransaction(DbTransaction transaction) {
|
||||||
_transaction = transaction;
|
_transaction = transaction;
|
||||||
return this as TSelect;
|
return this as TSelect;
|
||||||
@ -215,6 +222,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
var read = Utils.ExecuteArrayRowReadClassOrTuple(type, null, dr, 0, _commonUtils);
|
var read = Utils.ExecuteArrayRowReadClassOrTuple(type, null, dr, 0, _commonUtils);
|
||||||
ret.Add((TTuple)read.Value);
|
ret.Add((TTuple)read.Value);
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -230,6 +239,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
ret.Add((TTuple)read.Value);
|
ret.Add((TTuple)read.Value);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -243,6 +254,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
_orm.Ado.ExecuteReader(_transaction, dr => {
|
_orm.Ado.ExecuteReader(_transaction, dr => {
|
||||||
ret.Add(af.Read(dr));
|
ret.Add(af.Read(dr));
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -257,6 +270,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
ret.Add(af.Read(dr));
|
ret.Add(af.Read(dr));
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -283,6 +298,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
var index = -1;
|
var index = -1;
|
||||||
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, dr, ref index, false));
|
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, dr, ref index, false));
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -298,6 +315,8 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, dr, ref index, false));
|
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, dr, ref index, false));
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}, CommandType.Text, sql, _params.ToArray());
|
}, CommandType.Text, sql, _params.ToArray());
|
||||||
|
_orm.Aop.ToList?.Invoke(this, new AopToListEventArgs(ret));
|
||||||
|
_trackToList?.Invoke(ret);
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -480,6 +499,10 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
public TSelect Where(string sql, object parms = null) => this.WhereIf(true, sql, parms);
|
public TSelect Where(string sql, object parms = null) => this.WhereIf(true, sql, parms);
|
||||||
public TSelect WhereIf(bool condition, string sql, object parms = null) {
|
public TSelect WhereIf(bool condition, string sql, object parms = null) {
|
||||||
if (condition == false || string.IsNullOrEmpty(sql)) return this as TSelect;
|
if (condition == false || string.IsNullOrEmpty(sql)) return this as TSelect;
|
||||||
|
var args = new AopWhereEventArgs(sql, parms);
|
||||||
|
_orm.Aop.Where?.Invoke(this, new AopWhereEventArgs(sql, parms));
|
||||||
|
if (args.IsCancel == true) return this as TSelect;
|
||||||
|
|
||||||
_where.Append(" AND (").Append(sql).Append(")");
|
_where.Append(" AND (").Append(sql).Append(")");
|
||||||
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
||||||
return this as TSelect;
|
return this as TSelect;
|
||||||
|
@ -79,7 +79,5 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
var method = _select.GetType().GetMethod("ToSql", new[] { typeof(string) });
|
var method = _select.GetType().GetMethod("ToSql", new[] { typeof(string) });
|
||||||
return method.Invoke(_select, new object[] { field.Length > 0 ? field.Remove(0, 2).ToString() : null }) as string;
|
return method.Invoke(_select, new object[] { field.Length > 0 ? field.Remove(0, 2).ToString() : null }) as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,6 +117,10 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
public IUpdate<T1> Where(Expression<Func<T1, bool>> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, expression?.Body, null));
|
public IUpdate<T1> Where(Expression<Func<T1, bool>> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, expression?.Body, null));
|
||||||
public IUpdate<T1> Where(string sql, object parms = null) {
|
public IUpdate<T1> Where(string sql, object parms = null) {
|
||||||
if (string.IsNullOrEmpty(sql)) return this;
|
if (string.IsNullOrEmpty(sql)) return this;
|
||||||
|
var args = new AopWhereEventArgs(sql, parms);
|
||||||
|
_orm.Aop.Where?.Invoke(this, new AopWhereEventArgs(sql, parms));
|
||||||
|
if (args.IsCancel == true) return this;
|
||||||
|
|
||||||
_where.Append(" AND (").Append(sql).Append(")");
|
_where.Append(" AND (").Append(sql).Append(")");
|
||||||
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));
|
||||||
return this;
|
return this;
|
||||||
|
@ -494,7 +494,10 @@ namespace FreeSql.Internal {
|
|||||||
var findtbrefPkCsName = tbref.Primarys[a].CsName.TrimStart('_');
|
var findtbrefPkCsName = tbref.Primarys[a].CsName.TrimStart('_');
|
||||||
if (findtbrefPkCsName.StartsWith(tbref.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtbrefPkCsName = findtbrefPkCsName.Substring(tbref.Type.Name.Length).TrimStart('_');
|
if (findtbrefPkCsName.StartsWith(tbref.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtbrefPkCsName = findtbrefPkCsName.Substring(tbref.Type.Name.Length).TrimStart('_');
|
||||||
if (trytb.ColumnsByCs.TryGetValue($"{pnv.Name}{findtbrefPkCsName}", out var trycol) == false && //骆峰命名
|
if (trytb.ColumnsByCs.TryGetValue($"{pnv.Name}{findtbrefPkCsName}", out var trycol) == false && //骆峰命名
|
||||||
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_{findtbrefPkCsName}", out trycol) == false //下划线命名
|
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_{findtbrefPkCsName}", out trycol) == false && //下划线命名
|
||||||
|
tbref.Primarys.Length == 1 &&
|
||||||
|
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_Id", out trycol) == false &&
|
||||||
|
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}Id", out trycol) == false
|
||||||
) {
|
) {
|
||||||
//一对一,主键与主键查找
|
//一对一,主键与主键查找
|
||||||
if (isOnoToOne) {
|
if (isOnoToOne) {
|
||||||
|
@ -33,6 +33,7 @@ namespace FreeSql.MySql {
|
|||||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new MySqlDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new MySqlDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
||||||
|
|
||||||
public IAdo Ado { get; }
|
public IAdo Ado { get; }
|
||||||
|
public IAop Aop { get; }
|
||||||
public ICache Cache { get; }
|
public ICache Cache { get; }
|
||||||
public ICodeFirst CodeFirst { get; }
|
public ICodeFirst CodeFirst { get; }
|
||||||
public IDbFirst DbFirst { get; }
|
public IDbFirst DbFirst { get; }
|
||||||
@ -44,6 +45,7 @@ namespace FreeSql.MySql {
|
|||||||
|
|
||||||
this.Cache = new CacheProvider(cache, log);
|
this.Cache = new CacheProvider(cache, log);
|
||||||
this.Ado = new MySqlAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
this.Ado = new MySqlAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
||||||
|
this.Aop = new AopProvider();
|
||||||
|
|
||||||
this.DbFirst = new MySqlDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.DbFirst = new MySqlDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
this.CodeFirst = new MySqlCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.CodeFirst = new MySqlCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
|
@ -24,6 +24,7 @@ namespace FreeSql.Oracle {
|
|||||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new OracleDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new OracleDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
||||||
|
|
||||||
public IAdo Ado { get; }
|
public IAdo Ado { get; }
|
||||||
|
public IAop Aop { get; }
|
||||||
public ICache Cache { get; }
|
public ICache Cache { get; }
|
||||||
public ICodeFirst CodeFirst { get; }
|
public ICodeFirst CodeFirst { get; }
|
||||||
public IDbFirst DbFirst => null;
|
public IDbFirst DbFirst => null;
|
||||||
@ -35,6 +36,7 @@ namespace FreeSql.Oracle {
|
|||||||
|
|
||||||
this.Cache = new CacheProvider(cache, log);
|
this.Cache = new CacheProvider(cache, log);
|
||||||
this.Ado = new OracleAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
this.Ado = new OracleAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
||||||
|
this.Aop = new AopProvider();
|
||||||
|
|
||||||
this.CodeFirst = new OracleCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.CodeFirst = new OracleCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ namespace FreeSql.PostgreSQL {
|
|||||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new PostgreSQLDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new PostgreSQLDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
||||||
|
|
||||||
public IAdo Ado { get; }
|
public IAdo Ado { get; }
|
||||||
|
public IAop Aop { get; }
|
||||||
public ICache Cache { get; }
|
public ICache Cache { get; }
|
||||||
public ICodeFirst CodeFirst { get; }
|
public ICodeFirst CodeFirst { get; }
|
||||||
public IDbFirst DbFirst { get; }
|
public IDbFirst DbFirst { get; }
|
||||||
@ -71,6 +72,7 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
this.Cache = new CacheProvider(cache, log);
|
this.Cache = new CacheProvider(cache, log);
|
||||||
this.Ado = new PostgreSQLAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
this.Ado = new PostgreSQLAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
||||||
|
this.Aop = new AopProvider();
|
||||||
|
|
||||||
this.DbFirst = new PostgreSQLDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.DbFirst = new PostgreSQLDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
this.CodeFirst = new PostgreSQLCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.CodeFirst = new PostgreSQLCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
|
@ -23,6 +23,7 @@ namespace FreeSql.SqlServer {
|
|||||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new SqlServerDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new SqlServerDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
||||||
|
|
||||||
public IAdo Ado { get; }
|
public IAdo Ado { get; }
|
||||||
|
public IAop Aop { get; }
|
||||||
public ICache Cache { get; }
|
public ICache Cache { get; }
|
||||||
public ICodeFirst CodeFirst { get; }
|
public ICodeFirst CodeFirst { get; }
|
||||||
public IDbFirst DbFirst { get; }
|
public IDbFirst DbFirst { get; }
|
||||||
@ -34,6 +35,7 @@ namespace FreeSql.SqlServer {
|
|||||||
|
|
||||||
this.Cache = new CacheProvider(cache, log);
|
this.Cache = new CacheProvider(cache, log);
|
||||||
this.Ado = new SqlServerAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
this.Ado = new SqlServerAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
||||||
|
this.Aop = new AopProvider();
|
||||||
|
|
||||||
this.DbFirst = new SqlServerDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.DbFirst = new SqlServerDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
this.CodeFirst = new SqlServerCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.CodeFirst = new SqlServerCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
|
@ -24,6 +24,7 @@ namespace FreeSql.Sqlite {
|
|||||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new SqliteDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => new SqliteDelete<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
|
||||||
|
|
||||||
public IAdo Ado { get; }
|
public IAdo Ado { get; }
|
||||||
|
public IAop Aop { get; }
|
||||||
public ICache Cache { get; }
|
public ICache Cache { get; }
|
||||||
public ICodeFirst CodeFirst { get; }
|
public ICodeFirst CodeFirst { get; }
|
||||||
public IDbFirst DbFirst => null;
|
public IDbFirst DbFirst => null;
|
||||||
@ -35,6 +36,7 @@ namespace FreeSql.Sqlite {
|
|||||||
|
|
||||||
this.Cache = new CacheProvider(cache, log);
|
this.Cache = new CacheProvider(cache, log);
|
||||||
this.Ado = new SqliteAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
this.Ado = new SqliteAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
|
||||||
|
this.Aop = new AopProvider();
|
||||||
|
|
||||||
this.CodeFirst = new SqliteCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
this.CodeFirst = new SqliteCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
|
||||||
}
|
}
|
||||||
|
206
readme.md
206
readme.md
@ -25,59 +25,20 @@ FreeSql 是一个功能强大的 .NETStandard 库,用于对象关系映射程
|
|||||||
| 高手 | [《Repository》](https://github.com/2881099/FreeSql/wiki/Repository) \| [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) \| [《过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) \| [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) |
|
| 高手 | [《Repository》](https://github.com/2881099/FreeSql/wiki/Repository) \| [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) \| [《过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) \| [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) |
|
||||||
| 不朽 | [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) \| [《分区分表》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) \| [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \| [更新日志](https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97) |
|
| 不朽 | [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) \| [《分区分表》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) \| [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \| [更新日志](https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97) |
|
||||||
|
|
||||||
# 快速开始
|
# Quick start
|
||||||
|
> dotnet add package FreeSql
|
||||||
```csharp
|
```csharp
|
||||||
var connstr = "Data Source=127.0.0.1;User ID=root;Password=root;" +
|
|
||||||
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
|
|
||||||
|
|
||||||
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||||
.UseConnectionString(FreeSql.DataType.MySql, connstr)
|
.UseConnectionString(FreeSql.DataType.MySql, "connectionString")
|
||||||
.UseSlave("connectionString1", "connectionString2")
|
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
|
||||||
//读写分离,使用从数据库,支持多个
|
|
||||||
|
|
||||||
.UseMonitorCommand(
|
|
||||||
cmd => Console.WriteLine(cmd.CommandText),
|
|
||||||
//监听SQL命令对象,在执行前
|
|
||||||
(cmd, traceLog) => Console.WriteLine(traceLog))
|
|
||||||
//监听SQL命令对象,在执行后
|
|
||||||
|
|
||||||
.UseLogger(null)
|
|
||||||
//使用日志,不指定默认输出控制台 ILogger
|
|
||||||
.UseCache(null)
|
|
||||||
//使用缓存,不指定默认使用内存 IDistributedCache
|
|
||||||
|
|
||||||
.UseAutoSyncStructure(true)
|
|
||||||
//自动同步实体结构到数据库
|
|
||||||
.UseSyncStructureToLower(true)
|
|
||||||
//转小写同步结构
|
|
||||||
.UseSyncStructureToUpper(true)
|
|
||||||
//转大写同步结构
|
|
||||||
.UseConfigEntityFromDbFirst(true)
|
|
||||||
//若无配置实体类主键、自增,可从数据库导入
|
|
||||||
.UseNoneCommandParameter(true)
|
|
||||||
//不使用命令参数化执行,针对 Insert/Update,也可临时使用 IInsert/IUpdate.NoneParameter()
|
|
||||||
|
|
||||||
.UseLazyLoading(true)
|
|
||||||
//延时加载导航属性对象,导航属性需要声明 virtual
|
|
||||||
.Build();
|
.Build();
|
||||||
```
|
|
||||||
|
|
||||||
# 实体
|
|
||||||
|
|
||||||
FreeSql 使用模型执行数据访问,模型由实体类表示数据库表或视图,用于查询和保存数据。
|
|
||||||
|
|
||||||
可从现有数据库生成实体模型,提供 IDbFirst 生成实体模型。
|
|
||||||
|
|
||||||
或者手动创建模型,基于模型创建或修改数据库,提供 ICodeFirst 同步结构的 API(甚至可以做到开发阶段自动同步)。
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
class Song {
|
class Song {
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public DateTime? Create_time { get; set; }
|
|
||||||
public bool? Is_deleted { get; set; }
|
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
@ -91,113 +52,124 @@ class Song_tag {
|
|||||||
class Tag {
|
class Tag {
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
public int? Parent_id { get; set; }
|
public int? Parent_id { get; set; }
|
||||||
public virtual Tag Parent { get; set; }
|
public virtual Tag Parent { get; set; }
|
||||||
|
|
||||||
public decimal? Ddd { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
public virtual ICollection<Song> Songs { get; set; }
|
public virtual ICollection<Song> Songs { get; set; }
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
# 查询
|
# Query
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//OneToOne、ManyToOne
|
//OneToOne、ManyToOne
|
||||||
var t0 = fsql.Select<Tag>().Where(a => a.Parent.Parent.Name == "粤语").ToSql();
|
var t0 = fsql.Select<Tag>().Where(a => a.Parent.Parent.Name == "粤语").ToList();
|
||||||
//SELECT a.`Id`, a.`Parent_id`, a__Parent.`Id` as3, a__Parent.`Parent_id` as4, a__Parent.`Ddd`, a__Parent.`Name`, a.`Ddd` as7, a.`Name` as8
|
|
||||||
//FROM `Tag` a
|
|
||||||
//LEFT JOIN `Tag` a__Parent ON a__Parent.`Id` = a.`Parent_id`
|
|
||||||
//LEFT JOIN `Tag` a__Parent__Parent ON a__Parent__Parent.`Id` = a__Parent.`Parent_id`
|
|
||||||
//WHERE (a__Parent__Parent.`Name` = '粤语')
|
|
||||||
|
|
||||||
//OneToMany
|
//OneToMany
|
||||||
var t1 = fsql.Select<Tag>().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)).ToSql();
|
var t1 = fsql.Select<Tag>().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)).ToList();
|
||||||
//SELECT a.`Id`, a.`Parent_id`, a.`Ddd`, a.`Name`
|
|
||||||
//FROM `Tag` a
|
|
||||||
//WHERE (exists(SELECT 1
|
|
||||||
// FROM `Tag` t
|
|
||||||
// LEFT JOIN `Tag` t__Parent ON t__Parent.`Id` = t.`Parent_id`
|
|
||||||
// WHERE (t__Parent.`Id` = 10) AND (t.`Parent_id` = a.`Id`)
|
|
||||||
// limit 0,1))
|
|
||||||
|
|
||||||
//ManyToMany
|
//ManyToMany
|
||||||
var t2 = fsql.Select<Song>().Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语")).ToSql();
|
var t2 = fsql.Select<Song>().Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语")).ToList();
|
||||||
//SELECT a.`Id`, a.`Create_time`, a.`Is_deleted`, a.`Title`, a.`Url`
|
|
||||||
//FROM `Song` a
|
|
||||||
//WHERE(exists(SELECT 1
|
|
||||||
// FROM `Song_tag` Mt_Ms
|
|
||||||
// WHERE(Mt_Ms.`Song_id` = a.`Id`) AND(exists(SELECT 1
|
|
||||||
// FROM `Tag` t
|
|
||||||
// WHERE(t.`Name` = '国语') AND(t.`Id` = Mt_Ms.`Tag_id`)
|
|
||||||
// limit 0, 1))
|
|
||||||
// limit 0, 1))
|
|
||||||
```
|
```
|
||||||
更多前往wiki:[《Select查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2)
|
更多前往Wiki:[《Select 查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2)
|
||||||
|
|
||||||
# 表达式函数
|
# Lambda
|
||||||
|
```csharp
|
||||||
|
var t3 = f.Select<Song>.Where(a => new[] { 1, 2, 3 }.Contains(a.Id)).ToList();
|
||||||
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var t1 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToSql();
|
var t4 = select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToList();
|
||||||
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime`
|
|
||||||
//FROM `Song` a
|
|
||||||
//WHERE (a.`Id` in (1,2,3))
|
|
||||||
```
|
```
|
||||||
|
|
||||||
查找今天创建的数据
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var t2 = select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToSql();
|
var t5 = select.OrderBy(a => Guid.NewGuid()).Limit(1).ToList();
|
||||||
```
|
```
|
||||||
|
更多前往Wiki:[《表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
|
||||||
|
|
||||||
SqlServer 下随机获取记录
|
# Repository & UnitOfWork
|
||||||
|
> dotnet add package FreeSql.Repository
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var t3 = select.OrderBy(a => Guid.NewGuid()).Limit(1).ToSql();
|
using (var unitOfWork = fsql.CreateUnitOfWork()) {
|
||||||
//SELECT top 1 ...
|
|
||||||
//FROM [Song] a
|
var songRepository = uow.GetRepository<Song, int>();
|
||||||
//ORDER BY newid()
|
var tagRepository = uow.GetRepository<Tag, int>();
|
||||||
|
|
||||||
|
await songRepository.InsertAsync(new Song());
|
||||||
|
await tagRepository.InsertAsync(new Tag());
|
||||||
|
|
||||||
|
uow.Commit();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
更多前往wiki:[《Expression 表达式函数文档》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
|
# DbContext & DbSet
|
||||||
|
> dotnet add package FreeSql.DbContext
|
||||||
# 返回数据
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
List<Song> t1 = fsql.Select<Song>().Where(a => a.Id > 0).ToList();
|
public class SongContext : DbContext {
|
||||||
|
|
||||||
//返回普通字段 + 导航对象 Type 的数据
|
public DbSet<Song> Songs { get; set; }
|
||||||
List<Song> t2 = fsql.Select<Song>().LeftJoin(a => a.Type.Id == a.TypeId).ToList();
|
public DbSet<Song> Tags { get; set; }
|
||||||
|
|
||||||
//返回一个字段
|
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
List<int> t3 = fsql.Select<Song>().Where(a => a.Id > 0).ToList(a => a.Id);
|
builder.UseFreeSql(fsql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//返回匿名类型
|
long id = 0;
|
||||||
List<匿名类型> t4 = fsql.Select<Song>().Where(a => a.Id > 0).ToList(a => new { a.Id, a.Title });
|
using (var ctx = new SongContext()) {
|
||||||
|
var song = new Song { };
|
||||||
|
await ctx.Songs.AddAsync(song);
|
||||||
|
id = song.Id;
|
||||||
|
|
||||||
//返回元组
|
var adds = Enumerable.Range(0, 100).Select(a => new Song { Title = "xxxx" + a, Url = "url222" }).ToList();
|
||||||
List<(int, string)> t5 = fsql.Select<Song>().Where(a => a.Id > 0).ToList<(int, string)>("id, title");
|
await ctx.Songs.AddRangeAsync(adds);
|
||||||
|
|
||||||
//返回SQL字段
|
for (var a = 0; a < adds.Count; a++)
|
||||||
List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
|
adds[a].Title = "dkdkdkdk" + a;
|
||||||
.ToList(a => new {
|
|
||||||
a.Id, a.Title,
|
ctx.Songs.UpdateRange(adds);
|
||||||
cstitle = "substr(a.title, 0, 2)", //将 substr(a.title, 0, 2) 作为查询字段
|
ctx.Songs.RemoveRange(adds.Skip(10).Take(20).ToList());
|
||||||
csnow = Convert.ToDateTime("now()"), //将 now() 作为查询字段
|
ctx.Songs.Update(adds.Last());
|
||||||
//奇思妙想:怎么查询开窗函数的结果
|
|
||||||
});
|
await ctx.SaveChangesAsync();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
执行SQL返回数据
|
|
||||||
|
# DataFilter & Tenant
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
List<Song> t6 = fsql.Ado.Query<Song>("select * from song");
|
public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||||
List<(int, string ,string)> t7 = fsql.Ado.Query<(int, string, string)>("select id,title,url from song");
|
services.AddSingleton<IFreeSql>(fsql);
|
||||||
List<dynamic> t8 = fsql.Ado.Query<dynamic>("select * from song");
|
services.AddMvc();
|
||||||
```
|
|
||||||
更多前往wiki:[《Select查询数据》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2)
|
|
||||||
|
|
||||||
# 性能测试
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
|
builder.RegisterFreeRepository(filter => filter
|
||||||
|
.Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
|
||||||
|
.Apply<ITenant>("Tenant", a => a.TenantId == 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.Populate(services);
|
||||||
|
var container = builder.Build();
|
||||||
|
return new AutofacServiceProvider(container);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Temporary disable:
|
||||||
|
```csharp
|
||||||
|
var songRepository = fsql.GetRepository<Song, int>();
|
||||||
|
|
||||||
|
using (songRepository.DataFilter.Disable("Tenant")) {
|
||||||
|
//Tenant Invalid
|
||||||
|
}
|
||||||
|
//Tenant restore
|
||||||
|
```
|
||||||
|
|
||||||
|
# Performance
|
||||||
|
|
||||||
FreeSql Query & Dapper Query
|
FreeSql Query & Dapper Query
|
||||||
```shell
|
```shell
|
||||||
@ -223,11 +195,9 @@ Elapsed: 00:00:00.6707125; ToList Entity Counts: 131072; ORM: FreeSql*
|
|||||||
Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper
|
Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper
|
||||||
```
|
```
|
||||||
|
|
||||||
测试方法:运行两次,以第二次性能报告,避免了首个运行慢不公平的情况。[查看测试代码](FreeSql.Tests.PerformanceTests/MySqlAdoTest.cs)
|
[Test code](FreeSql.Tests.PerformanceTests/MySqlAdoTest.cs)
|
||||||
|
|
||||||
FreeSql 目前使用的ExpressionTree+缓存,因为支持更为复杂的数据类型,所以比 Dapper Emit 慢少许。
|
# Contributors
|
||||||
|
|
||||||
# 贡献者名单
|
|
||||||
|
|
||||||
[systemhejiyong](https://github.com/systemhejiyong)
|
[systemhejiyong](https://github.com/systemhejiyong)
|
||||||
[LambertW](https://github.com/LambertW)
|
[LambertW](https://github.com/LambertW)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user