Esempio n. 1
0
        public string GetHtmlModelString <T>()
        {
            DataConfigureAttribute attribute     = DataConfigureAttribute.GetAttribute <T>();
            StringBuilder          columnBuilder = new StringBuilder();

            if (attribute != null)
            {
                attribute.InitDisplayName();
                foreach (var item in attribute.GetViewPortDescriptors(true))
                {
                    columnBuilder.Append(GetHtmlModelString(item, item.DataType.Name));
                }
            }
            else
            {
                Type           tType     = typeof(T);
                PropertyInfo[] propertys = tType.GetProperties();
                foreach (var item in propertys)
                {
                    string typeName = item.PropertyType.IsGenericType ? item.PropertyType.GetGenericArguments()[0].Name : item.PropertyType.Name;
                    columnBuilder.AppendFormat("{0}:{{ Name: '{0}',DisplayName:'{0}',Width:150,DataType:'{1}',DateFormat:'',JsDateFormat:'',ValueDateFormat:'',Hidden:0 }},", item.Name, typeName);
                }
            }
            return(string.Format("{{{0}}}", columnBuilder.ToString().Trim(',')));
        }
Esempio n. 2
0
        public virtual int Delete <T>(DataFilter filter) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            filter = custAttribute.MetaData.DataAccess(filter);//数据权限
            return(Delete(GetTableName <T>(custAttribute), filter.ToString(), filter.GetParameterValues()));
        }
Esempio n. 3
0
        public string GetHtmlModelString <T>()
        {
            DataConfigureAttribute attribute     = DataConfigureAttribute.GetAttribute <T>();
            StringBuilder          ColumnBuilder = new StringBuilder();

            if (attribute != null)
            {
                foreach (var item in attribute.GetHtmlTags(true))
                {
                    ColumnBuilder.Append(GetHtmlModelString(item, item.DataType.Name));
                }
            }
            else
            {
                Type tType = typeof(T);
                System.Reflection.PropertyInfo[] propertys = tType.GetProperties();
                foreach (var item in propertys)
                {
                    string typeName = item.PropertyType.Name;
                    if (item.PropertyType.Name == "Nullable`1")
                    {
                        typeName = item.PropertyType.GetGenericArguments()[0].Name;
                    }
                    ColumnBuilder.AppendFormat("{0}:{{ Name: '{0}',DisplayName:'{0}',Width:150,DataType:'{1}',Format:'',,Hidden:0 }},", item.Name, typeName);
                }
            }
            return(string.Format("{{{0}}}", ColumnBuilder.ToString().Trim(',')));
        }
Esempio n. 4
0
        public virtual bool Update <T>(T item, params object[] primaryKeys) where T : class
        {
            DataFilter             filter        = new DataFilter();
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            List <PrimaryKey>      primaryKey    = GetPrimaryKeys(custAttribute);

            if (primaryKeys.Length != primaryKey.Count && primaryKeys.Length > 0)
            {
                throw new Exception("输入的参数与设置的主键个数不对应!");
            }
            if (primaryKeys.Length > 0)
            {
                for (int i = 0; i < primaryKey.Count; i++)
                {
                    filter.Where(primaryKey[i].ColumnName, OperatorType.Equal, primaryKeys[i]);
                }
            }
            else
            {
                Type entityType = typeof(T);

                foreach (var primary in primaryKey)
                {
                    PropertyInfo proper = entityType.GetProperty(primary.PropertyName);
                    if (proper != null && proper.CanRead)
                    {
                        filter.Where(primary.ColumnName, OperatorType.Equal, proper.GetValue(item, null));
                    }
                }
            }
            return(Update(item, filter));
        }
Esempio n. 5
0
        public virtual bool Update <T>(T item, DataFilter filter) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            string tableName = GetTableName <T>(custAttribute);

            PropertyInfo[] propertys = typeof(T).GetProperties();
            StringBuilder  builder   = new StringBuilder();
            List <KeyValuePair <string, object> > keyValue = new List <KeyValuePair <string, object> >();

            foreach (var property in propertys)
            {
                if (filter.UpdateProperties != null && !filter.UpdateProperties.Contains(property.Name))
                {
                    continue;
                }
                string name  = property.Name;
                object value = property.GetValue(item, null);

                if (custAttribute != null)
                {
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(name))
                    {
                        PropertyDataInfo data = custAttribute.MetaData.PropertyDataConfig[name];
                        if (data.IsRelation || data.Ignore || !data.CanUpdate)
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(data.ColumnName))
                        {
                            name = data.ColumnName;
                        }
                    }
                }
                if (value != null)
                {
                    builder.AppendFormat(builder.Length == 0 ? "[{0}]=@{0}" : ",[{0}]=@{0}", name);
                    KeyValuePair <string, object> kv = new KeyValuePair <string, object>(name, value);
                    keyValue.Add(kv);
                }
                else
                {
                    builder.AppendFormat(builder.Length == 0 ? "[{0}]=NULL" : ",[{0}]=NULL", name);
                }
            }
            if (builder.Length == 0)
            {
                return(false);
            }
            string condition = filter.ToString();

            if (!string.IsNullOrEmpty(condition))
            {
                builder.Append(" WHERE ");
                builder.Append(condition);
            }
            keyValue.AddRange(filter.GetParameterValues());
            return(Update(tableName, builder.ToString(), keyValue));
        }
