private string GetDataEntitySetCode(DbMapInfo info)
        {
            Type   targetType = Nullable.GetUnderlyingType(info.PropertyInfo.PropertyType) ?? info.PropertyInfo.PropertyType;
            string code, typeName;

            typeName = targetType.ToString();

            if (info.AttrColumn != null && info.AttrColumn.TimeStamp)
            {
                if (typeName == "System.Int64")
                {
                    typeName = "TimeStampToLong";
                }
                if (typeName == "System.Byte[]")
                {
                    typeName = "TimeStampToByte";
                }
            }

            if (s_dictEntiySetCode.TryGetValue(typeName, out code))
            {
                return(string.Format(code, info.NetName));
            }
            else
            {
                throw new NotSupportedException("不支持的实体数据类型");
            }
        }
 private string OutPropertyGetValue(DbMapInfo info, string varName)
 {
     if (info.PropertyInfo.PropertyType.IsClass)
     {
         return(string.Format("({0}.{1} ?? (object)DBNull.Value)", varName, info.NetName));
     }
     else if (info.PropertyInfo.PropertyType.IsNullableType())
     {
         return(string.Format("({0}.{1}.HasValue ? {0}.{1}.Value : (object)DBNull.Value)", varName, info.NetName));
     }
     else
     {
         return(string.Format("{0}.{1}", varName, info.NetName));
     }
 }
示例#3
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);
        }
 private void SetField(DbMapInfo info, string varName, string paraNamePrefix)
 {
     _sb.AppendFormat("query=query+ \" [{0}] = \" + (new SqlParameter(\"@{1}{3}\", {2}));\r\n",
                      info.DbName, paraNamePrefix, OutPropertyGetValue(info, varName), info.NetName);
 }
        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, 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;

                IgnoreColumnAttribute attrIgnore = property.GetMyAttribute <IgnoreColumnAttribute>();
                if (attrIgnore != null)
                {
                    continue;
                }

                DataColumnAttribute attrColumn = property.GetMyAttribute <DataColumnAttribute>();

                if (attrColumn != null)
                {
                    if (string.IsNullOrEmpty(attrColumn.Alias) == false)
                    {
                        fieldName = attrColumn.Alias;
                    }

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

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

                    if (attrColumn.PrimaryKey)
                    {
                        _keyFields.Add(info);
                    }
                }
                else
                {
                    _allFields.Add(new DbMapInfo(fieldName, property.Name, null, attrIgnore, property));
                }
            }
        }