Inheritance: ColumnAttribute
示例#1
0
        private static void PopulateDLinqColumn(Type entityType, ClassMap mapping, MemberInfo p, object c)
        {
            var name                 = DLinq.Instance.Column.Name(c) as string;
            var canBeNull            = (bool)DLinq.Instance.Column.CanBeNull(c);
            var isPrimaryKey         = (bool)DLinq.Instance.Column.IsPrimaryKey(c);
            var isDbGenerated        = (bool)DLinq.Instance.Column.IsDbGenerated(c);
            var isVersion            = (bool)DLinq.Instance.Column.IsVersion(c);
            var strDbType            = DLinq.Instance.Column.DbType(c) as string;
            var storage              = DLinq.Instance.Column.Storage(c) as string;
            const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            var member               = p;

            DBType?dbType    = null;
            int?   length    = null;
            byte?  precision = null;
            byte?  scale     = null;

            string[] parts   = null;
            var      strType = strDbType;

            if (strDbType.HasValue())
            {
                parts   = strDbType.Split(' ').Where(s => s.HasValue()).Select(s => s.Trim()).ToArray();
                strType = parts[0];
                var index = strType.IndexOf("(");

                if (index > 0)
                {
                    var str    = strType.Matches("(", ")").FirstOrDefault();
                    var parts2 = str.Split(',').Where(s => s.HasValue()).Select(s => s.Trim()).ToArray();
                    if (parts2.Length == 1)
                    {
                        length = int.Parse(parts2[0]);
                    }
                    else if (parts2.Length == 2)
                    {
                        precision = byte.Parse(parts2[0]);
                        scale     = byte.Parse(parts2[1]);
                    }

                    strType = strType.Substring(0, index);
                }


                TypeMappingInfo info;
                if (TypeMapping.SqlDbMap.TryGetValue(strType, out info))
                {
                    dbType = info.DbType;
                    //switch (info.NativeType)
                    //{
                    //    case "Text":
                    //        length = 2147483647;
                    //        break;
                    //    case "NText":
                    //        length = 1073741823;
                    //        break;
                    //    case "Image":
                    //        length = 2147483647;
                    //        break;

                    //}
                }

                if (parts.Length >= 3)
                {
                    if (parts[1].ToUpper() == "NOT" && parts[2].ToUpper() == "NULL")
                    {
                        canBeNull = false;
                    }

                    if (parts.Length == 4 && parts[3].ToUpper() == "IDENTITY")
                    {
                        isDbGenerated = true;
                    }
                }
            }

            ColumnAttribute columnMapper = null;

            if (isPrimaryKey)
            {
                var idMapper = new IdAttribute();
                if (isDbGenerated)
                {
                    idMapper.IsDbGenerated = true;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    idMapper.Name = name;
                }
                mapping.members.Add(member, idMapper);
                columnMapper = idMapper;
            }
            else if (isVersion)
            {
                columnMapper = new ColumnAttribute();
                if (!string.IsNullOrEmpty(name))
                {
                    columnMapper.Name = name;
                }
                columnMapper.IsNullable = canBeNull;
                mapping.members.Add(member, columnMapper);
            }
            else
            {
                columnMapper = new ColumnAttribute();
                if (!string.IsNullOrEmpty(name))
                {
                    columnMapper.Name = name;
                }
                columnMapper.IsNullable = canBeNull;
                mapping.members.Add(member, columnMapper);
            }

            if (dbType.HasValue)
            {
                columnMapper.DbType = dbType.Value;
            }
            if (length.HasValue)
            {
                columnMapper.Length = length.Value;
            }
            if (precision.HasValue)
            {
                columnMapper.Precision = precision.Value;
            }
            if (scale.HasValue)
            {
                columnMapper.Scale = scale.Value;
            }
            if (storage.HasValue())
            {
                columnMapper.Storage = storage;
            }
        }