Example #1
0
        // edit by cheery,2017/2/22
        // 增加对filed length的解析
        public virtual string GetColumnDefinition(SqlColumnDefine columnDefinition)
        {
            DbType?dbType;

            if (columnDefinition.ColumnAttribute?.DbType != null)
            {
                dbType = columnDefinition.ColumnAttribute.DbType;
            }
            else
            {
                if (columnDefinition.ValueType != null)
                {
                    if (!DbTypeMap.ColumnDbTypeMap.ContainsKey(columnDefinition.ValueType))
                    {
                        var isEnum = false;
                        if (!EnvHelper.IsNetFX)
                        {
                            isEnum = columnDefinition.ValueType.GetTypeInfo().IsEnum;
                        }
                        else
                        {
                            isEnum = columnDefinition.ValueType.GetTypeInfo().IsEnum;
                        }

                        if (isEnum)
                        {
                            dbType = DbType.Int32;
                        }
                        else
                        {
                            dbType = DbType.String;
                        }
                    }
                    else
                    {
                        dbType = DbTypeMap.ColumnDbTypeMap[columnDefinition.ValueType];
                    }
                }
                else
                {
                    dbType = DbType.String;
                }
            }
            var fieldLength      = columnDefinition.ColumnAttribute?.FieldLength;
            var columnTypeString = "";

            switch (dbType)
            {
            case DbType.AnsiString:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "255" : fieldLength;
                columnTypeString = DbTypeAnsiString(fieldLength);
                break;

            case DbType.AnsiStringFixedLength:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "255" : fieldLength;
                columnTypeString = DbTypeAnsiStringFixedLength(fieldLength);
                break;

            case DbType.String:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "255" : fieldLength;
                columnTypeString = DbTypeString(fieldLength);
                break;

            case DbType.StringFixedLength:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "255" : fieldLength;
                columnTypeString = DbTypeStringFixedLength(fieldLength);
                break;

            case DbType.Binary:
                columnTypeString = DbTypeBinary(fieldLength);
                break;

            case DbType.Boolean:
                columnTypeString = DbTypeBoolean(fieldLength);
                break;

            case DbType.Byte:
                columnTypeString = DbTypeByte(fieldLength);
                break;

            case DbType.Currency:
                columnTypeString = DbTypeCurrency(fieldLength);
                break;

            case DbType.Date:
                columnTypeString = DbTypeDate(fieldLength);
                break;

            case DbType.DateTime:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "3" : fieldLength;
                columnTypeString = DbTypeDateTime(fieldLength);
                break;

            case DbType.DateTime2:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "3" : fieldLength;
                columnTypeString = DbTypeDateTime2(fieldLength);
                break;

            case DbType.DateTimeOffset:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "3" : fieldLength;
                columnTypeString = DbTypeDateTimeOffset(fieldLength);
                break;

            case DbType.Decimal:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "38,6" : fieldLength;
                columnTypeString = DbTypeDecimal(fieldLength);
                break;

            case DbType.Double:
                columnTypeString = DbTypeDouble(fieldLength);
                break;

            case DbType.Guid:
                columnTypeString = DbTypeGuid(fieldLength);
                break;

            case DbType.Int16:
                columnTypeString = DbTypeInt16(fieldLength);
                break;

            case DbType.Int32:
                columnTypeString = DbTypeInt32(fieldLength);
                break;

            case DbType.Int64:
                columnTypeString = DbTypeInt64(fieldLength);
                break;

            case DbType.Object:
                columnTypeString = DbTypeObject(fieldLength);
                break;

            case DbType.SByte:
                columnTypeString = DbTypeSByte(fieldLength);
                break;

            case DbType.Single:
                columnTypeString = DbTypeSingle(fieldLength);
                break;

            case DbType.Time:
                columnTypeString = DbTypeTime(fieldLength);
                break;

            case DbType.UInt16:
                columnTypeString = DbTypeUInt16(fieldLength);
                break;

            case DbType.UInt32:
                columnTypeString = DbTypeUInt32(fieldLength);
                break;

            case DbType.UInt64:
                columnTypeString = DbTypeUInt64(fieldLength);
                break;

            case DbType.VarNumeric:
                fieldLength      = string.IsNullOrEmpty(fieldLength) ? "38,6" : fieldLength;
                columnTypeString = DbTypeVarNumeric(fieldLength);
                break;

            case DbType.Xml:
                columnTypeString = DbTypeXml(fieldLength);
                break;
            }

            return(columnTypeString);
        }
Example #2
0
        private static Tuple <SqlTableDefine, List <SqlColumnDefine> > GetEntityDefine(Type type)
        {
            //处理表定义
            var name = type.Name;

            DBTableAttribute tableAttr = new DBTableAttribute("");


            if (EnvHelper.IsNetFX)
            {
#if NETCOREAPP1_0 || NETSTANDARD1_6
#else
                tableAttr = type.GetCustomAttribute <DBTableAttribute>();
#endif
            }
            else
            {
                tableAttr = type.GetTypeInfo().GetCustomAttribute <DBTableAttribute>();
            }

            var sqlTableDef = new SqlTableDefine(tableAttr, name);

            //处理列定义
            var colDeflist = new List <SqlColumnDefine>();

            var columns = type.GetProperties();

            foreach (var cp in columns)
            {
                var ignore = cp.GetCustomAttribute <DBIgnoreAttribute>();

                if (ignore == null)
                {
                    var keyAttr      = cp.GetCustomAttribute <DBKeyAttribute>();
                    var columnAttr   = cp.GetCustomAttribute <DBColumnAttribute>();
                    var dataTypeAttr = cp.GetCustomAttribute <DBCustomeDataTypeAttribute>();

                    var indexAttr = cp.GetCustomAttribute <DBIndexAttribute>();

                    var cname = cp.Name;

                    var alias = cname;
                    if (columnAttr != null)
                    {
                        alias = columnAttr.Name;
                    }

                    // edit by cheery 2017-2-21
                    var nullable = true;
                    // 如果是Key 不允许空
                    if (keyAttr != null)
                    {
                        nullable = false;
                    }
                    // 如果字段定义上有是否允许空标记 则依赖该标记
                    else if (columnAttr?.Nullable != null)
                    {
                        nullable = columnAttr.Nullable.Value;
                    }
                    // 否则 根据类型判断
                    else
                    {
                        nullable = cp.PropertyType.IsNullableType();
                    }

                    //var nullable = keyAttr == null && (columnAttr?.Nullable ?? cp.PropertyType.IsNullableType());

                    var cd = new SqlColumnDefine(cname, alias, null, cp.PropertyType, nullable, columnAttr, keyAttr, dataTypeAttr, null, indexAttr);

                    colDeflist.Add(cd);
                }
            }

            return(new Tuple <SqlTableDefine, List <SqlColumnDefine> >(sqlTableDef, colDeflist));
        }