Example #1
0
 public void SetTables(params IDataTable[] tables)
 {
     var set = new NegativeSetting();
     set.CheckOnly = DAL.NegativeCheckOnly;
     set.NoDelete = DAL.NegativeNoDelete;
     OnSetTables(tables, set);
 }
Example #2
0
        private void CheckAllTables(IDataTable[] tables, NegativeSetting setting)
        {
            // 数据库表进入字典
            var dic      = new Dictionary <String, IDataTable>(StringComparer.OrdinalIgnoreCase);
            var dbtables = OnGetTables(new HashSet <String>(tables.Select(t => t.TableName), StringComparer.OrdinalIgnoreCase));

            if (dbtables != null && dbtables.Count > 0)
            {
                foreach (var item in dbtables)
                {
                    dic.Add(item.TableName, item);
                }
            }

            foreach (var item in tables)
            {
                try
                {
                    // 判断指定表是否存在于数据库中,以决定是创建表还是修改表
                    IDataTable dbtable = null;
                    if (dic.TryGetValue(item.TableName, out dbtable))
                    {
                        CheckTable(item, dbtable, setting);
                    }
                    else
                    {
                        CheckTable(item, null, setting);
                    }
                }
                catch (Exception ex)
                {
                    WriteLog(ex.ToString());
                }
            }
        }
Example #3
0
        public void SetTables(params IDataTable[] tables)
        {
            var set = new NegativeSetting();

            set.CheckOnly = DAL.NegativeCheckOnly;
            set.NoDelete  = DAL.NegativeNoDelete;
            OnSetTables(tables, set);
        }
