Пример #1
0
        public ReflectionTableColumn(IDatabaseDialect dialect, PropertyInfo prop, Type declaredColumnType, bool isNullable)
        {
            Property = prop ?? throw new ArgumentNullException(nameof(prop));
            Dialect  = dialect ?? throw new ArgumentNullException(nameof(dialect));
            if (declaredColumnType == null)
            {
                throw new ArgumentNullException(nameof(declaredColumnType));
            }

            Name = dialect.GetAliasOrDefault(prop);
            var clrType = GetClrType(declaredColumnType);

            if (clrType == null)
            {
                throw new ArgumentNullException($"The declared column type does not implement IDbType<T>. Check { prop.ReflectedType.FullName }.{ prop.Name } and ensure that the column type { declaredColumnType.FullName } implements this interface.", nameof(declaredColumnType));
            }

            var columnType   = new ReflectionColumnDataType(dialect, declaredColumnType, clrType);
            var autoIncrAttr = dialect.GetDialectAttribute <AutoIncrementAttribute>(declaredColumnType)
                               ?? dialect.GetDialectAttribute <AutoIncrementAttribute>(prop);

            if (autoIncrAttr != null)
            {
                if (!ValidAutoIncrementTypes.Contains(columnType.DataType))
                {
                    throw new ArgumentNullException($"The column { prop.ReflectedType.FullName }.{ prop.Name } is declared as being auto incrementing, which is not supported on a '{ columnType.DataType }' data type.", nameof(declaredColumnType));
                }

                AutoIncrement = new AutoIncrement(autoIncrAttr.InitialValue, autoIncrAttr.Increment);
            }

            Type       = columnType;
            IsNullable = isNullable;
        }
Пример #2
0
        public ReflectionColumnDataType(IDatabaseDialect dialect, Type columnType, Type clrType)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (columnType == null)
            {
                throw new ArgumentNullException(nameof(columnType));
            }
            if (clrType == null)
            {
                throw new ArgumentNullException(nameof(clrType));
            }

            var attr = dialect.GetDialectAttribute <DeclaredTypeAttribute>(columnType);

            if (attr == null)
            {
                throw new ArgumentException($"The column type { columnType.FullName } does not contain a definition for the dialect { dialect.GetType().FullName }");
            }

            var typeProvider = dialect.TypeProvider;

            if (typeProvider == null)
            {
                throw new ArgumentException("The given dialect does not contain a valid type provider.", nameof(dialect));
            }

            var collationAttr = dialect.GetDialectAttribute <CollationAttribute>(columnType);
            var typeMetadata  = new ColumnTypeMetadata
            {
                ClrType   = clrType,
                Collation = collationAttr?.CollationName != null
                    ? Option <Identifier> .Some(collationAttr.CollationName)
                    : Option <Identifier> .None,
                DataType         = attr.DataType,
                IsFixedLength    = attr.IsFixedLength,
                MaxLength        = attr.Length,
                NumericPrecision = attr.Precision > 0 && attr.Scale > 0
                    ? Option <INumericPrecision> .Some(new NumericPrecision(attr.Precision, attr.Scale))
                    : Option <INumericPrecision> .None,
            };
            var dbType = typeProvider.CreateColumnType(typeMetadata);

            // map dbType to properties, avoids keeping a reference
            TypeName         = dbType.TypeName;
            DataType         = dbType.DataType;
            Definition       = dbType.Definition;
            IsFixedLength    = dbType.IsFixedLength;
            MaxLength        = dbType.MaxLength;
            ClrType          = dbType.ClrType;
            NumericPrecision = dbType.NumericPrecision;
            Collation        = dbType.Collation;
        }
Пример #3
0
        /// <summary>
        /// Provides schema override for a type.
        /// </summary>
        /// <param name="dialect">A dialect that the schema override applies to.</param>
        /// <param name="type">The type of object that a schema override attribute may be applied to.</param>
        /// <returns>A schema override for a type if available, otherwise <c>null</c>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dialect"/> or <paramref name="type"/> is <c>null</c></exception>
        public static string?GetSchemaOverride(this IDatabaseDialect dialect, Type type)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var nameAttr = dialect.GetDialectAttribute <SchemaAttribute>(type);

            return(nameAttr?.Schema);
        }
Пример #4
0
        /// <summary>
        /// Provides an alias for a type, or the type name.
        /// </summary>
        /// <param name="dialect">A dialect that the alias applies to.</param>
        /// <param name="type">The type of object that the attribute is applied to.</param>
        /// <returns>An alias for a type if available, the type's name otherwise.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dialect"/> or <paramref name="type"/> is <c>null</c>.</exception>
        public static string GetAliasOrDefault(this IDatabaseDialect dialect, Type type)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var aliasAttr = dialect.GetDialectAttribute <AliasAttribute>(type);

            return(aliasAttr?.Alias ?? type.Name);
        }
Пример #5
0
        /// <summary>
        /// Provides an alias for a property, or the property name.
        /// </summary>
        /// <param name="dialect">A dialect that the alias applies to.</param>
        /// <param name="property">A property that may contain an alias attribute.</param>
        /// <returns>A name that should be used for the property, which is an alias if one is available, or the property name otherwise.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dialect"/> or <paramref name="property"/> is <c>null</c>.</exception>
        public static string GetAliasOrDefault(this IDatabaseDialect dialect, PropertyInfo property)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var aliasAttr = dialect.GetDialectAttribute <AliasAttribute>(property);

            return(aliasAttr?.Alias ?? property.Name);
        }