ICodeFirst 增加 ConfigEntity 方法,现实干净实体无特性的需求

This commit is contained in:
28810
2019-01-22 12:56:45 +08:00
parent 0bcacc706a
commit 4bf8d60361
22 changed files with 270 additions and 65 deletions

View File

@ -194,8 +194,11 @@ namespace FreeSql.Internal {
for (var a = tbidx + 1; a < _tables.Count; a++)
_tables[a].Type = SelectTableInfoType.From;
} else {
var find = _tables.Where((a, c) => c > 0 && a.Type == tbtype && string.IsNullOrEmpty(a.On)).LastOrDefault();
if (find != null) find.On = filter;
var find = _tables.Where((a, c) => c > 0 && (a.Type == tbtype || a.Type == SelectTableInfoType.From) && string.IsNullOrEmpty(a.On)).LastOrDefault();
if (find != null) {
find.Type = tbtype;
find.On = filter;
}
}
}
@ -355,13 +358,13 @@ namespace FreeSql.Internal {
if (isQuoteName) name = _common.QuoteSqlName(name);
return name;
}
Func<TableInfo, string, SelectTableInfo> getOrAddTable = (tbtmp, alias) => {
var finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
Func<TableInfo, string, bool, SelectTableInfo> getOrAddTable = (tbtmp, alias, isa) => {
var finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName).ToArray(); //外部表,内部表一起查
if (finds.Length > 1) {
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == $"__parent_{alias}_parent__").ToArray(); //查询外部表
if (finds.Any() == false) {
finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent).ToArray(); //查询内部表
if (finds.Length > 1) finds = _tables.Where((a2, c2) => a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent && a2.Alias == alias).ToArray();
finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent).ToArray(); //查询内部表
if (finds.Length > 1) finds = _tables.Where((a2, c2) => (isa || c2 > 0) && a2.Table.CsName == tbtmp.CsName && a2.Type != SelectTableInfoType.Parent && a2.Alias == alias).ToArray();
}
}
var find = finds.FirstOrDefault();
@ -388,7 +391,7 @@ namespace FreeSql.Internal {
if (tb2tmp != null) {
if (exp2.NodeType == ExpressionType.Parameter) alias2 = (exp2 as ParameterExpression).Name;
else alias2 = $"{alias2}__{mp2.Member.Name}";
find2 = getOrAddTable(tb2tmp, alias2);
find2 = getOrAddTable(tb2tmp, alias2, exp2.NodeType == ExpressionType.Parameter);
alias2 = find2.Alias;
tb2 = tb2tmp;
}
@ -397,7 +400,7 @@ namespace FreeSql.Internal {
if (_selectColumnMap != null) {
var tb3 = _common.GetTableByEntity(mp2.Type);
if (tb3 != null) {
var find3 = getOrAddTable(tb2tmp, $"{alias2}__{mp2.Member.Name}");
var find3 = getOrAddTable(tb2tmp, $"{alias2}__{mp2.Member.Name}", exp2.NodeType == ExpressionType.Parameter);
foreach (var tb3c in tb3.Columns.Values)
_selectColumnMap.Add(new SelectColumnInfo { Table = find3, Column = tb3c });

View File

@ -283,8 +283,8 @@ namespace FreeSql.Internal.CommonProvider {
if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
else dicfield.Add(quoteName, true);
} else {
var tb2 = _tables.Where(a => a.Table.Type == prop.PropertyType && a.Alias.Contains(prop.Name)).FirstOrDefault();
if (tb2 == null && props.Where(pw => pw.Value.PropertyType == prop.PropertyType).Count() == 1) tb2 = _tables.Where(a => a.Table.Type == prop.PropertyType).FirstOrDefault();
var tb2 = _tables.Where((a, b) => b > 0 && a.Table.Type == prop.PropertyType && a.Alias.Contains(prop.Name)).FirstOrDefault(); //判断 b > 0 防止 parent 递归关系
if (tb2 == null && props.Where(pw => pw.Value.PropertyType == prop.PropertyType).Count() == 1) tb2 = _tables.Where((a, b) => b > 0 && a.Table.Type == prop.PropertyType).FirstOrDefault();
if (tb2 == null) continue;
foreach (var col2 in tb2.Table.Columns.Values) {
if (index > 0) field.Append(", ");

View File

@ -74,7 +74,7 @@ namespace FreeSql.Internal.CommonProvider {
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, cols, binaryExpression, null);
if (cols.Any() == false) return this;
foreach (var col in cols) {
if (col.Column.Attribute.IsNullable) {
if (col.Column.Attribute.IsNullable == true) {
var replval = _orm.CodeFirst.GetDbInfo(col.Column.CsType.GenericTypeArguments.FirstOrDefault())?.defaultValue;
if (replval == null) continue;
var replname = _commonUtils.QuoteSqlName(col.Column.Attribute.Name);

View File

@ -1,6 +1,8 @@
using FreeSql.Internal.Model;
using FreeSql.DataAnnotations;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
@ -30,11 +32,45 @@ namespace FreeSql.Internal {
_orm = orm;
}
ConcurrentDictionary<Type, TableAttribute> dicConfigEntity = new ConcurrentDictionary<Type, TableAttribute>();
internal ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) {
var type = typeof(T);
var table = dicConfigEntity.GetOrAdd(type, new TableAttribute());
var fluent = new TableFluent<T>(table);
entity?.Invoke(fluent);
return _orm.CodeFirst;
}
internal TableAttribute GetEntityTableAttribute(Type entityType) {
var attr = entityType.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
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;
}
internal ColumnAttribute GetEntityColumnAttribute(Type entityType, PropertyInfo proto) {
var attr = proto.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
if (trytb._columns.TryGetValue(proto.Name, out var trycol) == false) return attr;
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.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue;
return attr;
}
internal string WhereObject(TableInfo table, string aliasAndDot, object dywhere) {
if (dywhere == null) return "";
var type = dywhere.GetType();
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
if (primarys.Length == 1 && type == primarys.First().CsType) {
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (primarys.Length == 1 && (type == primarys.First().CsType || type.IsNumberType() && primarys.First().CsType.IsNumberType())) {
return $"{aliasAndDot}{this.QuoteSqlName(primarys.First().Attribute.Name)} = {this.FormatSql("{0}", dywhere)}";
} else if (primarys.Length > 0 && type.FullName == table.Type.FullName) {
var sb = new StringBuilder();

View File

@ -30,7 +30,7 @@ namespace FreeSql.Internal {
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
if (common.CodeFirst.GetDbInfo(entity) != null) return null;
var tbattr = entity.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
var tbattr = common.GetEntityTableAttribute(entity);
trytb = new TableInfo();
trytb.Type = entity;
trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
@ -46,7 +46,7 @@ namespace FreeSql.Internal {
foreach (var p in trytb.Properties.Values) {
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
//if (tp == null) continue;
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
var colattr = common.GetEntityColumnAttribute(entity, p);
if (tp == null && colattr == null) {
if (common.CodeFirst.IsLazyLoading) {
var getIsVirtual = trytb.Type.GetMethod($"get_{p.Name}")?.IsVirtual;
@ -72,7 +72,7 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if ((colattr.IsNullable == false || colattr.IsIdentity || colattr.IsPrimary) && colattr.DbType.Contains("NOT NULL") == false) {
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
colattr.IsNullable = false;
colattr.DbType += " NOT NULL";
}
@ -89,10 +89,7 @@ namespace FreeSql.Internal {
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType;
colattr.DbDefautValue = Activator.CreateInstance(consturctorType);
}
if (colattr.IsIdentity && new[] {
typeof(sbyte), typeof(short), typeof(int), typeof(long),
typeof(byte), typeof(ushort), typeof(uint), typeof(ulong),
typeof(double), typeof(float), typeof(decimal) }.Contains(p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType) == false)
if (colattr.IsIdentity == true && (p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType)?.IsNumberType() == false)
colattr.IsIdentity = false;
var col = new ColumnInfo {
@ -104,9 +101,9 @@ namespace FreeSql.Internal {
trytb.Columns.Add(colattr.Name, col);
trytb.ColumnsByCs.Add(p.Name, col);
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).ToArray();
foreach (var col in trytb.Primarys)
col.Attribute.IsPrimary = true;
}

View File

@ -57,7 +57,7 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if ((colattr.IsNullable == false || colattr.IsIdentity || colattr.IsPrimary) && colattr.DbType.Contains("NOT NULL") == false) {
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false) {
colattr.IsNullable = false;
colattr.DbType += " NOT NULL";
}
@ -84,9 +84,9 @@ namespace FreeSql.Internal {
trytb.Columns.Add(colattr.Name, col);
trytb.ColumnsByCs.Add(p.Name, col);
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).ToArray();
foreach(var col in trytb.Primarys)
col.Attribute.IsPrimary = true;
}