Ejemplo n.º 1
0
            public static string Build(Expression expression)
            {
                var builder = new IsolationConditionBuilder();

                builder.Visit(expression);
                return(builder.condition.ToString());
            }
Ejemplo n.º 2
0
        /// <summary>
        /// 取顶层的最大order值。
        /// </summary>
        /// <returns></returns>
        private int GetNewOrderNumber(Expression <Func <TEntity> > isolation = null)
        {
            var sql = string.Format("SELECT MAX({0}) FROM {1} WHERE {2} = {3}",
                                    GetOrderExpression(),
                                    DbUtility.FormatByQuote(syntax, metadata.TableName),
                                    syntax.String.Length(QuoteColumn(metaTree.InnerSign)),
                                    metaTree.SignLength);

            if (isolation != null)
            {
                var condition = IsolationConditionBuilder.Build(isolation);
                if (!string.IsNullOrEmpty(condition))
                {
                    sql += " AND " + condition;
                }
            }

            return(database.ExecuteScalar((SqlCommand)sql).To <int>() + 1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 取参照实体的最大order值。
        /// </summary>
        /// <param name="bag"></param>
        /// <param name="mode"></param>
        /// <param name="offset"></param>
        /// <param name="isolation"></param>
        /// <returns></returns>
        private int GetNewOrderNumber(EntityTreeUpdatingBag bag, EntityTreePosition mode, int offset = 0, Expression <Func <TEntity> > isolation = null)
        {
            if (bag == null)
            {
                return(GetNewOrderNumber(isolation));
            }

            switch (mode)
            {
            case EntityTreePosition.Children:
                var sql = string.Format("SELECT MAX({0}) FROM {1} WHERE {2} LIKE {3}",
                                        GetOrderExpression(),
                                        DbUtility.FormatByQuote(syntax, metadata.TableName),
                                        QuoteColumn(metaTree.InnerSign),
                                        syntax.FormatParameter("pm"));
                var innerId = bag.InnerId;

                if (isolation != null)
                {
                    var condition = IsolationConditionBuilder.Build(isolation);
                    if (!string.IsNullOrEmpty(condition))
                    {
                        sql += " AND " + condition;
                    }
                }

                var parameters = new ParameterCollection {
                    { "pm", innerId + new string('_', metaTree.SignLength) }
                };
                return(database.ExecuteScalar((SqlCommand)sql, parameters).To <int>() + 1 + offset);

            case EntityTreePosition.Before:
                return(bag.Order + offset);

            case EntityTreePosition.After:
                return(bag.Order + 1 + offset);
            }

            return(0);
        }
Ejemplo n.º 4
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());
        }