Esempio n. 1
0
        public TableColumn(PropertyInfo prop, Action <PropertyInfo, object, object> setValue, Func <PropertyInfo, object, object> getValue, CreateFlags createFlags)
        {
            const string ImplicitPkName      = "Id";
            const string ImplicitIndexSuffix = "Id";

            var colAttr = (ColumnAttribute)prop.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault();

            _prop        = prop;
            PropertyName = prop.Name;
            PropertyType = prop.PropertyType;

            var propertyTypeInfo = PropertyType.GetTypeInfo();

            PropertyDefaultValue = (PropertyType != null && propertyTypeInfo.IsValueType && Nullable.GetUnderlyingType(PropertyType) == null) ? Activator.CreateInstance(PropertyType) : null;

            _setValue = setValue;
            _getValue = getValue;

            Name = colAttr == null ? prop.Name : colAttr.Name;
            //If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead
            ColumnType = Nullable.GetUnderlyingType(PropertyType) ?? PropertyType;

            var columnTypeInfo = ColumnType.GetTypeInfo();

            IsEnum = columnTypeInfo.IsEnum;

            var attr = prop.GetCustomAttributes(true);

            Collation = attr.OfType <CollationAttribute>().FirstOrDefault()?.Value ?? "";

            IsPK = attr.OfType <PrimaryKeyAttribute>().Any() || (createFlags.HasFlag(CreateFlags.ImplicitPK) && String.Equals(prop.Name, ImplicitPkName, StringComparison.OrdinalIgnoreCase));

            var isAuto = attr.OfType <AutoIncrementAttribute>().Any() || (IsPK && createFlags.HasFlag(CreateFlags.AutoIncPK));

            IsAutoGuid = isAuto && ColumnType == typeof(Guid);
            IsAutoInc  = isAuto && !IsAutoGuid;

            Indices = attr.OfType <IndexedAttribute>();

            if (!Indices.Any() &&
                !IsPK &&
                createFlags.HasFlag(CreateFlags.ImplicitIndex) &&
                Name.EndsWith(ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)
                )
            {
                Indices = new IndexedAttribute[] { new IndexedAttribute() };
            }
            IsNullable = !(IsPK || attr.OfType <NotNullAttribute>().Any());

            MaxStringLength = attr.OfType <MaxLengthAttribute>().FirstOrDefault()?.Value;
            DefaultValue    = attr.OfType <DefaultAttribute>().FirstOrDefault()?.Value;
            StoreAsText     = attr.OfType <StoreAsTextAttribute>().Any();
        }
Esempio n. 2
0
            public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
            {
                ColumnAttribute columnAttribute = (ColumnAttribute)prop.GetCustomAttributes(typeof(ColumnAttribute), inherit: true).FirstOrDefault();

                _prop      = prop;
                Name       = ((columnAttribute != null) ? columnAttribute.Name : prop.Name);
                ColumnType = (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                Collation  = Orm.Collation(prop);
                IsPK       = (Orm.IsPK(prop) || ((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK && string.Compare(prop.Name, "Id", StringComparison.OrdinalIgnoreCase) == 0));
                bool flag = Orm.IsAutoInc(prop) || (IsPK && (createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK);

                IsAutoGuid = (flag && ColumnType == typeof(Guid));
                IsAutoInc  = (flag && !IsAutoGuid);
                Indices    = Orm.GetIndices(prop);
                if (!Indices.Any() && !IsPK && (createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex && Name.EndsWith("Id", StringComparison.OrdinalIgnoreCase))
                {
                    Indices = new IndexedAttribute[1]
                    {
                        new IndexedAttribute()
                    };
                }
                IsNullable      = (!IsPK && !Orm.IsMarkedNotNull(prop));
                MaxStringLength = Orm.MaxStringLength(prop);
            }