Example #1
0
        public static MDataColumn GetColumns(Type typeInfo)
        {
            string key = "ColumnCache:" + typeInfo.FullName;

            if (columnCache.ContainsKey(key))
            {
                return(columnCache[key].Clone());
            }
            else
            {
                #region 获取列结构
                MDataColumn mdc = new MDataColumn();
                switch (StaticTool.GetSystemType(ref typeInfo))
                {
                case SysType.Base:
                case SysType.Enum:
                    mdc.Add(typeInfo.Name, DataType.GetSqlType(typeInfo), false);
                    return(mdc);

                case SysType.Generic:
                case SysType.Collection:
                    Type[] argTypes;
                    Tool.StaticTool.GetArgumentLength(ref typeInfo, out argTypes);
                    foreach (Type type in argTypes)
                    {
                        mdc.Add(type.Name, DataType.GetSqlType(type), false);
                    }
                    argTypes = null;
                    return(mdc);
                }

                PropertyInfo[] pis = StaticTool.GetPropertyInfo(typeInfo);

                SqlDbType sqlType;
                for (int i = 0; i < pis.Length; i++)
                {
                    sqlType = SQL.DataType.GetSqlType(pis[i].PropertyType);
                    mdc.Add(pis[i].Name, sqlType);
                    MCellStruct column = mdc[i];
                    column.MaxSize = DataType.GetMaxSize(sqlType);
                    if (i == 0)
                    {
                        column.IsPrimaryKey = true;
                        column.IsCanNull    = false;

                        if (column.ColumnName.ToLower().Contains("id") && (column.SqlType == System.Data.SqlDbType.Int || column.SqlType == SqlDbType.BigInt))
                        {
                            column.IsAutoIncrement = true;
                        }
                    }
                    else if (i > pis.Length - 3 && sqlType == SqlDbType.DateTime && pis[i].Name.EndsWith("Time"))
                    {
                        column.DefaultValue = SqlValue.GetDate;
                    }
                }
                pis = null;
                #endregion

                if (!columnCache.ContainsKey(key))
                {
                    columnCache.Add(typeInfo.FullName, mdc.Clone());
                }

                return(mdc);
            }
        }
Example #2
0
        public static MDataColumn GetColumns(Type typeInfo)
        {
            string key = "ColumnCache_" + typeInfo.FullName;

            if (_ColumnCache.ContainsKey(key))
            {
                return(_ColumnCache[key].Clone());
            }
            else
            {
                #region 获取列结构
                MDataColumn mdc = new MDataColumn();
                mdc.TableName = typeInfo.Name;
                switch (StaticTool.GetSystemType(ref typeInfo))
                {
                case SysType.Base:
                case SysType.Enum:
                    mdc.Add(typeInfo.Name, DataType.GetSqlType(typeInfo), false);
                    return(mdc);

                case SysType.Generic:
                case SysType.Collection:
                    Type[] argTypes;
                    Tool.StaticTool.GetArgumentLength(ref typeInfo, out argTypes);
                    foreach (Type type in argTypes)
                    {
                        mdc.Add(type.Name, DataType.GetSqlType(type), false);
                    }
                    argTypes = null;
                    return(mdc);
                }

                List <PropertyInfo> pis = StaticTool.GetPropertyInfo(typeInfo);
                if (pis.Count > 0)
                {
                    for (int i = 0; i < pis.Count; i++)
                    {
                        SetStruct(mdc, pis[i], null, i, pis.Count);
                    }
                }
                else
                {
                    List <FieldInfo> fis = StaticTool.GetFieldInfo(typeInfo);
                    if (fis.Count > 0)
                    {
                        for (int i = 0; i < fis.Count; i++)
                        {
                            SetStruct(mdc, null, fis[i], i, fis.Count);
                        }
                    }
                }
                object[] tableAttr = typeInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);//看是否设置了表特性,获取表名和表描述
                if (tableAttr != null && tableAttr.Length == 1)
                {
                    DescriptionAttribute attr = tableAttr[0] as DescriptionAttribute;
                    if (attr != null && !string.IsNullOrEmpty(attr.Description))
                    {
                        mdc.Description = attr.Description;
                    }
                }
                pis = null;
                #endregion

                if (!_ColumnCache.ContainsKey(key))
                {
                    _ColumnCache.Set(key, mdc.Clone());
                }

                return(mdc);
            }
        }