Example #1
0
        /// <summary>
        /// 获取兄弟及他们的孩子。
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="includeCurrent">是否包含当 <paramref name="argument"/>,当在它前面插入时,需要包含它。</param>
        /// <param name="excludeArg">要排除的实体。</param>
        /// <param name="isTop">是否遇到要排除的实体就终止。</param>
        /// <param name="isolation"></param>
        /// <returns></returns>
        private IEnumerable <TEntity> GetBrothersAndChildren(EntityTreeUpfydatingArgument argument, bool includeCurrent, EntityTreeUpfydatingArgument excludeArg, bool isTop = false, Expression <Func <TEntity> > isolation = null)
        {
            var keyId      = argument.OldValue.InnerId;
            var order      = argument.OldValue.Order;
            var level      = argument.OldValue.Level;
            var parameters = new ParameterCollection();
            var m          = EntityMetadataUnity.GetEntityMetadata(entityType);

            var sb = new StringBuilder();

            sb.Append("SELECT ");
            var assert = new AssertFlag();

            foreach (var property in GetUseableProperties())
            {
                if (property == null)
                {
                    continue;
                }

                if (!assert.AssertTrue())
                {
                    sb.Append(", ");
                }

                sb.AppendFormat("t.{0} {0}", QuoteColumn(property));
            }

            sb.AppendFormat(" FROM {0} t", GetTableName());
            sb.AppendFormat(" JOIN (SELECT f.{0} {0} FROM {1} f", QuoteColumn(metaTree.InnerSign), GetTableName());

            sb.AppendFormat(" WHERE {0} LIKE {1} AND {5} = {6} AND {2} {4} {3}", QuoteColumn(metaTree.InnerSign), syntax.FormatParameter("pn"),
                            GetOrderExpression(), order, includeCurrent ? ">=" : ">", GetLevelExpression(), level);

            if (m.DeleteProperty != null)
            {
                sb.AppendFormat(" AND {0} = {1}", QuoteColumn(m.DeleteProperty), 0);
            }

            if (excludeArg != null)
            {
                var excludeId = excludeArg.OldValue.InnerId;
                sb.AppendFormat(" AND NOT ({0} LIKE {1})", QuoteColumn(metaTree.InnerSign), syntax.FormatParameter("px"));
                parameters.Add("px", excludeId + "%");

                if (isTop)
                {
                    sb.AppendFormat(" AND {0} < {1}", QuoteColumn(metaTree.InnerSign), syntax.FormatParameter("px1"));
                    parameters.Add("px1", excludeId);
                }
            }

            if (!includeCurrent)
            {
                sb.AppendFormat(" AND {0} <> {1}", QuoteColumn(metaTree.InnerSign), syntax.FormatParameter("pm"));
                parameters.Add("pm", keyId);
            }

            var conIsolation = string.Empty;

            if (isolation != null)
            {
                conIsolation = IsolationConditionBuilder.Build(isolation);
                if (!string.IsNullOrEmpty(conIsolation))
                {
                    sb.AppendFormat(" AND {0}", conIsolation);
                }
            }

            sb.AppendFormat(") f ON t.{0} LIKE {1}", QuoteColumn(metaTree.InnerSign), syntax.String.Concat("f." + QuoteColumn(metaTree.InnerSign), "'%'"));

            if (!string.IsNullOrEmpty(conIsolation))
            {
                sb.AppendFormat(" WHERE {0}", conIsolation);
            }

            sb.AppendFormat("ORDER BY {0}", QuoteColumn(metaTree.InnerSign));

            keyId = GetPreviousKey(keyId) + "_%";
            parameters.Add("pn", keyId);

            return(database.ExecuteEnumerable((SqlCommand)sb.ToString(), parameters: parameters, rowMapper: new EntityRowMapper <TEntity>()).ToList());
        }
Example #2
0
        /// <summary>
        /// 尝试创建对应的表。
        /// </summary>
        /// <param name="service"></param>
        /// <param name="entityType">实体类型。</param>
        /// <returns></returns>
        public static bool TryCreate(IContextService service, Type entityType)
        {
            var tbGen = service.Provider.GetTableGenerateProvider();

            if (service == null)
            {
                return(false);
            }

            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);

            var instanceName = ContextInstanceManager.TryAdd(service.InitializeContext);

            var cacheKey = string.Concat(instanceName, ":", entityType.FullName);

            return(cache.GetOrAdd(cacheKey, () =>
            {
                //判断数据表是否已存在
                if (tbGen.IsExists(service.Database, metadata))
                {
                    try
                    {
                        //尝试添加新的字段
                        var properties = tbGen.TryAddFields(service.Database, metadata);
                        if (properties.Any())
                        {
                            //通知 context 仓储已经以身改变
                            service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                            {
                                Succeed = true,
                                EntityType = entityType,
                                EventType = RespositoryChangeEventType.AddFields,
                                AddedProperties = new ReadOnlyCollection <IProperty>(properties)
                            });
                        }
                    }
                    catch (Exception exp)
                    {
                        service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                        {
                            EntityType = entityType,
                            EventType = RespositoryChangeEventType.AddFields,
                            Exception = exp
                        });
                        return false;
                    }
                }
                else
                {
                    try
                    {
                        //尝试创建数据表
                        if (tbGen.TryCreate(service.Database, metadata))
                        {
                            //通知 context 仓储已经创建
                            service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                            {
                                Succeed = true,
                                EntityType = entityType,
                                EventType = RespositoryChangeEventType.CreateTable
                            });
                        }
                    }
                    catch (Exception exp)
                    {
                        service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                        {
                            EntityType = entityType,
                            EventType = RespositoryChangeEventType.CreateTable,
                            Exception = exp
                        });
                        return false;
                    }
                }

                return true;
            }));
        }
Example #3
0
 private EntityTreeMetadata GetMetadata()
 {
     return(metadata ?? (metadata = EntityMetadataUnity.GetEntityMetadata(typeof(TEntity)).EntityTree));
 }
 /// <summary>
 /// 使用所添加的变量解析实体映射的数据表名称。
 /// </summary>
 /// <param name="entityType">实体类型。</param>
 /// <returns></returns>
 public string GetVariableTableName(Type entityType)
 {
     Guard.ArgumentNull(entityType, nameof(entityType));
     return(GetVariableTableName(EntityMetadataUnity.GetEntityMetadata(entityType)));
 }