Пример #1
0
        /// <summary>
        /// 从数据库类创建描述类
        /// </summary>
        /// <param name="type"></param>
        /// <param name="clsLoader"></param>
        /// <returns></returns>
        public static ClassDesc CreateClassDesc(Type type, DbClassLoader dbClsLoader)
        {
            ClassDesc clsDesc = new ClassDesc();

            clsDesc.classType = type;

            if (!type.IsDefined(typeof(DbTableAttribute), false))
            {
                // 数据库类需要定义DbTableAttribute
                Debug.LogError(string.Format("Missing table name {0}", type.ToString()));
            }
            else
            {
                // 获取表名
                var atbAyy = type.GetCustomAttributes(typeof(DbTableAttribute), false);
                if (atbAyy.Length != 0)
                {
                    var atb = atbAyy[0] as DbTableAttribute;
                    clsDesc.tableName   = atb.TableName;
                    clsDesc.className   = atb.ClassName;
                    clsDesc.toClassName = atb.ToClassName;
                    clsDesc.toNameStr   = atb.ToNameStr;
                }
            }

            // 分析属性, 同时需要分析基类的属性
            var currentType = type;

            while (currentType != null && currentType != typeof(object))
            {
                CollectPropertyInfo(currentType.GetProperties(), clsDesc, dbClsLoader);
                currentType = currentType.BaseType;
            }

            return(clsDesc);
        }
Пример #2
0
        private static void CollectPropertyInfo(PropertyInfo[] piArr, ClassDesc clsDesc, DbClassLoader dbClsLoader)
        {
            for (int i = 0; i < piArr.Length; i++)
            {
                PropertyInfo pi = piArr[i];

                object[] atbArr           = pi.GetCustomAttributes(typeof(DbColumnAttribute), false);
                object[] splitAtbArr      = pi.GetCustomAttributes(typeof(DbSplitColumnAttribute), false);
                object[] splitRangeAtbArr = pi.GetCustomAttributes(typeof(DbSplitRangeAttribute), false);
                object[] mergeAtbArr      = pi.GetCustomAttributes(typeof(DbMergeColumnAttribute), false);
                if (atbArr.Length != 0)
                {
                    // 检测和获取主键列信息
                    var atb = atbArr[0] as DbColumnAttribute;
                    if (atb.KeyColumn)
                    {
                        // 主键一定不是外键
                        if (atb.IsForeignKey)
                        {
                            throw new Exception("Pk can't be foreign key.");
                        }

                        // 只有一个主键
                        if (clsDesc.keyAttribute != null)
                        {
                            Debug.LogError(string.Format("Key column already exist {0}", atb.ColumnName));
                        }
                        else
                        {
                            clsDesc.keyAttribute = atb;
                        }
                    }

                    if (atb.IsForeignKey)
                    {
                        // 保存外键信息
                        var foreignKeyPropertyDesc = new ForeignKeyPropertyDesc();
                        foreignKeyPropertyDesc.attribute    = atb;
                        foreignKeyPropertyDesc.propertyInfo = pi;

                        var refClsDesc = dbClsLoader.GetClassDesc(atb.ReferencedClass);
                        foreignKeyPropertyDesc.referencedPropertyInfo = refClsDesc.GetPropertyInfo(atb.ReferencedColumn).propertyInfo;

                        clsDesc.rootKeyAttribute = atb;
                        clsDesc.fkPropertyInfoList.Add(foreignKeyPropertyDesc);
                    }

                    if (clsDesc.propertyInfoDic.ContainsKey(atb.ColumnName))
                    {
                        Debug.LogWarning(string.Format("Duplicated column name {0}", atb.ColumnName));
                    }
                    else
                    {
                        var propertyDesc = new PropertyDesc();
                        propertyDesc.attribute    = atb;
                        propertyDesc.propertyInfo = pi;
                        clsDesc.propertyInfoDic.Add(atb.ColumnName, propertyDesc);
                    }
                    continue;
                }
                else if (splitAtbArr.Length != 0)
                {
                    var splitAtb = splitAtbArr[0] as DbSplitColumnAttribute;

                    if (clsDesc.splitPropertyInfoDic.ContainsKey(splitAtb.ColumnName))
                    {
                        Debug.LogWarning(string.Format("Duplicated column name {0}", splitAtb.ColumnName));
                    }
                    else
                    {
                        var propertyDesc = new SplitPropertyDesc();
                        propertyDesc.splitAttribute = splitAtb;
                        propertyDesc.propertyInfo   = pi;
                        clsDesc.splitPropertyInfoDic.Add(splitAtb.ColumnName, propertyDesc);
                    }
                    continue;
                }
                else if (mergeAtbArr.Length != 0)
                {
                    var mergeAtb = mergeAtbArr[0] as DbMergeColumnAttribute;
                    if (clsDesc.mergePropertyInfoDic.ContainsKey(mergeAtb.FieldTypeName))
                    {
                        Debug.LogWarning(string.Format("Duplicated column name {0}", mergeAtb.FieldTypeName));
                    }
                    else
                    {
                        var propertyDesc = new MergePropertyDesc();
                        propertyDesc.mergeAttribute = mergeAtb;
                        propertyDesc.propertyInfo   = pi;
                        clsDesc.mergePropertyInfoDic.Add(mergeAtb.FieldTypeName, propertyDesc);
                    }
                    continue;
                }
                else if (splitRangeAtbArr.Length != 0)
                {
                    var splitRangeAtb = splitRangeAtbArr[0] as DbSplitRangeAttribute;
                    if (clsDesc.splitRangePropertyInfoDic.ContainsKey(splitRangeAtb.ColumnName))
                    {
                        Debug.LogWarning(string.Format("Duplicated column name {0}", splitRangeAtb.ColumnName));
                    }
                    else
                    {
                        var propertyDesc = new SplitRangePropertyDesc();
                        propertyDesc.rangeAttribute = splitRangeAtb;
                        propertyDesc.propertyInfo   = pi;
                        clsDesc.splitRangePropertyInfoDic.Add(splitRangeAtb.ColumnName, propertyDesc);
                    }
                }

                // 获取子表信息
                atbArr = pi.GetCustomAttributes(typeof(DbColumnSubTableAttribute), false);
                if (atbArr.Length != 0)
                {
                    var atb = atbArr[0] as DbColumnSubTableAttribute;

                    var subTablePropertyDesc = new SubTablePropertyDesc();
                    subTablePropertyDesc.attribute    = atb;
                    subTablePropertyDesc.propertyInfo = pi;
                    clsDesc.subTablePropertyDescList.Add(subTablePropertyDesc);

                    continue;
                }
            }
        }