Ejemplo n.º 1
0
 public DbMapInfo(string dbName, string netName, DataColumnAttribute attr, PropertyInfo prop)
 {
     this.DbName       = dbName;
     this.NetName      = netName;
     this.Attr         = attr;
     this.PropertyInfo = prop;
 }
Ejemplo n.º 2
0
 public DbMapInfo(string dbName, string netName, DataColumnAttribute attrColumn, IgnoreColumnAttribute attrIgnore, PropertyInfo prop)
 {
     this.DbName       = dbName;
     this.NetName      = netName;
     this.AttrColumn   = attrColumn;
     this.PropertyInfo = prop;
     this.AttrIgnore   = AttrIgnore;
 }
Ejemplo n.º 3
0
 public DbMapInfo(string dbName, string netName, DataColumnAttribute attrColumn, ExtendColumnAttribute attrExtColumn, PropertyInfo prop)
 {
     this.DbName        = dbName;
     this.NetName       = netName;
     this.AttrColumn    = attrColumn;
     this.PropertyInfo  = prop;
     this.AttrExtColumn = attrExtColumn;
 }
Ejemplo n.º 4
0
        public static TypeDescription GetTypeDiscription(Type type)
        {
            TypeDescription description = s_typeInfoDict[type.FullName] as TypeDescription;

            if (description == null)
            {
                PropertyInfo[] properties           = type.GetProperties(s_flag);
                int            length               = properties.Length;
                Dictionary <string, DbMapInfo> dict = new Dictionary <string, DbMapInfo>(length, StringComparer.OrdinalIgnoreCase);

                foreach (PropertyInfo prop in properties)
                {
                    DbMapInfo info = null;

                    DataColumnAttribute   attrColumn = prop.GetMyAttribute <DataColumnAttribute>();
                    IgnoreColumnAttribute attrIgnore = prop.GetMyAttribute <IgnoreColumnAttribute>();

                    if (attrColumn != null)
                    {
                        info = new DbMapInfo(string.IsNullOrEmpty(attrColumn.Alias) ? prop.Name : attrColumn.Alias, prop.Name, attrColumn, attrIgnore, prop);
                    }
                    else
                    {
                        info = new DbMapInfo(prop.Name, prop.Name, null, attrIgnore, prop);
                    }

                    dict[info.DbName] = info;
                }

                description = new TypeDescription {
                    MemberDict = dict
                };

                // 添加到缓存字典
                s_typeInfoDict[type.FullName] = description;
            }

            return(description);
        }
        public static string GetValue <T>(this T entry, PropertyInfo property)
            where T : DataEntry
        {
            DataColumnAttribute customAttribute;
            Type   propertyType = property.PropertyType;
            object value        = property.GetValue(entry, null);

            if (!property.HasCustomAttribute <DataColumnAttribute>(true))
            {
                customAttribute = null;
            }
            else
            {
                customAttribute = property.GetCustomAttribute <DataColumnAttribute>(true);
            }
            DataColumnAttribute dataColumnAttribute = customAttribute;

            if (value == null && dataColumnAttribute != null && dataColumnAttribute.DefaultValue != null)
            {
                value        = dataColumnAttribute.DefaultValue;
                propertyType = value.GetType();
            }
            return(ReflectionUtilities.GetValue(propertyType, value));
        }
