/// <summary>
        /// 获取帮助类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public CompareAndShowResultHelperBase GetHelper(DBObjType type)
        {
            CompareAndShowResultHelperBase rel;

            switch (type)
            {
            case DBObjType.Table:
                rel = new TableCompareAndShowResultHelper();
                break;

            case DBObjType.View:
                rel = new ViewCompareAndShowResultHelper();
                break;

            case DBObjType.Trig:
                rel = new TrigCompareAndShowResultHelper();
                break;

            case DBObjType.Proc:
                rel = new ProcCompareAndShowResultHelper();
                break;

            case DBObjType.Func:
                rel = new FuncCompareAndShowResultHelper();
                break;

            case DBObjType.DBSetting:
                rel = new DataBaseCompareAndShowResultHelper();
                break;

            default:
                throw new Exception("未知的(DBObjType)");
            }
            return(rel);
        }
Ejemplo n.º 2
0
        private void StartCompare()
        {
            config.Setting = cs;


            if (oldDataSource == null || newDataSource == null)
            {
                SetStatus(false);
                AppendOutputText("无数据源", OutputType.Error);
                MessageBox.Show("无数据源", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DbModels oldDbModels = GetEmptyDbModel(oldDataSource.Type);
            DbModels newDbModels = GetEmptyDbModel(newDataSource.Type);


            cs.CheckCommon             = this.cheComm.Checked;
            cs.OutputComment           = xhkOutComment.Checked;
            cs.OutputDeleteSql         = chkOutDeleteSql.Checked;
            cs.OutputDeleteSqlIsCommon = chkOutDeleteSqlIsCommon.Checked;
            cs.IsSearFunc  = this.chkDiffFunc.Checked;
            cs.IsSearTable = this.chkDiffTable.Checked;
            cs.IsSearProc  = this.chkDiffProc.Checked;
            cs.IsSearTri   = this.chkDiffTrigger.Checked;
            cs.IsSearView  = this.chkDiffView.Checked;



            //处理数据源字符串
            config.OldConnection.ConnectionString = GetShowConnectionString(oldDataSource);
            config.NewConnection.ConnectionString = GetShowConnectionString(newDataSource);
            config.OldConnection.ProviderName     = oldDataSource.ProviderName;
            config.NewConnection.ProviderName     = newDataSource.ProviderName;

            startTime = DateTime.Now;
            SetTotalTime();

            ClearOutputText();
            //throw new Exception("testing");
            List <string>   tempList    = new List <string>();
            List <Function> tempFunList = new List <Function>();
            string          tempStt     = "";
            string          tempStr     = "";
            bool            tempBool    = false;

            SetStatus(true);

            AppendOutputText("我们正在准备一些事情,请耐心等待\n", OutputType.Comment);



            //获取数据库信息
            tempBool = true;
            DataBaseCompareAndShowResultHelper dbHelper = new DataBaseCompareAndShowResultHelper();

            dbHelper.OutputText          = AppendOutputText;
            dbHelper.ReplaceLastLineText = ReplaceLastLineText;


            #region 获取数据源

            if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
            {
                ////测试连接到数据库,以免报错
                //AppendOutputText("正在检查连接到新数据库的状态,请耐心等待\n", OutputType.Comment);
                //if (!helper.TestLine(10000))
                //{
                //    AppendOutputText("新数据库连接失败\n", OutputType.Comment);
                //    MessageBox.Show("新数据库连接失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    SetStatus(false);
                //    return;
                //}
                //else
                //{
                //    AppendOutputText("新数据库连接成功\n", OutputType.Comment);
                //}


                AppendOutputText("从文件中获取数据库信息\n", OutputType.Comment);
                tempStt = dbHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.DBSetting, ref oldDbModels);
                if (!string.IsNullOrWhiteSpace(tempStt))
                {
                    tempBool = false;
                    AppendOutputText(tempStt + "\r\n", OutputType.Error);
                }
            }
            else if (oldDataSource.Type == DBDataSourceType.MySql)
            {
                AppendOutputText("从数据库中获取数据库信息\n", OutputType.Comment);
                if (!dbHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                {
                    tempBool = false;
                }
            }
            if (newDataSource.Type == DBDataSourceType.DataSourceFile)
            {
                ////测试连接到数据库,以免报错
                //AppendOutputText("正在检查连接到旧数据库的状态,请耐心等待\n", OutputType.Comment);
                //if (!helper.TestLine(10000))
                //{
                //    AppendOutputText("旧数据库连接失败\n", OutputType.Comment);
                //    MessageBox.Show("旧数据库连接失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    SetStatus(false);
                //    return;
                //}
                //else
                //{
                //    AppendOutputText("旧数据库连接成功\n", OutputType.Comment);
                //}


                AppendOutputText("从文件中获取数据库信息\n", OutputType.Comment);
                tempStt = dbHelper.GetInfoByFile(newDataSource.Value, newpathCs.DBSetting, ref newDbModels);
                if (!string.IsNullOrWhiteSpace(tempStt))
                {
                    tempBool = false;
                    AppendOutputText(tempStt + "\r\n", OutputType.Error);
                }
            }
            else if (newDataSource.Type == DBDataSourceType.MySql)
            {
                AppendOutputText("从数据库中获取数据库信息\n", OutputType.Comment);
                if (!dbHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                {
                    tempBool = false;
                }
            }
            #endregion

            string resultStr = "";
            if (tempBool)
            {
                resultStr = Tools.GetDirFullPath(string.Format("/result/{0}-{1}-{2}/", DateTime.Now.ToString("yyyyMMddHHmmss"), oldDbModels.DbModel.DbName, newDbModels.DbModel.DbName));


                File.AppendAllText(resultStr + oldpathCs.DBSetting, JsonConvert.SerializeObject(oldDbModels.DbModel));
                File.AppendAllText(resultStr + newpathCs.DBSetting, JsonConvert.SerializeObject(newDbModels.DbModel));
            }
            else
            {
                throw new Exception("获取数据库信息失败");
            }


            ////从网卡层面判断是否联网
            //if (!Win32API.InternetGetConnectedState(ref int tempInx, 0))
            //{
            //    AppendOutputText("请检查你的网络状态", OutputType.Error);
            //    MessageBox.Show("结束对比:请检查你的网络状态", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    SetStatus(false);
            //    return;
            //}


            AppendOutputText("\n", OutputType.None);
            File.AppendAllText(resultStr + oldpathCs.Path, JsonConvert.SerializeObject(config));


            if (cs.IsDiff)
            {
                dbHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);

                if (string.IsNullOrEmpty(errorString) && tempBool)
                {
                    AppendOutputText("对比完毕\n\n", OutputType.Comment);
                    //compIsError = true;
                }
                else
                {
                    //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                    //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                File.AppendAllText(resultStr + diffpathCs.DBSetting, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
            }
            tempStr = RtxResult.Text;


            if (cs.IsSearTable)
            {
                tempBool = true;
                TableCompareAndShowResultHelper viewHelper = new TableCompareAndShowResultHelper();
                viewHelper.OutputText          = AppendOutputText;
                viewHelper.ReplaceLastLineText = ReplaceLastLineText;



                #region 获取数据源

                if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取表结构\n", OutputType.Comment);
                    tempStt = viewHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.Tables, ref oldDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Tables, JsonConvert.SerializeObject(oldDbModels.Tables.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (oldDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取表结构\n", OutputType.Comment);
                    if (viewHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Tables, JsonConvert.SerializeObject(oldDbModels.Tables.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }
                if (newDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取表结构\n", OutputType.Comment);
                    tempStt = viewHelper.GetInfoByFile(newDataSource.Value, newpathCs.Tables, ref newDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + newpathCs.Tables, JsonConvert.SerializeObject(newDbModels.Tables.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (newDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取表结构\n", OutputType.Comment);
                    if (viewHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                    {
                        File.AppendAllText(resultStr + newpathCs.Tables, JsonConvert.SerializeObject(newDbModels.Tables.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                #endregion


                if (cs.IsDiff)
                {
                    viewHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);

                    if (string.IsNullOrEmpty(errorString) && tempBool)
                    {
                        AppendOutputText("对比完毕\n\n", OutputType.Comment);
                        //compIsError = true;
                    }
                    else
                    {
                        //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                        //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    File.AppendAllText(resultStr + diffpathCs.Tables, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
                }
                tempStr = RtxResult.Text;
            }


            if (cs.IsSearView)
            {
                tempBool = true;
                ViewCompareAndShowResultHelper viewHelper = new ViewCompareAndShowResultHelper();
                viewHelper.OutputText          = AppendOutputText;
                viewHelper.ReplaceLastLineText = ReplaceLastLineText;

                #region 获取数据源

                if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取视图\n", OutputType.Comment);
                    tempStt = viewHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.Views, ref oldDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Views, JsonConvert.SerializeObject(oldDbModels.Views.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (oldDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取视图\n", OutputType.Comment);
                    if (viewHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Views, JsonConvert.SerializeObject(oldDbModels.Views.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                if (newDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取视图\n", OutputType.Comment);
                    tempStt = viewHelper.GetInfoByFile(newDataSource.Value, newpathCs.Views, ref newDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + newpathCs.Views, JsonConvert.SerializeObject(newDbModels.Views.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (newDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取视图\n", OutputType.Comment);
                    if (viewHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                    {
                        File.AppendAllText(resultStr + newpathCs.Views, JsonConvert.SerializeObject(newDbModels.Views.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                #endregion

                if (cs.IsDiff)
                {
                    viewHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);

                    if (string.IsNullOrEmpty(errorString) && tempBool)
                    {
                        AppendOutputText("对比完毕\n\n", OutputType.Comment);
                    }
                    else
                    {
                        //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                        //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    File.AppendAllText(resultStr + diffpathCs.Views, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
                }
                tempStr = RtxResult.Text;
            }

            if (cs.IsSearTri)
            {
                tempBool = true;
                TrigCompareAndShowResultHelper trigHelper = new TrigCompareAndShowResultHelper();
                trigHelper.OutputText          = AppendOutputText;
                trigHelper.ReplaceLastLineText = ReplaceLastLineText;


                #region 获取数据源


                if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取触发器\n", OutputType.Comment);
                    tempStt = trigHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.Trigs, ref oldDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Trigs, JsonConvert.SerializeObject(oldDbModels.Triggers.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (oldDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取触发器\n", OutputType.Comment);
                    if (trigHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Trigs, JsonConvert.SerializeObject(oldDbModels.Triggers.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                if (newDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取触发器\n", OutputType.Comment);
                    tempStt = trigHelper.GetInfoByFile(newDataSource.Value, newpathCs.Trigs, ref newDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + newpathCs.Trigs, JsonConvert.SerializeObject(newDbModels.Triggers.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (newDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取触发器\n", OutputType.Comment);
                    if (trigHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                    {
                        File.AppendAllText(resultStr + newpathCs.Trigs, JsonConvert.SerializeObject(newDbModels.Triggers.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                #endregion



                if (cs.IsDiff)
                {
                    trigHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);

                    if (string.IsNullOrEmpty(errorString) && tempBool)
                    {
                        AppendOutputText("对比完毕\n\n", OutputType.Comment);
                        File.AppendAllText(resultStr + diffpathCs.Trigs, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
                    }
                    else
                    {
                        //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                        //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                tempStr = RtxResult.Text;
            }



            if (cs.IsSearProc)
            {
                tempBool = true;
                ProcCompareAndShowResultHelper funcHelper = new ProcCompareAndShowResultHelper();
                funcHelper.OutputText          = AppendOutputText;
                funcHelper.ReplaceLastLineText = ReplaceLastLineText;


                #region 获取数据源

                if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取存储过程\n", OutputType.Comment);
                    tempStt = funcHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.Procs, ref oldDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Procs, JsonConvert.SerializeObject(oldDbModels.Procs.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (oldDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取存储过程\n", OutputType.Comment);
                    if (funcHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Procs, JsonConvert.SerializeObject(oldDbModels.Procs.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }
                if (newDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取存储过程\n", OutputType.Comment);
                    tempStt = funcHelper.GetInfoByFile(newDataSource.Value, newpathCs.Procs, ref newDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + newpathCs.Procs, JsonConvert.SerializeObject(newDbModels.Procs.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (newDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取存储过程\n", OutputType.Comment);
                    if (funcHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                    {
                        File.AppendAllText(resultStr + newpathCs.Procs, JsonConvert.SerializeObject(newDbModels.Procs.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                #endregion

                if (cs.IsDiff)
                {
                    funcHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);
                    //CompareAndShowResult(procs, newProcs, cs, out string errorString);

                    if (string.IsNullOrEmpty(errorString) && tempBool)
                    {
                        AppendOutputText("对比完毕\n\n", OutputType.Comment);
                        File.AppendAllText(resultStr + diffpathCs.Procs, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
                    }
                    else
                    {
                        //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                        //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                tempStr = RtxResult.Text;
            }



            if (cs.IsSearFunc)
            {
                tempBool = true;
                FuncCompareAndShowResultHelper funcHelper = new FuncCompareAndShowResultHelper();
                funcHelper.OutputText          = AppendOutputText;
                funcHelper.ReplaceLastLineText = ReplaceLastLineText;


                #region 获取数据源

                if (oldDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取函数\n", OutputType.Comment);
                    tempStt = funcHelper.GetInfoByFile(oldDataSource.Value, oldpathCs.Funcs, ref oldDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Funcs, JsonConvert.SerializeObject(oldDbModels.Functions.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (oldDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取函数\n", OutputType.Comment);
                    if (funcHelper.GetInfoByDb(oldDataSource.Value, ref oldDbModels))
                    {
                        File.AppendAllText(resultStr + oldpathCs.Funcs, JsonConvert.SerializeObject(oldDbModels.Functions.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                if (newDataSource.Type == DBDataSourceType.DataSourceFile)
                {
                    AppendOutputText("从文件中获取函数\n", OutputType.Comment);
                    tempStt = funcHelper.GetInfoByFile(newDataSource.Value, newpathCs.Funcs, ref newDbModels);
                    if (string.IsNullOrWhiteSpace(tempStt))
                    {
                        File.AppendAllText(resultStr + newpathCs.Funcs, JsonConvert.SerializeObject(newDbModels.Functions.Values));
                    }
                    else
                    {
                        tempBool = false;
                        AppendOutputText(tempStt + "\r\n", OutputType.Error);
                    }
                }
                else if (newDataSource.Type == DBDataSourceType.MySql)
                {
                    AppendOutputText("从数据库中获取函数\n", OutputType.Comment);
                    if (funcHelper.GetInfoByDb(newDataSource.Value, ref newDbModels))
                    {
                        File.AppendAllText(resultStr + newpathCs.Funcs, JsonConvert.SerializeObject(newDbModels.Functions.Values));
                    }
                    else
                    {
                        tempBool = false;
                    }
                }

                #endregion


                if (cs.IsDiff)
                {
                    funcHelper.CompareAndShow(ref oldDbModels, ref newDbModels, cs, out string errorString);

                    if (string.IsNullOrEmpty(errorString) && tempBool)
                    {
                        AppendOutputText("对比完毕\n\n", OutputType.Comment);
                        File.AppendAllText(resultStr + diffpathCs.Funcs, string.IsNullOrWhiteSpace(tempStr) ? RtxResult.Text : RtxResult.Text.Replace(tempStr, ""));
                    }
                    else
                    {
                        //string tips = string.Concat("对比中发现以下问题,请修正后重新进行比较:\n\n", errorString);
                        //MessageBox.Show(tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                tempStr = RtxResult.Text;
            }

            //bool compIsError = false;

            AppendOutputText("\n", OutputType.Comment);
            AppendOutputText("执行完毕\n", OutputType.Comment);

            SetStatus(false);
            //try
            //{
            //    AbortTh();
            //}
            //catch (Exception)
            //{
            //}
        }