Esempio n. 6
0
        public override IEnumerable <T> Get <T>(DataFilter filter, Pagination pagin)
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            string tableName = GetTableName <T>(custAttribute);
            List <KeyValuePair <string, string> > comMatch;
            string selectCol  = GetSelectColumn <T>(custAttribute, out comMatch);
            string condition  = filter.ToString();
            var    primaryKey = GetPrimaryKeys(custAttribute);

            foreach (var item in primaryKey)
            {
                filter.OrderBy(item, OrderType.Ascending);
            }
            string        orderby = filter.GetOrderString();
            StringBuilder builder = new StringBuilder();

            builder.Append("WITH T AS( ");
            builder.AppendFormat("SELECT TOP {0} ", pagin.PageSize * (pagin.PageIndex + 1));
            builder.Append(selectCol);
            builder.AppendFormat(",ROW_NUMBER() OVER ({0}) AS RowIndex ", orderby);
            builder.AppendFormat(" FROM [{0}] ", tableName);
            builder.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias);
            StringBuilder builderRela = new StringBuilder();

            if (custAttribute != null)
            {
                foreach (var item in custAttribute.MetaData.DataRelations)
                {
                    builder.Append(item);
                    builderRela.Append(item);
                }
            }
            builder.Append(string.IsNullOrEmpty(condition) ? "" : " WHERE " + condition);
            builder.AppendFormat(") SELECT * FROM T WHERE RowIndex>{0} AND RowIndex<={1}", pagin.PageIndex * pagin.PageSize, pagin.PageSize * (pagin.PageIndex + 1));
            DataTable table = GetTable(builder.ToString(), filter.GetParameterValues());

            if (table == null)
            {
                return(new List <T>());
            }
            List <T> list = new List <T>();
            Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties));
            }
            DataTable recordCound = GetTable(string.Format("SELECT COUNT(1) FROM [{0}] {3} {2} {1}",
                                                           tableName,
                                                           string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition,
                                                           builderRela,
                                                           custAttribute == null ? "T0" : custAttribute.MetaData.Alias), filter.GetParameterValues());

            pagin.RecordCount = Convert.ToInt64(recordCound.Rows[0][0]);
            return(list);
        }
Esempio n. 7
0
        public string GetJsonDataForGrid <T>(IEnumerable <T> data, long pageSize, long pageIndex, long recordCount)
        {
            string                 baseStr    = "{{ Columns: {0}, Rows: [{1}] ,PageIndex:{2},PageSize:{3},RecordCount:{4}}}";
            StringBuilder          rowBuilder = new StringBuilder();
            DataConfigureAttribute attribute  = DataConfigureAttribute.GetAttribute <T>();

            foreach (var item in data)
            {
                rowBuilder.Append(GetModelString(item, attribute));
            }
            return(string.Format(baseStr, GetHtmlModelString <T>(), rowBuilder.ToString().Trim(','), pageIndex, pageSize, recordCount));
        }
Esempio n. 8
0
 public DataFilter Where<T>(Expression<Func<T, object>> expression, OperatorType operatorType, object value)
 {
     string property = Common.GetLinqExpressionText(expression);
     DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute<T>();
     if (attribute != null && attribute.MetaData.PropertyDataConfig.ContainsKey(property))
     {
         string propertyMap = attribute.MetaData.PropertyDataConfig[property].ColumnName;
         if (!string.IsNullOrEmpty(propertyMap))
             property = propertyMap;
     }
     return Where(new Condition(property, operatorType, value));
 }
