/// <summary>
        /// 添加导航属性关联
        /// </summary>
        protected override void TanslateNavMember()
        {
            if (base.NavMembers == null || base.NavMembers.Count == 0)
            {
                return;
            }

            // 如果有一对多的导航属性,肯定会产生嵌套查询。那么内层查询别名肯定是t0,所以需要清掉
            if (this.HasMany)
            {
                _ag = new AliasGenerator(_ag.ReserveQty);
            }
            //开始产生LEFT JOIN 子句
            ISqlBuilder builder = this.JoinFragment;

            foreach (var nav in base.NavMembers)
            {
                string              key         = nav.Key;
                MemberExpression    m           = nav.Expression;
                TypeRuntimeInfo     typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(m.Expression.Type);
                ForeignKeyAttribute attribute   = typeRuntime.GetMemberAttribute <ForeignKeyAttribute>(m.Member.Name);

                string innerKey   = string.Empty;
                string outerKey   = key;
                string innerAlias = string.Empty;

                if (!m.Expression.Visitable())
                {
                    innerKey = m.Expression.NodeType == ExpressionType.Parameter
                        ? (m.Expression as ParameterExpression).Name
                        : (m.Expression as MemberExpression).Member.Name;
                }
                else
                {
                    MemberExpression mLeft = null;
                    if (m.Expression.NodeType == ExpressionType.MemberAccess)
                    {
                        mLeft = m.Expression as MemberExpression;
                    }
                    else if (m.Expression.NodeType == ExpressionType.Call)
                    {
                        mLeft = (m.Expression as MethodCallExpression).Object as MemberExpression;
                    }
                    string name = TypeRuntimeInfoCache.GetRuntimeInfo(mLeft.Type).TableFullName;
                    innerAlias = _ag.GetJoinTableAlias(name);

                    if (string.IsNullOrEmpty(innerAlias))
                    {
                        string keyLeft = mLeft.GetKeyWidthoutAnonymous();
                        if (base.NavMembers.Contains(keyLeft))
                        {
                            innerKey = keyLeft;
                        }
                        innerAlias = _ag.GetNavTableAlias(innerKey);
                    }
                }

                string alias1 = !string.IsNullOrEmpty(innerAlias) ? innerAlias : _ag.GetTableAlias(innerKey);
                string alias2 = _ag.GetNavTableAlias(outerKey);


                builder.AppendNewLine();
                builder.Append("LEFT JOIN ");
                Type type = m.Type;
                if (type.IsGenericType)
                {
                    type = type.GetGenericArguments()[0];
                }
                var typeRuntime2 = TypeRuntimeInfoCache.GetRuntimeInfo(type);
                builder.AppendTable(typeRuntime2.TableSchema, typeRuntime2.TableFullName, typeRuntime2.IsTemporary);
                builder.Append(" ");
                builder.Append(alias2);

                bool withNoLock = !typeRuntime2.IsTemporary && _isNoLock && !string.IsNullOrEmpty(_withNoLock);
                if (withNoLock)
                {
                    builder.Append(' ');
                    builder.Append(_withNoLock);
                }

                builder.Append(" ON ");
                for (int i = 0; i < attribute.InnerKeys.Length; i++)
                {
                    if (attribute.InnerKeys[i].StartsWith(AppConst.CONSTANT_FOREIGNKEY, StringComparison.Ordinal))
                    {
                        builder.Append(attribute.InnerKeys[i].Substring(7));
                    }
                    else
                    {
                        builder.Append(alias1);
                        builder.Append('.');
                        builder.AppendMember(attribute.InnerKeys[i]);
                    }

                    builder.Append(" = ");

                    if (attribute.OuterKeys[i].StartsWith(AppConst.CONSTANT_FOREIGNKEY, StringComparison.Ordinal))
                    {
                        builder.Append(attribute.OuterKeys[i].Substring(7));
                    }
                    else
                    {
                        builder.Append(alias2);
                        builder.Append('.');
                        builder.AppendMember(attribute.OuterKeys[i]);
                    }

                    if (i < attribute.InnerKeys.Length - 1)
                    {
                        builder.Append(" AND ");
                    }
                }

                if (nav.Predicate != null)
                {
                    string alias   = _ag.GetNavTableAlias(nav.Key);
                    var    visitor = new NavPredicateExpressionVisitor(_ag, builder, alias);
                    visitor.Visit(nav.Predicate);
                }
            }
        }
        /// <summary>
        /// 添加导航属性关联
        /// </summary>
        protected override void TanslateNavMember()
        {
            if (this.NavMembers == null || this.NavMembers.Count == 0)
            {
                return;
            }

            // 如果有一对多的导航属性,肯定会产生嵌套查询。那么内层查询别名肯定是t0,所以需要清掉
            if (this.HasMany)
            {
                _aliasGenerator = new AliasGenerator(_aliasGenerator.ReserveQty);
            }
            //开始产生 USING 子句
            ISqlBuilder jf    = this.JoinFragment;
            int         index = -1;

            // 未生成USING子句
            if (_aliasGenerator.ReserveQty <= 1)
            {
                jf.AppendNewLine();
                jf.Append(_keywordName);
            }
            else
            {
                jf.Append(',');
                jf.AppendNewLine();
            }

            foreach (var nav in this.NavMembers)
            {
                index++;
                string              key         = nav.Key;
                MemberExpression    m           = nav.Expression;
                TypeRuntimeInfo     typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(m.Expression.Type);
                ForeignKeyAttribute attribute   = typeRuntime.GetMemberAttribute <ForeignKeyAttribute>(m.Member.Name);

                string innerKey   = string.Empty;
                string outerKey   = key;
                string innerAlias = string.Empty;

                if (!m.Expression.Visitable())
                {
                    innerKey = m.Expression.NodeType == ExpressionType.Parameter
                        ? (m.Expression as ParameterExpression).Name
                        : (m.Expression as MemberExpression).Member.Name;
                }
                else
                {
                    MemberExpression mLeft = null;
                    if (m.Expression.NodeType == ExpressionType.MemberAccess)
                    {
                        mLeft = m.Expression as MemberExpression;
                    }
                    else if (m.Expression.NodeType == ExpressionType.Call)
                    {
                        mLeft = (m.Expression as MethodCallExpression).Object as MemberExpression;
                    }
                    string name = TypeRuntimeInfoCache.GetRuntimeInfo(mLeft.Type).TableFullName;
                    innerAlias = _aliasGenerator.GetJoinTableAlias(name);

                    if (string.IsNullOrEmpty(innerAlias))
                    {
                        string keyLeft = mLeft.GetKeyWidthoutAnonymous();
                        if (this.NavMembers.Contains(keyLeft))
                        {
                            innerKey = keyLeft;
                        }
                        innerAlias = _aliasGenerator.GetNavTableAlias(innerKey);
                    }
                }

                string alias1 = !string.IsNullOrEmpty(innerAlias) ? innerAlias : _aliasGenerator.GetTableAlias(innerKey);
                string alias2 = _aliasGenerator.GetNavTableAlias(outerKey);

                // 补充与USING字符串同等间距的空白
                if (_aliasGenerator.ReserveQty > 1 || index > 0)
                {
                    jf.Append(_pad);
                }

                Type type         = m.Type;
                var  typeRumtime2 = TypeRuntimeInfoCache.GetRuntimeInfo(type);
                if (type.IsGenericType)
                {
                    type = type.GetGenericArguments()[0];
                }
                jf.AppendTable(typeRumtime2.TableSchema, typeRumtime2.TableName, typeRumtime2.IsTemporary);
                jf.Append(' ');
                jf.Append(alias2);

                if (_onPhrase.Length > 0)
                {
                    _onPhrase.Append(" AND ");
                }
                for (int i = 0; i < attribute.InnerKeys.Length; i++)
                {
                    if (attribute.InnerKeys[i].StartsWith(AppConst.CONSTANT_FOREIGNKEY, StringComparison.Ordinal))
                    {
                        _onPhrase.Append(attribute.InnerKeys[i].Substring(7));
                    }
                    else
                    {
                        _onPhrase.Append(alias1);
                        _onPhrase.Append('.');
                        _onPhrase.AppendMember(attribute.InnerKeys[i]);
                    }

                    _onPhrase.Append(" = ");

                    if (attribute.OuterKeys[i].StartsWith(AppConst.CONSTANT_FOREIGNKEY, StringComparison.Ordinal))
                    {
                        _onPhrase.Append(attribute.OuterKeys[i].Substring(7));
                    }
                    else
                    {
                        _onPhrase.Append(alias2);
                        _onPhrase.Append('.');
                        _onPhrase.AppendMember(attribute.OuterKeys[i]);
                    }
                }

                if (nav.Predicate != null)
                {
                    string alias   = _aliasGenerator.GetNavTableAlias(nav.Key);
                    var    visitor = new NavPredicateExpressionVisitor(_aliasGenerator, _onPhrase, alias);
                    visitor.Visit(nav.Predicate);
                }

                if (index < this.NavMembers.Count - 1)
                {
                    jf.Append(',');
                    jf.AppendNewLine();
                }
            }
        }
