Пример #1
0
        /// <summary>
        /// Creates descriptor instance.
        /// </summary>
        /// <param name="mappingSchema">Mapping schema, associated with descriptor.</param>
        /// <param name="columnAttribute">Column attribute, from which descriptor data should be extracted.</param>
        /// <param name="memberAccessor">Column mapping member accessor.</param>
        public ColumnDescriptor(MappingSchema mappingSchema, ColumnAttribute columnAttribute, MemberAccessor memberAccessor)
        {
            MemberAccessor = memberAccessor;
            MemberInfo     = memberAccessor.MemberInfo;

            if (MemberInfo.IsFieldEx())
            {
                var fieldInfo = (FieldInfo)MemberInfo;
                MemberType = fieldInfo.FieldType;
            }
            else if (MemberInfo.IsPropertyEx())
            {
                var propertyInfo = (PropertyInfo)MemberInfo;
                MemberType = propertyInfo.PropertyType;
            }

            MemberName      = columnAttribute.MemberName ?? MemberInfo.Name;
            ColumnName      = columnAttribute.Name ?? MemberInfo.Name;
            Storage         = columnAttribute.Storage;
            PrimaryKeyOrder = columnAttribute.PrimaryKeyOrder;
            IsDiscriminator = columnAttribute.IsDiscriminator;
            DataType        = columnAttribute.DataType;
            DbType          = columnAttribute.DbType;
            CreateFormat    = columnAttribute.CreateFormat;

            if (columnAttribute.HasLength())
            {
                Length = columnAttribute.Length;
            }
            if (columnAttribute.HasPrecision())
            {
                Precision = columnAttribute.Precision;
            }
            if (columnAttribute.HasScale())
            {
                Scale = columnAttribute.Scale;
            }
            if (columnAttribute.HasOrder())
            {
                Order = columnAttribute.Order;
            }

            if (Storage == null)
            {
                StorageType = MemberType;
                StorageInfo = MemberInfo;
            }
            else
            {
                var expr = Expression.PropertyOrField(Expression.Constant(null, MemberInfo.DeclaringType), Storage);
                StorageType = expr.Type;
                StorageInfo = expr.Member;
            }

            var defaultCanBeNull = false;

            if (columnAttribute.HasCanBeNull())
            {
                CanBeNull = columnAttribute.CanBeNull;
            }
            else
            {
                var na = mappingSchema.GetAttribute <NullableAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (na != null)
                {
                    CanBeNull = na.CanBeNull;
                }
                else
                {
                    CanBeNull        = mappingSchema.GetCanBeNull(MemberType);
                    defaultCanBeNull = true;
                }
            }

            if (columnAttribute.HasIsIdentity())
            {
                IsIdentity = columnAttribute.IsIdentity;
            }
            else if (MemberName.IndexOf(".") < 0)
            {
                var a = mappingSchema.GetAttribute <IdentityAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);
                if (a != null)
                {
                    IsIdentity = true;
                }
            }

            SequenceName = mappingSchema.GetAttribute <SequenceNameAttribute>(memberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

            if (SequenceName != null)
            {
                IsIdentity = true;
            }

            SkipOnInsert = columnAttribute.HasSkipOnInsert() ? columnAttribute.SkipOnInsert : IsIdentity;
            SkipOnUpdate = columnAttribute.HasSkipOnUpdate() ? columnAttribute.SkipOnUpdate : IsIdentity;

            if (defaultCanBeNull && IsIdentity)
            {
                CanBeNull = false;
            }

            if (columnAttribute.HasIsPrimaryKey())
            {
                IsPrimaryKey = columnAttribute.IsPrimaryKey;
            }
            else if (MemberName.IndexOf(".") < 0)
            {
                var a = mappingSchema.GetAttribute <PrimaryKeyAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    IsPrimaryKey    = true;
                    PrimaryKeyOrder = a.Order;
                }
            }

            if (DbType == null || DataType == DataType.Undefined)
            {
                var a = mappingSchema.GetAttribute <DataTypeAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    if (DbType == null)
                    {
                        DbType = a.DbType;
                    }

                    if (DataType == DataType.Undefined && a.DataType.HasValue)
                    {
                        DataType = a.DataType.Value;
                    }
                }
            }
        }