Esempio n. 9
0
        public virtual void Add <T>(T item) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            string tableName = GetTableName <T>(custAttribute);
            Dictionary <string, object> allValues    = Reflection.ClassAction.GetPropertieValues <T>(item);
            Dictionary <string, object> insertValues = new Dictionary <string, object>();

            if (custAttribute != null)
            {
                foreach (var valueitem in allValues)
                {
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(valueitem.Key))
                    {
                        PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[valueitem.Key];

                        if (!config.Ignore && config.CanInsert)
                        {
                            insertValues.Add(config.ColumnName, valueitem.Value);
                        }
                    }
                    else
                    {
                        insertValues.Add(valueitem.Key, valueitem.Value);
                    }
                }
            }
            else
            {
                insertValues = allValues;
            }
            object resu = this.Insert(tableName, insertValues);

            if (custAttribute != null && resu != null && !resu.Equals(0))
            {
                if (custAttribute.MetaData.Primarykey != null && custAttribute.MetaData.Primarykey.Count == 1)
                {
                    foreach (var dataConfig in custAttribute.MetaData.PropertyDataConfig)
                    {
                        if (dataConfig.Value.IsPrimaryKey && dataConfig.Value.IsIncreasePrimaryKey)
                        {
                            PropertyInfo pro = typeof(T).GetProperty(dataConfig.Value.PropertyName);
                            if (pro != null && pro.CanWrite)
                            {
                                pro.SetValue(item, Easy.Reflection.ClassAction.ValueConvert(pro, resu), null);
                            }
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 10
0
        public virtual bool Update <T>(T item, params object[] primaryKeys) where T : class
        {
            DataFilter filter     = new DataFilter();
            var        primaryKey = new List <string> {
                "ID"
            };
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            if (custAttribute != null)
            {
                primaryKey = custAttribute.MetaData.Primarykey ?? primaryKey;
            }
            if (primaryKeys.Length != primaryKey.Count && primaryKeys.Length > 0)
            {
                throw new Exception("输入的参数与设置的主键个数不对应!");
            }
            if (primaryKeys.Length > 0)
            {
                for (int i = 0; i < primaryKey.Count; i++)
                {
                    filter.Where(primaryKey[i], OperatorType.Equal, primaryKeys[i]);
                }
            }
            else
            {
                Type entityType = typeof(T);
                foreach (var primary in primaryKey)
                {
                    PropertyInfo proper = null;
                    if (custAttribute != null)
                    {
                        foreach (string key in custAttribute.MetaData.PropertyDataConfig.Keys)
                        {
                            if (custAttribute.MetaData.PropertyDataConfig[key].ColumnName == primary)
                            {//属性名
                                proper = entityType.GetProperty(key);
                                break;
                            }
                        }
                    }
                    if (proper == null)
                    {
                        proper = entityType.GetProperty(primary);
                    }
                    if (proper != null && proper.CanRead)
                    {
                        filter.Where(primary, OperatorType.Equal, proper.GetValue(item, null));
                    }
                }
            }
            return(Update <T>(item, filter));
        }
Esempio n. 11
0
        public virtual void CreateTable <T>() where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            string tableName   = GetTableName <T>(custAttribute);
            var    buildColumn = new StringBuilder();

            GetCulumnSchema <T>(custAttribute).Each(m => buildColumn.Append(m));
            if (custAttribute != null && custAttribute.MetaData.Primarykey.Any())
            {
                var primaryKeyBuilder = new StringBuilder();
                custAttribute.MetaData.Primarykey.Each(m => primaryKeyBuilder.AppendFormat("[{0}],", m));
                buildColumn.Append(string.Format("PRIMARY KEY({0})", primaryKeyBuilder.ToString().Trim(',')));
            }
            CustomerSql(string.Format("Create Table [{0}] ({1}) ", tableName, buildColumn.ToString().Trim(','))).ExecuteNonQuery();
        }
        private List <KeyValuePair <string, string> > GetMap <T>()
        {
            DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute <T>();
            var map = new List <KeyValuePair <string, string> >();

            if (attribute != null && attribute.MetaData != null && attribute.MetaData.PropertyDataConfig != null)
            {
                attribute.MetaData.PropertyDataConfig.Each(m => map.Add(new KeyValuePair <string, string>(m.Value.ColumnName, m.Key)));
            }
            else
            {
                var proertyInfoArray = typeof(T).GetProperties();
                proertyInfoArray.Where(m => m.CanWrite).Each(m => map.Add(new KeyValuePair <string, string>(m.Name, m.Name)));
            }
            return(map);
        }
Esempio n. 13
0
        public virtual int Delete <T>(params object[] primaryKeys) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            var primaryKey = GetPrimaryKeys(custAttribute);

            if (primaryKeys.Length != primaryKey.Count)
            {
                throw new Exception("输入的参数与设置的主键个数不对应!");
            }
            var filter = new DataFilter();

            for (int i = 0; i < primaryKey.Count; i++)
            {
                filter.Where(primaryKey[i], OperatorType.Equal, primaryKeys[i]);
            }
            return(Delete <T>(filter));
        }
Esempio n. 14
0
        private static KeyValuePair <string, object> MemberConvert(MemberExpression expression, object obj)
        {
            Func <MemberExpression, object> valueGetter = exp =>
            {
                var objectMember = Expression.Convert(exp, typeof(object));
                var getterLambda = Expression.Lambda <Func <object> >(objectMember);
                return(getterLambda.Compile()());
            };

            switch (expression.Member.MemberType)
            {
            case MemberTypes.Field:
            {
                return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), valueGetter(expression)));
            }

            case MemberTypes.Property:
            {
                if (expression.Expression.NodeType == ExpressionType.MemberAccess)
                {
                    return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), valueGetter(expression)));
                }
                if (obj != null)
                {
                    var objType = obj.GetType();
                    if (expression.Member.ReflectedType != null && expression.Member.ReflectedType.IsAssignableFrom(objType))
                    {
                        var property = objType.GetProperty(expression.Member.Name);
                        if (property != null)
                        {
                            var value = property.GetValue(obj, null);
                            if (value != null)
                            {
                                return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), value));
                            }
                        }
                    }
                }
                DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute(expression.Member.ReflectedType);
                string column = attribute != null?attribute.GetPropertyMapper(expression.Member.Name) : expression.Member.Name;

                return(new KeyValuePair <string, object>(column, null));
            }
            }
            return(new KeyValuePair <string, object>());
        }
