示例#1
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 = ColumnProperty.Unsigned | ColumnProperty.NotNull;
                }

                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
        public ColumnPropertiesMapper GetAndMapColumnProperties(Column column)
        {
            ColumnPropertiesMapper mapper = GetColumnMapper(column);

            mapper.MapColumnProperties(column);
            if (column.DefaultValue != null)
            {
                mapper.Default = column.DefaultValue;
            }
            return(mapper);
        }
示例#3
0
        public virtual void ChangeColumn(string table, Column column)
        {
            if (!ColumnExists(table, column.Name))
            {
                Logger.Warn("Column {0}.{1} does not exist", table, column.Name);
                return;
            }

            ColumnPropertiesMapper mapper = _dialect.GetAndMapColumnProperties(column);

            ChangeColumn(table, mapper.ColumnSql);
        }
示例#4
0
        public void AddColumn(string table, string column, DbType type, object defaultValue)
        {
            if (ColumnExists(table, column))
            {
                Logger.Warn("Column {0}.{1} already exists", table, column);
                return;
            }

            ColumnPropertiesMapper mapper =
                _dialect.GetAndMapColumnProperties(new Column(column, type, defaultValue));

            AddColumn(table, mapper.ColumnSql);
        }