mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
- 修复 ISelect.ToList(true) 无效的 bug;
- 增加 IAop.ConfigEntity 配置实体特性,可实现使用其他 ORM 的实体特性,#36;
This commit is contained in:
@ -16,12 +16,6 @@ namespace FreeSql.DataAnnotations {
|
||||
/// 查询过滤SQL,实现类似 a.IsDeleted = 1 功能
|
||||
/// </summary>
|
||||
public string SelectFilter { get; set; }
|
||||
|
||||
internal bool? _RowVersion;
|
||||
/// <summary>
|
||||
/// 修改/删除时,启用行版本检查
|
||||
/// </summary>
|
||||
public bool RowVersion { get => _RowVersion ?? false; set => _RowVersion = value; }
|
||||
|
||||
internal ConcurrentDictionary<string, ColumnAttribute> _columns { get; } = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.5.3</Version>
|
||||
<Version>0.5.4</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||
|
@ -1,7 +1,9 @@
|
||||
using FreeSql.DatabaseModel;
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.DatabaseModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace FreeSql {
|
||||
public interface IAop {
|
||||
@ -20,6 +22,15 @@ namespace FreeSql {
|
||||
/// 可自定义解析表达式
|
||||
/// </summary>
|
||||
EventHandler<AopParseExpressionEventArgs> ParseExpression { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 自定义实体的配置,方便和多个 ORM 共同使用
|
||||
/// </summary>
|
||||
EventHandler<AopConfigEntityEventArgs> ConfigEntity { get; set; }
|
||||
/// <summary>
|
||||
/// 自定义实体的属性配置,方便和多个 ORM 共同使用
|
||||
/// </summary>
|
||||
EventHandler<AopConfigEntityPropertyEventArgs> ConfigEntityProperty { get; set; }
|
||||
}
|
||||
|
||||
public class AopToListEventArgs : EventArgs {
|
||||
@ -61,4 +72,39 @@ namespace FreeSql {
|
||||
/// </summary>
|
||||
public string Result { get; set; }
|
||||
}
|
||||
public class AopConfigEntityEventArgs : EventArgs {
|
||||
public AopConfigEntityEventArgs(Type entityType) {
|
||||
this.EntityType = entityType;
|
||||
this.ModifyResult = new TableAttribute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实体类型
|
||||
/// </summary>
|
||||
public Type EntityType { get; }
|
||||
/// <summary>
|
||||
/// 实体配置
|
||||
/// </summary>
|
||||
public TableAttribute ModifyResult { get; }
|
||||
}
|
||||
public class AopConfigEntityPropertyEventArgs : EventArgs {
|
||||
public AopConfigEntityPropertyEventArgs(Type entityType, PropertyInfo property) {
|
||||
this.EntityType = entityType;
|
||||
this.Property = property;
|
||||
this.ModifyResult = new ColumnAttribute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实体类型
|
||||
/// </summary>
|
||||
public Type EntityType { get; }
|
||||
/// <summary>
|
||||
/// 实体的属性
|
||||
/// </summary>
|
||||
public PropertyInfo Property { get; }
|
||||
/// <summary>
|
||||
/// 实体的属性配置
|
||||
/// </summary>
|
||||
public ColumnAttribute ModifyResult { get; }
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
public EventHandler<AopToListEventArgs> ToList { get; set; }
|
||||
public EventHandler<AopWhereEventArgs> Where { get; set; }
|
||||
public EventHandler<AopParseExpressionEventArgs> ParseExpression { get; set; }
|
||||
public EventHandler<AopConfigEntityEventArgs> ConfigEntity { get; set; }
|
||||
public EventHandler<AopConfigEntityPropertyEventArgs> ConfigEntityProperty { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -288,8 +288,10 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
return ret;
|
||||
});
|
||||
}
|
||||
public List<T1> ToList(bool includeNestedMembers = false) => this.ToListPrivate(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevel2());
|
||||
public Task<List<T1>> ToListAsync(bool includeNestedMembers = false) => this.ToListPrivateAsync(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevel2());
|
||||
public List<T1> ToList(bool includeNestedMembers = false) =>
|
||||
this.ToListPrivate(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevelAll());
|
||||
public Task<List<T1>> ToListAsync(bool includeNestedMembers = false) =>
|
||||
this.ToListPrivateAsync(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevelAll());
|
||||
public T1 ToOne() {
|
||||
this.Limit(1);
|
||||
return this.ToList().FirstOrDefault();
|
||||
@ -350,7 +352,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
public Func<IFreeSql, DbDataReader, T1> Read { get; set; }
|
||||
}
|
||||
protected GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevelAll() {
|
||||
return _dicGetAllFieldExpressionTree.GetOrAdd(string.Join("+", _tables.Select(a => $"{_orm.Ado.DataType}-{a.Table.DbName}-{a.Alias}-{a.Type}")), s => {
|
||||
return _dicGetAllFieldExpressionTree.GetOrAdd($"*{string.Join("+", _tables.Select(a => $"{_orm.Ado.DataType}-{a.Table.DbName}-{a.Alias}-{a.Type}"))}", s => {
|
||||
var type = _tables.First().Table.TypeLazy ?? _tables.First().Table.Type;
|
||||
var ormExp = Expression.Parameter(typeof(IFreeSql), "orm");
|
||||
var rowExp = Expression.Parameter(typeof(DbDataReader), "row");
|
||||
|
@ -58,31 +58,74 @@ namespace FreeSql.Internal {
|
||||
return dicConfigEntity.TryGetValue(type, out var trytb) ? trytb : null;
|
||||
}
|
||||
internal TableAttribute GetEntityTableAttribute(Type type) {
|
||||
var attr = type.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
|
||||
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
|
||||
TableAttribute attr = null;
|
||||
if (_orm.Aop.ConfigEntity != null) {
|
||||
var aope = new AopConfigEntityEventArgs(type);
|
||||
_orm.Aop.ConfigEntity(_orm, aope);
|
||||
attr = aope.ModifyResult;
|
||||
}
|
||||
if (attr == null) attr = new TableAttribute();
|
||||
|
||||
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trytb.Name;
|
||||
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trytb.OldName;
|
||||
if (string.IsNullOrEmpty(attr.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
|
||||
return attr;
|
||||
if (dicConfigEntity.TryGetValue(type, out var trytb)) {
|
||||
if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name;
|
||||
if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName;
|
||||
if (!string.IsNullOrEmpty(trytb.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
|
||||
}
|
||||
var attrs = type.GetCustomAttributes(typeof(TableAttribute), false);
|
||||
foreach (var tryattrobj in attrs) {
|
||||
var tryattr = tryattrobj as TableAttribute;
|
||||
if (tryattr == null) continue;
|
||||
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
|
||||
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
|
||||
if (!string.IsNullOrEmpty(tryattr.SelectFilter)) attr.SelectFilter = tryattr.SelectFilter;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(attr.Name)) return attr;
|
||||
if (!string.IsNullOrEmpty(attr.OldName)) return attr;
|
||||
if (!string.IsNullOrEmpty(attr.SelectFilter)) return attr;
|
||||
return null;
|
||||
}
|
||||
internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) {
|
||||
var attr = proto.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
|
||||
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
|
||||
if (trytb._columns.TryGetValue(proto.Name, out var trycol) == false) return attr;
|
||||
ColumnAttribute attr = null;
|
||||
if (_orm.Aop.ConfigEntityProperty != null) {
|
||||
var aope = new AopConfigEntityPropertyEventArgs(type, proto);
|
||||
_orm.Aop.ConfigEntityProperty(_orm, aope);
|
||||
attr = aope.ModifyResult;
|
||||
}
|
||||
if (attr == null) attr = new ColumnAttribute();
|
||||
|
||||
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trycol.Name;
|
||||
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trycol.OldName;
|
||||
if (string.IsNullOrEmpty(attr.DbType)) attr.DbType = trycol.DbType;
|
||||
if (attr._IsPrimary == null) attr._IsPrimary = trycol.IsPrimary;
|
||||
if (attr._IsIdentity == null) attr._IsIdentity = trycol.IsIdentity;
|
||||
if (attr._IsNullable == null) attr._IsNullable = trycol.IsNullable;
|
||||
if (attr._IsIgnore == null) attr._IsIgnore = trycol.IsIgnore;
|
||||
if (attr._IsVersion == null) attr._IsVersion = trycol.IsVersion;
|
||||
if (attr.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue;
|
||||
return attr;
|
||||
if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._columns.TryGetValue(proto.Name, out var trycol)) {
|
||||
if (!string.IsNullOrEmpty(trycol.Name)) attr.Name = trycol.Name;
|
||||
if (!string.IsNullOrEmpty(trycol.OldName)) attr.OldName = trycol.OldName;
|
||||
if (!string.IsNullOrEmpty(trycol.DbType)) attr.DbType = trycol.DbType;
|
||||
if (trycol._IsPrimary != null) attr._IsPrimary = trycol.IsPrimary;
|
||||
if (trycol._IsIdentity != null) attr._IsIdentity = trycol.IsIdentity;
|
||||
if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable;
|
||||
if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore;
|
||||
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
|
||||
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
|
||||
}
|
||||
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
|
||||
foreach (var tryattrobj in attrs) {
|
||||
var tryattr = tryattrobj as ColumnAttribute;
|
||||
if (tryattr == null) continue;
|
||||
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
|
||||
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
|
||||
if (!string.IsNullOrEmpty(tryattr.DbType)) attr.DbType = tryattr.DbType;
|
||||
if (tryattr._IsPrimary != null) attr._IsPrimary = tryattr.IsPrimary;
|
||||
if (tryattr._IsIdentity != null) attr._IsIdentity = tryattr.IsIdentity;
|
||||
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
|
||||
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
|
||||
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
|
||||
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(attr.Name)) return attr;
|
||||
if (!string.IsNullOrEmpty(attr.OldName)) return attr;
|
||||
if (!string.IsNullOrEmpty(attr.DbType)) return attr;
|
||||
if (attr._IsPrimary != null) return attr;
|
||||
if (attr._IsIdentity != null) return attr;
|
||||
if (attr._IsNullable != null) return attr;
|
||||
if (attr._IsIgnore != null) return attr;
|
||||
if (attr._IsVersion != null) return attr;
|
||||
if (attr.DbDefautValue != null) return attr;
|
||||
return null;
|
||||
}
|
||||
|
||||
internal string WhereObject(TableInfo table, string aliasAndDot, object dywhere) {
|
||||
|
Reference in New Issue
Block a user