Esempio n. 15
0
        public virtual IEnumerable <T> Get <T>(DataFilter filter) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            if (custAttribute != null)
            {
                filter = custAttribute.MetaData.DataAccess(filter);//数据权限
            }
            string tableName = GetTableName <T>(custAttribute);
            List <KeyValuePair <string, string> > comMatch;
            string selectCol = GetSelectColumn <T>(custAttribute, out comMatch);
            string condition = filter.ToString();
            string orderby   = filter.GetOrderString();
            var    selectCom = new StringBuilder();

            selectCom.Append("SELECT ");
            selectCom.Append(selectCol);
            selectCom.AppendFormat(" FROM [{0}] ", tableName);
            selectCom.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias);
            if (custAttribute != null)
            {
                foreach (var item in custAttribute.MetaData.DataRelations)
                {
                    selectCom.Append(item);
                }
            }
            if (!string.IsNullOrEmpty(condition))
            {
                selectCom.AppendFormat(" WHERE {0} ", condition);
            }
            selectCom.AppendFormat(orderby);
            DataTable table = this.GetTable(selectCom.ToString(), filter.GetParameterValues());

            if (table == null)
            {
                return(new List <T>());
            }
            var list = new List <T>();
            Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties));
            }
            return(list);
        }
Esempio n. 16
0
        public virtual JsonResult Delete(string ids)
        {
            try
            {
                string[]      id      = ids.Split(',');
                List <object> listIds = new List <object>();
                string        primary = DataConfigureAttribute.GetAttribute <TEntity>().MetaData.Primarykey[0].ColumnName;

                bool isString = typeof(TEntity).GetProperty(primary).PropertyType.Name.ToLower().Equals("string");
                foreach (string item in id)
                {
                    long test = 0;
                    if (!isString && long.TryParse(item, out test))
                    {
                        listIds.Add(test);
                    }
                    else
                    {
                        listIds.Add(item);
                    }
                }
                int result = Service.Delete(new DataFilter().Where(primary, OperatorType.In, listIds));
                if (result > 0)
                {
                    return(Json(new AjaxResult {
                        Status = AjaxStatus.Normal, Message = ids
                    }));
                }
                else
                {
                    return(Json(new AjaxResult {
                        Status = AjaxStatus.Warn, Message = "未删除任何数据!"
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new AjaxResult {
                    Status = AjaxStatus.Error, Message = ex.Message
                }));
            }
        }
Esempio n. 17
0
        public ViewModelDecode(T Entity)
        {
            Type entityType;

            if (Entity != null)
            {
                _entity    = Entity;
                _withValue = true;
                entityType = _entity.GetType();
                _attribute = DataConfigureAttribute.GetAttribute(entityType);
            }
            else
            {
                _entity    = ServiceLocator.Current.GetInstance <T>();
                entityType = typeof(T);
                _attribute = DataConfigureAttribute.GetAttribute <T>();
            }
            if (_attribute == null)
            {
                throw new Exception(entityType.FullName + "未使用特性,请在其上使用[EasyFrameWork.Attribute.DataConfigureAttribute]特性!");
            }
        }
Esempio n. 18
0
        public EasyModelMetaData(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
            : base(provider, containerType, modelAccessor, modelType, propertyName)
        {
            if (containerType != null)
            {
                DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute(containerType);
                if (custAttribute != null)
                {
                    custAttribute.InitDisplayName();
                    if (custAttribute.MetaData.ViewPortDescriptors.ContainsKey(propertyName))
                    {
                        ViewPortDescriptor  = custAttribute.MetaData.ViewPortDescriptors[propertyName];
                        DisplayFormatString = ViewPortDescriptor.ValueFormat;

                        if (!string.IsNullOrEmpty(ViewPortDescriptor.DisplayName))
                        {
                            DisplayName = ViewPortDescriptor.DisplayName;
                        }
                        else
                        {
                            DisplayName = ViewPortDescriptor.Name;
                        }
                        EditFormatString    = ViewPortDescriptor.ValueFormat;
                        IsReadOnly          = ViewPortDescriptor.IsReadOnly;
                        IsRequired          = ViewPortDescriptor.IsRequired;
                        Order               = ViewPortDescriptor.OrderIndex;
                        ShowForDisplay      = ViewPortDescriptor.IsShowForDisplay;
                        ShowForEdit         = ViewPortDescriptor.IsShowForEdit;
                        TemplateHint        = ViewPortDescriptor.TemplateName;
                        HideSurroundingHtml = ViewPortDescriptor.IsHidden;
                    }
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(propertyName))
                    {
                        PropertyData = custAttribute.MetaData.PropertyDataConfig[propertyName];
                    }
                }
            }
        }
Esempio n. 19
0
        public virtual T Get <T>(params object[] primaryKeys) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            var primaryKey = GetPrimaryKeys(custAttribute);

            if (primaryKeys.Length != primaryKey.Count)
            {
                throw new Exception("输入的参数与设置的主键个数不对应!");
            }
            DataFilter filter = new DataFilter();

            for (int i = 0; i < primaryKey.Count; i++)
            {
                filter.Where(primaryKey[i].ColumnName, OperatorType.Equal, primaryKeys[i]);
            }
            var list = Get <T>(filter);

            if (list.Count() == 1)
            {
                return(list.ElementAt(0));
            }
            return(default(T));
        }
Esempio n. 20
0
        public EasyModelMetaData(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
            : base(provider, containerType, modelAccessor, modelType, propertyName)
        {
            if (containerType != null)
            {
                DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute(containerType);
                if (custAttribute != null)
                {
                    if (custAttribute.MetaData.HtmlTags.ContainsKey(propertyName))
                    {
                        this.HtmlTag = custAttribute.MetaData.HtmlTags[propertyName];

                        this.DisplayFormatString = this.HtmlTag.ValueFormat;
                        if (!string.IsNullOrEmpty(this.HtmlTag.DisplayName))
                        {
                            this.DisplayName = this.HtmlTag.DisplayName;
                        }
                        else
                        {
                            this.DisplayName = this.HtmlTag.Name;
                        }
                        this.EditFormatString = this.HtmlTag.ValueFormat;
                        this.IsReadOnly       = this.HtmlTag.IsReadOnly;
                        this.IsRequired       = this.HtmlTag.IsRequired;
                        this.Order            = this.HtmlTag.OrderIndex;
                        this.ShowForDisplay   = this.HtmlTag.IsShowForDisplay;
                        this.ShowForEdit      = this.HtmlTag.IsShowForEdit;
                        this.TemplateHint     = this.HtmlTag.TemplateName;
                    }
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(propertyName))
                    {
                        this.PropertyData = custAttribute.MetaData.PropertyDataConfig[propertyName];
                    }
                }
            }
        }
Esempio n. 21
0
        public virtual long Count <T>(DataFilter filter) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            if (custAttribute != null)
            {
                filter = custAttribute.MetaData.DataAccess(filter);//数据权限
            }
            string tableName = GetTableName <T>(custAttribute);
            string condition = filter.ToString();
            string orderby   = filter.GetOrderString();
            var    selectCom = new StringBuilder();

            selectCom.Append("SELECT COUNT(1) ");
            selectCom.AppendFormat(" FROM [{0}] ", tableName);
            selectCom.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias);
            if (custAttribute != null)
            {
                foreach (var item in custAttribute.MetaData.DataRelations)
                {
                    selectCom.Append(item);
                }
            }
            if (!string.IsNullOrEmpty(condition))
            {
                selectCom.AppendFormat(" WHERE {0} ", condition);
            }
            selectCom.AppendFormat(orderby);
            DataTable table = this.GetTable(selectCom.ToString(), filter.GetParameterValues());

            if (table == null)
            {
                return(-1);
            }
            return(Convert.ToInt64(table.Rows[0][0]));
        }
