private static string GetJoinSql(DataTable chainRoot, SqlDefinition exp)
        {
            StringBuilder sql = new StringBuilder();

            using (var temp = TempIndex.Borrow())
            {
                var index = temp.Item;
                FillJoinSql(chainRoot, chainRoot, exp, sql, index);
            }

            return(sql.ToString());
        }
        /// <summary>
        /// 获取表<paramref name="chainRoot"/>需要查询的select字段
        /// </summary>
        /// <param name="chainRoot"></param>
        /// <param name="exp"></param>
        /// <param name="chain">可以为输出的字段前置对象链</param>
        /// <returns></returns>
        private static string GetSelectFieldsSql(DataTable chainRoot, SqlDefinition exp)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(GetChainRootSelectFieldsSql(chainRoot, exp).Trim());

            using (var temp = TempIndex.Borrow())
            {
                var index = temp.Item;
                sql.Append(GetSlaveSelectFieldsSql(chainRoot, chainRoot, exp, index).Trim());
            }
            sql.Length--;//移除最后一个逗号
            return(sql.ToString());
        }
Exemple #3
0
 private static void FillQuoteMiddlesByMaster(DataTable target, DataTable current, List <DataTable> result, TempIndex index)
 {
     foreach (var child in current.BuildtimeChilds)
     {
         if (!index.TryAdd(child))
         {
             continue;                       //尝试添加索引失败,证明已经处理,这通常是由于循环引用导致的死循环,用临时索引可以避免该问题
         }
         if (child.Type == DataTableType.Middle)
         {
             if (child.Master.Name.EqualsIgnoreCase(target.Name))
             {
                 result.Add(child);
             }
         }
         FillQuoteMiddlesByMaster(target, child, result, index);
     }
 }
        private static void FillJoinSqlByNoDerived(DataTable chainRoot, DataTable master, DataTable current, string masterChain, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            if (!ContainsTable(chainRoot, exp, current))
            {
                return;
            }
            var tip = current.MemberPropertyTip;

            var chain = current.GetChainCode(chainRoot);

            sql.AppendLine();

            string masterTableName = string.IsNullOrEmpty(masterChain) ? master.Name : masterChain;

            sql.AppendFormat(" left join {0} as {1} on {2}.{3}Id={1}.Id",
                             SqlStatement.Qualifier(current.Name),
                             SqlStatement.Qualifier(chain),
                             SqlStatement.Qualifier(masterTableName),
                             tip.PropertyName);

            FillChildJoinSql(chainRoot, current, exp, sql, index);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="chainRoot">是查询的根表</param>
        /// <param name="master"></param>
        /// <param name="exp"></param>
        /// <param name="masterProxyName"></param>
        /// <param name="sql"></param>
        private static void FillChildJoinSql(DataTable chainRoot, DataTable master, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            var masterChain = master.GetChainCode(chainRoot);

            foreach (var child in master.BuildtimeChilds)
            {
                if (!index.TryAdd(child))
                {
                    continue;                       //防止由于循环引用导致的死循环
                }
                if (child.IsDerived)
                {
                    FillJoinSqlByDerived(chainRoot, master, child, masterChain, exp, sql, index);
                }
                else
                {
                    FillJoinSqlByNoDerived(chainRoot, master, child, masterChain, exp, sql, index);
                }
            }
        }
 private static void FillJoinSql(DataTable chainRoot, DataTable master, SqlDefinition exp, StringBuilder sql, TempIndex index)
 {
     if (master.IsDerived)
     {
         var inheritedRoot = master.InheritedRoot;
         FillChildJoinSql(chainRoot, inheritedRoot, exp, sql, index);
         foreach (var derived in master.Deriveds)
         {
             FillChildJoinSql(chainRoot, derived, exp, sql, index);
         }
     }
     else
     {
         FillChildJoinSql(chainRoot, chainRoot, exp, sql, index);
     }
 }
        private static void FillFieldsSql(DataTable chainRoot, DataTable master, DataTable current, TableType tableType, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            if (!ContainsTable(chainRoot, exp, current))
            {
                return;
            }

            var chain = current.GetChainCode(chainRoot);

            sql.AppendLine();

            foreach (var field in current.Fields)
            {
                if (field.Tip.Lazy)
                {
                    continue;
                }

                if (tableType == TableType.Derived)
                {
                    if (field.Name == EntityObject.IdPropertyName || field.Name == current.Root.TableIdName)
                    {
                        continue;
                    }
                }

                var fieldName = string.Format("{0}_{1}", chain, field.Name);

                if (!ContainsField(fieldName, exp))
                {
                    continue;
                }

                sql.AppendFormat("{0}.{1} as {2},", SqlStatement.Qualifier(chain),
                                 SqlStatement.Qualifier(field.Name),
                                 SqlStatement.Qualifier(fieldName));
            }
            FillChildSelectFieldsSql(chainRoot, current, exp, sql, index);
        }
        private static void FillChildSelectFieldsSql(DataTable chainRoot, DataTable master, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            foreach (var child in master.BuildtimeChilds)
            {
                if (!index.TryAdd(child))
                {
                    continue;                       //防止由于循环引用导致的死循环
                }
                if (child.IsDerived)
                {
                    FillFieldsSql(chainRoot, master, child.InheritedRoot, TableType.InheritedRoot, exp, sql, index);

                    foreach (var derived in child.Deriveds)
                    {
                        FillFieldsSql(chainRoot, master, derived, TableType.Derived, exp, sql, index);
                    }
                }
                else
                {
                    FillFieldsSql(chainRoot, master, child, TableType.Common, exp, sql, index);
                }
            }
        }
        /// <summary>
        /// 填充查询链中从表的select的字段
        /// </summary>
        /// <param name="chainRoot"></param>
        /// <param name="master"></param>
        /// <param name="exp"></param>
        /// <param name="sql"></param>
        private static string GetSlaveSelectFieldsSql(DataTable chainRoot, DataTable master, SqlDefinition exp, TempIndex index)
        {
            StringBuilder sql = new StringBuilder();

            if (master.IsDerived)
            {
                FillChildSelectFieldsSql(chainRoot, master.InheritedRoot, exp, sql, index);

                foreach (var derived in master.Deriveds)
                {
                    FillChildSelectFieldsSql(chainRoot, derived, exp, sql, index);
                }
            }
            else
            {
                FillChildSelectFieldsSql(chainRoot, master, exp, sql, index);
            }
            return(sql.ToString());
        }
        private static void FillJoinSqlByNoDerived(DataTable chainRoot, DataTable master, DataTable current, string masterChain, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            if (!ContainsTable(chainRoot, exp, current))
            {
                return;
            }
            var    chain           = current.GetChainCode(chainRoot);
            string masterTableName = string.IsNullOrEmpty(masterChain) ? master.Name : masterChain;

            sql.AppendLine();
            if (current.IsMultiple)
            {
                var middle       = current.Middle;
                var masterIdName = middle.Root == middle.Master ? GeneratedField.RootIdName : GeneratedField.MasterIdName;
                if (current.Type == DataTableType.AggregateRoot)
                {
                    sql.AppendFormat(" left join {0} on {0}.{1}={2}.Id left join {3} as {4} on {0}.{5}={4}.Id",
                                     SqlStatement.Qualifier(middle.Name),
                                     SqlStatement.Qualifier(masterIdName),
                                     SqlStatement.Qualifier(masterTableName),
                                     SqlStatement.Qualifier(current.Name),
                                     SqlStatement.Qualifier(chain),
                                     GeneratedField.SlaveIdName);
                }
                else
                {
                    //中间的查询会多一个{4}.{6}={2}.Id的限定,
                    sql.AppendFormat(" left join {0} on {0}.{1}={2}.Id left join {3} as {4} on {0}.{5}={4}.Id and {4}.{6}={2}.Id",
                                     SqlStatement.Qualifier(middle.Name),
                                     SqlStatement.Qualifier(masterIdName),
                                     SqlStatement.Qualifier(masterTableName),
                                     SqlStatement.Qualifier(current.Name),
                                     SqlStatement.Qualifier(chain),
                                     GeneratedField.SlaveIdName,
                                     GeneratedField.RootIdName);
                }
            }
            else
            {
                var tip = current.MemberPropertyTip;
                sql.AppendFormat(" left join {0} as {1} on {2}.{3}Id={1}.Id",
                                 SqlStatement.Qualifier(current.Name),
                                 SqlStatement.Qualifier(chain),
                                 SqlStatement.Qualifier(masterTableName),
                                 tip.PropertyName);
            }

            FillChildJoinSql(chainRoot, current, exp, sql, index);
        }
Exemple #11
0
        private static void FillJoinSqlByNoDerived(DataTable chainRoot, DataTable master, DataTable current, string masterChain, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            if (!ContainsTable(chainRoot, exp, current))
            {
                return;
            }
            var    chain           = current.GetChainCode(chainRoot);
            string masterTableName = string.IsNullOrEmpty(masterChain) ? master.Name : masterChain;

            string currentTenantSql = current.IsEnabledMultiTenancy ? string.Format("and {0}.{1}=@{2}", SqlStatement.Qualifier(chain),
                                                                                    SqlStatement.Qualifier(GeneratedField.TenantIdName),
                                                                                    GeneratedField.TenantIdName) : string.Empty;

            sql.AppendLine();
            if (current.IsMultiple)
            {
                var middle       = current.Middle;
                var masterIdName = middle.Root == middle.Master ? GeneratedField.RootIdName : GeneratedField.MasterIdName;

                string middleTenantSql = middle.IsEnabledMultiTenancy ? string.Format("and {0}.{1}=@{2}", SqlStatement.Qualifier(middle.Name),
                                                                                      SqlStatement.Qualifier(GeneratedField.TenantIdName),
                                                                                      GeneratedField.TenantIdName) : string.Empty;


                if (current.Type == DataTableType.AggregateRoot)
                {
                    sql.AppendFormat(" left join {0}{6} on {0}.{1}={2}.Id {7} left join {3} as {4}{6} on {0}.{5}={4}.Id {8}",
                                     SqlStatement.Qualifier(middle.Name),
                                     SqlStatement.Qualifier(masterIdName),
                                     SqlStatement.Qualifier(masterTableName),
                                     SqlStatement.Qualifier(current.Name),
                                     SqlStatement.Qualifier(chain),
                                     GeneratedField.SlaveIdName,
                                     GetLockCode(QueryLevel.None),
                                     middleTenantSql,
                                     currentTenantSql);
                }
                else
                {
                    //中间的查询会多一个{4}.{6}={2}.Id的限定,
                    sql.AppendFormat(" left join {0}{7} on {0}.{1}={2}.Id {8} left join {3} as {4}{7} on {0}.{5}={4}.Id and {4}.{6}={2}.Id {9}",
                                     SqlStatement.Qualifier(middle.Name),
                                     SqlStatement.Qualifier(masterIdName),
                                     SqlStatement.Qualifier(masterTableName),
                                     SqlStatement.Qualifier(current.Name),
                                     SqlStatement.Qualifier(chain),
                                     GeneratedField.SlaveIdName,
                                     GeneratedField.RootIdName,
                                     GetLockCode(QueryLevel.None),
                                     middleTenantSql,
                                     currentTenantSql);
                }
            }
            else
            {
                if (current.Type == DataTableType.AggregateRoot)
                {
                    var tip = current.MemberPropertyTip;
                    sql.AppendFormat(" left join {0} as {1}{4} on {2}.{3}Id={1}.Id {5}",
                                     SqlStatement.Qualifier(current.Name),
                                     SqlStatement.Qualifier(chain),
                                     SqlStatement.Qualifier(masterTableName),
                                     tip.PropertyName,
                                     GetLockCode(QueryLevel.None),
                                     currentTenantSql);
                }
                else
                {
                    if (chainRoot.Type == DataTableType.AggregateRoot)
                    {
                        string rootTableName = chainRoot.Name;
                        var    tip           = current.MemberPropertyTip;
                        sql.AppendFormat(" left join {0} as {1}{4} on {2}.{3}Id={1}.Id and {1}.{5}={6}.Id {7}",
                                         SqlStatement.Qualifier(current.Name),
                                         SqlStatement.Qualifier(chain),
                                         SqlStatement.Qualifier(masterTableName),
                                         tip.PropertyName,
                                         GetLockCode(QueryLevel.None),
                                         GeneratedField.RootIdName,
                                         SqlStatement.Qualifier(rootTableName),
                                         currentTenantSql);
                    }
                    else
                    {
                        //查询不是从根表发出的,而是从引用表,那么直接用@RootId来限定
                        var tip = current.MemberPropertyTip;
                        sql.AppendFormat(" left join {0} as {1}{4} on {2}.{3}Id={1}.Id and {1}.{5}=@{5} {6}",
                                         SqlStatement.Qualifier(current.Name),
                                         SqlStatement.Qualifier(chain),
                                         SqlStatement.Qualifier(masterTableName),
                                         tip.PropertyName,
                                         GetLockCode(QueryLevel.None),
                                         GeneratedField.RootIdName,
                                         currentTenantSql);
                    }
                }
            }

            FillChildJoinSql(chainRoot, current, exp, sql, index);
        }
Exemple #12
0
        private static void FillFieldsSql(DataTable chainRoot, DataTable master, DataTable current, TableType tableType, SqlDefinition exp, StringBuilder sql, TempIndex index)
        {
            if (!ContainsTable(chainRoot, exp, current))
            {
                return;
            }

            var chain = current.GetChainCode(chainRoot);

            sql.AppendLine();

            foreach (var field in current.Fields)
            {
                if (field.IsAdditional)
                {
                    continue;                     //不输出附加字段,有这类需求请自行编码sql语句,因为附加字段的定制化需求统一由数据映射器处理
                }
                if (field.Tip.Lazy && !exp.SpecifiedField(field.Name))
                {
                    continue;
                }

                if (tableType == TableType.Derived)
                {
                    if (field.Name == EntityObject.IdPropertyName || field.Name == GeneratedField.RootIdName)
                    {
                        continue;
                    }
                }

                var fieldName = string.Format("{0}_{1}", chain, field.Name);

                if (!ContainsField(fieldName, exp))
                {
                    continue;
                }

                sql.AppendFormat("{0}.{1} as {2},", SqlStatement.Qualifier(chain),
                                 SqlStatement.Qualifier(field.Name),
                                 SqlStatement.Qualifier(fieldName));
            }

            if (current.IsEnabledMultiTenancy)
            {
                if (tableType != TableType.Derived)
                {
                    var fieldName = string.Format("{0}_{1}", chain, GeneratedField.TenantIdName);
                    sql.AppendFormat("{0}.{1} as {2},", SqlStatement.Qualifier(chain),
                                     SqlStatement.Qualifier(GeneratedField.TenantIdName),
                                     fieldName);
                }
            }

            FillChildSelectFieldsSql(chainRoot, current, exp, sql, index);
        }