Ejemplo n.º 6
0
        public CodeGenerator(Type entityType)
        {
            _entityType = entityType;
            _sb         = new StringBuilder(1024 * 20);

            DataEntityAttribute entityAttr = _entityType.GetMyAttribute <DataEntityAttribute>();
            string tableName;

            if (entityAttr == null)
            {
                tableName = _entityType.Name;
            }
            else
            {
                tableName = string.IsNullOrEmpty(entityAttr.Alias) ? _entityType.Name : entityAttr.Alias;
            }


            _tableInfo = new DbMapInfo(tableName, _entityType.Name, null, null);


            _allFields = new List <DbMapInfo>(20);
            _keyFields = new List <DbMapInfo>(3);

            DbMapInfo info = null;

            foreach (PropertyInfo property in _entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                string fieldName = property.Name;

                DataColumnAttribute attr = property.GetMyAttribute <DataColumnAttribute>();
                if (attr != null)
                {
                    if (string.IsNullOrEmpty(attr.Alias) == false)
                    {
                        fieldName = attr.Alias;
                    }

                    info = new DbMapInfo(fieldName, property.Name, attr, property);
                    _allFields.Add(new DbMapInfo(fieldName, property.Name, attr, property));

                    if (attr.Identity)
                    {
                        _identityField = info;
                    }
                    if (attr.TimeStamp)
                    {
                        _timeStampField = info;
                    }
                    if (attr.SeqGuid)
                    {
                        _seqGuidField = info;
                    }

                    if (attr.PrimaryKey)
                    {
                        _keyFields.Add(info);
                    }
                }
                else
                {
                    _allFields.Add(new DbMapInfo(fieldName, property.Name, null, property));
                }
            }
        }
        public static void SetValue(this object entry, PropertyInfo property, string value)
        {
            bool flag;
            DataColumnAttribute customAttribute;

            if (!entry.GetType().IsSubclassOf(typeof(DataEntry)))
            {
                throw new ArgumentException(string.Format("Type {0} is not subclass of {1}", entry.GetType().Name, typeof(DataEntry).Name));
            }
            if (!property.CanWrite)
            {
                return;
            }
            Type propertyType = property.PropertyType;

            if (string.IsNullOrEmpty(value))
            {
                if (!property.HasCustomAttribute <DataColumnAttribute>(true))
                {
                    customAttribute = null;
                }
                else
                {
                    customAttribute = property.GetCustomAttribute <DataColumnAttribute>(true);
                }
                DataColumnAttribute dataColumnAttribute = customAttribute;
                if (dataColumnAttribute != null && dataColumnAttribute.DefaultValue != null && dataColumnAttribute.DefaultValue.GetType() == propertyType)
                {
                    property.SetValue(entry, dataColumnAttribute.DefaultValue, null);
                }
                else if (propertyType != typeof(float))
                {
                    property.SetValue(entry, ReflectionUtilities.GetDefault(propertyType), null);
                }
                else
                {
                    property.SetValue(entry, -1f, null);
                }
                return;
            }
            if (propertyType == typeof(int))
            {
                property.SetValue(entry, value.ParseInt(), null);
            }
            else if (propertyType == typeof(short))
            {
                property.SetValue(entry, value.ParseShort(), null);
            }
            else if (propertyType == typeof(long))
            {
                property.SetValue(entry, value.ParseLong(), null);
            }
            else if (propertyType == typeof(bool))
            {
                if (value != null)
                {
                    if (value == "1")
                    {
                        flag = true;
                        goto Label0;
                    }
                    else
                    {
                        if (value != "0")
                        {
                            goto Label2;
                        }
                        flag = false;
                        goto Label0;
                    }
                }
Label2:
                flag = value.ParseBool();
Label0:
                property.SetValue(entry, flag, null);
            }
            else if (propertyType == typeof(string))
            {
                property.SetValue(entry, value, null);
            }
            else if (propertyType == typeof(DateTime))
            {
                property.SetValue(entry, value.ParseDateTime(), null);
            }
            else if (propertyType == typeof(float))
            {
                property.SetValue(entry, value.ParseFloat(), null);
            }
            else if (propertyType != typeof(double))
            {
                if (!propertyType.IsSubclassOf(typeof(DataEntry)))
                {
                    throw new NotSupportedException(string.Format("Type {0} is not supported in DB", propertyType));
                }
                property.SetValue(entry, DataEntry.GetInstance(propertyType, value.ParseInt()), null);
            }
            else
            {
                property.SetValue(entry, value.ParseDouble(), null);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 转换属性
        /// </summary>
        /// <param name="info">属性</param>
        /// <param name="entity">实体Sql信息</param>
        private void TransferProperty(PropertyInfo info, EntitySqlInfo entity)
        {
            DataColumnAttribute attrColumn = info.GetMyAttribute <DataColumnAttribute>();

            if (attrColumn != null)
            {
                var prop = "";
                if (!string.IsNullOrEmpty(attrColumn.Alias))
                {
                    prop = string.Format("{0}.[{1}]", entity.TableName, attrColumn.Alias);
                }
                else
                {
                    prop = string.Format("{0}.[{1}]", entity.TableName, info.Name);
                }
                entity.Properties.Add(prop);
                if (attrColumn.PrimaryKey)
                {
                    entity.PrimaryKey = prop;
                }
            }
            else
            {
                ExtendColumnAttribute attrExtColumn = info.GetMyAttribute <ExtendColumnAttribute>();
                if (attrExtColumn != null && attrExtColumn.ColumnType != 0)
                {
                    switch (attrExtColumn.ColumnType)
                    {
                    case ExtendColumnTypeEnum.KeyParamOption_Code:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN dbo.KeyParamOption AS {0} ON [{0}].[Code] = {1}.[{2}] AND [{0}].[ParamGroup] = '{3}'", info.Name, entity.TableName, attrExtColumn.RelationColumn, attrExtColumn.Expression));
                        entity.Properties.Add(string.Format("[{0}].[Name] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.KeyParamOption_Guid:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN dbo.KeyParamOption AS {0} ON [{0}].[KeyParamOptionID] = {1}.[{2}]", info.Name, entity.TableName, attrExtColumn.RelationColumn));
                        entity.Properties.Add(string.Format("[{0}].[Name] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.UserName_Guid:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_UserInfo AS {1} ON [{1}].[UserGUID] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn));
                        entity.Properties.Add(string.Format("[{0}].[UserName_Chn] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.UserName_Code:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_UserInfo AS {1} ON [{1}].[UserCode] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn));
                        entity.Properties.Add(string.Format("[{0}].[UserName_Chn] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.BusinessUnit_Guid:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_BusinessUnit AS {1} ON [{1}].[BUGUID] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn));
                        entity.Properties.Add(string.Format("[{0}].[BUName] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.BusinessUnit_Code:
                        entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_BusinessUnit AS {1} ON [{1}].[BUCode] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn));
                        entity.Properties.Add(string.Format("[{0}].[BUName] AS {0}", info.Name));
                        break;

                    case ExtendColumnTypeEnum.Common:
                        entity.ExtendTables.Add(attrExtColumn.Expression);
                        entity.Properties.Add(attrExtColumn.RelationColumn);
                        break;
                    }
                }
            }
        }