Esempio n. 22
0
        public DataFilter GetDataFilter <T>()
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            DataFilter             filter        = new DataFilter();
            int            conditionIndex        = -1;
            int            groupIndex            = -1;
            int            groupConditionIndex   = -1;
            ConditionGroup Group = new ConditionGroup();

            foreach (var item in _form.AllKeys)
            {
                #region 单件
                int tempIndex = GetConditionIndex(item);
                if (tempIndex >= 0 && conditionIndex != tempIndex)
                {
                    conditionIndex = tempIndex;
                    string value = _form[string.Format("Conditions[{0}][Value]", tempIndex)];
                    if (!string.IsNullOrEmpty(value))
                    {
                        Condition condition = new Condition();
                        condition.Property = _form[string.Format("Conditions[{0}][Property]", tempIndex)];
                        if (custAttribute != null)
                        {
                            condition.Property = custAttribute.GetPropertyMapper(condition.Property);
                        }
                        condition.OperatorType  = (OperatorType)int.Parse(_form[string.Format("Conditions[{0}][OperatorType]", tempIndex)]);
                        condition.ConditionType = (ConditionType)int.Parse(_form[string.Format("Conditions[{0}][ConditionType]", tempIndex)]);
                        condition.Value         = ConvertValue(_form[string.Format("Conditions[{0}][DataType]", tempIndex)], value);
                        filter.Where(condition);
                    }
                }
                #endregion
                #region 条件组合
                int tempGroupIndex = GetConditionGroupIndex(item);
                if (tempGroupIndex >= 0)
                {
                    if (tempGroupIndex != groupIndex && groupIndex != -1)
                    {
                        filter.Where(Group);
                        groupConditionIndex = -1;
                        groupIndex          = -1;
                        Group = new ConditionGroup();
                    }
                    groupIndex = tempGroupIndex;
                    int tempGroupConditionIndex = GetGroupConditionIndex(tempGroupIndex, item);
                    if (tempGroupConditionIndex >= 0 && groupConditionIndex != tempGroupConditionIndex)
                    {
                        groupConditionIndex = tempGroupConditionIndex;
                        string value = _form[string.Format("ConditionGroups[{0}][Conditions][{1}][Value]", tempGroupIndex, tempGroupConditionIndex)];
                        if (!string.IsNullOrEmpty(value))
                        {
                            Condition condition = new Condition();
                            string    proterty  = _form[string.Format("ConditionGroups[{0}][Conditions][{1}][Property]", tempGroupIndex, tempGroupConditionIndex)];
                            condition.Property = proterty;
                            if (custAttribute != null)
                            {
                                condition.Property = custAttribute.GetPropertyMapper(condition.Property);
                            }
                            condition.OperatorType  = (OperatorType)int.Parse(_form[string.Format("ConditionGroups[{0}][Conditions][{1}][OperatorType]", tempGroupIndex, tempGroupConditionIndex)]);
                            condition.ConditionType = (ConditionType)int.Parse(_form[string.Format("ConditionGroups[{0}][Conditions][{1}][ConditionType]", tempGroupIndex, tempGroupConditionIndex)]);
                            condition.Value         = ConvertValue(_form[string.Format("ConditionGroups[{0}][Conditions][{1}][ConditionType]", tempGroupIndex, tempGroupConditionIndex)], value);
                            if (custAttribute != null)
                            {
                                PropertyDataInfo propertyDataInfo = custAttribute.MetaData.PropertyDataConfig[proterty];
                                if (propertyDataInfo.Search != null)
                                {
                                    condition = propertyDataInfo.Search(condition);
                                }
                            }
                            Group.Add(condition);
                        }
                    }
                }
                #endregion
            }
            if (Group.Conditions.Count > 0)
            {
                filter.Where(Group);
            }
            for (int i = 0; ; i++)
            {
                string orderp    = _form[string.Format("OrderBy[{0}][OrderCol]", i)];
                string orderType = _form[string.Format("OrderBy[{0}][OrderType]", i)];
                if (string.IsNullOrEmpty(orderp) || string.IsNullOrEmpty(orderType))
                {
                    break;
                }

                Order order = new Order();
                order.Property = orderp;
                if (custAttribute != null)
                {
                    order.Property = custAttribute.GetPropertyMapper(order.Property);
                }
                order.OrderType = (OrderType)int.Parse(orderType);
                filter.OrderBy(order);
            }

            return(filter);
        }
