示例#1
0
            public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None, IColumnInformationProvider infoProvider = null)
            {
                infoProvider ??= new DefaultColumnInformationProvider();

                _prop = prop;
                Name  = infoProvider.GetColumnName(prop);
                //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(prop.PropertyType) ?? prop.PropertyType;
                Collation  = Orm.Collation(prop);

                IsPK = Orm.IsPK(prop) ||
                       (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) &&
                        string.Compare(prop.Name, Orm.ImplicitPkName, StringComparison.OrdinalIgnoreCase) == 0);

                var isAuto = Orm.IsAutoInc(prop) ||
                             (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK));

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

                DefaultValue = Orm.GetDefaultValue(prop);

                Indices = Orm.GetIndices(prop);
                if (!Indices.Any() &&
                    !IsPK &&
                    ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex) &&
                    Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase))
                {
                    Indices = new[] { new IndexedAttribute() };
                }
                IsNullable      = !(IsPK || Orm.IsMarkedNotNull(prop));
                MaxStringLength = Orm.MaxStringLength(prop);
            }
示例#2
0
            public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
            {
                var colAttr = (ColumnAttribute)prop.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault();

                _prop = prop;
                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(prop.PropertyType) ?? prop.PropertyType;
                Collation  = Orm.Collation(prop);

                IsPK = Orm.IsPK(prop) ||
                       (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) &&
                        string.Compare(prop.Name, Orm.ImplicitPkName, StringComparison.OrdinalIgnoreCase) == 0);

                IsCK = Orm.IsCK(prop);

                IsFK = Orm.IsFK(prop);
                if (IsFK)
                {
                    ForeignKeyAttribute attr     = prop.GetCustomAttribute <ForeignKeyAttribute>(true);
                    TableMapping        refTable = new TableMapping(attr.ReferenceTable);
                    string refColumnName         = attr.ReferenceColumn ?? this.Name;
                    var    refColumns            = refTable.Columns.Select(c => c).Where(c => c.Name == refColumnName).ToArray();
                    if (refColumns.Length > 0)
                    {
                        FK = new ForeignKey(this, attr.ReferenceTable, refColumns[0]);
                    }
                    else
                    {
                        throw new Exception(string.Format("The referenced column \"{0}\" in {1} doesn't exist", refColumnName, this.Name));
                    }
                }

                var isAuto = Orm.IsAutoInc(prop) || (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK));

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

                Indices = Orm.GetIndices(prop);
                if (!Indices.Any() &&
                    !IsPK &&
                    ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex) &&
                    Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)
                    )
                {
                    Indices = new IndexedAttribute[] { new IndexedAttribute() };
                }
                IsNullable      = !(IsPK || Orm.IsMarkedNotNull(prop));
                MaxStringLength = Orm.MaxStringLength(prop);

                StoreAsText = prop.PropertyType.GetTypeInfo().GetCustomAttribute(typeof(StoreAsTextAttribute), false) != null;
            }
示例#3
0
        public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
        {
            var colAttr = prop.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(ColumnAttribute));

            _prop = prop;
#if ENABLE_IL2CPP
            var ca = prop.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
            Name = ca == null ? prop.Name : ca.Name;
#else
            Name = (colAttr != null && colAttr.ConstructorArguments.Count > 0) ?
                   colAttr.ConstructorArguments[0].Value?.ToString() :
                   prop.Name;
#endif
            //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(prop.PropertyType) ?? prop.PropertyType;
            Collation  = Orm.Collation(prop);

            IsPK = Orm.IsPK(prop) ||
                   (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) &&
                    string.Compare(prop.Name, Orm.ImplicitPkName, StringComparison.OrdinalIgnoreCase) == 0);

            var isAuto = Orm.IsAutoInc(prop) || (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK));
            IsAutoGuid  = isAuto && ColumnType == typeof(Guid);
            IsAutoInc   = isAuto && !IsAutoGuid;
            IsAutomatic = Orm.IsAutomatic(prop);

            Indices = Orm.GetIndices(prop);
            if (!Indices.Any() &&
                !IsPK &&
                ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex) &&
                Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)
                )
            {
                Indices = new IndexedAttribute[] { new IndexedAttribute() };
            }
            IsNullable      = !(IsPK || Orm.IsMarkedNotNull(prop));
            MaxStringLength = Orm.MaxStringLength(prop);

            StoreAsText = prop.PropertyType.GetTypeInfo().CustomAttributes.Any(x => x.AttributeType == typeof(StoreAsTextAttribute));
        }