Ejemplo n.º 1
11
        /// <summary>
        /// Initialises a new instance of the <see cref="TableInfo"/> class.
        /// </summary>
        /// <param name="columns">The columns that are mapped for the table.</param>
        /// <param name="identifierStrategy">The identifier strategy used by the table.</param>
        /// <param name="name">The name of the table.</param>
        /// <param name="schema">The database schema the table exists within (e.g. 'dbo'); otherwise null.</param>
        /// <exception cref="ArgumentNullException">Thrown if columns or name are null.</exception>
        /// <exception cref="MappingException">Thrown if no there is a problem with the column mappings.</exception>
        public TableInfo(
            IList<ColumnInfo> columns,
            IdentifierStrategy identifierStrategy,
            string name,
            string schema)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            this.columns = new ReadOnlyCollection<ColumnInfo>(columns);
            this.identifierStrategy = identifierStrategy;
            this.name = name;
            this.schema = schema;

            this.identifierColumn = columns.FirstOrDefault(c => c.IsIdentifier);

            this.insertColumnCount = columns.Count(c => c.AllowInsert);
            this.updateColumnCount = columns.Count(c => c.AllowUpdate);

            this.ValidateColumns();
        }
Ejemplo n.º 2
0
        public void ConstructorSetsPropertyValues()
        {
            var columnName = "Name";
            var propertyInfo = typeof(Customer).GetProperty("Name");
            var isIdentifier = true;
            var allowInsert = true;
            var allowUpdate = true;

            var columnInfo = new ColumnInfo(columnName, propertyInfo, isIdentifier, allowInsert, allowUpdate);

            Assert.Equal(columnName, columnInfo.ColumnName);
            Assert.Equal(propertyInfo, columnInfo.PropertyInfo);
            Assert.Equal(isIdentifier, columnInfo.IsIdentifier);
            Assert.Equal(allowInsert, columnInfo.AllowInsert);
            Assert.Equal(allowUpdate, columnInfo.AllowUpdate);
        }
        private List<ColumnInfo> CreateColumnInfos(Type forType, IdentifierStrategy identifierStrategy)
        {
            var properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns = new List<ColumnInfo>(properties.Length);

            foreach (var property in properties.OrderBy(p => p.Name))
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                if (this.settings.Ignore(property))
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.ConventionMappingConvention_IgnoringProperty, forType.Name, property.Name);
                    }

                    continue;
                }

                var isIdentifier = this.settings.IsIdentifier(property);

                var columnInfo = new ColumnInfo(
                    columnName: isIdentifier ? this.settings.ResolveIdentifierColumnName(property) : this.settings.ResolveColumnName(property),
                    dbType: this.settings.ResolveDbType(property),
                    propertyInfo: property,
                    isIdentifier: isIdentifier,
                    allowInsert: isIdentifier ? identifierStrategy == IdentifierStrategy.Assigned : this.settings.AllowInsert(property),
                    allowUpdate: isIdentifier ? false : this.settings.AllowUpdate(property),
                    sequenceName: isIdentifier && identifierStrategy == IdentifierStrategy.Sequence ? this.settings.ResolveSequenceName(property) : null);

                if (this.log.IsDebug)
                {
                    this.log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return columns;
        }
        private List<ColumnInfo> CreateColumnInfos(Type forType, ref IdentifierStrategy identifierStrategy)
        {
            var properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns = new List<ColumnInfo>(properties.Length);

            foreach (var property in properties)
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                var columnAttribute = property.GetAttribute<ColumnAttribute>(inherit: true);

                if (columnAttribute == null)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.AttributeMappingConvention_IgnoringProperty, forType.FullName, property.Name);
                    }

                    continue;
                }

                var identifierAttribute = property.GetAttribute<IdentifierAttribute>(inherit: true);

                if (identifierAttribute != null)
                {
                    identifierStrategy = identifierAttribute.IdentifierStrategy;
                }

                var columnInfo = new ColumnInfo(
                    columnName: columnAttribute.Name,
                    propertyInfo: property,
                    isIdentifier: identifierAttribute != null,
                    allowInsert: identifierAttribute != null ? identifierStrategy == IdentifierStrategy.Assigned : columnAttribute.AllowInsert,
                    allowUpdate: identifierAttribute != null ? false : columnAttribute.AllowUpdate);

                if (this.log.IsDebug)
                {
                    this.log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return columns;
        }
Ejemplo n.º 5
0
        private static string GetSqlType(ColumnInfo columnInfo)
        {
            switch (columnInfo.PropertyInfo.PropertyType.Name)
            {
                case "Byte":
                    return "tinyint";

                case "Int16":
                    return "smallint";

                case "Int32":
                    return "int";

                case "Int64":
                    return "bigint";

                default:
                    throw new NotSupportedException(columnInfo.PropertyInfo.PropertyType.Name);
            }
        }