Example #4
0
        protected override string CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            foreach (var item in entitytable.Columns)
            {
                // 自增字段必须是主键
                if (item.Identity && !item.PrimaryKey)
                {
                    // 取消所有主键
                    item.Table.Columns.ForEach(dc => dc.PrimaryKey = false);

                    // 自增字段作为主键
                    item.PrimaryKey = true;
                    break;
                }
            }

            //String sql = base.CheckColumnsChange(entitytable, dbtable, onlySql);
            // 把onlySql设为true,让基类只产生语句而不执行
            var set = new NegativeSetting();

            set.CheckOnly = true;
            set.NoDelete  = setting.NoDelete;
            var sql = base.CheckColumnsChange(entitytable, dbtable, set);

            if (String.IsNullOrEmpty(sql))
            {
                return(sql);
            }

            sql = ReBuildTable(entitytable, dbtable);
            if (String.IsNullOrEmpty(sql) || setting.CheckOnly)
            {
                return(sql);
            }

            Boolean flag = true;

            // 如果设定不允许删
            if (setting.NoDelete)
            {
                // 看看有没有数据库里面有而实体库里没有的
                foreach (var item in dbtable.Columns)
                {
                    var dc = entitytable.GetColumn(item.Name);
                    if (dc == null)
                    {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag)
            {
                Database.CreateSession().Execute(sql);
            }

            return(sql);
        }
Example #5
0
File: DAL.cs Project: EagleSmith/X
 /// <summary>在当前连接上检查指定数据表的架构</summary>
 /// <param name="set"></param>
 /// <param name="tables"></param>
 public void SetTables(NegativeSetting set, params IDataTable[] tables)
 {
     if (set == null)
     {
         set           = new NegativeSetting();
         set.CheckOnly = Setting.Current.Negative.CheckOnly;
         set.NoDelete  = Setting.Current.Negative.NoDelete;
     }
     Db.CreateMetaData().SetTables(set, tables);
 }
Example #6
0
        /// <summary>在当前连接上检查指定数据表的架构</summary>
        /// <param name="tables"></param>
        public void SetTables(params IDataTable[] tables)
        {
            var set = new NegativeSetting();

            set.CheckOnly = DAL.NegativeCheckOnly;
            set.NoDelete  = DAL.NegativeNoDelete;
            //if (set.CheckOnly && DAL.Debug) WriteLog("XCode.Negative.CheckOnly设置为True,只是检查不对数据库进行操作");
            //if (set.NoDelete && DAL.Debug) WriteLog("XCode.Negative.NoDelete设置为True,不会删除数据表多余字段");
            Db.CreateMetaData().SetTables(set, tables);
        }
Example #7
0
 /// <summary>在当前连接上检查指定数据表的架构</summary>
 /// <param name="set"></param>
 /// <param name="tables"></param>
 public void SetTables(NegativeSetting set, params IDataTable[] tables)
 {
     if (set == null)
     {
         set           = new NegativeSetting();
         set.CheckOnly = Setting.Current.Negative.CheckOnly;
         set.NoDelete  = Setting.Current.Negative.NoDelete;
     }
     //if (set.CheckOnly && DAL.Debug) WriteLog("XCode.Negative.CheckOnly设置为True,只是检查不对数据库进行操作");
     //if (set.NoDelete && DAL.Debug) WriteLog("XCode.Negative.NoDelete设置为True,不会删除数据表多余字段");
     Db.CreateMetaData().SetTables(set, tables);
 }
Example #8
0
        private void CheckDatabase(NegativeSetting setting)
        {
            if (hasCheckedDatabase)
            {
                return;
            }
            hasCheckedDatabase = true;

            //数据库检查
            Boolean dbExist = true;

            try
            {
                dbExist = (Boolean)SetSchema(DDLSchema.DatabaseExist, null);
            }
            catch
            {
                // 如果异常,默认认为数据库存在
                dbExist = true;
            }

            if (!dbExist)
            {
                if (!setting.CheckOnly)
                {
                    WriteLog("创建数据库:{0}", ConnName);
                    SetSchema(DDLSchema.CreateDatabase, null, null);
                }
                else
                {
                    var sql = GetSchemaSQL(DDLSchema.CreateDatabase, null, null);
                    if (String.IsNullOrEmpty(sql))
                    {
                        WriteLog("请为连接{0}创建数据库!", ConnName);
                    }
                    else
                    {
                        WriteLog("请为连接{0}创建数据库,使用以下语句:{1}", ConnName, Environment.NewLine + sql);
                    }
                }
            }
        }
Example #9
0
        ///// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
        ///// <param name="tables"></param>
        //[EditorBrowsable(EditorBrowsableState.Never)]
        //[Obsolete("请改用多参数版本!")]
        //public void SetTables(params IDataTable[] tables)
        //{
        //    //var set = new NegativeSetting();
        //    //set.CheckOnly = DAL.NegativeCheckOnly;
        //    //set.NoDelete = DAL.NegativeNoDelete;
        //    var set = Setting.Current.Negative;
        //    OnSetTables(tables, set);
        //}

        /// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
        /// <param name="setting">设置</param>
        /// <param name="tables"></param>
        public void SetTables(NegativeSetting setting, params IDataTable[] tables)
        {
            OnSetTables(tables, setting);
        }
Example #10
0
        /// <summary>检查字段改变。某些数据库(如SQLite)没有添删改字段的DDL语法,可重载该方法,使用重建表方法ReBuildTable</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        protected virtual String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            #region 准备工作
            var onlySql   = setting.CheckOnly;
            var sql       = String.Empty;
            var sb        = new StringBuilder();
            var entitydic = new Dictionary <String, IDataColumn>(StringComparer.OrdinalIgnoreCase);
            if (entitytable.Columns != null)
            {
                foreach (var item in entitytable.Columns)
                {
                    if (entitydic.ContainsKey(item.ColumnName.ToLower()))
                    {
                        WriteLog("《" + entitytable.Name + "》实体中存在重复列名,请检查《" + entitytable.TableName + "》表《" + item.Name + "》属性的ColumnName配置(目前配置为:" + item.ColumnName + ")。");
                        continue;
                    }
                    entitydic.Add(item.ColumnName.ToLower(), item);
                }
            }
            var dbdic = new Dictionary <String, IDataColumn>(StringComparer.OrdinalIgnoreCase);
            if (dbtable.Columns != null)
            {
                foreach (var item in dbtable.Columns)
                {
                    dbdic.Add(item.ColumnName.ToLower(), item);
                }
            }
            #endregion

            #region 新增列
            foreach (IDataColumn item in entitytable.Columns)
            {
                if (!dbdic.ContainsKey(item.ColumnName.ToLower()))
                {
                    //AddColumn(sb, item, onlySql);
                    PerformSchema(sb, onlySql, DDLSchema.AddColumn, item);
                    if (!String.IsNullOrEmpty(item.Description))
                    {
                        PerformSchema(sb, onlySql, DDLSchema.AddColumnDescription, item);
                    }

                    //! 以下已经不需要了,目前只有SQLite会采用重建表的方式添加删除字段。如果这里提前添加了字段,重建表的时候,会导致失败。

                    //// 这里必须给dbtable加加上当前列,否则下面如果刚好有删除列的话,会导致增加列成功,然后删除列重建表的时候没有新加的列
                    //dbtable.Columns.Add(item.Clone(dbtable));
                }
            }
            #endregion

            #region  除列
            var sbDelete = new StringBuilder();
            for (int i = dbtable.Columns.Count - 1; i >= 0; i--)
            {
                var item = dbtable.Columns[i];
                if (!entitydic.ContainsKey(item.ColumnName.ToLower()))
                {
                    if (!String.IsNullOrEmpty(item.Description))
                    {
                        PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, item);
                    }
                    PerformSchema(sbDelete, setting.NoDelete, DDLSchema.DropColumn, item);
                }
            }
            if (sbDelete.Length > 0)
            {
                if (setting.NoDelete)
                {
                    //不许删除列,显示日志
                    WriteLog("数据表中发现有多余字段,请手工执行以下语句删除:" + Environment.NewLine + sbDelete.ToString());
                }
                else
                {
                    if (sb.Length > 0)
                    {
                        sb.AppendLine(";");
                    }
                    sb.Append(sbDelete.ToString());
                }
            }
            #endregion

            #region 修改列
            // 开发时的实体数据库
            var entityDb = DbFactory.Create(entitytable.DbType);

            foreach (var item in entitytable.Columns)
            {
                IDataColumn dbf = null;
                if (!dbdic.TryGetValue(item.ColumnName, out dbf))
                {
                    continue;
                }

                if (IsColumnTypeChanged(item, dbf))
                {
                    WriteLog("字段{0}.{1}类型需要由{2}改变为{3}", entitytable.Name, item.Name, dbf.DataType, item.DataType);
                    PerformSchema(sb, onlySql, DDLSchema.AlterColumn, item, dbf);
                }
                if (IsColumnChanged(item, dbf, entityDb))
                {
                    PerformSchema(sb, onlySql, DDLSchema.AlterColumn, item, dbf);
                }
                if (IsColumnDefaultChanged(item, dbf, entityDb))
                {
                    ChangeColmnDefault(sb, onlySql, item, dbf, entityDb);
                }

                if (item.Description + "" != dbf.Description + "")
                {
                    // 先删除旧注释
                    //if (!String.IsNullOrEmpty(dbf.Description)) DropColumnDescription(sb, dbf, onlySql);
                    //if (!String.IsNullOrEmpty(dbf.Description)) PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf);
                    if (dbf.Description != null)
                    {
                        PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf);
                    }

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

            return(sb.ToString());
        }