Пример #2
0
        void Init()
        {
            var ta = _mappingSchema.GetAttribute <TableAttribute>(TypeAccessor.Type, a => a.Configuration);

            if (ta != null)
            {
                TableName    = ta.Name;
                SchemaName   = ta.Schema;
                DatabaseName = ta.Database;
                IsColumnAttributeRequired = ta.IsColumnAttributeRequired;
            }

            if (TableName == null)
            {
                TableName = TypeAccessor.Type.Name;

                if (TypeAccessor.Type.IsInterfaceEx() && TableName.Length > 1 && TableName[0] == 'I')
                {
                    TableName = TableName.Substring(1);
                }
            }

            var attrs = new List <ColumnAttribute>();

            foreach (var member in TypeAccessor.Members)
            {
                var aa = _mappingSchema.GetAttribute <AssociationAttribute>(member.MemberInfo, attr => attr.Configuration);

                if (aa != null)
                {
                    Associations.Add(new AssociationDescriptor(
                                         TypeAccessor.Type, member.MemberInfo, aa.GetThisKeys(), aa.GetOtherKeys(), aa.Storage, aa.CanBeNull));
                    continue;
                }

                var ca = _mappingSchema.GetAttribute <ColumnAttribute>(member.MemberInfo, attr => attr.Configuration);

                if (ca != null)
                {
                    if (ca.IsColumn)
                    {
                        if (ca.MemberName != null)
                        {
                            attrs.Add(new ColumnAttribute(member.Name, ca));
                        }
                        else
                        {
                            var cd = new ColumnDescriptor(_mappingSchema, ca, member);
                            Columns.Add(cd);
                            _columnNames.Add(member.Name, cd);
                        }
                    }
                }
                else if (
                    !IsColumnAttributeRequired && _mappingSchema.IsScalarType(member.Type) ||
                    _mappingSchema.GetAttribute <IdentityAttribute>(member.MemberInfo, attr => attr.Configuration) != null ||
                    _mappingSchema.GetAttribute <PrimaryKeyAttribute>(member.MemberInfo, attr => attr.Configuration) != null)
                {
                    var cd = new ColumnDescriptor(_mappingSchema, new ColumnAttribute(), member);
                    Columns.Add(cd);
                    _columnNames.Add(member.Name, cd);
                }
                else
                {
                    var caa = _mappingSchema.GetAttribute <ColumnAliasAttribute>(member.MemberInfo, attr => attr.Configuration);

                    if (caa != null)
                    {
                        if (Aliases == null)
                        {
                            Aliases = new Dictionary <string, string>();
                        }

                        Aliases.Add(member.Name, caa.MemberName);
                    }
                }
            }

            var typeColumnAttrs = _mappingSchema.GetAttributes <ColumnAttribute>(TypeAccessor.Type, a => a.Configuration);

            foreach (var attr in typeColumnAttrs.Concat(attrs))
            {
                if (attr.IsColumn)
                {
                    SetColumn(attr);
                }
            }
        }