Esempio n. 23
0
        public virtual IEnumerable <T> Get <T>(DataFilter filter, Pagination pagin) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            string alias = "T0";

            if (custAttribute != null)
            {
                filter = custAttribute.MetaData.DataAccess(filter);//数据权限
                alias  = custAttribute.MetaData.Alias;
            }
            string tableName = GetTableName <T>(custAttribute);
            List <KeyValuePair <string, string> > comMatch;
            string selectCol = GetSelectColumn <T>(custAttribute, out comMatch);
            string condition = filter.ToString();

            var primaryKey = GetPrimaryKeys(custAttribute);

            foreach (var item in primaryKey)
            {
                filter.OrderBy(string.Format("[{0}].[{1}]", alias, item), OrderType.Ascending);
            }
            string orderby         = filter.GetOrderString();
            string orderByContrary = filter.GetContraryOrderString();

            var builderRela = new StringBuilder();

            if (custAttribute != null)
            {
                foreach (var item in custAttribute.MetaData.DataRelations)
                {
                    builderRela.Append(item);
                }
            }
            const string formatCount = "SELECT COUNT(*) FROM [{0}] {3} {2} {1}";
            DataTable    recordCound = this.GetTable(string.Format(formatCount,
                                                                   tableName,
                                                                   string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition,
                                                                   builderRela,
                                                                   alias), filter.GetParameterValues());

            pagin.RecordCount = Convert.ToInt64(recordCound.Rows[0][0]);
            if (pagin.AllPage == pagin.PageIndex && pagin.RecordCount > 0)
            {
                pagin.PageIndex--;
            }
            int pageSize = pagin.PageSize;

            if ((pagin.PageIndex + 1) * pagin.PageSize > pagin.RecordCount && pagin.RecordCount != 0)
            {
                pageSize = (int)(pagin.RecordCount - pagin.PageIndex * pagin.PageSize);
                if (pageSize < 0)
                {
                    pageSize = pagin.PageSize;
                }
            }
            var          builder     = new StringBuilder();
            const string formatTable = "SELECT * FROM (SELECT TOP {1} * FROM (SELECT TOP {2} {0} FROM {3} {6} {5} {4}) TEMP0 {7}) TEMP1 {8}";

            builder.AppendFormat(formatTable,
                                 selectCol,
                                 pageSize,
                                 pagin.PageSize * (pagin.PageIndex + 1),
                                 string.Format("[{0}] {1}", tableName, custAttribute == null ? "T0" : custAttribute.MetaData.Alias),
                                 orderby,
                                 string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition,
                                 builderRela,
                                 orderByContrary.Replace("[{0}].".FormatWith(alias), ""),
                                 orderby.Replace("[{0}].".FormatWith(alias), ""));


            DataTable table = this.GetTable(builder.ToString(), filter.GetParameterValues());

            if (table == null)
            {
                return(new List <T>());
            }
            ;
            var list = new List <T>();
            Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties));
            }
            return(list);
        }
