示例#1
0
文件: TypeCache.cs 项目: yhhno/CRL3
 /// <summary>
 /// 获取表属性,如果要获取表名,调用GetTableName方法
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static Attribute.TableAttribute GetTable(Type type)
 {
     if (typeCache.ContainsKey(type))
         return typeCache[type];
     object[] objAttrs = type.GetCustomAttributes(typeof(Attribute.TableAttribute), true);
     Attribute.TableAttribute des;
     if (objAttrs == null || objAttrs.Length == 0)
     {
         des = new Attribute.TableAttribute() { TableName = type.Name };
     }
     else
     {
         des = objAttrs[0] as Attribute.TableAttribute;
     }
     des.Type = type;
     lock (lockObj)
     {
         if (!typeCache.ContainsKey(type))
         {
             typeCache.Add(type, des);
         }
     }
     if (string.IsNullOrEmpty(des.TableName))
     {
         des.TableName = type.Name;
     }
     SetProperties(des);
     return des;
 }
示例#2
0
 public static Attribute.TableAttribute GetTable(Type type)
 {
     if (typeCache.ContainsKey(type))
     {
         return(typeCache[type]);
     }
     object[] objAttrs = type.GetCustomAttributes(typeof(Attribute.TableAttribute), true);
     Attribute.TableAttribute des;
     if (objAttrs == null || objAttrs.Length == 0)
     {
         des = new Attribute.TableAttribute()
         {
             TableName = type.Name
         };
     }
     else
     {
         des = objAttrs[0] as Attribute.TableAttribute;
     }
     des.Type = type;
     lock (lockObj)
     {
         if (!typeCache.ContainsKey(type))
         {
             typeCache.Add(type, des);
         }
     }
     if (string.IsNullOrEmpty(des.TableName))
     {
         des.TableName = type.Name;
     }
     SetProperties(des);
     return(des);
 }
示例#3
0
        /// <summary>
        /// 获取一个表
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public Table GetTable(string dbName, Attribute.TableAttribute table)
        {
            var name = table.TableName.ToUpper();

            if (Tables[dbName].ContainsKey(name))
            {
                return(Tables[dbName][name]);
            }
            return(null);
        }
示例#4
0
        /// <summary>
        /// 获取表属性,如果要获取表名,调用GetTableName方法
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Attribute.TableAttribute GetTable(Type type)
        {
            Attribute.TableAttribute table;
            var b = typeCache.TryGetValue(type, out table);

            if (b)
            {
                if (table.Fields.Count > 0)
                {
                    return(table);
                }
            }
            object[] objAttrs = type.GetCustomAttributes(typeof(Attribute.TableAttribute), true);
            Attribute.TableAttribute des;
            if (objAttrs == null || objAttrs.Length == 0)
            {
                des = new Attribute.TableAttribute()
                {
                    TableName = type.Name
                };
            }
            else
            {
                des = objAttrs[0] as Attribute.TableAttribute;
            }
            des.Type = type;
            //lock (lockObj)
            //{
            //    if (!typeCache.ContainsKey(type))
            //    {
            //        typeCache.Add(type, des);
            //    }
            //}
            if (!typeCache.ContainsKey(type))
            {
                typeCache.TryAdd(type, des);
            }
            if (string.IsNullOrEmpty(des.TableName))
            {
                des.TableName = type.Name;
            }
            des.TableNameFormat = string.Format("[{0}]", des.TableName);
            SetProperties(des);
            return(des);
        }
示例#5
0
        /// <summary>
        /// 检查字段
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public List <Attribute.FieldAttribute> CheckFieldExists(string dbName, Attribute.TableAttribute table)
        {
            var fields    = table.Fields;
            var tableName = table.TableName.ToUpper();
            var returns   = new List <Attribute.FieldAttribute>();
            var tb        = Tables[dbName][tableName];

            if (tb.ColumnChecked)
            {
                return(returns);
            }
            if (tb.Fields.Count == 0)//首次不检查
            {
                SaveTable(dbName, table);
                return(returns);
            }
            //检查字段是否一致
            foreach (var item in fields)
            {
                if (item.FieldType != Attribute.FieldType.数据库字段)
                {
                    continue;
                }
                if (!tb.Fields.Contains(item.Name.ToUpper()))
                {
                    returns.Add(item);
                }
            }
            tb.ColumnChecked = true;
            if (returns.Count > 0)
            {
                var fields2 = new List <string>();
                fields.ForEach(b =>
                {
                    fields2.Add(b.Name.ToUpper());
                });
                tb.Fields = fields2;
                Save();
            }
            return(returns);
        }
