Пример #1
0
        //protected override void CreateTable(StringBuilder sb, IDataTable table, Boolean onlySql)
        //{
        //    base.CreateTable(sb, table, onlySql);

        //    if (!onlySql)
        //    {
        //        IDatabase entityDb = null;
        //        foreach (IDataColumn dc in table.Columns)
        //        {
        //            // 如果实体存在默认值,则增加
        //            if (!String.IsNullOrEmpty(dc.Default))
        //            {
        //                var tc = Type.GetTypeCode(dc.DataType);
        //                String dv = dc.Default;
        //                // 特殊处理时间
        //                if (tc == TypeCode.DateTime)
        //                {
        //                    if (entityDb != null && dv == entityDb.DateTimeNow) dc.Default = Database.DateTimeNow;
        //                }
        //                // 特殊处理Guid
        //                else if (tc == TypeCode.String || dc.DataType == typeof(Guid))
        //                {
        //                    if (entityDb != null && dv == entityDb.NewGuid) dc.Default = Database.NewGuid;
        //                }

        //                PerformSchema(sb, onlySql, DDLSchema.AddDefault, dc);

        //                // 还原
        //                dc.Default = dv;
        //            }
        //        }
        //    }
        //}

        public override String CreateTableSQL(IDataTable table)
        {
            var sql = base.CreateTableSQL(table);

            var pks = table.PrimaryKeys;

            if (String.IsNullOrEmpty(sql) || pks.Length < 2)
            {
                return(sql);
            }

            // 处理多主键
            var names = pks.Select(e => e.ColumnName).ToArray();
            var di    = ModelHelper.GetIndex(table, names);

            if (di == null)
            {
                di            = table.CreateIndex();
                di.PrimaryKey = true;
                di.Unique     = true;
                di.Columns    = names;
            }
            // Access里面的主键索引名必须叫这个
            di.Name = "PrimaryKey";

            sql += ";" + Environment.NewLine;
            sql += CreateIndexSQL(di);
            return(sql);
        }
Пример #2
0
        public override string CreateTableSQL(IDataTable table)
        {
            String sql = base.CreateTableSQL(table);

            if (String.IsNullOrEmpty(sql) || table.PrimaryKeys == null || table.PrimaryKeys.Length < 2)
            {
                return(sql);
            }

            // 处理多主键
            String[] names = new String[table.PrimaryKeys.Length];
            for (int i = 0; i < table.PrimaryKeys.Length; i++)
            {
                names[i] = table.PrimaryKeys[i].Name;
            }
            IDataIndex di = ModelHelper.GetIndex(table, names);

            if (di == null)
            {
                di            = table.CreateIndex();
                di.PrimaryKey = true;
                di.Unique     = true;
                di.Columns    = names;
            }
            // Access里面的主键索引名必须叫这个
            di.Name = "PrimaryKey";

            sql += ";" + Environment.NewLine;
            sql += CreateIndexSQL(di);
            return(sql);
        }