Esempio n. 24
0
        public override string ToString(bool widthLabel)
        {
            StringBuilder builder = new StringBuilder();

            if (_propertyType.IsGenericType)
            {
                var genericType = _propertyType.GetGenericArguments()[0];
                var attribute   = DataConfigureAttribute.GetAttribute(genericType);
                if (widthLabel)
                {
                    builder.AppendFormat("<span class=\"input-group-addon {1}\">{0}</span>", this.DisplayName, this.IsRequired ? "required" : "");
                }
                builder.Append("<div class='input-group-collection container-fluid'>");
                builder.Append("<div class='Template' style='display:none'>");
                {
                    builder.Append("<div class='row item'>");
                    attribute.GetHtmlTags(false).Each(m =>
                    {
                        builder.Append("<div class='col-lg-3 col-md-4 col-sm-6'>");
                        builder.Append("<div class='input-group'>");
                        m.NamePreFix = this.Name + "[{0}].";
                        builder.AppendLine(m.ToString(widthLabel));
                        builder.Append("</div>");
                        builder.Append("</div>");
                    });
                    attribute.GetHtmlHiddenTags().Each(m =>
                    {
                        m.NamePreFix = this.Name + "[{0}].";
                        builder.AppendLine(m.ToString(false));
                    });
                    builder.AppendFormat("<div class='col-md-12'><input type='button' value='删除' class='btn btn-danger btn-xs delete' data-value='{0}'/></div>", Constant.ActionType.Unattached);
                    builder.Append("</div>");
                }
                builder.Append("</div>");

                builder.AppendFormat("<div class='row'><div class='col-md-12'><input type=\"button\" value=\"添加\" class='btn btn-primary btn-xs add' data-value='{0}' /></div></div>", Constant.ActionType.Create);
                if (this.Value != null)
                {
                    if (this.Value is IEnumerable)
                    {
                        int index = 0;
                        foreach (var item in (this.Value as IEnumerable))
                        {
                            builder.Append("<div class='row item'>");
                            attribute.GetHtmlTags(false).Each(m =>
                            {
                                builder.Append("<div class='col-lg-3 col-md-4 col-sm-6'>");
                                builder.Append("<div class='input-group'>");
                                m.NamePreFix = this.Name + "[{0}].".FormatWith(index);
                                m.SetValue(Easy.Reflection.ClassAction.GetObjPropertyValue(item, m.Name));
                                builder.AppendLine(m.ToString(widthLabel));
                                builder.Append("</div>");
                                builder.Append("</div>");
                            });
                            attribute.GetHtmlHiddenTags().Each(m =>
                            {
                                m.NamePreFix = this.Name + "[{0}].".FormatWith(index);
                                m.SetValue(Easy.Reflection.ClassAction.GetObjPropertyValue(item, m.Name));
                                builder.AppendLine(m.ToString(false));
                            });
                            builder.AppendFormat("<div class='col-md-12'><input type='button' value='删除' class='btn btn-danger btn-xs delete' data-value='{0}'/></div>", Constant.ActionType.Delete);
                            builder.Append("</div>");
                            index++;
                        }
                    }
                }
                builder.Append("</div>");
                return(builder.ToString());
            }
            return(base.ToString(widthLabel));
        }
Esempio n. 25
0
        public virtual void Add <T>(T item) where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            if (custAttribute != null)
            {
                custAttribute.MetaData.PropertyDataConfig.Each(dic =>
                {
                    if (dic.Value.CanInsert && dic.Value.ValueProvider != null)
                    {
                        Reflection.ClassAction.SetPropertyValue(item, dic.Value.PropertyName, dic.Value.ValueProvider.GenerateValue());
                    }
                });
            }
            string tableName = GetTableName <T>(custAttribute);
            Dictionary <string, object> allValues    = Reflection.ClassAction.GetPropertieValues <T>(item);
            Dictionary <string, object> insertValues = new Dictionary <string, object>();

            if (custAttribute != null)
            {
                foreach (var valueitem in allValues)
                {
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(valueitem.Key))
                    {
                        PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[valueitem.Key];

                        if (!config.Ignore && config.CanInsert)
                        {
                            insertValues.Add(config.ColumnName, valueitem.Value);
                        }
                    }
                    else
                    {
                        insertValues.Add(valueitem.Key, valueitem.Value);
                    }
                }
            }
            else
            {
                insertValues = allValues;
            }
            object resu = this.Insert(tableName, insertValues);

            if (custAttribute != null && resu != null && !resu.Equals(0))
            {
                if (custAttribute.MetaData.Primarykey != null)
                {
                    custAttribute.MetaData.Primarykey.Each(key =>
                    {
                        if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(key.PropertyName))
                        {
                            var config = custAttribute.MetaData.PropertyDataConfig[key.PropertyName];
                            if (config.IsIncreasePrimaryKey && config.ValueProvider == null)
                            {
                                Reflection.ClassAction.SetPropertyValue(item, key.PropertyName, resu);
                            }
                        }
                    });
                }
            }
        }