示例#6
0
        /// <summary>
        /// 保存表字段
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="table"></param>
        public void SaveTable(string dbName, Attribute.TableAttribute table)
        {
            var tableName = table.TableName.ToUpper();
            var fields    = table.Fields;

            lock (lockObj)
            {
                var fields2 = new List <string>();
                fields.ForEach(b =>
                {
                    fields2.Add(b.Name.ToUpper());
                });
                var tb = new Table()
                {
                    Name = tableName, Fields = fields2
                };
                Tables[dbName].Remove(tableName);
                Tables[dbName].Add(tableName, tb);
            }
            Save();
        }
示例#7
0
        static void SetProperties(Attribute.TableAttribute table)
        {
            if (table.Fields.Count > 0)
            {
                return;
            }
            var type = table.Type;
            List <Attribute.FieldAttribute> list = new List <CRL.Attribute.FieldAttribute>();
            var fieldDic = new IgnoreCaseDictionary <Attribute.FieldAttribute>();
            //string fieldPat = @"^([A-Z][a-z|\d]+)+$";
            int n = 0;

            Attribute.FieldAttribute keyField = null;
            #region 读取
            var typeArry = table.Type.GetProperties().ToList();
            //移除重复的
            var dic = new Dictionary <string, PropertyInfo>();
            foreach (PropertyInfo info in typeArry)
            {
                if (!dic.ContainsKey(info.Name))
                {
                    dic.Add(info.Name, info);
                }
            }
            foreach (PropertyInfo info in dic.Values)
            {
                //if (!System.Text.RegularExpressions.Regex.IsMatch(info.Name, fieldPat))
                //{
                //    throw new CRLException(string.Format("属性名:{0} 不符合规则:{1}", info.Name, fieldPat));
                //}
                //排除没有SET方法的属性
                if (info.GetSetMethod() == null)
                {
                    continue;
                }
                Type propertyType          = info.PropertyType;
                Attribute.FieldAttribute f = new CRL.Attribute.FieldAttribute();
                //排除集合类型
                if (propertyType.FullName.IndexOf("System.Collections") > -1)
                {
                    continue;
                }

                object[] attrs = info.GetCustomAttributes(typeof(Attribute.FieldAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    f = attrs[0] as Attribute.FieldAttribute;
                }
                f.SetPropertyInfo(info);
                f.PropertyType = propertyType;
                f.MemberName   = info.Name;
                f.TableName    = table.TableName;
                f.ModelType    = table.Type;
                //if (!string.IsNullOrEmpty(f.VirtualField))
                //{
                //    if (SettingConfig.StringFormat != null)
                //    {
                //        f.VirtualField = SettingConfig.StringFormat(f.VirtualField);
                //    }
                //    f.VirtualField = f.VirtualField.Replace("$", "{" + type.FullName + "}");//虚拟字段使用Type名
                //}
                //排除不映射字段
                if (!f.MapingField)
                {
                    continue;
                }
                if (propertyType == typeof(System.String))
                {
                    if (f.Length == 0)
                    {
                        f.Length = 30;
                    }
                }
                if (f.IsPrimaryKey)//保存主键
                {
                    table.PrimaryKey = f;
                    f.FieldIndexType = Attribute.FieldIndexType.非聚集唯一;
                    keyField         = f;
                    n += 1;
                }
                //if (f.FieldType != Attribute.FieldType.关联字段)
                //{
                fieldDic.Add(f.MemberName, f);
                //}
                f.MapingNameFormat = string.Format("[{0}]", f.MapingName);
                list.Add(f);
            }
            if (n == 0)
            {
                //throw new CRLException(string.Format("对象{0}未设置任何主键", type.Name));
            }
            else if (n > 1)
            {
                throw new CRLException(string.Format("对象{0}设置的主键字段太多 {1}", type.Name, n));
            }
            #endregion
            //主键排前面
            if (keyField != null)
            {
                list.Remove(keyField);
                list.Insert(0, keyField);
            }
            table.Fields    = list;
            table.FieldsDic = fieldDic;
        }