GetAndMapColumnProperties() public method

public GetAndMapColumnProperties ( Migrator.Framework.Column column ) : ColumnPropertiesMapper
column Migrator.Framework.Column
return ColumnPropertiesMapper
        /// <summary>
        /// Add a new table
        /// </summary>
        /// <param name="name">Table name</param>
        /// <param name="columns">Columns</param>
        /// <param name="engine">the database storage engine to use</param>
        /// <example>
        /// Adds the Test table with two columns:
        /// <code>
        /// Database.AddTable("Test", "INNODB",
        ///	                  new Column("Id", typeof(int), ColumnProperty.PrimaryKey),
        ///	                  new Column("Title", typeof(string), 100)
        ///	                 );
        /// </code>
        /// </example>
        public virtual void AddTable(string name, string engine, params Column[] columns)
        {
            if (TableExists(name))
            {
                Logger.Warn("Table {0} already exists", name);
                return;
            }

            List <string> pks = GetPrimaryKeys(columns);
            bool          compoundPrimaryKey = pks.Count > 1;

            var columnProviders = new List <ColumnPropertiesMapper>(columns.Length);

            foreach (Column column in columns)
            {
                // Remove the primary key notation if compound primary key because we'll add it back later
                if (compoundPrimaryKey && column.IsPrimaryKey)
                {
                    column.ColumnProperty = column.ColumnProperty ^ ColumnProperty.PrimaryKey;
                    column.ColumnProperty = column.ColumnProperty | ColumnProperty.NotNull;                     // PK is always not-null
                }

                ColumnPropertiesMapper mapper = _dialect.GetAndMapColumnProperties(column);
                columnProviders.Add(mapper);
            }

            string columnsAndIndexes = JoinColumnsAndIndexes(columnProviders);

            AddTable(name, engine, columnsAndIndexes);

            if (compoundPrimaryKey)
            {
                AddPrimaryKey(String.Format("PK_{0}", name), name, pks.ToArray());
            }
        }
示例#2
0
        /// <summary>
        /// Add a new table
        /// </summary>
        /// <param name="name">Table name</param>
        /// <param name="columns">Columns</param>
        /// <param name="engine">the database storage engine to use</param>
        /// <example>
        /// Adds the Test table with two columns:
        /// <code>
        /// Database.AddTable("Test", "INNODB",
        ///	                  new Column("Id", typeof(int), ColumnProperty.PrimaryKey),
        ///	                  new Column("Title", typeof(string), 100)
        ///	                 );
        /// </code>
        /// </example>
        public virtual void AddTable(string name, string engine, params Column[] columns)
        {
            if (TableExists(name))
            {
                Logger.Warn("Table {0} already exists", name);
                return;
            }

            List <string> pks = GetPrimaryKeys(columns);
            bool          compoundPrimaryKey = pks.Count > 1;

            List <ColumnPropertiesMapper> columnProviders = new List <ColumnPropertiesMapper>(columns.Length);

            foreach (Column column in columns)
            {
                // Remove the primary key notation if compound primary key because we'll add it back later
                if (compoundPrimaryKey && column.IsPrimaryKey)
                {
                    column.ColumnProperty = column.ColumnProperty & ~ColumnProperty.PrimaryKeyBase;
                }

                ColumnPropertiesMapper mapper = _dialect.GetAndMapColumnProperties(column);
                columnProviders.Add(mapper);
            }

            string columnsAndIndexes = JoinColumnsAndIndexes(columnProviders);

            AddTable(name, engine, columnsAndIndexes);

            if (compoundPrimaryKey)
            {
                AddPrimaryKey(String.Format("PK_{0}", name), name, pks.ToArray());
            }

            // Add indexes that were unsupported on create table
            foreach (var column in columns)
            {
                if (!Dialect.SupportsIndexOnCreateTable && column.IsIndexed)
                {
                    AddIndex(string.Format("IX_{0}_{1}", name, column.Name), name, column.Name);
                }
            }
        }