Пример #3
0
        public ColumnDescriptor(MappingSchema mappingSchema, ColumnAttribute columnAttribute, MemberAccessor memberAccessor)
        {
            MappingSchema  = mappingSchema;
            MemberAccessor = memberAccessor;
            MemberInfo     = memberAccessor.MemberInfo;

            if (MemberInfo.IsFieldEx())
            {
                var fieldInfo = (FieldInfo)MemberInfo;
                MemberType = fieldInfo.FieldType;
            }
            else if (MemberInfo.IsPropertyEx())
            {
                var propertyInfo = (PropertyInfo)MemberInfo;
                MemberType = propertyInfo.PropertyType;
            }

            MemberName      = columnAttribute.MemberName ?? MemberInfo.Name;
            ColumnName      = columnAttribute.Name ?? MemberInfo.Name;
            Storage         = columnAttribute.Storage;
            PrimaryKeyOrder = columnAttribute.PrimaryKeyOrder;
            IsDiscriminator = columnAttribute.IsDiscriminator;
            DataType        = columnAttribute.DataType;
            DbType          = columnAttribute.DbType;
            Length          = columnAttribute.Length;
            Precision       = columnAttribute.Precision;
            Scale           = columnAttribute.Scale;

            var defaultCanBeNull = false;

            if (columnAttribute.HasCanBeNull())
            {
                CanBeNull = columnAttribute.CanBeNull;
            }
            else
            {
                var na = mappingSchema.GetAttribute <NullableAttribute>(MemberInfo, attr => attr.Configuration);

                if (na != null)
                {
                    CanBeNull = na.CanBeNull;
                }
                else
                {
                    CanBeNull        = mappingSchema.GetCanBeNull(MemberType);
                    defaultCanBeNull = true;
                }
            }

            if (columnAttribute.HasIsIdentity())
            {
                IsIdentity = columnAttribute.IsIdentity;
            }
            else
            {
                var a = mappingSchema.GetAttribute <IdentityAttribute>(MemberInfo, attr => attr.Configuration);
                if (a != null)
                {
                    IsIdentity = true;
                }
            }

            SkipOnInsert = columnAttribute.HasSkipOnInsert() ? columnAttribute.SkipOnInsert : IsIdentity;
            SkipOnUpdate = columnAttribute.HasSkipOnUpdate() ? columnAttribute.SkipOnUpdate : IsIdentity;

            if (defaultCanBeNull && IsIdentity)
            {
                CanBeNull = false;
            }

            if (columnAttribute.HasIsPrimaryKey())
            {
                IsPrimaryKey = columnAttribute.IsPrimaryKey;
            }
            else
            {
                var a = mappingSchema.GetAttribute <PrimaryKeyAttribute>(MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    IsPrimaryKey    = true;
                    PrimaryKeyOrder = a.Order;
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Creates descriptor instance.
        /// </summary>
        /// <param name="mappingSchema">Mapping schema, associated with descriptor.</param>
        /// <param name="columnAttribute">Column attribute, from which descriptor data should be extracted.</param>
        /// <param name="memberAccessor">Column mapping member accessor.</param>
        public ColumnDescriptor(MappingSchema mappingSchema, ColumnAttribute?columnAttribute, MemberAccessor memberAccessor)
        {
            MappingSchema  = mappingSchema;
            MemberAccessor = memberAccessor;
            MemberInfo     = memberAccessor.MemberInfo;

            if (MemberInfo.IsFieldEx())
            {
                var fieldInfo = (FieldInfo)MemberInfo;
                MemberType = fieldInfo.FieldType;
            }
            else if (MemberInfo.IsPropertyEx())
            {
                var propertyInfo = (PropertyInfo)MemberInfo;
                MemberType = propertyInfo.PropertyType;
            }

            var dataType = mappingSchema.GetDataType(MemberType);

            if (dataType.Type.DataType == DataType.Undefined)
            {
                dataType = mappingSchema.GetUnderlyingDataType(dataType.SystemType, out var _);
            }

            if (columnAttribute == null)
            {
                columnAttribute = new ColumnAttribute();

                columnAttribute.DataType = dataType.Type.DataType;
                columnAttribute.DbType   = dataType.Type.DbType;

                if (dataType.Type.Length != null)
                {
                    columnAttribute.Length = dataType.Type.Length.Value;
                }
                if (dataType.Type.Precision != null)
                {
                    columnAttribute.Precision = dataType.Type.Precision.Value;
                }
                if (dataType.Type.Scale != null)
                {
                    columnAttribute.Scale = dataType.Type.Scale.Value;
                }
            }
            else if (columnAttribute.DataType == DataType.Undefined || columnAttribute.DataType == dataType.Type.DataType)
            {
                if (dataType.Type.Length != null && !columnAttribute.HasLength())
                {
                    columnAttribute.Length = dataType.Type.Length.Value;
                }
                if (dataType.Type.Precision != null && !columnAttribute.HasPrecision())
                {
                    columnAttribute.Precision = dataType.Type.Precision.Value;
                }
                if (dataType.Type.Scale != null && !columnAttribute.HasScale())
                {
                    columnAttribute.Scale = dataType.Type.Scale.Value;
                }
            }

            MemberName        = columnAttribute.MemberName ?? MemberInfo.Name;
            ColumnName        = columnAttribute.Name ?? MemberInfo.Name;
            Storage           = columnAttribute.Storage;
            PrimaryKeyOrder   = columnAttribute.PrimaryKeyOrder;
            IsDiscriminator   = columnAttribute.IsDiscriminator;
            SkipOnEntityFetch = columnAttribute.SkipOnEntityFetch;
            DataType          = columnAttribute.DataType;
            DbType            = columnAttribute.DbType;
            CreateFormat      = columnAttribute.CreateFormat;

            if (columnAttribute.HasLength())
            {
                Length = columnAttribute.Length;
            }
            if (columnAttribute.HasPrecision())
            {
                Precision = columnAttribute.Precision;
            }
            if (columnAttribute.HasScale())
            {
                Scale = columnAttribute.Scale;
            }
            if (columnAttribute.HasOrder())
            {
                Order = columnAttribute.Order;
            }

            if (Storage == null)
            {
                StorageType = MemberType;
                StorageInfo = MemberInfo;
            }
            else
            {
                var expr = ExpressionHelper.PropertyOrField(Expression.Constant(null, MemberInfo.DeclaringType), Storage);
                StorageType = expr.Type;
                StorageInfo = expr.Member;
            }

            var defaultCanBeNull = false;

            if (columnAttribute.HasCanBeNull())
            {
                CanBeNull = columnAttribute.CanBeNull;
            }
            else
            {
                var na = mappingSchema.GetAttribute <NullableAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (na != null)
                {
                    CanBeNull = na.CanBeNull;
                }
                else
                {
                    CanBeNull        = mappingSchema.GetCanBeNull(MemberType);
                    defaultCanBeNull = true;
                }
            }

            if (columnAttribute.HasIsIdentity())
            {
                IsIdentity = columnAttribute.IsIdentity;
            }
            else if (MemberName.IndexOf(".") < 0)
            {
                var a = mappingSchema.GetAttribute <IdentityAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);
                if (a != null)
                {
                    IsIdentity = true;
                }
            }

            SequenceName = mappingSchema.GetAttribute <SequenceNameAttribute>(memberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

            if (SequenceName != null)
            {
                IsIdentity = true;
            }

            SkipOnInsert = columnAttribute.HasSkipOnInsert() ? columnAttribute.SkipOnInsert : IsIdentity;
            SkipOnUpdate = columnAttribute.HasSkipOnUpdate() ? columnAttribute.SkipOnUpdate : IsIdentity;

            if (defaultCanBeNull && IsIdentity)
            {
                CanBeNull = false;
            }

            if (columnAttribute.HasIsPrimaryKey())
            {
                IsPrimaryKey = columnAttribute.IsPrimaryKey;
            }
            else if (MemberName.IndexOf(".") < 0)
            {
                var a = mappingSchema.GetAttribute <PrimaryKeyAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    IsPrimaryKey    = true;
                    PrimaryKeyOrder = a.Order;
                }
            }

            if (DbType == null || DataType == DataType.Undefined)
            {
                var a = mappingSchema.GetAttribute <DataTypeAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    if (DbType == null)
                    {
                        DbType = a.DbType;
                    }

                    if (DataType == DataType.Undefined && a.DataType.HasValue)
                    {
                        DataType = a.DataType.Value;
                    }
                }
            }

            if (MemberType.ToNullableUnderlying().IsEnum)
            {
                if (DataType == DataType.Undefined)
                {
                    var enumtype = mappingSchema.GetDefaultFromEnumType(MemberType);

                    if (enumtype != null)
                    {
                        DataType = mappingSchema.GetDataType(enumtype).Type.DataType;
                    }
                }

                if (DataType == DataType.Undefined && MemberType.IsNullable())
                {
                    var enumtype = mappingSchema.GetDefaultFromEnumType(MemberType.ToNullableUnderlying());

                    if (enumtype != null)
                    {
                        DataType = mappingSchema.GetDataType(enumtype).Type.DataType;
                    }
                }

                if (DataType == DataType.Undefined)
                {
                    var enumtype = mappingSchema.GetDefaultFromEnumType(typeof(Enum));

                    if (enumtype != null)
                    {
                        DataType = mappingSchema.GetDataType(enumtype).Type.DataType;
                    }
                }

                if (DataType == DataType.Undefined)
                {
                    DataType = mappingSchema.GetUnderlyingDataType(MemberType, out var canBeNull).Type.DataType;
                    if (canBeNull)
                    {
                        CanBeNull = canBeNull;
                    }
                }
            }

            if (DataType == DataType.Undefined)
            {
                DataType = mappingSchema.GetDataType(MemberType).Type.DataType;
            }

            var skipValueAttributes = mappingSchema.GetAttributes <SkipBaseAttribute>(MemberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

            if (skipValueAttributes.Length > 0)
            {
                SkipBaseAttributes    = skipValueAttributes;
                SkipModificationFlags = SkipBaseAttributes.Aggregate(SkipModification.None, (s, c) => s | c.Affects);
            }

            var vc = mappingSchema.GetAttribute <ValueConverterAttribute>(memberAccessor.TypeAccessor.Type, MemberInfo, attr => attr.Configuration);

            if (vc != null)
            {
                ValueConverter = vc.GetValueConverter(this);
            }
        }
Пример #5
0
        void Init(MappingSchema mappingSchema)
        {
            var ta = mappingSchema.GetAttribute <TableAttribute>(TypeAccessor.Type, a => a.Configuration);

            if (ta != null)
            {
                TableName    = ta.Name;
                SchemaName   = ta.Schema;
                DatabaseName = ta.Database;
                IsColumnAttributeRequired = ta.IsColumnAttributeRequired;
            }

            if (TableName == null)
            {
                TableName = TypeAccessor.Type.Name;

                if (TypeAccessor.Type.IsInterfaceEx() && TableName.Length > 1 && TableName[0] == 'I')
                {
                    TableName = TableName.Substring(1);
                }
            }

            var attrs   = new List <ColumnAttribute>();
            var members = TypeAccessor.Members.Concat(
                mappingSchema.GetDynamicColumns(ObjectType).Select(dc => new MemberAccessor(TypeAccessor, dc)));

            foreach (var member in members)
            {
                var aa = mappingSchema.GetAttribute <AssociationAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration);

                if (aa != null)
                {
                    Associations.Add(new AssociationDescriptor(
                                         TypeAccessor.Type, member.MemberInfo, aa.GetThisKeys(), aa.GetOtherKeys(), aa.ExpressionPredicate, aa.Storage, aa.CanBeNull));
                    continue;
                }

                var ca = mappingSchema.GetAttribute <ColumnAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration);

                if (ca != null)
                {
                    if (ca.IsColumn)
                    {
                        if (ca.MemberName != null)
                        {
                            attrs.Add(new ColumnAttribute(member.Name, ca));
                        }
                        else
                        {
                            var cd = new ColumnDescriptor(mappingSchema, ca, member);
                            Columns.Add(cd);
                            _columnNames.Add(member.Name, cd);
                        }
                    }
                }
                else if (
                    !IsColumnAttributeRequired && mappingSchema.IsScalarType(member.Type) ||
                    mappingSchema.GetAttribute <IdentityAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration) != null ||
                    mappingSchema.GetAttribute <PrimaryKeyAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration) != null)
                {
                    var cd = new ColumnDescriptor(mappingSchema, new ColumnAttribute(), member);
                    Columns.Add(cd);
                    _columnNames.Add(member.Name, cd);
                }
                else
                {
                    var caa = mappingSchema.GetAttribute <ColumnAliasAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration);

                    if (caa != null)
                    {
                        if (Aliases == null)
                        {
                            Aliases = new Dictionary <string, string>();
                        }

                        Aliases.Add(member.Name, caa.MemberName);
                    }
                }

                // dynamic columns store property
                var dcsProp = mappingSchema.GetAttribute <DynamicColumnsStoreAttribute>(TypeAccessor.Type, member.MemberInfo, attr => attr.Configuration);

                if (dcsProp != null)
                {
                    DynamicColumnsStore = new ColumnDescriptor(mappingSchema, new ColumnAttribute(member.Name), member);
                }
            }

            var typeColumnAttrs = mappingSchema.GetAttributes <ColumnAttribute>(TypeAccessor.Type, a => a.Configuration);

            foreach (var attr in typeColumnAttrs.Concat(attrs))
            {
                if (attr.IsColumn)
                {
                    SetColumn(attr, mappingSchema);
                }
            }
        }