Example #11
0
        protected virtual void CheckTable(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            //Boolean onlySql = DAL.NegativeCheckOnly;

            if (dbtable == null)
            {
                // 没有字段的表不创建
                if (entitytable.Columns.Count < 1)
                {
                    return;
                }

                #region 创建表
                WriteLog("创建表:{0}({1})", entitytable.TableName, entitytable.Description);

                var sb = new StringBuilder();
                // 建表,如果不是onlySql,执行时DAL会输出SQL日志
                CreateTable(sb, entitytable, setting.CheckOnly);

                // 仅获取语句
                //if (onlySql) WriteLog("XCode.Negative.Enable没有设置为True,请手工创建表:" + entitytable.Name + Environment.NewLine + sb.ToString());
                if (setting.CheckOnly)
                {
                    WriteLog("只检查不对数据库进行操作,请手工创建表:" + entitytable.TableName + Environment.NewLine + sb.ToString());
                }
                #endregion
            }
            else
            {
                #region 修改表
                String sql = CheckColumnsChange(entitytable, dbtable, setting);
                if (!String.IsNullOrEmpty(sql))
                {
                    sql += ";";
                }
                sql += CheckTableDescriptionAndIndex(entitytable, dbtable, setting.CheckOnly);
                if (!String.IsNullOrEmpty(sql) && setting.CheckOnly)
                {
                    WriteLog("只检查不对数据库进行操作,请手工使用以下语句修改表:" + Environment.NewLine + sql);
                }
                #endregion
            }
        }
