예제 #1
0
        /// <summary>
        /// 重写列表变化事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnListChanged(ListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemChanged)
            {
                // 添加以后需要强制设置为状态跟踪
                this[e.NewIndex].SetTraceChange(true);

                DTOBase changeItem = this[e.NewIndex];

                // 将对象的UnChange状态修改为Update状态
                if (changeItem.GetState() == DTOState.UnChange)
                {
                    changeItem.SetState(DTOState.Update);
                }
            }
            else if (e.ListChangedType == ListChangedType.ItemAdded)
            {
                // 添加以后需要强制设置为状态跟踪
                this[e.NewIndex].SetTraceChange(true);

                this[e.NewIndex].SetState(DTOState.New);
            }
            base.OnListChanged(e);
        }
예제 #2
0
        /// <summary>
        /// 构造查询语句
        /// </summary>
        /// <param name="Entity">DTO</param>
        /// <returns></returns>
        public static string Build(DTOBase Entity)
        {
            StringBuilder sql = new StringBuilder(256);

            var type = Entity.GetType();

            sql.Append("select ");

            if (Entity.SelectedColumns.Count == 0)
            {
                type.GetProperties().ToList().ForEach(o =>
                {
                    var notfieldAttr = o.GetCustomAttributes(typeof(NotFieldAttribute), false);
                    if (!notfieldAttr.Any())
                    {
                        var dattr = o.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (!dattr.Any())
                        {
                            var fattr = o.GetCustomAttributes(typeof(FictitiousAttribute), false);
                            if (!fattr.Any())
                            {
                                throw new DTOException("属性" + o.Name + "未标记为DescriptionAttribute或者FictitiousAttribute");
                            }
                            else
                            {
                                if (string.IsNullOrWhiteSpace((fattr[0] as FictitiousAttribute)?.DescribColmun))
                                {
                                    sql.Append(o);
                                    sql.Append(',');
                                }
                                else
                                {
                                    sql.Append((fattr[0] as FictitiousAttribute)?.DescribColmun);
                                    sql.Append(' ');
                                    sql.Append(o);
                                    sql.Append(',');
                                }
                            }
                        }
                        else
                        {
                            sql.Append(o.Name);
                            sql.Append(',');
                        }
                    }
                });
            }
            else
            {
                var propertys = type.GetProperties().ToList();
                Entity.SelectedColumns.ForEach(o =>
                {
                    var property = propertys.FirstOrDefault(p => o == p.Name);
                    if (property == null)
                    {
                        throw new DTOException("在实体" + type.Name + "中,未找到属性" + o);
                    }
                    else
                    {
                        var dattr = property.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (!dattr.Any())
                        {
                            var fattr = property.GetCustomAttributes(typeof(FictitiousAttribute), false);
                            if (!fattr.Any())
                            {
                                throw new DTOException("属性" + o + "未标记为DescriptionAttribute或者FictitiousAttribute");
                            }
                            else
                            {
                                if (string.IsNullOrWhiteSpace((fattr[0] as FictitiousAttribute)?.DescribColmun))
                                {
                                    sql.Append(o);
                                    sql.Append(',');
                                }
                                else
                                {
                                    sql.Append((fattr[0] as FictitiousAttribute)?.DescribColmun);
                                    sql.Append(' ');
                                    sql.Append(o);
                                    sql.Append(',');
                                }
                            }
                        }
                        else
                        {
                            sql.Append(o);
                            sql.Append(',');
                        }
                    }
                });
            }
            sql = sql.Remove(sql.Length - 1, 1);
            sql.Append(" from (");

            if (string.IsNullOrWhiteSpace(Entity.GetDefaultSQL()))
            {
                if (string.IsNullOrWhiteSpace(Entity.QuerySql) || Entity.QuerySql.ToUpper().IndexOf("Select", StringComparison.Ordinal) < 0)
                {
                    throw new DTOException("Sql语句无法执行,请检查传入的WHERE条件和" + type.Name + "类是否标记了DefaultSql属性");
                }

                sql.AppendFormat(" {0}", Entity.QuerySql);
                sql.Append(")");
            }
            else
            {
                sql.Append(Entity.GetDefaultSQL());
                sql.Append(")");
                sql.AppendFormat(" {0}", Entity.QuerySql);
            }
            return(sql.ToString());
        }