Пример #3
0
        /// <summary>检查表说明和索引</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        protected virtual String CheckTableDescriptionAndIndex(IDataTable entitytable, IDataTable dbtable, Migration mode)
        {
            var onlySql  = mode <= Migration.ReadOnly;
            var noDelete = mode < Migration.Full;

            var sb = new StringBuilder();

            #region 表说明
            if (entitytable.Description + "" != dbtable.Description + "")
            {
                //// 先删除旧注释
                //if (!String.IsNullOrEmpty(dbtable.Description)) PerformSchema(sb, onlySql, DDLSchema.DropTableDescription, dbtable);

                // 加上新注释
                if (!String.IsNullOrEmpty(entitytable.Description))
                {
                    PerformSchema(sb, onlySql, DDLSchema.AddTableDescription, entitytable);
                }
            }
            #endregion

            #region  除索引
            var dbdis = dbtable.Indexes;
            if (dbdis != null)
            {
                foreach (var item in dbdis.ToArray())
                {
                    // 计算的索引不需要删除
                    //if (item.Computed) continue;

                    // 主键的索引不能删
                    if (item.PrimaryKey)
                    {
                        continue;
                    }

                    var di = ModelHelper.GetIndex(entitytable, item.Columns);
                    if (di != null && di.Unique == item.Unique)
                    {
                        continue;
                    }

                    PerformSchema(sb, noDelete, DDLSchema.DropIndex, item);
                    dbdis.Remove(item);
                }
            }
            #endregion

            #region 新增索引
            var edis = entitytable.Indexes;
            if (edis != null)
            {
                foreach (var item in edis.ToArray())
                {
                    if (item.PrimaryKey)
                    {
                        continue;
                    }

                    var di = ModelHelper.GetIndex(dbtable, item.Columns);
                    // 计算出来的索引,也表示没有,需要创建
                    if (di != null && di.Unique == item.Unique)
                    {
                        continue;
                    }
                    //// 如果这个索引的唯一字段是主键,则无需建立索引
                    //if (item.Columns.Length == 1 && entitytable.GetColumn(item.Columns[0]).PrimaryKey) continue;
                    // 如果索引全部就是主键,无需创建索引
                    if (entitytable.GetColumns(item.Columns).All(e => e.PrimaryKey))
                    {
                        continue;
                    }

                    PerformSchema(sb, onlySql, DDLSchema.CreateIndex, item);

                    if (di == null)
                    {
                        edis.Add(item.Clone(dbtable));
                    }
                    //else
                    //    di.Computed = false;
                }
            }
            #endregion

            if (!onlySql)
            {
                return(null);
            }

            return(sb.ToString());
        }
Пример #4
0
        /// <summary>检查表说明和索引</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="onlySql"></param>
        /// <returns></returns>
        protected virtual String CheckTableDescriptionAndIndex(IDataTable entitytable, IDataTable dbtable, Boolean onlySql)
        {
            var sb = new StringBuilder();

            #region 表说明
            if (entitytable.Description + "" != dbtable.Description + "")
            {
                // 先删除旧注释
                //if (!String.IsNullOrEmpty(dbtable.Description)) DropTableDescription(sb, dbtable, onlySql);
                if (!String.IsNullOrEmpty(dbtable.Description))
                {
                    PerformSchema(sb, onlySql, DDLSchema.DropTableDescription, dbtable);
                }

                // 加上新注释
                //if (!String.IsNullOrEmpty(entitytable.Description)) AddTableDescription(sb, entitytable, onlySql);
                if (!String.IsNullOrEmpty(entitytable.Description))
                {
                    PerformSchema(sb, onlySql, DDLSchema.AddTableDescription, entitytable);
                }
            }
            #endregion

            #region  除索引
            if (dbtable.Indexes != null)
            {
                for (int i = dbtable.Indexes.Count - 1; i >= 0; i--)
                {
                    var item = dbtable.Indexes[i];
                    // 计算的索引不需要删除
                    if (item.Computed)
                    {
                        continue;
                    }

                    // 主键的索引不能删
                    if (item.PrimaryKey)
                    {
                        continue;
                    }

                    var di = ModelHelper.GetIndex(entitytable, item.Columns);
                    if (di != null && di.Unique == item.Unique)
                    {
                        continue;
                    }

                    PerformSchema(sb, onlySql, DDLSchema.DropIndex, item);
                    dbtable.Indexes.RemoveAt(i);
                }
            }
            #endregion

            #region 新增索引
            if (entitytable.Indexes != null)
            {
                foreach (var item in entitytable.Indexes)
                {
                    if (item.PrimaryKey)
                    {
                        continue;
                    }

                    var di = ModelHelper.GetIndex(dbtable, item.Columns);
                    // 计算出来的索引,也表示没有,需要创建
                    if (di != null && di.Unique == item.Unique && !di.Computed)
                    {
                        continue;
                    }
                    //// 如果这个索引的唯一字段是主键,则无需建立索引
                    //if (item.Columns.Length == 1 && entitytable.GetColumn(item.Columns[0]).PrimaryKey) continue;
                    // 如果索引全部就是主键,无需创建索引
                    if (entitytable.GetColumns(item.Columns).All(e => e.PrimaryKey))
                    {
                        continue;
                    }

                    PerformSchema(sb, onlySql, DDLSchema.CreateIndex, item);

                    if (di == null)
                    {
                        dbtable.Indexes.Add(item.Clone(dbtable));
                    }
                    else
                    {
                        di.Computed = false;
                    }
                }
            }
            #endregion

            return(sb.ToString());
        }