Example #12
0
        /// <summary>已重载。因为内存数据库无法检测到架构,不知道表是否已存在,所以需要自己维护</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="setting"></param>
        protected override void CheckTable(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            if (dbtable == null && (Database as SQLite).IsMemoryDatabase)
            {
                if (memoryTables.Any(t => t.TableName.EqualIgnoreCase(entitytable.TableName))) return;

                memoryTables.Add(entitytable);
            }

            base.CheckTable(entitytable, dbtable, setting);
        }
Example #13
0
        private void CheckAllTables(IDataTable[] tables, NegativeSetting setting)
        {
            // 数据库表进入字典
            var dic = new Dictionary<String, IDataTable>(StringComparer.OrdinalIgnoreCase);
            var dbtables = OnGetTables(new HashSet<String>(tables.Select(t => t.TableName), StringComparer.OrdinalIgnoreCase));
            if (dbtables != null && dbtables.Count > 0)
            {
                foreach (var item in dbtables)
                {
                    dic.Add(item.TableName, item);
                }
            }

            foreach (var item in tables)
            {
                try
                {
                    // 判断指定表是否存在于数据库中,以决定是创建表还是修改表
                    IDataTable dbtable = null;
                    if (dic.TryGetValue(item.TableName, out dbtable))
                        CheckTable(item, dbtable, setting);
                    else
                        CheckTable(item, null, setting);
                }
                catch (Exception ex)
                {
                    WriteLog(ex.ToString());
                }
            }
        }
