Exemplo n.º 1
0
        public static ColumnDefinition GetColumnDefinition(Type modelType, PropertyInfo propertyInfo, string columnName, string tableName)
        {
            var definition = new ColumnDefinition{ Name = columnName, TableName = tableName, ModificationType = ModificationType.Create };

            //Look for specific Null setting attributed a column
            var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
            if (nullSettingAttribute != null)
            {
                definition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
            }

            //Look for specific DbType attributed a column
            var databaseTypeAttribute = propertyInfo.FirstAttribute<SpecialDbTypeAttribute>();
            if (databaseTypeAttribute != null)
            {
                definition.HasSpecialDbType = true;
                definition.DbType = databaseTypeAttribute.DatabaseType;
            }
            else
            {
                definition.PropertyType = propertyInfo.PropertyType;
            }

            //Look for Primary Key for the current column
            var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
            if (primaryKeyColumnAttribute != null)
            {
                string primaryKeyName = string.IsNullOrEmpty(primaryKeyColumnAttribute.Name)
                                            ? string.Format("PK_{0}", tableName)
                                            : primaryKeyColumnAttribute.Name;

                definition.IsPrimaryKey = true;
                definition.IsIdentity = primaryKeyColumnAttribute.AutoIncrement;
                definition.IsIndexed = primaryKeyColumnAttribute.Clustered;
                definition.PrimaryKeyName = primaryKeyName;
                definition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
                definition.Seeding = primaryKeyColumnAttribute.IdentitySeed;
            }

            //Look for Size/Length of DbType
            var lengthAttribute = propertyInfo.FirstAttribute<LengthAttribute>();
            if (lengthAttribute != null)
            {
                definition.Size = lengthAttribute.Length;
            }

            //Look for Constraint for the current column
            var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
            if (constraintAttribute != null)
            {
                definition.ConstraintName = constraintAttribute.Name ?? string.Empty;
                definition.DefaultValue = constraintAttribute.Default ?? string.Empty;
            }

            return definition;
        }
Exemplo n.º 2
0
 private static string GetModelPropertyName(PropertyInfo prop)
 {
     var dataMemberAttr = prop.FirstAttribute<DataMemberAttribute>();
     if (dataMemberAttr != null && !dataMemberAttr.Name.IsNullOrEmpty()) 
         return dataMemberAttr.Name;
     
     return UseCamelCaseModelPropertyNames
         ? (UseLowercaseUnderscoreModelPropertyNames ? prop.Name.ToLowercaseUnderscore() : prop.Name.ToCamelCase())
         : prop.Name;
 }
        public MetadataPropertyType ToProperty(PropertyInfo pi, object instance = null)
        {
            var property = new MetadataPropertyType
            {
                Name = pi.Name,
                Attributes = ToAttributes(pi.GetCustomAttributes(false)),
                Type = pi.PropertyType.GetMetadataPropertyType(),
                IsValueType = pi.PropertyType.IsValueType() ? true : (bool?)null,
                IsSystemType = pi.PropertyType.IsSystemType() ? true : (bool?)null,
                IsEnum = pi.PropertyType.IsEnum() ? true : (bool?)null,
                TypeNamespace = pi.PropertyType.Namespace,
                DataMember = ToDataMember(pi.GetDataMember()),
                GenericArgs = pi.PropertyType.IsGenericType()
                    ? pi.PropertyType.GetGenericArguments().Select(x => x.ExpandTypeName()).ToArray()
                    : null,
                Description = pi.GetDescription(),
            };

            var apiMember = pi.FirstAttribute<ApiMemberAttribute>();
            if (apiMember != null)
            {
                if (apiMember.IsRequired)
                    property.IsRequired = true;

                property.ParamType = apiMember.ParameterType;
                property.DisplayType = apiMember.DataType;
            }

            var apiAllowableValues = pi.FirstAttribute<ApiAllowableValuesAttribute>();
            if (apiAllowableValues != null)
            {
                property.AllowableValues = apiAllowableValues.Values;
                property.AllowableMin = apiAllowableValues.Min;
                property.AllowableMax = apiAllowableValues.Max;
            }

            if (instance != null)
            {
                var value = pi.GetValue(instance, null);
                if (value != null
                    && !value.Equals(pi.PropertyType.GetDefaultValue()))
                {
                    if (pi.PropertyType.IsEnum())
                    {
                        property.Value = "{0}.{1}".Fmt(pi.PropertyType.Name, value);
                    }
                    else if (pi.PropertyType == typeof(Type))
                    {
                        var type = (Type)value;
                        property.Value = $"typeof({type.FullName})";
                    }
                    else
                    {
                        var strValue = value as string;
                        property.Value = strValue ?? value.ToJson();
                    }
                }

                if (pi.GetSetMethod() == null) //ReadOnly is bool? to minimize serialization
                    property.ReadOnly = true;
            }
            return property;
        }
Exemplo n.º 4
0
        public static ColumnDefinition GetColumnDefinition(Type modelType, PropertyInfo propertyInfo, string columnName, string tableName)
        {
            var definition = new ColumnDefinition{ Name = columnName, TableName = tableName, ModificationType = ModificationType.Create };

            //Look for specific Null setting attributed a column
            var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
            if (nullSettingAttribute != null)
            {
                definition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
            }

            //Look for specific DbType attributed a column
            var databaseTypeAttribute = propertyInfo.FirstAttribute<SpecialDbTypeAttribute>();
            if (databaseTypeAttribute != null)
            {
                definition.HasSpecialDbType = true;
                definition.DbType = databaseTypeAttribute.DatabaseType;
            }
            else
            {
                definition.PropertyType = propertyInfo.PropertyType;
            }

            //Look for Primary Key for the current column
            var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
            if (primaryKeyColumnAttribute != null)
            {
                string primaryKeyName = string.IsNullOrEmpty(primaryKeyColumnAttribute.Name)
                                            ? string.Format("PK_{0}", tableName)
                                            : primaryKeyColumnAttribute.Name;

                definition.IsPrimaryKey = true;
                definition.IsIdentity = primaryKeyColumnAttribute.AutoIncrement;
                definition.IsIndexed = primaryKeyColumnAttribute.Clustered;
                definition.PrimaryKeyName = primaryKeyName;
                definition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
                definition.Seeding = primaryKeyColumnAttribute.IdentitySeed;
            }

            //Look for Size/Length of DbType
            var lengthAttribute = propertyInfo.FirstAttribute<LengthAttribute>();
            if (lengthAttribute != null)
            {
                definition.Size = lengthAttribute.Length;
            }

            //Look for Constraint for the current column
            var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
            if (constraintAttribute != null)
            {
                //Special case for MySQL as it can't have multiple default DateTime values, which 
                //is what the umbracoServer table definition is trying to create
                if (SqlSyntaxContext.SqlSyntaxProvider is MySqlSyntaxProvider && definition.TableName == "umbracoServer" &&
                        definition.TableName.ToLowerInvariant() == "lastNotifiedDate".ToLowerInvariant())
                    return definition;

                definition.ConstraintName = constraintAttribute.Name ?? string.Empty;
                definition.DefaultValue = constraintAttribute.Default ?? string.Empty;
            }

            return definition;
        }