コード例 #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
        /// <summary>
        /// Retrieves the resolved schema-qualified name for an object type.
        /// </summary>
        /// <param name="dialect">A dialect that the name should be qualified for.</param>
        /// <param name="database">The database that an object should be qualified for.</param>
        /// <param name="type">The type of object that the attribute is applied to.</param>
        /// <returns>A schema-qualified name for a database object.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dialect"/>, <paramref name="database"/>, or <paramref name="type"/> is <c>null</c></exception>
        public static Identifier GetQualifiedNameOrDefault(this IDatabaseDialect dialect, IRelationalDatabase database, Type type)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (database == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var schemaName = dialect.GetSchemaOverride(type);

            if (schemaName.IsNullOrWhiteSpace())
            {
                schemaName = database.IdentifierDefaults.Schema;
            }

            var localName = dialect.GetAliasOrDefault(type);

            return(Identifier.CreateQualifiedIdentifier(schemaName, localName));
        }
コード例 #3
0
        public ReflectionDatabaseTableIndex(IDatabaseDialect dialect, IRelationalDatabaseTable table, IModelledIndex index)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }
            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            IsUnique = index.IsUnique;

            Name = dialect.GetAliasOrDefault(index.Property !);

            var tableType      = index.Property !.ReflectedType;
            var propertyLookup = tableType.GetProperties()
                                 .Select(p => new KeyValuePair <string, PropertyInfo>(p.Name, p))
                                 .ToDictionary();

            var tableColumnLookup = table.GetColumnLookup();
            var columns           = new List <IDatabaseIndexColumn>();
            var includedColumns   = new List <IDatabaseColumn>();

            var isFunctionBasedIndex = false;

            foreach (var indexColumn in index.Columns)
            {
                if (indexColumn.Expression.IsIdentity)
                {
                    var expressionName = indexColumn.Expression.DependentNames.Single().LocalName;
                    var columnName     = dialect.GetAliasOrDefault(propertyLookup[expressionName]);
                    var tableColumn    = tableColumnLookup[columnName];
                    var textExpression = indexColumn.Expression.ToSql(dialect);
                    var column         = new DatabaseIndexColumn(textExpression, tableColumn, indexColumn.Order);
                    columns.Add(column);
                }
                else
                {
                    isFunctionBasedIndex = true;
                    var tableColumns = indexColumn.Expression.DependentNames
                                       .Select(name => propertyLookup.ContainsKey(name.LocalName) ? propertyLookup[name.LocalName] : null)
                                       .Where(prop => prop != null)
                                       .Select(prop => dialect.GetAliasOrDefault(prop !))
                                       .Select(name => tableColumnLookup[name])
                                       .ToList();

                    var textExpression = indexColumn.Expression.ToSql(dialect);
                    var column         = new DatabaseIndexColumn(textExpression, tableColumns, indexColumn.Order);
                    columns.Add(column);
                }
            }

            foreach (var includedColumn in index.IncludedColumns)
            {
                var includedColumnName = dialect.GetAliasOrDefault(includedColumn.Property !);
                var column             = tableColumnLookup[includedColumnName];
                includedColumns.Add(column);
            }

            IsFunctionBased = isFunctionBasedIndex;

            Columns         = columns;
            IncludedColumns = includedColumns;
        }