Example #14
0
        protected override string CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            foreach (var item in entitytable.Columns)
            {
                // 自增字段必须是主键
                if (item.Identity && !item.PrimaryKey)
                {
                    // 取消所有主键
                    item.Table.Columns.ForEach(dc => dc.PrimaryKey = false);

                    // 自增字段作为主键
                    item.PrimaryKey = true;
                    break;
                }
            }

            //String sql = base.CheckColumnsChange(entitytable, dbtable, onlySql);
            // 把onlySql设为true,让基类只产生语句而不执行
            var set = new NegativeSetting();

            set.CheckOnly = true;
            set.NoDelete  = setting.NoDelete;
            var sql = base.CheckColumnsChange(entitytable, dbtable, set);

            if (String.IsNullOrEmpty(sql))
            {
                return(sql);
            }

            // 只有修改字段、删除字段需要重建表
            if (!sql.Contains("Alter Column") && !sql.Contains("Drop Column"))
            {
                if (!setting.CheckOnly)
                {
                    Database.CreateSession().Execute(sql);
                }
                return(sql);
            }

            var sql2 = sql;

            sql = ReBuildTable(entitytable, dbtable);
            if (String.IsNullOrEmpty(sql) || setting.CheckOnly)
            {
                return(sql);
            }

            // 输出日志,说明重建表的理由
            WriteLog("SQLite需要重建表,因无法执行:{0}", sql2);

            Boolean flag = true;

            // 如果设定不允许删
            if (setting.NoDelete)
            {
                // 看看有没有数据库里面有而实体库里没有的
                foreach (var item in dbtable.Columns)
                {
                    var dc = entitytable.GetColumn(item.ColumnName);
                    if (dc == null)
                    {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag)
            {
                Database.CreateSession().Execute(sql);
            }

            return(sql);
        }
Example #15
0
        protected override string CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            foreach (var item in entitytable.Columns)
            {
                // 自增字段必须是主键
                if (item.Identity && !item.PrimaryKey)
                {
                    // 取消所有主键
                    item.Table.Columns.ForEach(dc => dc.PrimaryKey = false);

                    // 自增字段作为主键
                    item.PrimaryKey = true;
                    break;
                }
            }

            //String sql = base.CheckColumnsChange(entitytable, dbtable, onlySql);
            // 把onlySql设为true,让基类只产生语句而不执行
            var set = new NegativeSetting();
            set.CheckOnly = true;
            set.NoDelete = setting.NoDelete;
            var sql = base.CheckColumnsChange(entitytable, dbtable, set);
            if (String.IsNullOrEmpty(sql)) return sql;

            // 只有修改字段、删除字段需要重建表
            if (!sql.Contains("Alter Column") && !sql.Contains("Drop Column"))
            {
                if (!setting.CheckOnly) Database.CreateSession().Execute(sql);
                return sql;
            }

            var sql2 = sql;

            sql = ReBuildTable(entitytable, dbtable);
            if (String.IsNullOrEmpty(sql) || setting.CheckOnly) return sql;

            // 输出日志,说明重建表的理由
            WriteLog("SQLite需要重建表,因无法执行:{0}", sql2);

            Boolean flag = true;
            // 如果设定不允许删
            if (setting.NoDelete)
            {
                // 看看有没有数据库里面有而实体库里没有的
                foreach (var item in dbtable.Columns)
                {
                    var dc = entitytable.GetColumn(item.ColumnName);
                    if (dc == null)
                    {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag) Database.CreateSession().Execute(sql);

            return sql;
        }
Example #16
0
        private void CheckDatabase(NegativeSetting setting)
        {
            if (hasCheckedDatabase) return;
            hasCheckedDatabase = true;

            //数据库检查
            Boolean dbExist = true;
            try
            {
                dbExist = (Boolean)SetSchema(DDLSchema.DatabaseExist, null);
            }
            catch
            {
                // 如果异常,默认认为数据库存在
                dbExist = true;
            }

            if (!dbExist)
            {
                if (!setting.CheckOnly)
                {
                    WriteLog("创建数据库:{0}", ConnName);
                    SetSchema(DDLSchema.CreateDatabase, null, null);
                }
                else
                {
                    var sql = GetSchemaSQL(DDLSchema.CreateDatabase, null, null);
                    if (String.IsNullOrEmpty(sql))
                        WriteLog("请为连接{0}创建数据库!", ConnName);
                    else
                        WriteLog("请为连接{0}创建数据库,使用以下语句:{1}", ConnName, Environment.NewLine + sql);
                }
            }
        }
Example #17
0
        protected virtual void OnSetTables(IDataTable[] tables, NegativeSetting setting)
        {
            CheckDatabase(setting);

            CheckAllTables(tables, setting);
        }
Example #18
0
        ///// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
        ///// <param name="tables"></param>
        //[EditorBrowsable(EditorBrowsableState.Never)]
        //[Obsolete("请改用多参数版本!")]
        //public void SetTables(params IDataTable[] tables)
        //{
        //    //var set = new NegativeSetting();
        //    //set.CheckOnly = DAL.NegativeCheckOnly;
        //    //set.NoDelete = DAL.NegativeNoDelete;
        //    var set = Setting.Current.Negative;
        //    OnSetTables(tables, set);
        //}

        /// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
        /// <param name="setting">设置</param>
        /// <param name="tables"></param>
        public void SetTables(NegativeSetting setting, params IDataTable[] tables)
        {
            OnSetTables(tables, setting);
        }
Example #19
0
        /// <summary>检查字段改变。某些数据库(如SQLite)没有添删改字段的DDL语法,可重载该方法,使用重建表方法ReBuildTable</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        protected virtual String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            #region 准备工作
            var onlySql = setting.CheckOnly;
            var sql = String.Empty;
            var sb = new StringBuilder();
            var entitydic = new Dictionary<String, IDataColumn>(StringComparer.OrdinalIgnoreCase);
            if (entitytable.Columns != null)
            {
                foreach (var item in entitytable.Columns)
                {
                    if (entitydic.ContainsKey(item.ColumnName.ToLower()))
                    {
                        WriteLog("《" + entitytable.Name + "》实体中存在重复列名,请检查《" + entitytable.TableName + "》表《" + item.Name + "》属性的ColumnName配置(目前配置为:" + item.ColumnName + ")。");
                        continue;
                    }
                    entitydic.Add(item.ColumnName.ToLower(), item);
                }
            }
            var dbdic = new Dictionary<String, IDataColumn>(StringComparer.OrdinalIgnoreCase);
            if (dbtable.Columns != null)
            {
                foreach (var item in dbtable.Columns)
                {
                    dbdic.Add(item.ColumnName.ToLower(), item);
                }
            }
            #endregion

            #region 新增列
            foreach (IDataColumn item in entitytable.Columns)
            {
                if (!dbdic.ContainsKey(item.ColumnName.ToLower()))
                {
                    //AddColumn(sb, item, onlySql);
                    PerformSchema(sb, onlySql, DDLSchema.AddColumn, item);
                    if (!String.IsNullOrEmpty(item.Description)) PerformSchema(sb, onlySql, DDLSchema.AddColumnDescription, item);

                    //! 以下已经不需要了,目前只有SQLite会采用重建表的方式添加删除字段。如果这里提前添加了字段,重建表的时候,会导致失败。

                    //// 这里必须给dbtable加加上当前列,否则下面如果刚好有删除列的话,会导致增加列成功,然后删除列重建表的时候没有新加的列
                    //dbtable.Columns.Add(item.Clone(dbtable));
                }
            }
            #endregion

            #region 删除列
            var sbDelete = new StringBuilder();
            for (int i = dbtable.Columns.Count - 1; i >= 0; i--)
            {
                var item = dbtable.Columns[i];
                if (!entitydic.ContainsKey(item.ColumnName.ToLower()))
                {
                    if (!String.IsNullOrEmpty(item.Description)) PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, item);
                    PerformSchema(sbDelete, setting.NoDelete, DDLSchema.DropColumn, item);
                }
            }
            if (sbDelete.Length > 0)
            {
                if (setting.NoDelete)
                {
                    //不许删除列,显示日志
                    WriteLog("数据表中发现有多余字段,请手工执行以下语句删除:" + Environment.NewLine + sbDelete.ToString());
                }
                else
                {
                    if (sb.Length > 0) sb.AppendLine(";");
                    sb.Append(sbDelete.ToString());
                }
            }
            #endregion

            #region 修改列
            // 开发时的实体数据库
            var entityDb = DbFactory.Create(entitytable.DbType);

            foreach (var item in entitytable.Columns)
            {
                IDataColumn dbf = null;
                if (!dbdic.TryGetValue(item.ColumnName, out dbf)) continue;

                if (IsColumnTypeChanged(item, dbf))
                {
                    WriteLog("字段{0}.{1}类型需要由{2}改变为{3}", entitytable.Name, item.Name, dbf.DataType, item.DataType);
                    PerformSchema(sb, onlySql, DDLSchema.AlterColumn, item, dbf);
                }
                if (IsColumnChanged(item, dbf, entityDb)) PerformSchema(sb, onlySql, DDLSchema.AlterColumn, item, dbf);
                if (IsColumnDefaultChanged(item, dbf, entityDb)) ChangeColmnDefault(sb, onlySql, item, dbf, entityDb);

                if (item.Description + "" != dbf.Description + "")
                {
                    // 先删除旧注释
                    //if (!String.IsNullOrEmpty(dbf.Description)) DropColumnDescription(sb, dbf, onlySql);
                    //if (!String.IsNullOrEmpty(dbf.Description)) PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf);
                    if (dbf.Description != null) PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf);

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

            return sb.ToString();
        }
Example #20
0
        protected virtual void CheckTable(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            //Boolean onlySql = DAL.NegativeCheckOnly;

            if (dbtable == null)
            {
                // 没有字段的表不创建
                if (entitytable.Columns.Count < 1) return;

                #region 创建表
                WriteLog("创建表:{0}({1})", entitytable.TableName, entitytable.Description);

                var sb = new StringBuilder();
                // 建表,如果不是onlySql,执行时DAL会输出SQL日志
                CreateTable(sb, entitytable, setting.CheckOnly);

                // 仅获取语句
                //if (onlySql) WriteLog("XCode.Negative.Enable没有设置为True,请手工创建表:" + entitytable.Name + Environment.NewLine + sb.ToString());
                if (setting.CheckOnly) WriteLog("只检查不对数据库进行操作,请手工创建表:" + entitytable.TableName + Environment.NewLine + sb.ToString());
                #endregion
            }
            else
            {
                #region 修改表
                String sql = CheckColumnsChange(entitytable, dbtable, setting);
                if (!String.IsNullOrEmpty(sql)) sql += ";";
                sql += CheckTableDescriptionAndIndex(entitytable, dbtable, setting.CheckOnly);
                if (!String.IsNullOrEmpty(sql) && setting.CheckOnly)
                {
                    WriteLog("只检查不对数据库进行操作,请手工使用以下语句修改表:" + Environment.NewLine + sql);
                }
                #endregion
            }
        }
Example #21
0
        protected virtual void OnSetTables(IDataTable[] tables, NegativeSetting setting)
        {
            CheckDatabase(setting);

            CheckAllTables(tables, setting);
        }
Example #22
0
        /// <summary>已重载。因为内存数据库无法检测到架构,不知道表是否已存在,所以需要自己维护</summary>
        /// <param name="entitytable"></param>
        /// <param name="dbtable"></param>
        /// <param name="setting"></param>
        protected override void CheckTable(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting)
        {
            if (dbtable == null && (Database as SQLite).IsMemoryDatabase)
            {
                if (memoryTables.Any(t => t.TableName.EqualIgnoreCase(entitytable.TableName)))
                {
                    return;
                }

                memoryTables.Add(entitytable);
            }

            base.CheckTable(entitytable, dbtable, setting);
        }
Example #23
0
File: DAL.cs Project: tommybiteme/X
 /// <summary>在当前连接上检查指定数据表的架构</summary>
 /// <param name="set"></param>
 /// <param name="tables"></param>
 public void SetTables(NegativeSetting set, params IDataTable[] tables)
 {
     if (set == null)
     {
         set = new NegativeSetting();
         set.CheckOnly = Setting.Current.Negative.CheckOnly;
         set.NoDelete = Setting.Current.Negative.NoDelete;
     }
     //if (set.CheckOnly && DAL.Debug) WriteLog("XCode.Negative.CheckOnly设置为True,只是检查不对数据库进行操作");
     //if (set.NoDelete && DAL.Debug) WriteLog("XCode.Negative.NoDelete设置为True,不会删除数据表多余字段");
     Db.CreateMetaData().SetTables(set, tables);
 }
Example #24
0
 /// <summary>�ڵ�ǰ�����ϼ��ָ�����ݱ�ļܹ�</summary>
 /// <param name="tables"></param>
 public void SetTables(params IDataTable[] tables)
 {
     var set = new NegativeSetting();
     set.CheckOnly = DAL.NegativeCheckOnly;
     set.NoDelete = DAL.NegativeNoDelete;
     //if (set.CheckOnly && DAL.Debug) WriteLog("XCode.Negative.CheckOnly����ΪTrue��ֻ�Ǽ�鲻�����ݿ���в���");
     //if (set.NoDelete && DAL.Debug) WriteLog("XCode.Negative.NoDelete����ΪTrue������ɾ�����ݱ�����ֶ�");
     Db.CreateMetaData().SetTables(set, tables);
 }
Example #25
0
        private void btnCreateDb_Click(object sender, EventArgs e)
        {
            if (cbConn.SelectedItem == null) return;

            var dal = DAL.Create("" + cbConn.SelectedItem);
            if (dal == null) return;

            try
            {
                var md = dal.Db.CreateMetaData();
                var set = new NegativeSetting();
                set.CheckOnly = false;
                set.NoDelete = false;
                md.SetTables(set, Tables.ToArray());

                MessageBox.Show("成功建立" + Tables.Count + "张数据表!", this.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("建表失败!" + Environment.NewLine + ex.Message, this.Text);
            }
        }