Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
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;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        protected string GetSelectColumn <T>(DataConfigureAttribute custAttribute, out List <KeyValuePair <string, string> > comMatch) where T : class
        {
            PropertyInfo[] propertys = typeof(T).GetProperties();
            var            selectCom = new StringBuilder();

            comMatch = new List <KeyValuePair <string, string> >();
            foreach (var item in propertys)
            {
                if (custAttribute != null)
                {
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(item.Name))
                    {
                        PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[item.Name];
                        if (!config.Ignore)
                        {
                            if (!string.IsNullOrEmpty(config.ColumnName))
                            {
                                string alias = config.IsRelation ? config.TableAlias : custAttribute.MetaData.Alias;
                                selectCom.AppendFormat("[{0}].[{1}],", alias, config.ColumnName);
                                var keyVale = new KeyValuePair <string, string>(config.ColumnName, item.Name);
                                comMatch.Add(keyVale);
                            }
                            else
                            {
                                string alias = config.IsRelation ? config.TableAlias : custAttribute.MetaData.Alias;
                                selectCom.AppendFormat("[{0}].[{1}],", alias, item.Name);
                                var keyVale = new KeyValuePair <string, string>(item.Name, item.Name);
                                comMatch.Add(keyVale);
                            }
                        }
                    }
                    else
                    {
                        selectCom.AppendFormat("[{0}].[{1}],", custAttribute.MetaData.Alias, item.Name);
                        var keyVale = new KeyValuePair <string, string>(item.Name, item.Name);
                        comMatch.Add(keyVale);
                    }
                }
                else
                {
                    selectCom.AppendFormat("[{0}],", item.Name);
                    var keyVale = new KeyValuePair <string, string>(item.Name, item.Name);
                    comMatch.Add(keyVale);
                }
            }
            return(selectCom.ToString().Trim(','));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 数据配置,与数据库对应
        /// </summary>
        /// <param name="property">实体字段名称</param>
        /// <returns></returns>
        protected PropertyDataInfoHelper DataConfig(string property)
        {
            PropertyDataInfo data;

            if (PropertyDataConfig.ContainsKey(property))
            {
                data = PropertyDataConfig[property];
            }
            else
            {
                data            = new PropertyDataInfo(property);
                data.TableAlias = "T0";
                data.ColumnName = property;
                PropertyDataConfig.Add(property, data);
            }
            data.Ignore = false;
            return(new PropertyDataInfoHelper(data, this));
        }
Ejemplo n.º 5
0
        private IEnumerable <string> GetCulumnSchema <T>(DataConfigureAttribute custAttribute) where T : class
        {
            PropertyInfo[] propertys = typeof(T).GetProperties();
            var            result    = new List <string>();

            foreach (var item in propertys)
            {
                string isNull = "null";
                var    code   = Type.GetTypeCode(item.PropertyType.Name == "Nullable`1" ? item.PropertyType.GetGenericArguments()[0] : item.PropertyType);
                if (custAttribute != null)
                {
                    if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(item.Name))
                    {
                        PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[item.Name];
                        if (config.IsPrimaryKey)
                        {
                            isNull = "not null";
                        }
                        if (config.Ignore || config.IsRelation)
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(config.ColumnName))
                        {
                            result.Add(string.Format("[{0}] {1} {2},", config.ColumnName, config.IsIncreasePrimaryKey ? "INT IDENTITY" : GetDBTypeStr(Common.ConvertToDbType(code), config.StringLength), isNull));
                        }
                        else
                        {
                            result.Add(string.Format("[{0}] {1} {2},", config.PropertyName, GetDBTypeStr(Common.ConvertToDbType(code), config.StringLength), isNull));
                        }
                    }
                    else
                    {
                        result.Add(string.Format("[{0}] {1} {2},", item.Name, GetDBTypeStr(Common.ConvertToDbType(code)), isNull));
                    }
                }
                else
                {
                    result.Add(string.Format("[{0}] {1} {2},", item.Name, GetDBTypeStr(Common.ConvertToDbType(code)), isNull));
                }
            }
            return(result);
        }
 /// <summary>
 /// 获取属性对表的映射
 /// </summary>
 /// <param name="property"></param>
 /// <returns></returns>
 public string GetPropertyMapper(string property)
 {
     if (this.MetaData.PropertyDataConfig.ContainsKey(property))
     {
         PropertyDataInfo config = MetaData.PropertyDataConfig[property];
         if (!string.IsNullOrEmpty(config.ColumnName))
         {
             string alias = config.IsRelation ? config.TableAlias : MetaData.Alias;
             return(string.Format("[{0}].[{1}]", alias, config.ColumnName));
         }
         else
         {
             string alias = config.IsRelation ? config.TableAlias : MetaData.Alias;
             return(string.Format("[{0}].[{1}]", alias, property));
         }
     }
     else
     {
         return(property);
     }
 }
Ejemplo n.º 7
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);
                            }
                        }
                    });
                }
            }
        }
Ejemplo n.º 8
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);
        }