Esempio n. 3
0
        // 获取成员包装器的外键特性
        private ForeignKeyAttribute GetForeignKeyAttribute(FieldAccessorBase m, string memberName)
        {
            if (m == null)
            {
                throw new XFrameworkException("Member {0}.{1} not found.", _type.Name, memberName);
            }
            var attribute = m.GetCustomAttribute <ForeignKeyAttribute>();

            if (attribute == null)
            {
                // 如果是属性,要求标记为 virtual
                var property = m as PropertyAccessor;
                if (property != null)
                {
                    var getMethod = property.Member.GetGetMethod(true);
                    if (getMethod != null && !getMethod.IsVirtual)
                    {
                        return(null);
                    }
                }

                // 区分一对一和一对多导航属性
                var navEntityType  = m.CLRType.IsGenericType ? m.CLRType.GetGenericArguments()[0] : m.CLRType;
                var navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navEntityType);
                if (TypeUtils.IsCollectionType(m.CLRType))
                {
                    // 1:n 关系,外键实体必须持有当前实体的所有主键属性

                    int      index     = 0;
                    string[] innerKeys = null;
                    string[] outerKeys = null;
                    foreach (FieldAccessorBase inner in this.KeyMembers)
                    {
                        if (!navTypeRuntime.Members.Contains(inner.Name))
                        {
                            innerKeys = null;
                            outerKeys = null;
                            break;
                        }
                        else
                        {
                            var outer = navTypeRuntime.GetMember <FieldAccessorBase>(inner.Name);
                            if (outer == null)
                            {
                                innerKeys = null;
                                outerKeys = null;
                                break;
                            }
                            else
                            {
                                if (innerKeys == null)
                                {
                                    innerKeys = new string[this.KeyMembers.Count];
                                }
                                if (outerKeys == null)
                                {
                                    outerKeys = new string[this.KeyMembers.Count];
                                }
                                innerKeys[index] = inner.Column != null && !string.IsNullOrEmpty(inner.Column.Name) ? inner.Column.Name : inner.Name;
                                outerKeys[index] = inner.Column != null && !string.IsNullOrEmpty(inner.Column.Name) ? inner.Column.Name : inner.Name;
                            }
                        }

                        index += 1;
                    }

                    if (innerKeys != null)
                    {
                        attribute = new ForeignKeyAttribute(innerKeys, outerKeys);
                    }
                }
                else
                {
                    // 1:1 关系,分两种情况
                    // 1. 当前实体持有外键实体的主键属性
                    // 2. 外键实体持有当前实体的主键属性

                    if (navTypeRuntime.KeyMembers.Count > 0)
                    {
                        int      index     = 0;
                        string[] innerKeys = null;
                        string[] outerKeys = null;
                        foreach (FieldAccessorBase outer in navTypeRuntime.KeyMembers)
                        {
                            if (!this.Members.Contains(outer.Name))
                            {
                                innerKeys = null;
                                outerKeys = null;
                                break;
                            }
                            else
                            {
                                var inner = this.GetMember <FieldAccessorBase>(outer.Name);
                                if (inner == null)
                                {
                                    innerKeys = null;
                                    outerKeys = null;
                                    break;
                                }
                                else
                                {
                                    if (innerKeys == null)
                                    {
                                        innerKeys = new string[navTypeRuntime.KeyMembers.Count];
                                    }
                                    if (outerKeys == null)
                                    {
                                        outerKeys = new string[navTypeRuntime.KeyMembers.Count];
                                    }
                                    innerKeys[index] = inner.Column != null && !string.IsNullOrEmpty(inner.Column.Name) ? inner.Column.Name : inner.Name;
                                    outerKeys[index] = outer.Column != null && !string.IsNullOrEmpty(outer.Column.Name) ? outer.Column.Name : outer.Name;
                                }
                            }

                            index += 1;
                        }

                        if (innerKeys != null)
                        {
                            attribute = new ForeignKeyAttribute(innerKeys, outerKeys);
                        }
                    }

                    if (attribute == null)
                    {
                        int      index     = 0;
                        string[] innerKeys = null;
                        string[] outerKeys = null;
                        foreach (FieldAccessorBase inner in this.KeyMembers)
                        {
                            if (!navTypeRuntime.Members.Contains(inner.Name))
                            {
                                innerKeys = null;
                                outerKeys = null;
                                break;
                            }
                            else
                            {
                                var outer = navTypeRuntime.GetMember <FieldAccessorBase>(inner.Name);
                                if (outer == null)
                                {
                                    innerKeys = null;
                                    outerKeys = null;
                                    break;
                                }
                                else
                                {
                                    if (innerKeys == null)
                                    {
                                        innerKeys = new string[this.KeyMembers.Count];
                                    }
                                    if (outerKeys == null)
                                    {
                                        outerKeys = new string[this.KeyMembers.Count];
                                    }
                                    innerKeys[index] = inner.Column != null && !string.IsNullOrEmpty(inner.Column.Name) ? inner.Column.Name : inner.Name;
                                    outerKeys[index] = inner.Column != null && !string.IsNullOrEmpty(inner.Column.Name) ? inner.Column.Name : inner.Name;
                                }
                            }

                            index += 1;
                        }

                        if (innerKeys != null)
                        {
                            attribute = new ForeignKeyAttribute(innerKeys, outerKeys);
                        }
                    }
                }
            }

            return(attribute);
        }