Esempio n. 26
0
        public void Buid <T>() where T : class
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();

            TargeType = typeof(T);
            System.Reflection.PropertyInfo[] propertys = TargeType.GetProperties();
            if (custAttribute != null)
            {
                if (!DataBase.IsExistTable(custAttribute.MetaData.Table))
                {
                    DataBase.CreateTable <T>();
                }
                else
                {
                    foreach (var item in propertys)
                    {
                        TypeCode code = Type.GetTypeCode(item.PropertyType.IsGenericType ? item.PropertyType.GetGenericArguments()[0] : item.PropertyType);

                        if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(item.Name))
                        {
                            PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[item.Name];

                            if (!config.Ignore && !config.IsRelation)
                            {
                                string columnName = string.IsNullOrEmpty(config.ColumnName)
                                    ? config.PropertyName
                                    : config.ColumnName;
                                if (!DataBase.IsExistColumn(custAttribute.MetaData.Table, columnName))
                                {
                                    DataBase.AddColumn(custAttribute.MetaData.Table, columnName, Common.ConvertToDbType(code),
                                                       config.StringLength);
                                }
                                else
                                {
                                    DataBase.AlterColumn(custAttribute.MetaData.Table, columnName,
                                                         Common.ConvertToDbType(code),
                                                         config.StringLength);
                                }
                            }
                        }
                        else
                        {
                            if (!DataBase.IsExistColumn(custAttribute.MetaData.Table, item.Name))
                            {
                                DataBase.AddColumn(custAttribute.MetaData.Table, item.Name, Common.ConvertToDbType(code));
                            }
                            else
                            {
                                DataBase.AlterColumn(custAttribute.MetaData.Table, item.Name, Common.ConvertToDbType(code));
                            }
                        }
                    }
                }
            }
            else
            {
                if (!DataBase.IsExistTable(TargeType.Name))
                {
                    DataBase.CreateTable <T>();
                }
                else
                {
                    foreach (var item in propertys)
                    {
                        TypeCode code = Type.GetTypeCode(item.PropertyType.IsGenericType ? item.PropertyType.GetGenericArguments()[0] : item.PropertyType);
                        if (!DataBase.IsExistColumn(TargeType.Name, item.Name))
                        {
                            DataBase.AddColumn(TargeType.Name, item.Name, Common.ConvertToDbType(code));
                        }
                        else
                        {
                            DataBase.AlterColumn(TargeType.Name, item.Name, Common.ConvertToDbType(code));
                        }
                    }
                }
            }
        }
Esempio n. 27
0
 public RepositoryBase()
 {
     _dataConfigure   = DataConfigureAttribute.GetAttribute <T>();
     _iEnumerableType = typeof(IEnumerable);
 }
Esempio n. 28
0
        public DataFilter GetDataFilter <T>()
        {
            DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>();
            DataFilter             filter        = new DataFilter();
            List <Condition>       conditions    = new List <Condition>();
            List <ConditionGroup>  groups        = new List <ConditionGroup>();

            _propertyInfos = typeof(T).GetProperties();
            foreach (var item in _form.AllKeys)
            {
                string property = GetConditionProperty(item);
                if (property.IsNullOrEmpty())
                {
                    continue;
                }

                #region 单个件

                int conditionIndex = GetConditionIndex(item);
                if (conditionIndex >= 0)
                {
                    if (conditionIndex == conditions.Count)
                    {
                        conditions.Add(new Condition());
                    }
                    SetCondition(conditions[conditionIndex], property, _form[item]);
                }

                #endregion

                #region 组合条件

                int groupIndex = GetConditionGroupIndex(item);
                if (groupIndex >= 0)
                {
                    if (groupIndex == groups.Count)
                    {
                        groups.Add(new ConditionGroup());
                    }
                    int groupConditionIndex = GetGroupConditionIndex(item);
                    if (groupConditionIndex >= 0)
                    {
                        if (groupConditionIndex == groups[groupIndex].Conditions.Count)
                        {
                            groups[groupIndex].Conditions.Add(new Condition());
                        }
                        SetCondition(groups[groupIndex].Conditions[groupConditionIndex], property, _form[item]);
                    }
                    else
                    {
                        SetGroup(groups[groupIndex], property, _form[item]);
                    }
                }
                #endregion
            }

            foreach (var con in conditions)
            {
                if (custAttribute != null)
                {
                    con.Property = custAttribute.GetPropertyMapper(con.Property);
                }
                filter.Where(con);
            }

            foreach (var gro in groups)
            {
                if (custAttribute != null)
                {
                    foreach (var con in gro.Conditions)
                    {
                        con.Property = custAttribute.GetPropertyMapper(con.Property);
                    }
                }
                filter.Where(gro);
            }
            for (int i = 0; ; i++)
            {
                string orderp    = _form[string.Format("OrderBy[{0}][OrderCol]", i)];
                string orderType = _form[string.Format("OrderBy[{0}][OrderType]", i)];
                if (string.IsNullOrEmpty(orderp) || string.IsNullOrEmpty(orderType))
                {
                    break;
                }
                Order order = new Order {
                    Property = orderp
                };
                if (custAttribute != null)
                {
                    order.Property = custAttribute.GetPropertyMapper(order.Property);
                }
                order.OrderType = (OrderType)int.Parse(orderType);
                filter.OrderBy(order);
            }
            return(filter);
        }
Esempio n. 29
0
        public ViewModelDecode()
        {
            var entityType = typeof(T);

            _attribute = DataConfigureAttribute.GetAttribute <T>();
        }